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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import solutions.bellatrix.data.configuration.DataSettings;
import solutions.bellatrix.data.http.authentication.Authentication;
import solutions.bellatrix.data.http.authentication.AuthenticationMethod;
import solutions.bellatrix.data.http.infrastructure.HttpHeader;

import java.util.LinkedHashSet;
import java.util.function.Consumer;

@Data
Expand All @@ -21,13 +23,16 @@ public class HttpSettings {
private Authentication authentication;
@SerializedName("urlEncoderEnabled")
private boolean urlEncoderEnabled;
@SerializedName("headers")
private LinkedHashSet<HttpHeader> headers;

public HttpSettings(HttpSettings httpSettings) {
setBaseUrl(httpSettings.getBaseUrl());
setBasePath(httpSettings.getBasePath());
setContentType(httpSettings.getContentType());
setUrlEncoderEnabled(httpSettings.isUrlEncoderEnabled());
setAuthentication(httpSettings.getAuthentication());
setHeaders(httpSettings.getHeaders());
}

public static HttpSettings custom(Consumer<HttpSettings> httpSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import solutions.bellatrix.data.http.authentication.AuthenticationMethod;
import solutions.bellatrix.data.http.configuration.HttpSettings;
import solutions.bellatrix.data.http.infrastructure.HTTPMethod;
import solutions.bellatrix.data.http.infrastructure.HttpHeader;
import solutions.bellatrix.data.http.infrastructure.QueryParameter;

import java.util.*;
Expand All @@ -18,16 +19,16 @@ public class HttpContext {
private final HttpSettings httpSettings;
private final LinkedList<Object> pathParameters;
private final LinkedHashSet<QueryParameter> queryParameters;
@Setter
private RequestSpecBuilder specBuilder;
@Getter
private String requestBody;
private final LinkedHashSet<HttpHeader> httpHeaders;
@Setter private RequestSpecBuilder specBuilder;
@Getter private String requestBody;
@Getter private HTTPMethod httpMethod;

public HttpContext(HttpSettings settings) {
this.httpSettings = settings;
this.pathParameters = new LinkedList<>();
this.queryParameters = new LinkedHashSet<>();
this.httpHeaders = httpSettings.getHeaders();
this.specBuilder = createInitialSpecBuilder(httpSettings);
}

Expand All @@ -36,6 +37,7 @@ public HttpContext(HttpContext httpContext) {
this.queryParameters = httpContext.getQueryParameters();
this.pathParameters = httpContext.getPathParameters();
this.specBuilder = httpContext.getRequestBuilder();
this.httpHeaders = httpContext.getHeaders();
}

public HttpSettings getHttpSettings() {
Expand All @@ -58,6 +60,10 @@ public LinkedList<Object> getPathParameters() {
return new LinkedList<>(pathParameters);
}

public LinkedHashSet<HttpHeader> getHeaders() {
return new LinkedHashSet<>(httpHeaders);
}

public LinkedHashSet<QueryParameter> getQueryParameters() {
return new LinkedHashSet<>(queryParameters);
}
Expand All @@ -67,14 +73,16 @@ private RequestSpecBuilder getRequestBuilder() {
}

public RequestSpecification requestSpecification() {
if (requestBody != null) {
if (Objects.nonNull(requestBody)) {
specBuilder.setBody(requestBody);
}

if (!queryParameters.isEmpty()) {
specBuilder.addQueryParams(getRequestQueryParameters());
}

specBuilder.addHeaders(getRequestHeaders());

specBuilder.setBasePath(buildRequestPath());

return specBuilder.build();
Expand All @@ -98,10 +106,27 @@ public void addQueryParameters(Collection<QueryParameter> parameters) {
parameters.forEach(this::addQueryParameter);
}

public void addHeader(HttpHeader httpHeader) {
httpHeaders.add(httpHeader);
}

public void addHeaders(Collection<HttpHeader> headers) {
headers.forEach(this::addHeader);
}

public void addQueryParameter(QueryParameter parameter) {
queryParameters.add(parameter);
}

private Map<String, String> getRequestHeaders() {
LinkedHashMap<String, String> requestHeaders = new LinkedHashMap<>();
httpHeaders.forEach(x -> {
requestHeaders.put(x.getName(), x.getValue());
});

return requestHeaders;
}

private Map<String, String> getRequestQueryParameters() {
//todo: this logic should be extracted because create tightly coupling with settings
LinkedHashMap<String, String> queryParams = new LinkedHashMap<>();
Expand Down Expand Up @@ -139,7 +164,6 @@ private Map<String, String> getRequestQueryParameters() {
return queryParams;
}


public String buildRequestPath() {
if (!pathParameters.isEmpty()) {
String[] pathList = pathParameters.stream().filter(path -> !String.valueOf(path).isEmpty()).map(String::valueOf).toArray(String[]::new);
Expand All @@ -151,9 +175,16 @@ public String buildRequestPath() {
}

private RequestSpecBuilder createInitialSpecBuilder(HttpSettings httpSettings) {
LinkedHashMap<String, String> initialHeaders = new LinkedHashMap<>();
httpHeaders.forEach(x -> {
initialHeaders.put(x.getName(), x.getValue());
});

return new RequestSpecBuilder()
.setBaseUri(httpSettings.getBaseUrl())
.setBasePath(httpSettings.getBasePath()).setContentType(httpSettings.getContentType())
.addHeaders(initialHeaders)
.setBasePath(httpSettings.getBasePath())
.setContentType(httpSettings.getContentType())
.setAuth(AuthSchemaFactory.getAuthenticationScheme(httpSettings.getAuthentication()))
.setUrlEncodingEnabled(httpSettings.isUrlEncoderEnabled())
.log(LogDetail.ALL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import solutions.bellatrix.data.contracts.Repository;

@SuperBuilder
@SuppressWarnings("unchecked")
public abstract class Entity<TIdentifier, TEntity> {
public TEntity get() {
var repository = (Repository<Entity>)RepositoryFactory.INSTANCE.getRepository(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package solutions.bellatrix.data.http.infrastructure;

import io.restassured.response.Response;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
Expand All @@ -12,7 +11,7 @@
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public abstract class HttpEntity<TIdentifier, TEntity> extends Entity<TIdentifier, TEntity> implements Queryable {
private transient Response response;
private transient HttpResponse response;

public boolean hasInvalidIdentifier() {
return Objects.isNull(this.getIdentifier());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package solutions.bellatrix.data.http.infrastructure;

import lombok.Getter;

@Getter
public class HttpHeader {
private final String name;
private final String value;

public HttpHeader(String name, String value) {
this.name = name;
this.value = value;
}

@Override
public boolean equals(Object obj) {
HttpHeader ob = (HttpHeader)obj;
return this.value.equals(ob.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static solutions.bellatrix.data.http.infrastructure.HTTPMethod.*;

@SuppressWarnings("unchecked")
public abstract class HttpRepository<THttpEntity extends HttpEntity> implements Repository<THttpEntity> {
public static final EventListener<HttpRequestEventArgs> SENDING_REQUEST = new EventListener<>();
public static final EventListener<ResponseProcessingEventArgs> REQUEST_SENT = new EventListener<>();
Expand Down Expand Up @@ -161,11 +162,11 @@ private <R> R deserializeInternal(HttpResponse response, DeserializationMode mod
try {
if (mode == DeserializationMode.LIST) {
List<THttpEntity> entities = objectConverter.fromStringToList(response.getBody(), entityType);
entities.forEach(entity -> entity.setResponse(response.getResponse()));
entities.forEach(entity -> entity.setResponse(response));
return (R)entities;
} else {
THttpEntity entity = objectConverter.fromString(response.getBody(), entityType);
entity.setResponse(response.getResponse());
entity.setResponse(response);
return (R)entity;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import io.restassured.response.Response;
import lombok.Getter;
import solutions.bellatrix.data.http.infrastructure.internal.HttpStatusCode;

@Getter
public class HttpResponse {
private final String body;
private final Response response;
private final Response nativeResponse;
private final HttpStatusCode statusCode;

public HttpResponse(String body, Response response) {
this.body = body;
this.response = response;
this.nativeResponse = response;
statusCode = HttpStatusCode.parse(response.getStatusCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package solutions.bellatrix.data.http.infrastructure.internal;

import lombok.Getter;

@Getter
public enum HttpStatusCode {
// 1xx Informational responses
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
PROCESSING(102, "Processing"),
EARLY_HINTS(103, "Early Hints"),

// 2xx Success
OK(200, "OK"),
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NO_CONTENT(204, "No Content"),
// 3xx Redirection
MULTIPLE_CHOICES(300, "Multiple Choices"),
MOVED_PERMANENTLY(301, "Moved Permanently"),
FOUND(302, "Found"),

// 4xx Client errors
BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),

// 5xx Server errors
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
NOT_IMPLEMENTED(501, "Not Implemented"),
BAD_GATEWAY(502, "Bad Gateway"),
SERVICE_UNAVAILABLE(503, "Service Unavailable");

private final int code;
private final String reasonPhrase;

HttpStatusCode(int code, String reasonPhrase) {
this.code = code;
this.reasonPhrase = reasonPhrase;
}

public static HttpStatusCode parse(int statusCode) {
for (var state : values()) {
int enumDisplayValue = state.getCode();
if (enumDisplayValue == statusCode) {
return state;
}
}

throw new IllegalArgumentException("Not found");
}
}
29 changes: 29 additions & 0 deletions framework-tests/bellatrix.data.tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>bellatrix.data.tests</artifactId>

<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix.data</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package infrastructure.artist;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import solutions.bellatrix.data.http.infrastructure.HttpEntity;

@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class Artist extends HttpEntity<String, Artist> {
@SerializedName("ArtistId")
private String id;

@SerializedName("Name")
private String name;

@Override
public String getIdentifier() {
return id;
}
}
Loading