Skip to content

Instantly share code, notes, and snippets.

@Ankit-Laddha
Created September 4, 2018 10:14
Show Gist options
  • Select an option

  • Save Ankit-Laddha/f6b4be0c7211b00581fadafc4fe473d1 to your computer and use it in GitHub Desktop.

Select an option

Save Ankit-Laddha/f6b4be0c7211b00581fadafc4fe473d1 to your computer and use it in GitHub Desktop.
package com.citi.cpb.sit.assertions;
import io.restassured.response.Response;
import static org.assertj.core.api.Assertions.assertThat;
public class ResponseAssertion {
private static final int maxChars = 2000;
public static final int STATUS_OK = 200;
private static final int STATUS_CREATED = 201;
public static final int STATUS_ACCEPTED = 202;
public static final int STATUS_BAD_REQUEST = 400;
public static final int STATUS_NOT_AUTHENTICATED = 401;
public static final int STATUS_FORBIDDEN = 403;
public static final int STATUS_NOT_FOUND = 404;
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
private static Response response = null;
public ResponseAssertion(Response response) {
this.response = response;
}
public ResponseAssertion assertResponseStatusOK() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_OK, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_OK);
return this;
}
public ResponseAssertion assertResponseStatusAccepted() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_ACCEPTED, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_ACCEPTED);
return this;
}
public ResponseAssertion assertResponseStatusCreated() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_CREATED, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_CREATED);
return this;
}
public ResponseAssertion assertResponseStatusBadRequest() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_BAD_REQUEST, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_BAD_REQUEST);
return this;
}
public ResponseAssertion assertResponseStatusUnauthenticated() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_NOT_AUTHENTICATED, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_NOT_AUTHENTICATED);
return this;
}
public ResponseAssertion assertResponseStatusForbidden() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_FORBIDDEN, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_FORBIDDEN);
return this;
}
public ResponseAssertion assertResponseStatusPageNotFound() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_NOT_FOUND, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_NOT_FOUND);
return this;
}
public ResponseAssertion assertResponseStatusInternalServerError() {
assertThat(response.statusCode())
.as(String.format("Expected Status: '%d;, Returned: '%d' and Details: '%s'", STATUS_INTERNAL_SERVER_ERROR, response.statusCode(), getErrorDetails(response)))
.isEqualTo(STATUS_INTERNAL_SERVER_ERROR);
return this;
}
public static String getErrorDetails(Response response) {
String body = withoutUnprintableCharacters(response.getBody().asString());
int length = body.length();
if (length <= maxChars) {
return body;
}
String truncatedBody = body.substring(0, maxChars);
int charactersRemoved = length - maxChars;
return String.format("%s ... <truncated %d characters>", truncatedBody, charactersRemoved);
}
private static String withoutUnprintableCharacters(String str) {
return str.replaceAll("\\p{C}", "?");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment