Skip to content

Commit b1e00db

Browse files
committed
Port Test Cases from EndpointService to Other Services
Also introduce some defaults to models which came from DefectDojo: We had some fields null by default which will be empty string/array by default in DefectDojo responses. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 669d50b commit b1e00db

36 files changed

+2733
-659
lines changed

src/main/java/io/securecodebox/persistence/defectdojo/model/Engagement.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonProperty;
99
import lombok.*;
1010

11+
import java.util.ArrayList;
1112
import java.util.List;
1213
import java.util.Map;
1314

@@ -19,7 +20,8 @@
1920
@JsonInclude(JsonInclude.Include.NON_NULL)
2021
public final class Engagement implements Model, HasId, HasName {
2122
@JsonProperty("branch_tag")
22-
private String branch;
23+
@Builder.Default
24+
private String branch = "";
2325

2426
@JsonProperty
2527
private long id;
@@ -48,7 +50,8 @@ public final class Engagement implements Model, HasId, HasName {
4850
private Status status = Status.IN_PROGRESS;
4951

5052
@JsonProperty
51-
private List<String> tags;
53+
@Builder.Default
54+
private List<String> tags = new ArrayList<>();
5255

5356
@JsonProperty
5457
private String tracker;
@@ -72,7 +75,8 @@ public final class Engagement implements Model, HasId, HasName {
7275
private long orchestrationEngine;
7376

7477
@JsonProperty
75-
private String description;
78+
@Builder.Default
79+
private String description = "";
7680

7781
@JsonProperty("deduplication_on_engagement")
7882
private boolean deduplicationOnEngagement;
@@ -90,7 +94,8 @@ public final class Engagement implements Model, HasId, HasName {
9094
private boolean checkList;
9195

9296
@JsonProperty
93-
private String version;
97+
@Builder.Default
98+
private String version = "";
9499

95100
@Override
96101
public boolean equalsQueryString(Map<String, Object> queryParams) {

src/main/java/io/securecodebox/persistence/defectdojo/model/Finding.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import lombok.*;
1212

1313
import java.time.OffsetDateTime;
14+
import java.util.ArrayList;
1415
import java.util.LinkedList;
1516
import java.util.List;
1617
import java.util.Map;
@@ -99,7 +100,8 @@ public final class Finding implements Model, HasId {
99100
private OffsetDateTime mitigatedAt;
100101

101102
@JsonProperty("accepted_risks")
102-
private List<RiskAcceptance> acceptedRisks;
103+
@Builder.Default
104+
private List<RiskAcceptance> acceptedRisks = new ArrayList<>();
103105

104106
@JsonProperty("numerical_severity")
105107
public String getNumericalSeverity() {

src/main/java/io/securecodebox/persistence/defectdojo/model/Product.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonProperty;
99
import lombok.*;
1010

11+
import java.util.ArrayList;
1112
import java.util.List;
1213
import java.util.Map;
1314

@@ -46,7 +47,8 @@ public final class Product implements Model, HasId, HasName {
4647
private boolean enableFullRiskAcceptance;
4748

4849
@JsonProperty("authorization_groups")
49-
private List<Long> authorizationGroups;
50+
@Builder.Default
51+
private List<Long> authorizationGroups = new ArrayList<>();
5052

5153
@JsonProperty
5254
private String lifecycle;

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

+41-42
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import com.fasterxml.jackson.core.JsonProcessingException;
77
import io.securecodebox.persistence.defectdojo.model.Endpoint;
8-
import org.junit.jupiter.api.Disabled;
98
import org.junit.jupiter.api.Test;
109

1110
import java.io.IOException;
@@ -22,6 +21,7 @@
2221
* Tests for {@link EndpointService}
2322
*/
2423
final 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

Comments
 (0)