Skip to content

Commit 4da46b3

Browse files
committed
Port UserProfileServiceTest To WireMock
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent f256c04 commit 4da46b3

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

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

+28-43
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,49 @@
44

55
package io.securecodebox.persistence.defectdojo.service;
66

7-
import com.fasterxml.jackson.core.JsonProcessingException;
8-
import io.securecodebox.persistence.defectdojo.config.Config;
97
import io.securecodebox.persistence.defectdojo.model.User;
108
import io.securecodebox.persistence.defectdojo.model.UserProfile;
119
import org.junit.jupiter.api.Test;
12-
import org.springframework.http.MediaType;
13-
import org.springframework.test.web.client.MockRestServiceServer;
1410

11+
import java.io.IOException;
1512
import java.net.URISyntaxException;
16-
import java.util.Arrays;
17-
import java.util.List;
1813

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;
2219

2320

2421
/**
25-
* Tests for UserProfileService
22+
* Tests for {@link UserProfileService}
2623
* <p>
2724
* This test is special because the defectdojo api does not return a list, but the generic code assumes every endpoint
2825
* returns a list.
2926
* </p>
30-
*
31-
* TODO: Add WireMock integration test.
3227
*/
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());
5630

5731
@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+
);
6651
}
6752
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
{}
1+
{
2+
"user": {
3+
"id": 0,
4+
"username": "username",
5+
"first_name": "first_name",
6+
"last_name": "last_name",
7+
"email": "user@example.com",
8+
"last_login": "2022-11-01T16:20:19.373Z",
9+
"is_active": true,
10+
"is_superuser": true,
11+
"configuration_permissions": [0]
12+
}
13+
}

0 commit comments

Comments
 (0)