From 4677475d4bce185c1e923e747ac2dd9afe4dbe08 Mon Sep 17 00:00:00 2001 From: Felseje Date: Tue, 31 Mar 2026 16:24:57 -0300 Subject: [PATCH] Improving the request and response encapsulating classes --- pom.xml | 2 +- .../{RequestCall.java => ApiRequest.java} | 10 +- .../core/request/RetryRequest.java | 16 +- .../core/response/ApiResponse.java | 155 ++++++++++++++++++ .../core/response/ResponseContext.java | 136 --------------- 5 files changed, 169 insertions(+), 150 deletions(-) rename src/main/java/io/github/felseje/apitestautomation/core/request/{RequestCall.java => ApiRequest.java} (61%) create mode 100644 src/main/java/io/github/felseje/apitestautomation/core/response/ApiResponse.java delete mode 100644 src/main/java/io/github/felseje/apitestautomation/core/response/ResponseContext.java diff --git a/pom.xml b/pom.xml index ac5f880..6c7a439 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.felseje java-api-test-automation-framework - 1.0.2 + 1.0.3 1.0.12 diff --git a/src/main/java/io/github/felseje/apitestautomation/core/request/RequestCall.java b/src/main/java/io/github/felseje/apitestautomation/core/request/ApiRequest.java similarity index 61% rename from src/main/java/io/github/felseje/apitestautomation/core/request/RequestCall.java rename to src/main/java/io/github/felseje/apitestautomation/core/request/ApiRequest.java index 39489d7..566b7c1 100644 --- a/src/main/java/io/github/felseje/apitestautomation/core/request/RequestCall.java +++ b/src/main/java/io/github/felseje/apitestautomation/core/request/ApiRequest.java @@ -1,20 +1,20 @@ package io.github.felseje.apitestautomation.core.request; -import io.github.felseje.apitestautomation.core.response.ResponseContext; +import io.github.felseje.apitestautomation.core.response.ApiResponse; import io.github.felseje.apitestautomation.util.ArgumentValidator; import java.util.function.Supplier; -public class RequestCall { +public class ApiRequest { - private final Supplier action; + private final Supplier action; - public RequestCall(Supplier action) { + public ApiRequest(Supplier action) { ArgumentValidator.requireNotNull(action, "The 'action' cannot be null"); this.action = action; } - public ResponseContext execute() { + public ApiResponse execute() { return action.get(); } diff --git a/src/main/java/io/github/felseje/apitestautomation/core/request/RetryRequest.java b/src/main/java/io/github/felseje/apitestautomation/core/request/RetryRequest.java index 19af22f..407a91a 100644 --- a/src/main/java/io/github/felseje/apitestautomation/core/request/RetryRequest.java +++ b/src/main/java/io/github/felseje/apitestautomation/core/request/RetryRequest.java @@ -1,6 +1,6 @@ package io.github.felseje.apitestautomation.core.request; -import io.github.felseje.apitestautomation.core.response.ResponseContext; +import io.github.felseje.apitestautomation.core.response.ApiResponse; import io.github.felseje.apitestautomation.util.ArgumentValidator; import org.awaitility.Awaitility; @@ -14,14 +14,14 @@ public class RetryRequest { private static final String NULL_TIME_UNIT_ERROR = "The 'unit' cannot be null"; - private final Supplier action; + private final Supplier action; private Duration timeout = Duration.ofSeconds(10); private Duration poll = Duration.ofMillis(500); private RetryStrategy strategy = RetryStrategy.AT_MOST; private Duration minDuration; private Duration maxDuration; - public RetryRequest(Supplier action) { + public RetryRequest(Supplier action) { ArgumentValidator.requireNotNull(action, "The 'action' cannot be null"); this.action = action; } @@ -55,16 +55,16 @@ public RetryRequest pollEvery(long time, TimeUnit unit) { return this; } - public ResponseContext until(Predicate condition) { + public ApiResponse until(Predicate condition) { ArgumentValidator.requireNotNull(condition, "The 'condition' cannot be null"); - AtomicReference last = new AtomicReference<>(); + AtomicReference last = new AtomicReference<>(); switch (strategy) { case AT_MOST -> Awaitility.await() .atMost(timeout) .pollInterval(poll) .ignoreExceptions() .until(() -> { - ResponseContext response = action.get(); + ApiResponse response = action.get(); last.set(response); return condition.test(response); }); @@ -74,7 +74,7 @@ public ResponseContext until(Predicate condition) { .pollInterval(poll) .ignoreExceptions() .until(() -> { - ResponseContext response = action.get(); + ApiResponse response = action.get(); last.set(response); return condition.test(response); }); @@ -85,7 +85,7 @@ public ResponseContext until(Predicate condition) { .pollInterval(poll) .ignoreExceptions() .until(() -> { - ResponseContext response = action.get(); + ApiResponse response = action.get(); last.set(response); return condition.test(response); }); diff --git a/src/main/java/io/github/felseje/apitestautomation/core/response/ApiResponse.java b/src/main/java/io/github/felseje/apitestautomation/core/response/ApiResponse.java new file mode 100644 index 0000000..ea525b3 --- /dev/null +++ b/src/main/java/io/github/felseje/apitestautomation/core/response/ApiResponse.java @@ -0,0 +1,155 @@ +package io.github.felseje.apitestautomation.core.response; + +import io.github.felseje.apitestautomation.util.ArgumentValidator; +import io.restassured.common.mapper.TypeRef; +import io.restassured.module.jsv.JsonSchemaValidator; +import io.restassured.response.Response; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +import static org.apache.http.HttpStatus.*; +import static org.hamcrest.Matchers.*; + +@RequiredArgsConstructor +public class ApiResponse { + + private static final int SC_TOO_MANY_REQUESTS = 429; + private final Response response; + + public ApiResponse shouldHaveStatus(int expectedStatusCode) { + response.then() + .assertThat() + .statusCode(expectedStatusCode); + return this; + } + + public ApiResponse shouldBeOk() { + return shouldHaveStatus(SC_OK); + } + + public ApiResponse shouldBeCreated() { + return shouldHaveStatus(SC_CREATED); + } + + public ApiResponse shouldBeAccepted() { + return shouldHaveStatus(SC_ACCEPTED); + } + + public ApiResponse shouldBeNoContent() { + return shouldHaveStatus(SC_NO_CONTENT); + } + + public ApiResponse shouldBeBadRequest() { + return shouldHaveStatus(SC_BAD_REQUEST); + } + + public ApiResponse shouldBeUnauthorized() { + return shouldHaveStatus(SC_UNAUTHORIZED); + } + + public ApiResponse shouldBeForbidden() { + return shouldHaveStatus(SC_FORBIDDEN); + } + + public ApiResponse shouldBeNotFound() { + return shouldHaveStatus(SC_NOT_FOUND); + } + + public ApiResponse shouldBeUnprocessableEntity() { + return shouldHaveStatus(SC_UNPROCESSABLE_ENTITY); + } + + public ApiResponse shouldBeTooManyRequests() { + return shouldHaveStatus(SC_TOO_MANY_REQUESTS); + } + + public ApiResponse shouldBeInternalServerError() { + return shouldHaveStatus(SC_INTERNAL_SERVER_ERROR); + } + + public ApiResponse shouldBeBadGateway() { + return shouldHaveStatus(SC_BAD_GATEWAY); + } + + public ApiResponse shouldBeServiceUnavailable() { + return shouldHaveStatus(SC_SERVICE_UNAVAILABLE); + } + + public ApiResponse shouldBeGatewayTimeout() { + return shouldHaveStatus(SC_GATEWAY_TIMEOUT); + } + + public ApiResponse shouldHaveJsonField(String path, Object value) { + ArgumentValidator.requireNotBlank(path, "Json path cannot be blank"); + response.then() + .assertThat() + .body(path, equalTo(value)); + return this; + } + + public ApiResponse shouldHaveHeader(String headerName, String expectedValue) { + ArgumentValidator.requireNotBlank(headerName, "Header name cannot be blank"); + ArgumentValidator.requireNotBlank(expectedValue, "Expected header value cannot be blank"); + response.then() + .assertThat() + .header(headerName, equalTo(expectedValue)); + return this; + } + + public ApiResponse shouldHaveHeaderContaining(String headerName, String expectedSubstring) { + ArgumentValidator.requireNotBlank(headerName, "Header name cannot be blank"); + ArgumentValidator.requireNotBlank(expectedSubstring, "Expected substring cannot be blank"); + response.then() + .assertThat() + .header(headerName, containsString(expectedSubstring)); + return this; + } + + public ApiResponse shouldHaveHeaderMatching(String headerName, String regex) { + ArgumentValidator.requireNotBlank(headerName, "Header name cannot be blank"); + ArgumentValidator.requireNotBlank(regex, "Regex cannot be blank"); + response.then() + .assertThat() + .header(headerName, matchesPattern(regex)); + return this; + } + + public ApiResponse shouldHaveXmlField(String xpath, String value) { + ArgumentValidator.requireNotBlank(xpath, "Xml path cannot be blank"); + response.then() + .assertThat() + .body(hasXPath(xpath, equalTo(value))); + return this; + } + + public ApiResponse shouldMatchJsonSchema(String schemaJson) { + ArgumentValidator.requireNotBlank(schemaJson, "Schema JSON cannot be blank"); + response.then() + .assertThat() + .body(JsonSchemaValidator.matchesJsonSchema(schemaJson)); + return this; + } + + public ApiResponse shouldMatchJsonSchemaInClasspath(String classpathResource) { + ArgumentValidator.requireNotBlank(classpathResource, "Classpath resource cannot be blank"); + response.then() + .assertThat() + .body(JsonSchemaValidator.matchesJsonSchemaInClasspath(classpathResource)); + return this; + } + + public Response raw() { + return response; + } + + public T as(Class clazz) { + return response.as(clazz); + } + + public List asList(Class clazz) { + return response.as(new TypeRef>() { + }); + } + +} diff --git a/src/main/java/io/github/felseje/apitestautomation/core/response/ResponseContext.java b/src/main/java/io/github/felseje/apitestautomation/core/response/ResponseContext.java deleted file mode 100644 index 1d109b1..0000000 --- a/src/main/java/io/github/felseje/apitestautomation/core/response/ResponseContext.java +++ /dev/null @@ -1,136 +0,0 @@ -package io.github.felseje.apitestautomation.core.response; - -import io.github.felseje.apitestautomation.util.ArgumentValidator; -import io.restassured.module.jsv.JsonSchemaValidator; -import io.restassured.response.Response; - -public class ResponseContext { - - private final Response response; - - public ResponseContext(Response response) { - ArgumentValidator.requireNotNull(response, "The 'response' cannot be null"); - this.response = response; - } - - public int statusCode() { - return response.getStatusCode(); - } - - public ResponseContext assertStatusCode(int expectedStatusCode) { - if (statusCode() != expectedStatusCode) { - String errorMessage = "Status code does not match the expected value. Actual: %d, Expected: %d." - .formatted(statusCode(), expectedStatusCode); - throw new AssertionError(errorMessage); - } - return this; - } - - public String header(String name) { - ArgumentValidator.requireNotBlank(name, "The header name cannot be blank"); - return response.getHeader(name); - } - - public ResponseContext assertHeader(String name, String expectedValue) { - String actual = header(name); - if (!expectedValue.equals(actual)) { - throw new AssertionError("Header '" + name + "' does not match. Actual: " + actual + ", Expected: " + expectedValue); - } - return this; - } - - public T as(Class clazz) { - ArgumentValidator.requireNotNull(clazz, "The 'clazz' cannot be null"); - return response.as(clazz); - } - - public String body() { - return response.getBody().asString(); - } - - public ResponseContext assertBodyEquals(String expectedBody) { - ArgumentValidator.requireNotNull(expectedBody, "Expected body cannot be null"); - if (!body().equals(expectedBody)) { - throw new AssertionError( - "Body does not match. Actual: " + body() + ", Expected: " + expectedBody - ); - } - return this; - } - - public ResponseContext assertBodyContains(String expectedFragment) { - ArgumentValidator.requireNotNull(expectedFragment, "Expected fragment cannot be null"); - if (!body().contains(expectedFragment)) { - throw new AssertionError( - "Body does not contain expected fragment. Actual: " + body() + ", Expected fragment: " + expectedFragment - ); - } - return this; - } - - public ResponseContext assertJsonSchema(String schemaJson) { - ArgumentValidator.requireNotBlank(schemaJson, "Schema JSON cannot be blank"); - try { - response.then().assertThat().body(JsonSchemaValidator.matchesJsonSchema(schemaJson)); - } catch (AssertionError e) { - throw new AssertionError("JSON Schema validation failed: " + e.getMessage(), e); - } - return this; - } - - public ResponseContext assertJsonSchemaInClasspath(String classpathResource) { - ArgumentValidator.requireNotBlank(classpathResource, "Classpath resource cannot be blank"); - try { - var matches = JsonSchemaValidator.matchesJsonSchemaInClasspath(classpathResource); - response.then() - .assertThat() - .body(matches); - } catch (AssertionError e) { - throw new AssertionError("JSON Schema validation failed for resource: " + classpathResource + ".", e); - } - return this; - } - - public Response getRawResponse() { - return response; - } - - public T path(String jsonPath) { - ArgumentValidator.requireNotBlank(jsonPath, "The 'jsonPath' cannot be blank"); - return response.jsonPath().get(jsonPath); - } - - public boolean jsonPathExists(String jsonPath) { - return path(jsonPath) != null; - } - - public boolean isSuccess() { - return statusCode() >= 200 && statusCode() < 300; - } - - public boolean isClientError() { - return statusCode() >= 400 && statusCode() < 500; - } - - public boolean isServerError() { - return statusCode() >= 500 && statusCode() < 600; - } - - public ResponseContext logBody() { - response.then().log().body(); - return this; - } - - public ResponseContext logAll() { - response.then().log().all(); - return this; - } - - public ResponseContext logIfError() { - if (statusCode() < 400) { - return this; - } - return logAll(); - } - -}