RestAssured #15 - API Automation Practice (Part 2)- Update Crocodiles (PUT & PATCH Request)

RestAssured #15 - API Automation Practice (Part 2)- Update Crocodiles (PUT & PATCH Request)

Difference between PUT and PATCH request | How to send PUT & PATCH request / Replace Crocodile | How to handle 301 HTTP status code

Below are details of the Test case covered in this article


Difference between PUT and PATCH request

To update resources there are two methods

  • PUT - It is used to replace resources where entire representation of the resource needs to be sent in the request body. If only one field needs to be updated, the entire resource must be sent, and any missing fields will be set to null or default values.
  • PATCH - It is used to update existing resources where only the specified fields in the resource are sent. This means that only the fields that need to be updated must be sent in the request body, and any other fields will remain unchanged.

PUT is used to completely replace a resource, while PATCH is used to partially update a resource.


How to send PUT request / Replace Crocodile

In this example, we will update the crocodile name using a PUT request, as a PUT request replace the resource on the server we need to send other fields as well even if we don't want to update these field.

How to handle 301 HTTP status code

When we first send a PUT request to the server it returns a 301 status code indicating that the resource has been moved permanently to another endpoint which is given in the header under the Location field. To complete the request, we need to resend the PUT request to a new endpoint given the Location field.

Below is the flow for sending PUT requests, you can see here when we sent the first PUT request Content-Length was 0 means the response has no body, for the second request Content-Length was 91 as the request was successfully processed with status code 200.

Code

@Test
public void replaceCrocodilesTest(){
    String crocodileName = "Test Crocodile";
    String gender = "M";
    String dob = "2010-06-27";
    String crocodileNewName = "Replaced Crocodile";

    APIResponseDetailsExtractor createCrocodileResponse = createCrocodile(crocodileName,gender,dob);
    //Validate status code is 201
    Assert.assertEquals(createCrocodileResponse.getStatusCode(),201);
    //Get crocodile id
    CrocodilesPOJO crocodileDetails = createCrocodileFlow.getCrocodilesPojoResponse();
    int crocodileId = crocodileDetails.getId();
    System.out.println("Crocodile id is: "+crocodileId);

    APIResponseDetailsExtractor replacedCrocodileResponse = createCrocodileFlow.replaceCrocodile(authToken,crocodileId,crocodileNewName,gender,dob);
    Assert.assertEquals(replacedCrocodileResponse.getStatusCode(),200);

    //Validate crocodile name is updated
    CrocodilesPOJO replacedCrocodileDetails = createCrocodileFlow.getCrocodilesPojoResponse();
    Assert.assertEquals(replacedCrocodileDetails.getCrocodileName(),crocodileNewName);
}
public APIResponseDetailsExtractor replaceCrocodile(String authToken, int crocodileId, String crocodileName,String crocodileGender, String crocodileDOB) {
    CrocodilesPOJO replaceCrocodilePOJO = new CrocodilesPOJO(crocodileName,crocodileGender,crocodileDOB);
    Response response = RestAssured.given()
            .contentType(ContentType.JSON)
            .header("Authorization","Bearer "+authToken)
            .body(replaceCrocodilePOJO)
            .log().all()
            .put(crocodilesBasePath+crocodileId);

    response.then().log().all();

    String redirectUrl = response.getHeader("Location");
    response = RestAssured.given()
            .contentType(ContentType.JSON)
            .header("Authorization","Bearer "+authToken)
            .body(replaceCrocodilePOJO)
            .log().all()
            .put(baseUri+redirectUrl);

    crocodilesPojoResponse = response.as(CrocodilesPOJO.class);

    response.then().log().all();

    return new APIResponseDetailsExtractor(response);
}

How to send PATCH request / Update Crocodile

In this example, we will update the crocodile name using the PATCH request, as the PATCH request is used to partially update resources we only need to send the field which we want to update.

In PATCH requests also a server first sends 301 requests with a location header and we need to handle them in the same way as discussed in the previous section.

@Test
public void updateCrocodilesTest(){
    String crocodileName = "Test Crocodile";
    String gender = "M";
    String dob = "2010-06-27";
    String crocodileNewName = "Updated Crocodile";

    APIResponseDetailsExtractor createCrocodileResponse = createCrocodile(crocodileName,gender,dob);
    //Validate status code is 201
    Assert.assertEquals(createCrocodileResponse.getStatusCode(),201);
    //Get crocodile id
    CrocodilesPOJO crocodileDetails = createCrocodileFlow.getCrocodilesPojoResponse();
    int crocodileId = crocodileDetails.getId();
    System.out.println("Crocodile id is: "+crocodileId);

    APIResponseDetailsExtractor updatedCrocodileResponse = createCrocodileFlow.updateCrocodile(authToken,crocodileId,crocodileNewName);
    Assert.assertEquals(updatedCrocodileResponse.getStatusCode(),200);

    //Validate crocodile name is updated
    CrocodilesPOJO updatedCrocodileDetails = createCrocodileFlow.getCrocodilesPojoResponse();
    Assert.assertEquals(updatedCrocodileDetails.getCrocodileName(),crocodileNewName);
}
public APIResponseDetailsExtractor updateCrocodile(String authToken,int crocodileId, String crocodileName){
    String newName = "{\"name\": \""+crocodileName+"\"}";
    Response response = RestAssured.given()
            .contentType(ContentType.JSON)
            .header("Authorization","Bearer "+authToken)
            .body(newName)
            .log().body()
            .patch(crocodilesBasePath+crocodileId);

    response.then().log().body();

    String redirectUrl = response.getHeader("Location");
    System.out.println("Location "+ redirectUrl);
    response = RestAssured.given()
            .contentType(ContentType.JSON)
            .header("Authorization","Bearer "+authToken)
            .body(newName)
            .log().all()
            .patch(baseUri+redirectUrl);

    crocodilesPojoResponse = response.as(CrocodilesPOJO.class);

    return new APIResponseDetailsExtractor(response);
}

You can full code at Git Hub Repo Link - https://github.com/sksingh329/RestAssuredTutorials

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!