forked from oskardudycz/EventSourcing.JVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiSpecification.java
206 lines (153 loc) · 6.26 KB
/
ApiSpecification.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package io.eventdriven.ecommerce.testing;
import io.eventdriven.ecommerce.core.http.ETag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.*;
public abstract class ApiSpecification {
protected String apiPrefix;
@LocalServerPort
protected int port;
@Autowired
protected TestRestTemplate restTemplate;
public ApiSpecification(String apiPrefix) {
this.apiPrefix = apiPrefix;
}
public String getApiUrl() {
return "http://localhost:%s/%s".formatted(port, apiPrefix);
}
public ApiSpecificationBuilder given(Supplier<Object> define) {
return new ApiSpecificationBuilder(this, define);
}
///////////////////
//// WHEN ////
///////////////////
public <T> BiFunction<TestRestTemplate, Object, ResponseEntity<T>> GET(Class<T> entityClass) {
return GET("", entityClass);
}
public <T> BiFunction<TestRestTemplate, Object, ResponseEntity<T>> GET(String urlSuffix, Class<T> entityClass) {
return GET(urlSuffix, null, entityClass);
}
public <T> BiFunction<TestRestTemplate, Object, ResponseEntity<T>> GET(@Nullable ETag eTag, Class<T> entityClass) {
return GET("", eTag, entityClass);
}
public <T> BiFunction<TestRestTemplate, Object, ResponseEntity<T>> GET(String urlSuffix, @Nullable ETag eTag, Class<T> entityClass) {
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if (eTag != null) {
headers.setIfNoneMatch(eTag.value());
}
return (api, request) -> this.restTemplate
.exchange(
getApiUrl() + urlSuffix + (request != "" ? "/%s".formatted(request) : request),
HttpMethod.GET,
new HttpEntity<>(null, getIfNoneMatchHeader(eTag)),
entityClass
);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> POST = POST("");
public BiFunction<TestRestTemplate, Object, ResponseEntity> POST(String urlSuffix) {
return (api, request) -> this.restTemplate
.postForEntity(getApiUrl() + urlSuffix, request, Void.class);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> POST(String urlSuffix, ETag eTag) {
return (api, request) -> this.restTemplate
.postForEntity(
getApiUrl() + urlSuffix,
new HttpEntity<>(request, getIfMatchHeader(eTag)),
Void.class
);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> PUT(ETag eTag) {
return PUT("", eTag);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> PUT(String urlSuffix, ETag eTag) {
return PUT(urlSuffix, eTag, true);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> PUT(String urlSuffix, ETag eTag, boolean withEmptyBody) {
return (api, request) -> this.restTemplate
.exchange(
getApiUrl() + urlSuffix + (withEmptyBody ? "/%s".formatted(request) : ""),
HttpMethod.PUT,
new HttpEntity<>(!withEmptyBody ? request : null, getIfMatchHeader(eTag)),
Void.class
);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> DELETE(ETag eTag) {
return DELETE("", eTag);
}
public BiFunction<TestRestTemplate, Object, ResponseEntity> DELETE(String urlSuffix, ETag eTag) {
return (api, request) -> this.restTemplate
.exchange(
getApiUrl() + urlSuffix + (request != "" ? "/%s".formatted(request) : request),
HttpMethod.DELETE,
new HttpEntity<>(null, getIfMatchHeader(eTag)),
Void.class
);
}
HttpHeaders getHeaders(Consumer<HttpHeaders> consumer) {
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
consumer.accept(headers);
return headers;
}
HttpHeaders getIfMatchHeader(ETag eTag) {
return getHeaders(headers -> headers.setIfMatch(eTag.value()));
}
HttpHeaders getIfNoneMatchHeader(@Nullable ETag eTag) {
return getHeaders(headers -> {
if (eTag != null)
headers.setIfNoneMatch(eTag.value());
});
}
///////////////////
//// THEN ////
///////////////////
public Consumer<ResponseEntity> OK = assertResponseStatus(HttpStatus.OK);
public Consumer<ResponseEntity> CREATED =
response -> {
assertResponseStatus(HttpStatus.CREATED).accept(response);
var locationHeader = response.getHeaders().getLocation();
assertNotNull(locationHeader);
var location = locationHeader.toString();
assertTrue(location.startsWith(apiPrefix));
assertDoesNotThrow(() -> UUID.fromString(location.substring(apiPrefix.length() + 1)));
};
public Consumer<ResponseEntity> BAD_REQUEST = assertResponseStatus(HttpStatus.BAD_REQUEST);
public Consumer<ResponseEntity> NOT_FOUND = assertResponseStatus(HttpStatus.NOT_FOUND);
public Consumer<ResponseEntity> CONFLICT = assertResponseStatus(HttpStatus.CONFLICT);
public Consumer<ResponseEntity> PRECONDITION_FAILED = assertResponseStatus(HttpStatus.PRECONDITION_FAILED);
public Consumer<ResponseEntity> METHOD_NOT_ALLOWED = assertResponseStatus(HttpStatus.METHOD_NOT_ALLOWED);
private Consumer<ResponseEntity> assertResponseStatus(HttpStatus status) {
return (response) -> assertEquals(status, response.getStatusCode());
}
/////////////////////
//// BUILDER ////
/////////////////////
protected class ApiSpecificationBuilder {
private final ApiSpecification api;
private final Supplier<Object> given;
private BiFunction<TestRestTemplate, Object, ResponseEntity> when;
private ResponseEntity response;
private ApiSpecificationBuilder(ApiSpecification api, Supplier<Object> given) {
this.api = api;
this.given = given;
}
public ApiSpecificationBuilder when(BiFunction<TestRestTemplate, Object, ResponseEntity> when) {
this.when = when;
return this;
}
public ApiSpecificationBuilder then(Consumer<ResponseEntity> then) {
var request = given.get();
var response = when.apply(api.restTemplate, request);
then.accept(response);
return this;
}
}
}