5
5
6
6
import com .fasterxml .jackson .core .JsonProcessingException ;
7
7
import io .securecodebox .persistence .defectdojo .model .Endpoint ;
8
- import org .junit .jupiter .api .Disabled ;
9
8
import org .junit .jupiter .api .Test ;
10
9
11
10
import java .io .IOException ;
22
21
* Tests for {@link EndpointService}
23
22
*/
24
23
final class EndpointServiceTest extends WireMockBaseTestCase {
24
+ private static final String RESPONSE_LIST_FIXTURE_JSON = "EndpointService_response_list_fixture.json" ;
25
25
private final EndpointService sut = new EndpointService (conf ());
26
26
private final Endpoint [] expectedFromSearch = new Endpoint []{
27
27
Endpoint .builder ()
@@ -62,7 +62,7 @@ final class EndpointServiceTest extends WireMockBaseTestCase {
62
62
63
63
@ Test
64
64
void search () throws URISyntaxException , IOException {
65
- final var response = readFixtureFile ("EndpointService_response_fixture.json" );
65
+ final var response = readFixtureFile (RESPONSE_LIST_FIXTURE_JSON );
66
66
stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
67
67
.withQueryParam ("limit" , equalTo ("100" ))
68
68
.withQueryParam ("offset" , equalTo ("0" ))
@@ -81,7 +81,7 @@ void search() throws URISyntaxException, IOException {
81
81
82
82
@ Test
83
83
void search_withQueryParams () throws URISyntaxException , IOException {
84
- final var response = readFixtureFile ("EndpointService_response_fixture.json" );
84
+ final var response = readFixtureFile (RESPONSE_LIST_FIXTURE_JSON );
85
85
stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
86
86
.withQueryParam ("limit" , equalTo ("100" ))
87
87
.withQueryParam ("offset" , equalTo ("0" ))
@@ -150,25 +150,17 @@ void get_byId() {
150
150
void searchUnique_withSearchObjectWhichReturnsEmptyResult () throws URISyntaxException , JsonProcessingException {
151
151
// Here we only test that the object properties are correctly mapped to get params,
152
152
// since the response parsing and binding is covered by the other tests.
153
- final var response = """
154
- {
155
- "count": 0,
156
- "next": null,
157
- "previous": null,
158
- "results": [],
159
- "prefetch": {}
160
- }
161
- """ ;
162
153
stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
163
154
.withQueryParam ("limit" , equalTo ("100" ))
164
- .withQueryParam ("product" , equalTo ("285" ))
165
- .withQueryParam ("id" , equalTo ("42" ))
166
155
.withQueryParam ("offset" , equalTo ("0" ))
156
+ .withQueryParam ("id" , equalTo ("42" ))
157
+ .withQueryParam ("product" , equalTo ("285" ))
158
+ // Defaults from model:
167
159
.withQueryParam ("port" , equalTo ("0" ))
168
160
.withQueryParam ("mitigated" , equalTo ("false" ))
169
161
.willReturn (ok ()
170
- .withHeaders (responseHeaders (response .length ()))
171
- .withBody (response )
162
+ .withHeaders (responseHeaders (EMPTY_SEARCH_RESULT_RESPONSE_FIXTURE .length ()))
163
+ .withBody (EMPTY_SEARCH_RESULT_RESPONSE_FIXTURE )
172
164
));
173
165
final var searchObject = Endpoint .builder ()
174
166
.id (42 )
@@ -184,23 +176,14 @@ void searchUnique_withSearchObjectWhichReturnsEmptyResult() throws URISyntaxExce
184
176
void searchUnique_withQueryParamsWhichReturnsEmptyResult () throws URISyntaxException , JsonProcessingException {
185
177
// Here we only test that the object properties are correctly mapped to get params,
186
178
// since the response parsing and binding is covered by the other tests.
187
- final var response = """
188
- {
189
- "count": 0,
190
- "next": null,
191
- "previous": null,
192
- "results": [],
193
- "prefetch": {}
194
- }
195
- """ ;
196
179
stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
197
180
.withQueryParam ("limit" , equalTo ("100" ))
198
181
.withQueryParam ("offset" , equalTo ("0" ))
199
182
.withQueryParam ("foo" , equalTo ("42" ))
200
183
.withQueryParam ("bar" , equalTo ("23" ))
201
184
.willReturn (ok ()
202
- .withHeaders (responseHeaders (response .length ()))
203
- .withBody (response )
185
+ .withHeaders (responseHeaders (EMPTY_SEARCH_RESULT_RESPONSE_FIXTURE .length ()))
186
+ .withBody (EMPTY_SEARCH_RESULT_RESPONSE_FIXTURE )
204
187
));
205
188
final var queryParams = new HashMap <String , Object >();
206
189
queryParams .put ("foo" , 42 );
@@ -213,26 +196,33 @@ void searchUnique_withQueryParamsWhichReturnsEmptyResult() throws URISyntaxExcep
213
196
214
197
@ Test
215
198
void create () {
216
- final var expectedRequest = "{\" id\" :0,\" protocol\" :\" tcp\" ,\" host\" :\" www.owasp.org\" ,\" port\" :443,\" product\" :285,\" mitigated\" :false}" ;
217
- final var response = "{\" id\" :42,\" protocol\" :\" tcp\" ,\" host\" :\" www.owasp.org\" ,\" port\" :443,\" product\" :285,\" mitigated\" :false}" ;
218
-
199
+ final var json = """
200
+ {
201
+ "id": 42,
202
+ "protocol": "tcp",
203
+ "host": "www.owasp.org",
204
+ "port":443,
205
+ "product": 285,
206
+ "mitigated": false
207
+ }
208
+ """ ;
219
209
stubFor (post (urlPathEqualTo ("/api/v2/endpoints/" ))
220
- .withRequestBody (equalTo ( expectedRequest ))
210
+ .withRequestBody (equalToJson ( json ))
221
211
.willReturn (created ()
222
- .withHeaders (responseHeaders (response .length ()))
223
- .withBody (response )
212
+ .withHeaders (responseHeaders (json .length ()))
213
+ .withBody (json ) // Typically the entity with new assigned id is returned, but we ignore this here.
224
214
));
225
-
226
- final var endpoint = Endpoint . builder ( )
215
+ final var toCreate = Endpoint . builder ()
216
+ . id ( 42 )
227
217
.protocol ("tcp" )
228
218
.host ("www.owasp.org" )
229
219
.port (443 )
230
220
.product (285 )
231
221
.build ();
232
222
233
- final var result = sut .create (endpoint );
223
+ final var result = sut .create (toCreate );
234
224
235
- assertThat (result . getId () , is (42L ));
225
+ assertThat (result , is (toCreate ));
236
226
}
237
227
238
228
@ Test
@@ -247,24 +237,33 @@ void delete_byId() {
247
237
248
238
@ Test
249
239
void update () {
250
- final var json = "{\" id\" :42,\" protocol\" :\" tcp\" ,\" host\" :\" www.owasp.org\" ,\" port\" :443,\" product\" :285,\" mitigated\" :false}" ;
240
+ final var json = """
241
+ {
242
+ "id": 42,
243
+ "protocol": "tcp",
244
+ "host": "www.owasp.org",
245
+ "port": 443,
246
+ "product":285,
247
+ "mitigated": false
248
+ }
249
+ """ ;
251
250
stubFor (put (urlPathEqualTo ("/api/v2/endpoints/42/" ))
252
- .withRequestBody (equalTo (json ))
251
+ .withRequestBody (equalToJson (json ))
253
252
.willReturn (ok ()
254
253
.withHeaders (responseHeaders (json .length ()))
255
254
.withBody (json )
256
255
));
257
256
258
- final var endpoint = Endpoint .builder ()
257
+ final var toUpdate = Endpoint .builder ()
259
258
.id (42 )
260
259
.protocol ("tcp" )
261
260
.host ("www.owasp.org" )
262
261
.port (443 )
263
262
.product (285 )
264
263
.build ();
265
264
266
- final var updated = sut .update (endpoint , 42L );
265
+ final var result = sut .update (toUpdate , 42L );
267
266
268
- assertThat (updated , is (endpoint ));
267
+ assertThat (result , is (toUpdate ));
269
268
}
270
269
}
0 commit comments