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 -

  1. Status Code - 204 (DELETE Request) No content

  2. To GET updated crocodile details for the user send a GET request

    1. Endpoint - https://test-api.k6.io/my/crocodiles/

    2. Status Code - 200 (GET Request)

    3. Response Body

      1. Update crocs count

      2. Validate that deleted crocs is not there in response

      3. 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

Did you find this article valuable?

Support subodh's blog by becoming a sponsor. Any amount is appreciated!