package com.seleniumtests.tests.api_testing;

import static io.restassured.RestAssured.given;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.greaterThan;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.hamcrest.MatcherAssert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import io.restassured.response.Response;

public class RestAPITest3 extends CoreAPITest {

    @Test
    public void specifyAuth() {
        // in each request
        given().auth()
                .basic("username", "password")
                .get(API_PAGE_2)
                .then()
                .body("data.email", hasItems("michael.lawson@reqres.in", "lindsay.ferguson@reqres.in"));

        // or base class setup i.e. in CoreAPITest class
    }

    @Test(dataProvider = "basicAuthCredential")
    public void preemptiveBasicAuth(String userName, String password) {
        given()
                .auth()
                .preemptive()
                .basic(userName, password)
                .get("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.01753906557239593")
                .then()
                .log()
                .cookies()
                .assertThat()
                .cookie("LastPassword", equalTo(password));
    }

    @DataProvider(name = "basicAuthCredential")
    public Object[][] getBasicAuthCredentials() {
        return new Object[][]{{"wrong username", UUID.randomUUID().toString()},
                {"httpwatch", UUID.randomUUID().toString()}};
    }

    @Test
    public void ChallengedBasicAuth() {
        // remove API preemptive from preemptiveBasicAuth example :)
    }

    @Test
    public void postToCreate() {

        Map<String, String> jsonAsMap = new HashMap<>();
        jsonAsMap.put("name", "seleniumtest");
        jsonAsMap.put("job", "qa");

        Response response = given()
                .body(jsonAsMap)
                .when()
                .post("https://reqres.in/api/users")
                .then()
                .statusCode(HTTP_CREATED)
                .log()
                .body()
                .extract()
                .response();

        MatcherAssert.assertThat(Integer.valueOf(response.path("id")), greaterThan(0));

    }

}

@@ -1885,17 +1885,16 @@
 ate() %7B%0A
-%0A
         
@@ -2430,18 +2430,1610 @@
 an(0));%0A
-%0A
     %7D%0A%0A
+    @Test%0A    public void postUsingJavaObject() %7B%0A%0A        User user = new User(%22testuser1%22, %22qa_engineer%22);%0A%0A        Response response = given()%0A                .body(user)%0A                .when()%0A                .post(%22https://reqres.in/api/users%22)%0A                .then()%0A                .statusCode(HTTP_CREATED)%0A                .log()%0A                .body()%0A                .extract()%0A                .response();%0A%0A        MatcherAssert.assertThat(Integer.valueOf(response.path(%22id%22)), greaterThan(0));%0A    %7D%0A%0A    @Test%0A    public void deserializeResponse() %7B%0A%0A        User user = new User(%22testuser1%22, %22qa_engineer%22);%0A%0A        UserCreationStats userCreationStats = given()%0A                .body(user)%0A                .when()%0A                .post(%22https://reqres.in/api/users%22)%0A                .then()%0A                .statusCode(HTTP_CREATED)%0A                .log()%0A                .body()%0A                .extract()%0A                .response()%0A                .as(UserCreationStats.class);%0A%0A        System.out.println(%22id is: %22 + userCreationStats.id);%0A        System.out.println(%22created at : %22 + userCreationStats.createdAt);%0A    %7D%0A%0A    public class User %7B%0A        private String name;%0A        private String job;%0A%0A        public User(String name, String job) %7B%0A            this.name = name;%0A            this.job = job;%0A        %7D%0A    %7D%0A%0A    public class UserCreationStats %7B%0A        private String id;%0A        private String createdAt;%0A%0A        public UserCreationStats(String id, String createdAt) %7B%0A            this.id = id;%0A            this.createdAt = createdAt;%0A        %7D%0A    %7D%0A
 %7D%0A

Add Serialization and Deserialization examples using REST Assured