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.3</version>
<version>1.0.4</version>

<properties>
<owner.version>1.0.12</owner.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import io.github.felseje.apitestautomation.core.request.RequestContext;
import io.github.felseje.apitestautomation.factory.RequestSpecificationFactory;
import io.github.felseje.apitestautomation.factory.ResponseSpecificationFactory;
import io.github.felseje.apitestautomation.util.Strings;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
Expand Down Expand Up @@ -47,7 +49,7 @@ private Response execute(RequestContext context, Method method, String endpoint)
RequestSpecification requestSpecification = RequestSpecificationFactory.getStandard();
ResponseSpecification responseSpecification = ResponseSpecificationFactory.getStandard();

if (context.getBaseUrl() != null) {
if (!Strings.isBlank(context.getBaseUrl())) {
requestSpecification.baseUri(context.getBaseUrl());
}

Expand All @@ -63,12 +65,24 @@ private Response execute(RequestContext context, Method method, String endpoint)
requestSpecification.queryParams(context.getQueryParams());
}

if (context.getContentType() != null) {
requestSpecification.contentType(context.getContentType());
}

if (context.getBody() != null) {
if (!context.getMultipartParts().isEmpty()) {
context.getMultipartParts().forEach(requestSpecification::multiPart);
} else if (!context.getFormParams().isEmpty()) {
context.getFormParams().forEach(requestSpecification::formParam);
if (context.getContentType() != null) {
requestSpecification.contentType(context.getContentType());
} else {
requestSpecification.contentType(ContentType.URLENC);
}
} else if (context.getBody() != null) {
requestSpecification.body(context.getBody());
if (context.getContentType() != null) {
requestSpecification.contentType(context.getContentType());
} else {
requestSpecification.contentType(ContentType.JSON);
}
} else {
requestSpecification.noContentType();
}

return RestAssured.given(requestSpecification, responseSpecification)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class RequestContext {
private final Map<String, Object> headers;
private final Map<String, Object> queryParams;
private final Map<String, Object> pathParams;
private final Map<String, Object> multipartParts;
private final Map<String, Object> formParams;
private final Object body;
private final ContentType contentType;

Expand All @@ -26,6 +28,8 @@ private RequestContext(RequestContextBuilder builder) {
this.headers = Map.copyOf(builder.headers);
this.pathParams = Map.copyOf(builder.pathParams);
this.queryParams = Map.copyOf(builder.queryParams);
this.multipartParts = Map.copyOf(builder.multipartParts);
this.formParams = Map.copyOf(builder.formParams);
this.body = builder.body;
}

Expand All @@ -35,11 +39,13 @@ public static RequestContextBuilder builder() {

public static class RequestContextBuilder {

private String baseUrl;
private ContentType contentType;
private final Map<String, Object> headers = new HashMap<>();
private final Map<String, Object> queryParams = new HashMap<>();
private final Map<String, Object> pathParams = new HashMap<>();
private final Map<String, Object> multipartParts = new HashMap<>();
private final Map<String, Object> formParams = new HashMap<>();
private String baseUrl;
private ContentType contentType;
private Object body;

public RequestContextBuilder baseUrl(String baseUrl) {
Expand All @@ -62,6 +68,16 @@ public RequestContextBuilder pathParam(String key, Object value) {
return this;
}

public RequestContextBuilder multipartPart(String key, Object value) {
multipartParts.put(key, value);
return this;
}

public RequestContextBuilder formParam(String key, Object value) {
formParams.put(key, value);
return this;
}

public RequestContextBuilder body(Object body) {
this.body = body;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ public ApiResponse shouldMatchJsonSchemaInClasspath(String classpathResource) {
return this;
}

public boolean isInformational() {
return isStatusCodeInRange(100, 200);
}

public boolean isSuccess() {
return isStatusCodeInRange(200, 300);
}

public boolean isRedirection() {
return isStatusCodeInRange(300, 400);
}

public boolean isClientError() {
return isStatusCodeInRange(400, 500);
}

public boolean isServerError() {
return isStatusCodeInRange(500, 600);
}

public boolean isStatusCodeInRange(int fromInclusive, int toExclusive) {
int statusCode = response.getStatusCode();
return statusCode >= fromInclusive && statusCode < toExclusive;
}

public Response raw() {
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@

import io.github.felseje.apitestautomation.config.Config;
import io.github.felseje.apitestautomation.config.ConfigurationManager;
import io.github.felseje.apitestautomation.util.Strings;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.filter.log.LogDetail;
import io.restassured.specification.RequestSpecification;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RequestSpecificationFactory {

private RequestSpecificationFactory() {
throw new IllegalStateException("Utility class cannot be instantiated");
}

public static RequestSpecification getStandard() {
Config config = ConfigurationManager.getConfig();
RestAssuredConfig restAssuredConfig = new RestAssuredConfig().httpClient(getHttpClientConfig(config));
return new RequestSpecBuilder()
.setBaseUri(config.baseUrl())
RequestSpecBuilder builder = new RequestSpecBuilder()
.setConfig(restAssuredConfig)
.setRelaxedHTTPSValidation()
.log(LogDetail.METHOD)
.log(LogDetail.URI)
.log(LogDetail.HEADERS)
.log(LogDetail.PARAMS)
.log(LogDetail.BODY)
.build();
.log(LogDetail.BODY);
if (!Strings.isBlank(config.baseUrl())) {
builder.setBaseUri(config.baseUrl());
}
return builder.build();
}

private static HttpClientConfig getHttpClientConfig(Config config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.filter.log.LogDetail;
import io.restassured.specification.ResponseSpecification;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ResponseSpecificationFactory {

private ResponseSpecificationFactory() {
throw new IllegalStateException("Utility class cannot be instantiated");
}

public static ResponseSpecification getStandard() {
return new ResponseSpecBuilder()
.log(LogDetail.ALL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
package io.github.felseje.apitestautomation.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ArgumentValidator {

private static final String DEFAULT_NULL_MESSAGE = "Argument cannot be null";
private static final String DEFAULT_BLANK_MESSAGE = "Argument cannot be blank";

private ArgumentValidator() {
throw new IllegalStateException("Utility class cannot be instantiated");
}

public static void requireNotNull(Object argument, String errorMessage) {
if (argument != null) {
return;
}
throw new IllegalArgumentException(
isBlank(errorMessage) ? DEFAULT_NULL_MESSAGE : errorMessage
Strings.isBlank(errorMessage) ? DEFAULT_NULL_MESSAGE : errorMessage
);
}

public static void requireNotBlank(String argument, String errorMessage) {
if (!isBlank(argument)) {
if (!Strings.isBlank(argument)) {
return;
}
throw new IllegalArgumentException(
isBlank(errorMessage) ? DEFAULT_BLANK_MESSAGE : errorMessage
Strings.isBlank(errorMessage) ? DEFAULT_BLANK_MESSAGE : errorMessage
);
}

private static boolean isBlank(String string) {
return string == null || string.isBlank();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.felseje.apitestautomation.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Strings {

public static boolean isBlank(String value) {
return value == null || value.isBlank();
}

}
Loading