Skip to content

Commit 4b7616a

Browse files
committed
Even More Integration Tests for EndpointService
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent ab14344 commit 4b7616a

File tree

1 file changed

+130
-34
lines changed

1 file changed

+130
-34
lines changed

src/test/java/io/securecodebox/persistence/defectdojo/service/EndpointServiceTest.java

+130-34
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package io.securecodebox.persistence.defectdojo.service;
55

6+
import com.fasterxml.jackson.core.JsonProcessingException;
67
import io.securecodebox.persistence.defectdojo.model.Endpoint;
78
import org.junit.jupiter.api.Disabled;
89
import org.junit.jupiter.api.Test;
@@ -11,6 +12,7 @@
1112
import java.net.URISyntaxException;
1213
import java.util.HashMap;
1314

15+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
1416
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1517
import static org.hamcrest.MatcherAssert.assertThat;
1618
import static org.hamcrest.Matchers.*;
@@ -61,14 +63,13 @@ final class EndpointServiceTest extends WireMockBaseTestCase {
6163
@Test
6264
void search() throws URISyntaxException, IOException {
6365
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+
));
7273

7374
final var result = sut.search();
7475

@@ -81,17 +82,18 @@ void search() throws URISyntaxException, IOException {
8182
@Test
8283
void search_withQueryParams() throws URISyntaxException, IOException {
8384
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+
));
9294
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);
9597

9698
final var result = sut.search(params);
9799

@@ -125,14 +127,11 @@ void get_byId() {
125127
"prefetch": {}
126128
}
127129
""";
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+
));
136135
final var expected = Endpoint.builder()
137136
.id(42)
138137
.protocol("tcp")
@@ -148,27 +147,124 @@ void get_byId() {
148147

149148

150149
@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));
153181
}
154182

155183
@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));
158212
}
159213

160214
@Test
161-
@Disabled("TODO: Implement test.")
162215
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));
163236
}
164237

165238
@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/")));
168246
}
169247

170248
@Test
171-
@Disabled("TODO: Implement test.")
172249
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));
173269
}
174270
}

0 commit comments

Comments
 (0)