RestAssured #14 - API Automation Practice (Part 1)- Crocodile API - Create Crocodiles (POST Request)
This article will cover the Create Crocodiles test case automation to demonstrate POST Request automation using POJO for request and response payload. Also, it will create a basic structure to organise code in a better way.
Below are the steps we need to add for the above test case -
Create a new user - https://test-api.k6.io/user/register/
Get access token - https://test-api.k6.io/auth/token/login/
Create crocodile using endpoint - https://test-api.k6.io/my/crocodiles/
Project Structure
I will create 3 layers as below for automating test cases -
Core Layer- This will contain utils to perform common actions like DateFormatUtils, RandomNumberUtils, APIResponseDetailsExtractor
Business/Application Layer
POJO - It will contain POJO classes for request and response body
Flows - It will contain steps to perform HTTP methods using POJO
Presentation/Test Layer- This is a test layer which will call Flows to perform the test
First will create CrocodilesPOJO to represent Crocodiles JSON which will be used for request and response payload in src/main directory under package "app.e2e.crocodiles.pojo"
public class CrocodilesPOJO {
@JsonProperty("name")
private String crocodileName;
@JsonProperty("sex")
private String gender;
@JsonProperty("date_of_birth")
private String crocodileDOB;
@JsonProperty("age")
private int age;
@JsonProperty("id")
private int id;
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public CrocodilesPOJO(){
}
public CrocodilesPOJO(String crocodileName, String gender, String crocodileDOB) {
this.crocodileName = crocodileName;
this.gender = gender;
this.crocodileDOB = crocodileDOB;
}
public String getCrocodileName() {
return crocodileName;
}
public void setCrocodileName(String crocodileName) {
this.crocodileName = crocodileName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCrocodileDOB() {
return crocodileDOB;
}
public void setCrocodileDOB(String crocodileDOB) {
this.crocodileDOB = crocodileDOB;
}
public int getAge() {
return age;
}
public int getId() {
return id;
}
}
Second will create CreateCrocodilesFlow class which will send POST request to create crocodile and return response object for validation in src/main directory under package "app.e2e.crocodiles.flows"
public class CreateCrocodilesFlow {
private final String baseUri = "https://test-api.k6.io";
private final String createCrocodilesPath = "/my/crocodiles/";
private CrocodilesPOJO crocodilesPOJO;
public CrocodilesPOJO getCrocodilesPojoResponse() {
return crocodilesPojoResponse;
}
private CrocodilesPOJO crocodilesPojoResponse;
public CreateCrocodilesFlow(String crocodileName, String gender, String crocodileDOB){
crocodilesPOJO = new CrocodilesPOJO(crocodileName,gender,crocodileDOB);
RestAssured.baseURI = baseUri;
}
public APIResponseDetailsExtractor createCrocodile(String authToken){
Response response = RestAssured.given()
.contentType(ContentType.JSON)
.header("Authorization","Bearer "+authToken)
.body(crocodilesPOJO)
.log().body()
.post(createCrocodilesPath);
response.then().log().body();
crocodilesPojoResponse = response.as(CrocodilesPOJO.class);
return new APIResponseDetailsExtractor(response);
}
}
Lastly will create CrocodilesTest class in src/test directory under the package "e2e.crocodiles.tests"
which will create a crocodile and validate the response using createCrocodilesTest method
public class CrocodilesTest {
private String authToken;
@BeforeTest
public void createUserAndGetToken(){
String dateTime = DateFormatUtils.getTimeStamp("yyyyMMddHHmmss");
int fourDigitRandom = RandomNumberUtils.getRandomNumber(4);
String password = "1";
String firstName = "TestUser";
String lastName = dateTime + fourDigitRandom;
String userName = firstName + lastName;
String email = userName + "@tester.com";
CreateUserFlow newUser = new CreateUserFlow(userName,firstName,lastName,email,password);
APIResponseDetailsExtractor newUserResponse = newUser.createUser();
Assert.assertEquals(newUserResponse.getStatusCode(),201);
LoginUserFlow userLogin = new LoginUserFlow(userName,password);
APIResponseDetailsExtractor userLoginResponse = userLogin.performLoginUsingTokenAuth();
authToken = userLoginResponse.getResponseBodyUsingKey("access");
}
@Test
public void createCrocodilesTest(){
String crocodileName = "Test Crocodile";
String gender = "M";
String dob = "2010-06-27";
CreateCrocodilesFlow createCrocodile = new CreateCrocodilesFlow(crocodileName,gender,dob);
APIResponseDetailsExtractor createCrocodileResponse = createCrocodile.createCrocodile(authToken);
//Validate status code is 201
Assert.assertEquals(createCrocodileResponse.getStatusCode(),201);
//Validate response body
CrocodilesPOJO crocodileDetails = createCrocodile.getCrocodilesPojoResponse();
Assert.assertEquals(crocodileDetails.getCrocodileName(),crocodileName);
Assert.assertEquals(crocodileDetails.getCrocodileDOB(),dob);
Assert.assertEquals(crocodileDetails.getGender(),gender);
assert crocodileDetails.getId() > 0;
assert crocodileDetails.getAge() > 0;
}
}
Git Hub Repo Link - https://github.com/sksingh329/RestAssuredTutorials