Skip to content

Commit ab4584b

Browse files
committed
Implement first capture replay test for ProductTypeService
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 00afda1 commit ab4584b

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

src/main/java/io/securecodebox/persistence/defectdojo/service/ProductTypeService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import io.securecodebox.persistence.defectdojo.model.PaginatedResult;
1111
import io.securecodebox.persistence.defectdojo.model.ProductType;
1212

13-
public class ProductTypeService extends GenericDefectDojoService<ProductType> {
13+
public final class ProductTypeService extends GenericDefectDojoService<ProductType> {
1414

1515
public ProductTypeService(Config config) {
1616
super(config);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.securecodebox.persistence.defectdojo.service;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import io.securecodebox.persistence.defectdojo.config.Config;
5+
import io.securecodebox.persistence.defectdojo.model.ProductType;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.IOException;
9+
import java.net.URISyntaxException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.Objects;
12+
13+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.containsInAnyOrder;
16+
import static org.hamcrest.Matchers.hasSize;
17+
import static org.junit.jupiter.api.Assertions.assertAll;
18+
19+
/**
20+
* Tests for {@link ProductTypeService}
21+
*/
22+
@WireMockTest(httpPort = ProductTypeServiceTest.PORT)
23+
class ProductTypeServiceTest {
24+
public static final int PORT = 8888;
25+
private final Config conf = new Config(
26+
String.format("http://localhost:%d/", PORT),
27+
"not-required-for-tests");
28+
private final ProductTypeService sut = new ProductTypeService(conf);
29+
30+
private String readResponseBodyFromFixture(String name) throws IOException {
31+
try (final var input = getClass().getClassLoader().getResourceAsStream(name)) {
32+
final var bytes = Objects.requireNonNull(input).readAllBytes();
33+
return new String(bytes, StandardCharsets.UTF_8);
34+
}
35+
}
36+
37+
@Test
38+
void search() throws URISyntaxException, IOException {
39+
stubFor(
40+
get("/api/v2/product_types/?offset=0&limit=100")
41+
.willReturn(
42+
ok()
43+
.withBody(readResponseBodyFromFixture("io/securecodebox/persistence/defectdojo/service/fixture_ProductTypeService.json"))
44+
)
45+
);
46+
47+
var result = sut.search();
48+
49+
assertAll(
50+
() -> assertThat(result, hasSize(2)),
51+
() -> assertThat(result, containsInAnyOrder(
52+
ProductType.builder()
53+
.id(1)
54+
.name("Research and Development")
55+
.criticalProduct(true)
56+
.keyProduct(false)
57+
.build(),
58+
ProductType.builder()
59+
.id(2)
60+
.name("secureCodeBox")
61+
.criticalProduct(false)
62+
.keyProduct(false)
63+
.build()
64+
))
65+
);
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"count": 2,
3+
"next": null,
4+
"previous": null,
5+
"results": [
6+
{
7+
"id": 2,
8+
"name": "secureCodeBox",
9+
"description": null,
10+
"critical_product": false,
11+
"key_product": false,
12+
"updated": "2024-03-01T15:06:29.409840Z",
13+
"created": "2024-03-01T15:06:29.409873Z",
14+
"members": [
15+
3,
16+
6
17+
],
18+
"authorization_groups": []
19+
},
20+
{
21+
"id": 1,
22+
"name": "Research and Development",
23+
"description": null,
24+
"critical_product": true,
25+
"key_product": false,
26+
"updated": null,
27+
"created": null,
28+
"members": [
29+
1
30+
],
31+
"authorization_groups": []
32+
}
33+
],
34+
"prefetch": {}
35+
}

0 commit comments

Comments
 (0)