Skip to content

Commit 66515d3

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 214d5fd commit 66515d3

36 files changed

+1998
-620
lines changed

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

Lines changed: 9 additions & 4 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 10 additions & 27 deletions
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);

0 commit comments

Comments
 (0)