Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.felseje</groupId>
<artifactId>java-api-test-automation-framework</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>

<properties>
<owner.version>1.0.12</owner.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ResponseContext> action;
private final Supplier<ApiResponse> action;

public RequestCall(Supplier<ResponseContext> action) {
public ApiRequest(Supplier<ApiResponse> action) {
ArgumentValidator.requireNotNull(action, "The 'action' cannot be null");
this.action = action;
}

public ResponseContext execute() {
public ApiResponse execute() {
return action.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,14 +14,14 @@ public class RetryRequest {

private static final String NULL_TIME_UNIT_ERROR = "The 'unit' cannot be null";

private final Supplier<ResponseContext> action;
private final Supplier<ApiResponse> 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<ResponseContext> action) {
public RetryRequest(Supplier<ApiResponse> action) {
ArgumentValidator.requireNotNull(action, "The 'action' cannot be null");
this.action = action;
}
Expand Down Expand Up @@ -55,16 +55,16 @@ public RetryRequest pollEvery(long time, TimeUnit unit) {
return this;
}

public ResponseContext until(Predicate<ResponseContext> condition) {
public ApiResponse until(Predicate<ApiResponse> condition) {
ArgumentValidator.requireNotNull(condition, "The 'condition' cannot be null");
AtomicReference<ResponseContext> last = new AtomicReference<>();
AtomicReference<ApiResponse> 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);
});
Expand All @@ -74,7 +74,7 @@ public ResponseContext until(Predicate<ResponseContext> condition) {
.pollInterval(poll)
.ignoreExceptions()
.until(() -> {
ResponseContext response = action.get();
ApiResponse response = action.get();
last.set(response);
return condition.test(response);
});
Expand All @@ -85,7 +85,7 @@ public ResponseContext until(Predicate<ResponseContext> condition) {
.pollInterval(poll)
.ignoreExceptions()
.until(() -> {
ResponseContext response = action.get();
ApiResponse response = action.get();
last.set(response);
return condition.test(response);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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> T as(Class<T> clazz) {
return response.as(clazz);
}

public <T> List<T> asList(Class<T> clazz) {
return response.as(new TypeRef<List<T>>() {
});
}

}

This file was deleted.

Loading