|
4 | 4 |
|
5 | 5 | package io.securecodebox.persistence.defectdojo.service; |
6 | 6 |
|
7 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
8 | | -import io.securecodebox.persistence.defectdojo.config.Config; |
9 | 7 | import io.securecodebox.persistence.defectdojo.model.User; |
10 | 8 | import io.securecodebox.persistence.defectdojo.model.UserProfile; |
11 | 9 | import org.junit.jupiter.api.Test; |
12 | | -import org.springframework.http.MediaType; |
13 | | -import org.springframework.test.web.client.MockRestServiceServer; |
14 | 10 |
|
| 11 | +import java.io.IOException; |
15 | 12 | import java.net.URISyntaxException; |
16 | | -import java.util.Arrays; |
17 | | -import java.util.List; |
18 | 13 |
|
19 | | -import static org.junit.jupiter.api.Assertions.assertIterableEquals; |
20 | | -import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; |
21 | | -import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; |
| 14 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 15 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 16 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 17 | +import static org.hamcrest.Matchers.hasSize; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertAll; |
22 | 19 |
|
23 | 20 |
|
24 | 21 | /** |
25 | | - * Tests for UserProfileService |
| 22 | + * Tests for {@link UserProfileService} |
26 | 23 | * <p> |
27 | 24 | * This test is special because the defectdojo api does not return a list, but the generic code assumes every endpoint |
28 | 25 | * returns a list. |
29 | 26 | * </p> |
30 | | - * |
31 | | - * TODO: Add WireMock integration test. |
32 | 27 | */ |
33 | | -class UserProfileServiceTest { |
34 | | - /** |
35 | | - * This string does not contain every field of the api response as those are not implemented |
36 | | - */ |
37 | | - private static final String API_RESPONSE = """ |
38 | | - { |
39 | | - "user": { |
40 | | - "id": 0, |
41 | | - "username": "username", |
42 | | - "first_name": "first_name", |
43 | | - "last_name": "last_name", |
44 | | - "email": "user@example.com", |
45 | | - "last_login": "2022-11-01T16:20:19.373Z", |
46 | | - "is_active": true, |
47 | | - "is_superuser": true, |
48 | | - "configuration_permissions": [0] |
49 | | - } |
50 | | - } |
51 | | - """; |
52 | | - |
53 | | - private final Config config = new Config("https://defectdojo.example.com", "abc", 42); |
54 | | - private final UserProfileService sut = new UserProfileService(config); |
55 | | - private final MockRestServiceServer server = MockRestServiceServer.createServer(sut.getRestTemplate()); |
| 28 | +final class UserProfileServiceTest extends WireMockBaseTestCase { |
| 29 | + private final UserProfileService sut = new UserProfileService(conf()); |
56 | 30 |
|
57 | 31 | @Test |
58 | | - void search() throws JsonProcessingException, URISyntaxException { |
59 | | - final var url = String.format("%s/api/v2/%s/?offset=0&limit=100", config.getUrl(), sut.getUrlPath()); |
60 | | - server.expect(requestTo(url)).andRespond(withSuccess(API_RESPONSE, MediaType.APPLICATION_JSON)); |
61 | | - |
62 | | - final var expected = new UserProfile(new User(0L, "username", "first_name", "last_name")); |
63 | | - |
64 | | - assertIterableEquals(List.of(expected), sut.search()); |
65 | | - server.verify(); |
| 32 | + void search() throws URISyntaxException, IOException { |
| 33 | + stubFor( |
| 34 | + get("/api/v2/user_profile/?offset=0&limit=100") |
| 35 | + .willReturn( |
| 36 | + ok() |
| 37 | + .withBody(readFixtureFile("UserProfileService_response_fixture.json")) |
| 38 | + ) |
| 39 | + ); |
| 40 | + |
| 41 | + final var result = sut.search(); |
| 42 | + |
| 43 | + assertAll( |
| 44 | + () -> assertThat(result, hasSize(1)), |
| 45 | + () -> assertThat(result, containsInAnyOrder( |
| 46 | + UserProfile.builder() |
| 47 | + .user(new User(0L, "username", "first_name", "last_name")) |
| 48 | + .build() |
| 49 | + )) |
| 50 | + ); |
66 | 51 | } |
67 | 52 | } |
0 commit comments