|
| 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 | +} |
0 commit comments