Tuesday, July 9, 2019

All Rest Assured operation in one


package test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.restassured.http.ContentType;
import io.restassured.http.Cookie;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.internal.path.json.mapping.JsonObjectDeserializer;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.response.ValidatableResponse;
import io.restassured.config.RestAssuredConfig;
import io.restassured.config.XmlConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matcher;
import io.restassured.matcher.ResponseAwareMatcher;
import io.restassured.matcher.RestAssuredMatchers.*;
import io.restassured.matcher.ResponseAwareMatcherComposer.*;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.specification.ResponseSpecification;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import io.restassured.mapper.ObjectMapperType;
import static io.restassured.path.json.JsonPath.*;
import io.restassured.path.json.JsonPath;
import io.restassured.path.xml.XmlPath;
import static org.hamcrest.Matchers.*;
public class AllRestAPIOperations {
ResponseBody<?> responseBody;
String id;
@Test
public void allInOne() {
String name = "muttu1111111";
// Post request creating employee
responseBody = given().accept(ContentType.JSON)
.body("{\"name\":\"" + name + "\",\"salary\":\"123\",\"age\":\"23\"}").when()
.post("http://dummy.restapiexample.com/api/v1/create").body();
id = responseBody.jsonPath().getString("id");
String salary = "233"; //Salary amount needs be updated
// Put request- Update salary for the above employee
responseBody = given().accept(ContentType.JSON)
.body("{\"name\":\"" + name + "\",\"salary\":\"" + salary + "\",\"age\":\"23\"}").when()
.put("http://dummy.restapiexample.com/api/v1/update/" + id).body();
salary = responseBody.jsonPath().getString("salary");
System.out.println(id + " " + salary);
// Get request after updating the salary- fetch the details of an employee
String body = when().get("http://dummy.restapiexample.com/api/v1/employee/" + id + "").body().asString();
System.out.println(body);
// Delete Employee by id
String as = when().delete("http://dummy.restapiexample.com/api/v1/delete/" + id).asString();
System.out.println(as);
}
}

No comments:

Post a Comment