RestAssured #17 - API Automation Practice (Part 4)- Delete Crocodiles (DELETE Request)
Validation for DELETE Crocodiles
End point (DELETE Request)- https://test-api.k6.io/my/crocodiles/{id}
Below are validations for deleted crocodiles -
Status Code - 204 (DELETE Request) No content
To GET updated crocodile details for the user send a GET request
Endpoint - https://test-api.k6.io/my/crocodiles/
Status Code - 200 (GET Request)
Response Body
Update crocs count
Validate that deleted crocs is not there in response
Validate that other crocs exist
Example
Test method to validate deleted crocodiles - deleteCrocodilesTest
First will create 3 crocodiles using POST request from 2D array
String[][] crocodileDetails = {{"Test Crocodile1", "M", "2010-06-27"}, {"Test Crocodile2", "F", "2018-06-27"}, {"Test Crocodile3", "M", "2007-06-27"}};
List<Integer> crocodileIds = new ArrayList<>();
for (String[] crocodileDetail : crocodileDetails) {
createCrocodileResponse = createCrocodile(crocodileDetail[0], crocodileDetail[1], crocodileDetail[2]);
assertThat(createCrocodileResponse.getStatusCode(), equalTo(201));
crocodileIds.add(Integer.parseInt(createCrocodileResponse.getResponseBodyUsingKey("id")));
}
Delete 1st crocs using id
int crocodileIdToDelete = crocodileIds.get(0);
APIResponseDetailsExtractor deleteCrocodileResponse = createCrocodileFlow.deleteCrocodile(authToken,crocodileIdToDelete);
Validation of deleted crocodiles
//Validate status code of DELETE response
assertThat(deleteCrocodileResponse.getStatusCode(),equalTo(204));
APIResponseDetailsExtractor getCrocodileResponse = createCrocodileFlow.getCrocodile(authToken);
//Validate status code of GET response
assertThat(getCrocodileResponse.getStatusCode(),equalTo(200));
//Validate response body
JsonPath jsonPath = getCrocodileResponse.getResponseJsonPath();
int crocodilesCount = jsonPath.getList("").size();
assertThat(crocodilesCount,equalTo(crocodileDetails.length-1));
List<Integer> crocodileIdsList = jsonPath.getList("id");
//Validate that deleted crocodile id does not exist
assertThat(crocodileIdsList,not(hasItems(crocodileIdToDelete)));
//Validate that other crocodile id exist
assertThat(crocodileIdsList,hasItems(crocodileIds.get(1),crocodileIds.get(2)));
@Test
public void deleteCrocodilesTest() {
APIResponseDetailsExtractor createCrocodileResponse;
String[][] crocodileDetails = {{"Test Crocodile1", "M", "2010-06-27"}, {"Test Crocodile2", "F", "2018-06-27"}, {"Test Crocodile3", "M", "2007-06-27"}};
List<Integer> crocodileIds = new ArrayList<>();
for (String[] crocodileDetail : crocodileDetails) {
createCrocodileResponse = createCrocodile(crocodileDetail[0], crocodileDetail[1], crocodileDetail[2]);
assertThat(createCrocodileResponse.getStatusCode(), equalTo(201));
crocodileIds.add(Integer.parseInt(createCrocodileResponse.getResponseBodyUsingKey("id")));
}
int crocodileIdToDelete = crocodileIds.get(0);
APIResponseDetailsExtractor deleteCrocodileResponse = createCrocodileFlow.deleteCrocodile(authToken,crocodileIdToDelete);
//Validate status code of DELETE response
assertThat(deleteCrocodileResponse.getStatusCode(),equalTo(204));
APIResponseDetailsExtractor getCrocodileResponse = createCrocodileFlow.getCrocodile(authToken);
//Validate status code of GET response
assertThat(getCrocodileResponse.getStatusCode(),equalTo(200));
//Validate response body
JsonPath jsonPath = getCrocodileResponse.getResponseJsonPath();
int crocodilesCount = jsonPath.getList("").size();
assertThat(crocodilesCount,equalTo(crocodileDetails.length-1));
List<Integer> crocodileIdsList = jsonPath.getList("id");
//Validate that deleted crocodile id does not exist
assertThat(crocodileIdsList,not(hasItems(crocodileIdToDelete)));
//Validate that other crocodile id exist
assertThat(crocodileIdsList,hasItems(crocodileIds.get(1),crocodileIds.get(2)));
}
DELETE Request for crocodile using id -
public APIResponseDetailsExtractor deleteCrocodile(String authToken,int crocodileId) {
Response response = RestAssured.given()
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + authToken)
.delete(crocodilesBasePath+crocodileId);
response.then().log().all();
String redirectUrl = response.getHeader("Location");
response = RestAssured.given()
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + authToken)
.delete(baseUri+redirectUrl);
response.then().log().all();
return new APIResponseDetailsExtractor(response);
}
You can find full code at Git Hub Repo Link - https://github.com/sksingh329/RestAssuredTutorials
Subscribe to my newsletter
Read articles from SUBODH SINGH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
SUBODH SINGH
SUBODH SINGH
An Automation Engineer, experienced in the automation of front-end and back-end applications with 11+ years of experience. Experience in designing, developing, and implementing automation frameworks. I like to learn and share knowledge on test automation.