Skip to content

Commit dd9ef3b

Browse files
Feature/adding getting started (#80)
Initial setup included getting-started, data-tests, and headers functionality, along with config and Maven build fixes. Documentation was created and updated, including README improvements and new sections like "Create Entity."
1 parent 5dbe094 commit dd9ef3b

File tree

28 files changed

+1356
-14
lines changed

28 files changed

+1356
-14
lines changed

bellatrix.data/src/main/java/solutions/bellatrix/data/http/configuration/HttpSettings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import solutions.bellatrix.data.configuration.DataSettings;
77
import solutions.bellatrix.data.http.authentication.Authentication;
88
import solutions.bellatrix.data.http.authentication.AuthenticationMethod;
9+
import solutions.bellatrix.data.http.infrastructure.HttpHeader;
910

11+
import java.util.LinkedHashSet;
1012
import java.util.function.Consumer;
1113

1214
@Data
@@ -21,13 +23,16 @@ public class HttpSettings {
2123
private Authentication authentication;
2224
@SerializedName("urlEncoderEnabled")
2325
private boolean urlEncoderEnabled;
26+
@SerializedName("headers")
27+
private LinkedHashSet<HttpHeader> headers;
2428

2529
public HttpSettings(HttpSettings httpSettings) {
2630
setBaseUrl(httpSettings.getBaseUrl());
2731
setBasePath(httpSettings.getBasePath());
2832
setContentType(httpSettings.getContentType());
2933
setUrlEncoderEnabled(httpSettings.isUrlEncoderEnabled());
3034
setAuthentication(httpSettings.getAuthentication());
35+
setHeaders(httpSettings.getHeaders());
3136
}
3237

3338
public static HttpSettings custom(Consumer<HttpSettings> httpSettings) {

bellatrix.data/src/main/java/solutions/bellatrix/data/http/httpContext/HttpContext.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import solutions.bellatrix.data.http.authentication.AuthenticationMethod;
1010
import solutions.bellatrix.data.http.configuration.HttpSettings;
1111
import solutions.bellatrix.data.http.infrastructure.HTTPMethod;
12+
import solutions.bellatrix.data.http.infrastructure.HttpHeader;
1213
import solutions.bellatrix.data.http.infrastructure.QueryParameter;
1314

1415
import java.util.*;
@@ -18,16 +19,16 @@ public class HttpContext {
1819
private final HttpSettings httpSettings;
1920
private final LinkedList<Object> pathParameters;
2021
private final LinkedHashSet<QueryParameter> queryParameters;
21-
@Setter
22-
private RequestSpecBuilder specBuilder;
23-
@Getter
24-
private String requestBody;
22+
private final LinkedHashSet<HttpHeader> httpHeaders;
23+
@Setter private RequestSpecBuilder specBuilder;
24+
@Getter private String requestBody;
2525
@Getter private HTTPMethod httpMethod;
2626

2727
public HttpContext(HttpSettings settings) {
2828
this.httpSettings = settings;
2929
this.pathParameters = new LinkedList<>();
3030
this.queryParameters = new LinkedHashSet<>();
31+
this.httpHeaders = httpSettings.getHeaders();
3132
this.specBuilder = createInitialSpecBuilder(httpSettings);
3233
}
3334

@@ -36,6 +37,7 @@ public HttpContext(HttpContext httpContext) {
3637
this.queryParameters = httpContext.getQueryParameters();
3738
this.pathParameters = httpContext.getPathParameters();
3839
this.specBuilder = httpContext.getRequestBuilder();
40+
this.httpHeaders = httpContext.getHeaders();
3941
}
4042

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

63+
public LinkedHashSet<HttpHeader> getHeaders() {
64+
return new LinkedHashSet<>(httpHeaders);
65+
}
66+
6167
public LinkedHashSet<QueryParameter> getQueryParameters() {
6268
return new LinkedHashSet<>(queryParameters);
6369
}
@@ -67,14 +73,16 @@ private RequestSpecBuilder getRequestBuilder() {
6773
}
6874

6975
public RequestSpecification requestSpecification() {
70-
if (requestBody != null) {
76+
if (Objects.nonNull(requestBody)) {
7177
specBuilder.setBody(requestBody);
7278
}
7379

7480
if (!queryParameters.isEmpty()) {
7581
specBuilder.addQueryParams(getRequestQueryParameters());
7682
}
7783

84+
specBuilder.addHeaders(getRequestHeaders());
85+
7886
specBuilder.setBasePath(buildRequestPath());
7987

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

109+
public void addHeader(HttpHeader httpHeader) {
110+
httpHeaders.add(httpHeader);
111+
}
112+
113+
public void addHeaders(Collection<HttpHeader> headers) {
114+
headers.forEach(this::addHeader);
115+
}
116+
101117
public void addQueryParameter(QueryParameter parameter) {
102118
queryParameters.add(parameter);
103119
}
104120

121+
private Map<String, String> getRequestHeaders() {
122+
LinkedHashMap<String, String> requestHeaders = new LinkedHashMap<>();
123+
httpHeaders.forEach(x -> {
124+
requestHeaders.put(x.getName(), x.getValue());
125+
});
126+
127+
return requestHeaders;
128+
}
129+
105130
private Map<String, String> getRequestQueryParameters() {
106131
//todo: this logic should be extracted because create tightly coupling with settings
107132
LinkedHashMap<String, String> queryParams = new LinkedHashMap<>();
@@ -139,7 +164,6 @@ private Map<String, String> getRequestQueryParameters() {
139164
return queryParams;
140165
}
141166

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

153177
private RequestSpecBuilder createInitialSpecBuilder(HttpSettings httpSettings) {
178+
LinkedHashMap<String, String> initialHeaders = new LinkedHashMap<>();
179+
httpHeaders.forEach(x -> {
180+
initialHeaders.put(x.getName(), x.getValue());
181+
});
182+
154183
return new RequestSpecBuilder()
155184
.setBaseUri(httpSettings.getBaseUrl())
156-
.setBasePath(httpSettings.getBasePath()).setContentType(httpSettings.getContentType())
185+
.addHeaders(initialHeaders)
186+
.setBasePath(httpSettings.getBasePath())
187+
.setContentType(httpSettings.getContentType())
157188
.setAuth(AuthSchemaFactory.getAuthenticationScheme(httpSettings.getAuthentication()))
158189
.setUrlEncodingEnabled(httpSettings.isUrlEncoderEnabled())
159190
.log(LogDetail.ALL);

bellatrix.data/src/main/java/solutions/bellatrix/data/http/infrastructure/Entity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import solutions.bellatrix.data.contracts.Repository;
66

77
@SuperBuilder
8+
@SuppressWarnings("unchecked")
89
public abstract class Entity<TIdentifier, TEntity> {
910
public TEntity get() {
1011
var repository = (Repository<Entity>)RepositoryFactory.INSTANCE.getRepository(this.getClass());

bellatrix.data/src/main/java/solutions/bellatrix/data/http/infrastructure/HttpEntity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package solutions.bellatrix.data.http.infrastructure;
22

3-
import io.restassured.response.Response;
43
import lombok.Data;
54
import lombok.EqualsAndHashCode;
65
import lombok.experimental.SuperBuilder;
@@ -12,7 +11,7 @@
1211
@SuperBuilder
1312
@EqualsAndHashCode(callSuper = true)
1413
public abstract class HttpEntity<TIdentifier, TEntity> extends Entity<TIdentifier, TEntity> implements Queryable {
15-
private transient Response response;
14+
private transient HttpResponse response;
1615

1716
public boolean hasInvalidIdentifier() {
1817
return Objects.isNull(this.getIdentifier());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package solutions.bellatrix.data.http.infrastructure;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class HttpHeader {
7+
private final String name;
8+
private final String value;
9+
10+
public HttpHeader(String name, String value) {
11+
this.name = name;
12+
this.value = value;
13+
}
14+
15+
@Override
16+
public boolean equals(Object obj) {
17+
HttpHeader ob = (HttpHeader)obj;
18+
return this.value.equals(ob.getValue());
19+
}
20+
}

bellatrix.data/src/main/java/solutions/bellatrix/data/http/infrastructure/HttpRepository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
@SuppressWarnings("unchecked")
1920
public abstract class HttpRepository<THttpEntity extends HttpEntity> implements Repository<THttpEntity> {
2021
public static final EventListener<HttpRequestEventArgs> SENDING_REQUEST = new EventListener<>();
2122
public static final EventListener<ResponseProcessingEventArgs> REQUEST_SENT = new EventListener<>();
@@ -161,11 +162,11 @@ private <R> R deserializeInternal(HttpResponse response, DeserializationMode mod
161162
try {
162163
if (mode == DeserializationMode.LIST) {
163164
List<THttpEntity> entities = objectConverter.fromStringToList(response.getBody(), entityType);
164-
entities.forEach(entity -> entity.setResponse(response.getResponse()));
165+
entities.forEach(entity -> entity.setResponse(response));
165166
return (R)entities;
166167
} else {
167168
THttpEntity entity = objectConverter.fromString(response.getBody(), entityType);
168-
entity.setResponse(response.getResponse());
169+
entity.setResponse(response);
169170
return (R)entity;
170171
}
171172
} catch (Exception e) {

bellatrix.data/src/main/java/solutions/bellatrix/data/http/infrastructure/HttpResponse.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import io.restassured.response.Response;
44
import lombok.Getter;
5+
import solutions.bellatrix.data.http.infrastructure.internal.HttpStatusCode;
56

67
@Getter
78
public class HttpResponse {
89
private final String body;
9-
private final Response response;
10+
private final Response nativeResponse;
11+
private final HttpStatusCode statusCode;
1012

1113
public HttpResponse(String body, Response response) {
1214
this.body = body;
13-
this.response = response;
15+
this.nativeResponse = response;
16+
statusCode = HttpStatusCode.parse(response.getStatusCode());
1417
}
1518
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package solutions.bellatrix.data.http.infrastructure.internal;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public enum HttpStatusCode {
7+
// 1xx Informational responses
8+
CONTINUE(100, "Continue"),
9+
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
10+
PROCESSING(102, "Processing"),
11+
EARLY_HINTS(103, "Early Hints"),
12+
13+
// 2xx Success
14+
OK(200, "OK"),
15+
CREATED(201, "Created"),
16+
ACCEPTED(202, "Accepted"),
17+
NO_CONTENT(204, "No Content"),
18+
// 3xx Redirection
19+
MULTIPLE_CHOICES(300, "Multiple Choices"),
20+
MOVED_PERMANENTLY(301, "Moved Permanently"),
21+
FOUND(302, "Found"),
22+
23+
// 4xx Client errors
24+
BAD_REQUEST(400, "Bad Request"),
25+
UNAUTHORIZED(401, "Unauthorized"),
26+
FORBIDDEN(403, "Forbidden"),
27+
NOT_FOUND(404, "Not Found"),
28+
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
29+
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
30+
31+
// 5xx Server errors
32+
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
33+
NOT_IMPLEMENTED(501, "Not Implemented"),
34+
BAD_GATEWAY(502, "Bad Gateway"),
35+
SERVICE_UNAVAILABLE(503, "Service Unavailable");
36+
37+
private final int code;
38+
private final String reasonPhrase;
39+
40+
HttpStatusCode(int code, String reasonPhrase) {
41+
this.code = code;
42+
this.reasonPhrase = reasonPhrase;
43+
}
44+
45+
public static HttpStatusCode parse(int statusCode) {
46+
for (var state : values()) {
47+
int enumDisplayValue = state.getCode();
48+
if (enumDisplayValue == statusCode) {
49+
return state;
50+
}
51+
}
52+
53+
throw new IllegalArgumentException("Not found");
54+
}
55+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>solutions.bellatrix</groupId>
8+
<artifactId>bellatrix</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<relativePath>../../pom.xml</relativePath>
11+
</parent>
12+
13+
<artifactId>bellatrix.data.tests</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>19</maven.compiler.source>
17+
<maven.compiler.target>19</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>solutions.bellatrix</groupId>
24+
<artifactId>bellatrix.data</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package infrastructure.artist;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
import lombok.experimental.SuperBuilder;
7+
import solutions.bellatrix.data.http.infrastructure.HttpEntity;
8+
9+
@Data
10+
@SuperBuilder
11+
@EqualsAndHashCode(callSuper = true)
12+
public class Artist extends HttpEntity<String, Artist> {
13+
@SerializedName("ArtistId")
14+
private String id;
15+
16+
@SerializedName("Name")
17+
private String name;
18+
19+
@Override
20+
public String getIdentifier() {
21+
return id;
22+
}
23+
}

0 commit comments

Comments
 (0)