3
3
// SPDX-License-Identifier: Apache-2.0
4
4
package io .securecodebox .persistence .defectdojo .service ;
5
5
6
+ import com .fasterxml .jackson .core .JsonProcessingException ;
6
7
import io .securecodebox .persistence .defectdojo .model .Endpoint ;
7
8
import org .junit .jupiter .api .Disabled ;
8
9
import org .junit .jupiter .api .Test ;
11
12
import java .net .URISyntaxException ;
12
13
import java .util .HashMap ;
13
14
15
+ import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
14
16
import static com .github .tomakehurst .wiremock .client .WireMock .*;
15
17
import static org .hamcrest .MatcherAssert .assertThat ;
16
18
import static org .hamcrest .Matchers .*;
@@ -61,14 +63,13 @@ final class EndpointServiceTest extends WireMockBaseTestCase {
61
63
@ Test
62
64
void search () throws URISyntaxException , IOException {
63
65
final var response = readFixtureFile ("EndpointService_response_fixture.json" );
64
- stubFor (
65
- get ("/api/v2/endpoints/?offset=0&limit=100" )
66
- .willReturn (
67
- ok ()
68
- .withHeaders (responseHeaders (response .length ()))
69
- .withBody (response )
70
- )
71
- );
66
+ stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
67
+ .withQueryParam ("limit" , equalTo ("100" ))
68
+ .withQueryParam ("offset" , equalTo ("0" ))
69
+ .willReturn (ok ()
70
+ .withHeaders (responseHeaders (response .length ()))
71
+ .withBody (response )
72
+ ));
72
73
73
74
final var result = sut .search ();
74
75
@@ -81,17 +82,18 @@ void search() throws URISyntaxException, IOException {
81
82
@ Test
82
83
void search_withQueryParams () throws URISyntaxException , IOException {
83
84
final var response = readFixtureFile ("EndpointService_response_fixture.json" );
84
- stubFor (
85
- get ("/api/v2/endpoints/?limit=100&bar=42&offset=0&foo=23" )
86
- .willReturn (
87
- ok ()
88
- .withHeaders (responseHeaders (response .length ()))
89
- .withBody (response )
90
- )
91
- );
85
+ stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
86
+ .withQueryParam ("limit" , equalTo ("100" ))
87
+ .withQueryParam ("offset" , equalTo ("0" ))
88
+ .withQueryParam ("foo" , equalTo ("42" ))
89
+ .withQueryParam ("bar" , equalTo ("23" ))
90
+ .willReturn (ok ()
91
+ .withHeaders (responseHeaders (response .length ()))
92
+ .withBody (response )
93
+ ));
92
94
final var params = new HashMap <String , Object >();
93
- params .put ("foo" , 23 );
94
- params .put ("bar" , 42 );
95
+ params .put ("foo" , 42 );
96
+ params .put ("bar" , 23 );
95
97
96
98
final var result = sut .search (params );
97
99
@@ -125,14 +127,11 @@ void get_byId() {
125
127
"prefetch": {}
126
128
}
127
129
""" ;
128
- stubFor (
129
- get ("/api/v2/endpoints/42" )
130
- .willReturn (
131
- ok ()
132
- .withHeaders (responseHeaders (response .length ()))
133
- .withBody (response )
134
- )
135
- );
130
+ stubFor (get (urlPathEqualTo ("/api/v2/endpoints/42" ))
131
+ .willReturn (ok ()
132
+ .withHeaders (responseHeaders (response .length ()))
133
+ .withBody (response )
134
+ ));
136
135
final var expected = Endpoint .builder ()
137
136
.id (42 )
138
137
.protocol ("tcp" )
@@ -148,27 +147,124 @@ void get_byId() {
148
147
149
148
150
149
@ Test
151
- @ Disabled ("TODO: Implement test." )
152
- void searchUnique_withSearchObject () {
150
+ void searchUnique_withSearchObjectWhichReturnsEmptyResult () throws URISyntaxException , JsonProcessingException {
151
+ // Here we only test that the object properties are correctly mapped to get params,
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
+ stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
163
+ .withQueryParam ("limit" , equalTo ("100" ))
164
+ .withQueryParam ("product" , equalTo ("285" ))
165
+ .withQueryParam ("id" , equalTo ("42" ))
166
+ .withQueryParam ("offset" , equalTo ("0" ))
167
+ .withQueryParam ("port" , equalTo ("0" ))
168
+ .withQueryParam ("mitigated" , equalTo ("false" ))
169
+ .willReturn (ok ()
170
+ .withHeaders (responseHeaders (response .length ()))
171
+ .withBody (response )
172
+ ));
173
+ final var searchObject = Endpoint .builder ()
174
+ .id (42 )
175
+ .product (285 )
176
+ .build ();
177
+
178
+ final var result = sut .searchUnique (searchObject );
179
+
180
+ assertThat (result .isEmpty (), is (true ));
153
181
}
154
182
155
183
@ Test
156
- @ Disabled ("TODO: Implement test." )
157
- void searchUnique_withQueryParams () {
184
+ void searchUnique_withQueryParamsWhichReturnsEmptyResult () throws URISyntaxException , JsonProcessingException {
185
+ // Here we only test that the object properties are correctly mapped to get params,
186
+ // 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
+ stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
197
+ .withQueryParam ("limit" , equalTo ("100" ))
198
+ .withQueryParam ("offset" , equalTo ("0" ))
199
+ .withQueryParam ("foo" , equalTo ("42" ))
200
+ .withQueryParam ("bar" , equalTo ("23" ))
201
+ .willReturn (ok ()
202
+ .withHeaders (responseHeaders (response .length ()))
203
+ .withBody (response )
204
+ ));
205
+ final var queryParams = new HashMap <String , Object >();
206
+ queryParams .put ("foo" , 42 );
207
+ queryParams .put ("bar" , 23 );
208
+
209
+ final var result = sut .searchUnique (queryParams );
210
+
211
+ assertThat (result .isEmpty (), is (true ));
158
212
}
159
213
160
214
@ Test
161
- @ Disabled ("TODO: Implement test." )
162
215
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
+
219
+ stubFor (post (urlPathEqualTo ("/api/v2/endpoints/" ))
220
+ .withRequestBody (equalTo (expectedRequest ))
221
+ .willReturn (created ()
222
+ .withHeaders (responseHeaders (response .length ()))
223
+ .withBody (response )
224
+ ));
225
+
226
+ final var endpoint = Endpoint .builder ()
227
+ .protocol ("tcp" )
228
+ .host ("www.owasp.org" )
229
+ .port (443 )
230
+ .product (285 )
231
+ .build ();
232
+
233
+ final var result = sut .create (endpoint );
234
+
235
+ assertThat (result .getId (), is (42L ));
163
236
}
164
237
165
238
@ Test
166
- @ Disabled ("TODO: Implement test." )
167
- void delete () {
239
+ void delete_byId () {
240
+ stubFor (delete (urlPathEqualTo ("/api/v2/endpoints/42/" ))
241
+ .willReturn (ok ()));
242
+
243
+ sut .delete (42L );
244
+
245
+ verify (deleteRequestedFor (urlPathEqualTo ("/api/v2/endpoints/42/" )));
168
246
}
169
247
170
248
@ Test
171
- @ Disabled ("TODO: Implement test." )
172
249
void update () {
250
+ final var json = "{\" id\" :42,\" protocol\" :\" tcp\" ,\" host\" :\" www.owasp.org\" ,\" port\" :443,\" product\" :285,\" mitigated\" :false}" ;
251
+ stubFor (put (urlPathEqualTo ("/api/v2/endpoints/42/" ))
252
+ .withRequestBody (equalTo (json ))
253
+ .willReturn (ok ()
254
+ .withHeaders (responseHeaders (json .length ()))
255
+ .withBody (json )
256
+ ));
257
+
258
+ final var endpoint = Endpoint .builder ()
259
+ .id (42 )
260
+ .protocol ("tcp" )
261
+ .host ("www.owasp.org" )
262
+ .port (443 )
263
+ .product (285 )
264
+ .build ();
265
+
266
+ final var updated = sut .update (endpoint , 42L );
267
+
268
+ assertThat (updated , is (endpoint ));
173
269
}
174
270
}
0 commit comments