-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestArchitectureResourceShould.java
More file actions
270 lines (229 loc) · 11.5 KB
/
TestArchitectureResourceShould.java
File metadata and controls
270 lines (229 loc) · 11.5 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package org.finos.calm.resources;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import org.finos.calm.domain.*;
import org.finos.calm.domain.exception.ArchitectureNotFoundException;
import org.finos.calm.domain.exception.ArchitectureVersionExistsException;
import org.finos.calm.domain.exception.ArchitectureVersionNotFoundException;
import org.finos.calm.domain.exception.NamespaceNotFoundException;
import org.finos.calm.store.ArchitectureStore;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@QuarkusTest
@ExtendWith(MockitoExtension.class)
public class TestArchitectureResourceShould {
@InjectMock
ArchitectureStore mockArchitectureStore;
@Test
void return_a_404_when_an_invalid_namespace_is_provided_on_get_architectures() throws NamespaceNotFoundException {
when(mockArchitectureStore.getArchitecturesForNamespace(anyString())).thenThrow(new NamespaceNotFoundException());
given()
.when()
.get("/calm/namespaces/invalid/architectures")
.then()
.statusCode(404);
verify(mockArchitectureStore, times(1)).getArchitecturesForNamespace("invalid");
}
@Test
void return_list_of_architecture_ids_when_valid_namespace_provided_on_get_architectures() throws NamespaceNotFoundException {
when(mockArchitectureStore.getArchitecturesForNamespace(anyString())).thenReturn(Arrays.asList(12345, 54321));
given()
.when()
.get("/calm/namespaces/finos/architectures")
.then()
.statusCode(200)
.body(equalTo("{\"values\":[12345,54321]}"));
verify(mockArchitectureStore, times(1)).getArchitecturesForNamespace("finos");
}
@Test
void return_a_404_when_invalid_namespace_is_provided_on_create_architecture() throws NamespaceNotFoundException {
when(mockArchitectureStore.createArchitectureForNamespace(any(Architecture.class)))
.thenThrow(new NamespaceNotFoundException());
String architecture = "{ \"test\": \"json\" }";
given()
.header("Content-Type", "application/json")
.body(architecture)
.when()
.post("/calm/namespaces/invalid/architectures")
.then()
.statusCode(404);
Architecture expectedArchitecture = new Architecture.ArchitectureBuilder()
.setArchitecture(architecture)
.setNamespace("invalid")
.build();
verify(mockArchitectureStore, times(1)).createArchitectureForNamespace(expectedArchitecture);
}
// //FIXME add a test to catch a JSONParseException and create a 400 response
@Test
void return_a_created_with_location_of_architecture_when_creating_architecture() throws NamespaceNotFoundException {
String architectureJson = "{ \"test\": \"json\" }";
String namespace = "finos";
Architecture stubbedReturnArchitecture = new Architecture.ArchitectureBuilder()
.setArchitecture(architectureJson)
.setVersion("1.0.0")
.setId(12)
.setNamespace(namespace)
.build();
when(mockArchitectureStore.createArchitectureForNamespace(any(Architecture.class))).thenReturn(stubbedReturnArchitecture);
given()
.header("Content-Type", "application/json")
.body(architectureJson)
.when()
.post("/calm/namespaces/finos/architectures")
.then()
.statusCode(201)
//Derived from stubbed architecture in resource
.header("Location", containsString("/calm/namespaces/finos/architectures/12/versions/1.0.0"));
Architecture expectedArchitectureToCreate = new Architecture.ArchitectureBuilder()
.setArchitecture(architectureJson)
.setNamespace(namespace)
.build();
verify(mockArchitectureStore, times(1)).createArchitectureForNamespace(expectedArchitectureToCreate);
}
private void verifyExpectedArchitectureForVersions(String namespace) throws NamespaceNotFoundException, ArchitectureNotFoundException {
Architecture expectedArchitectureToRetrieve = new Architecture.ArchitectureBuilder()
.setNamespace(namespace)
.setId(12)
.build();
verify(mockArchitectureStore, times(1)).getArchitectureVersions(expectedArchitectureToRetrieve);
}
static Stream<Arguments> provideParametersForArchitectureVersionTests() {
return Stream.of(
Arguments.of("invalid", new NamespaceNotFoundException(), 404),
Arguments.of("valid", new ArchitectureNotFoundException(), 404),
Arguments.of("valid", null, 200)
);
}
@ParameterizedTest
@MethodSource("provideParametersForArchitectureVersionTests")
void respond_correctly_to_get_architecture_versions_query(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, NamespaceNotFoundException {
var versions = List.of("1.0.0", "1.0.1");
if(exceptionToThrow != null) {
when(mockArchitectureStore.getArchitectureVersions(any(Architecture.class))).thenThrow(exceptionToThrow);
} else {
when(mockArchitectureStore.getArchitectureVersions(any(Architecture.class))).thenReturn(versions);
}
if(expectedStatusCode == 200) {
String expectedBody = "{\"values\":[\"1.0.0\",\"1.0.1\"]}";
given()
.when()
.get("/calm/namespaces/" + namespace + "/architectures/12/versions")
.then()
.statusCode(expectedStatusCode)
.body(equalTo(expectedBody));
} else {
given()
.when()
.get("/calm/namespaces/" + namespace + "/architectures/12/versions")
.then()
.statusCode(expectedStatusCode);
}
verifyExpectedArchitectureForVersions(namespace);
}
private void verifyExpectedGetArchitecture(String namespace) throws ArchitectureNotFoundException, NamespaceNotFoundException, ArchitectureVersionNotFoundException {
Architecture expectedArchitectureToRetrieve = new Architecture.ArchitectureBuilder()
.setNamespace(namespace)
.setId(12)
.setVersion("1.0.0")
.build();
verify(mockArchitectureStore, times(1)).getArchitectureForVersion(expectedArchitectureToRetrieve);
}
static Stream<Arguments> provideParametersForGetArchitectureTests() {
return Stream.of(
Arguments.of("invalid", new NamespaceNotFoundException(), 404),
Arguments.of("valid", new ArchitectureNotFoundException(), 404),
Arguments.of("valid", new ArchitectureVersionNotFoundException(), 404),
Arguments.of("valid", null, 200)
);
}
@ParameterizedTest
@MethodSource("provideParametersForGetArchitectureTests")
void respond_correctly_to_get_architecture_for_a_specific_version_correctly(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureVersionNotFoundException, ArchitectureNotFoundException, NamespaceNotFoundException {
if(exceptionToThrow != null) {
when(mockArchitectureStore.getArchitectureForVersion(any(Architecture.class))).thenThrow(exceptionToThrow);
} else {
String architecture = "{ \"test\": \"json\" }";
when(mockArchitectureStore.getArchitectureForVersion(any(Architecture.class))).thenReturn(architecture);
}
if(expectedStatusCode == 200) {
given()
.when()
.get("/calm/namespaces/" + namespace + "/architectures/12/versions/1.0.0")
.then()
.statusCode(expectedStatusCode)
.body(equalTo("{ \"test\": \"json\" }"));
} else {
given()
.when()
.get("/calm/namespaces/" + namespace + "/architectures/12/versions/1.0.0")
.then()
.statusCode(expectedStatusCode);
}
verifyExpectedGetArchitecture(namespace);
}
static Stream<Arguments> provideParametersForCreateArchitectureTests() {
return Stream.of(
Arguments.of(new NamespaceNotFoundException(), 404),
Arguments.of(new ArchitectureNotFoundException(), 404),
Arguments.of(new ArchitectureVersionExistsException(), 409),
Arguments.of(null, 201)
);
}
@ParameterizedTest
@MethodSource("provideParametersForCreateArchitectureTests")
void respond_correctly_to_create_architecture(Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, ArchitectureVersionExistsException, NamespaceNotFoundException {
Architecture expectedArchitecture = new Architecture.ArchitectureBuilder()
.setNamespace("test")
.setVersion("1.0.1")
.setArchitecture("{ \"test\": \"json\" }")
.setId(20)
.build();
if(exceptionToThrow != null) {
when(mockArchitectureStore.createArchitectureForVersion(expectedArchitecture)).thenThrow(exceptionToThrow);
} else {
when(mockArchitectureStore.createArchitectureForVersion(expectedArchitecture)).thenReturn(expectedArchitecture);
}
if(expectedStatusCode == 201) {
given()
.header("Content-Type", "application/json")
.body(expectedArchitecture.getArchitectureJson())
.when()
.post("/calm/namespaces/test/architectures/20/versions/1.0.1")
.then()
.statusCode(expectedStatusCode)
//Derived from stubbed architecture in resource
.header("Location", containsString("/calm/namespaces/test/architectures/20/versions/1.0.1"));
} else {
given()
.header("Content-Type", "application/json")
.body(expectedArchitecture.getArchitectureJson())
.when()
.post("/calm/namespaces/test/architectures/20/versions/1.0.1")
.then()
.statusCode(expectedStatusCode);
}
verify(mockArchitectureStore, times(1)).createArchitectureForVersion(expectedArchitecture);
}
@Test
void return_forbidden_for_put_operations_on_architectures_default_and_when_configured() {
given()
.header("Content-Type", "application/json")
.body("{ \"test\": \"json\" }")
.when()
.put("/calm/namespaces/test/architectures/20/versions/1.0.1")
.then()
.statusCode(403);
}
}