55
66import com .fasterxml .jackson .core .JsonProcessingException ;
77import io .securecodebox .persistence .defectdojo .model .Endpoint ;
8- import org .junit .jupiter .api .Disabled ;
98import org .junit .jupiter .api .Test ;
109
1110import java .io .IOException ;
2221 * Tests for {@link EndpointService}
2322 */
2423final class EndpointServiceTest extends WireMockBaseTestCase {
24+ private static final String RESPONSE_LIST_FIXTURE_JSON = "EndpointService_response_list_fixture.json" ;
2525 private final EndpointService sut = new EndpointService (conf ());
2626 private final Endpoint [] expectedFromSearch = new Endpoint []{
2727 Endpoint .builder ()
@@ -62,7 +62,7 @@ final class EndpointServiceTest extends WireMockBaseTestCase {
6262
6363 @ Test
6464 void search () throws URISyntaxException , IOException {
65- final var response = readFixtureFile ("EndpointService_response_fixture.json" );
65+ final var response = readFixtureFile (RESPONSE_LIST_FIXTURE_JSON );
6666 stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
6767 .withQueryParam ("limit" , equalTo ("100" ))
6868 .withQueryParam ("offset" , equalTo ("0" ))
@@ -81,7 +81,7 @@ void search() throws URISyntaxException, IOException {
8181
8282 @ Test
8383 void search_withQueryParams () throws URISyntaxException , IOException {
84- final var response = readFixtureFile ("EndpointService_response_fixture.json" );
84+ final var response = readFixtureFile (RESPONSE_LIST_FIXTURE_JSON );
8585 stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
8686 .withQueryParam ("limit" , equalTo ("100" ))
8787 .withQueryParam ("offset" , equalTo ("0" ))
@@ -150,25 +150,17 @@ void get_byId() {
150150 void searchUnique_withSearchObjectWhichReturnsEmptyResult () throws URISyntaxException , JsonProcessingException {
151151 // Here we only test that the object properties are correctly mapped to get params,
152152 // 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- """ ;
162153 stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
163154 .withQueryParam ("limit" , equalTo ("100" ))
164- .withQueryParam ("product" , equalTo ("285" ))
165- .withQueryParam ("id" , equalTo ("42" ))
166155 .withQueryParam ("offset" , equalTo ("0" ))
156+ .withQueryParam ("id" , equalTo ("42" ))
157+ .withQueryParam ("product" , equalTo ("285" ))
158+ // Defaults from model:
167159 .withQueryParam ("port" , equalTo ("0" ))
168160 .withQueryParam ("mitigated" , equalTo ("false" ))
169161 .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 )
172164 ));
173165 final var searchObject = Endpoint .builder ()
174166 .id (42 )
@@ -184,23 +176,14 @@ void searchUnique_withSearchObjectWhichReturnsEmptyResult() throws URISyntaxExce
184176 void searchUnique_withQueryParamsWhichReturnsEmptyResult () throws URISyntaxException , JsonProcessingException {
185177 // Here we only test that the object properties are correctly mapped to get params,
186178 // 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- """ ;
196179 stubFor (get (urlPathEqualTo ("/api/v2/endpoints/" ))
197180 .withQueryParam ("limit" , equalTo ("100" ))
198181 .withQueryParam ("offset" , equalTo ("0" ))
199182 .withQueryParam ("foo" , equalTo ("42" ))
200183 .withQueryParam ("bar" , equalTo ("23" ))
201184 .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 )
204187 ));
205188 final var queryParams = new HashMap <String , Object >();
206189 queryParams .put ("foo" , 42 );
@@ -213,26 +196,33 @@ void searchUnique_withQueryParamsWhichReturnsEmptyResult() throws URISyntaxExcep
213196
214197 @ Test
215198 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+ """ ;
219209 stubFor (post (urlPathEqualTo ("/api/v2/endpoints/" ))
220- .withRequestBody (equalTo ( expectedRequest ))
210+ .withRequestBody (equalToJson ( json ))
221211 .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.
224214 ));
225-
226- final var endpoint = Endpoint . builder ( )
215+ final var toCreate = Endpoint . builder ()
216+ . id ( 42 )
227217 .protocol ("tcp" )
228218 .host ("www.owasp.org" )
229219 .port (443 )
230220 .product (285 )
231221 .build ();
232222
233- final var result = sut .create (endpoint );
223+ final var result = sut .create (toCreate );
234224
235- assertThat (result . getId () , is (42L ));
225+ assertThat (result , is (toCreate ));
236226 }
237227
238228 @ Test
@@ -247,24 +237,33 @@ void delete_byId() {
247237
248238 @ Test
249239 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+ """ ;
251250 stubFor (put (urlPathEqualTo ("/api/v2/endpoints/42/" ))
252- .withRequestBody (equalTo (json ))
251+ .withRequestBody (equalToJson (json ))
253252 .willReturn (ok ()
254253 .withHeaders (responseHeaders (json .length ()))
255254 .withBody (json )
256255 ));
257256
258- final var endpoint = Endpoint .builder ()
257+ final var toUpdate = Endpoint .builder ()
259258 .id (42 )
260259 .protocol ("tcp" )
261260 .host ("www.owasp.org" )
262261 .port (443 )
263262 .product (285 )
264263 .build ();
265264
266- final var updated = sut .update (endpoint , 42L );
265+ final var result = sut .update (toUpdate , 42L );
267266
268- assertThat (updated , is (endpoint ));
267+ assertThat (result , is (toUpdate ));
269268 }
270269}
0 commit comments