|
| 1 | +package org.zendesk.client.v2; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.ok; |
| 6 | +import static com.github.tomakehurst.wiremock.client.WireMock.put; |
| 7 | +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; |
| 8 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 9 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 10 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
| 11 | +import static io.netty.handler.codec.http.HttpHeaders.Values.APPLICATION_JSON; |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 15 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 16 | +import com.github.tomakehurst.wiremock.junit.WireMockClassRule; |
| 17 | +import java.util.Collections; |
| 18 | +import org.apache.commons.text.RandomStringGenerator; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.ClassRule; |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.zendesk.client.v2.model.User; |
| 25 | + |
| 26 | +/** |
| 27 | + * An initial attempt at a unit test that uses wiremock to test the client without requiring a running zendesk client |
| 28 | + * @author rbolles on 2/8/18. |
| 29 | + */ |
| 30 | +public class UserTest { |
| 31 | + |
| 32 | + private static final String MOCK_URL_FORMATTED_STRING = "http://localhost:%d"; |
| 33 | + public static final RandomStringGenerator RANDOM_STRING_GENERATOR = |
| 34 | + new RandomStringGenerator.Builder().withinRange('a', 'z').build(); |
| 35 | + private static final String MOCK_API_TOKEN = RANDOM_STRING_GENERATOR.generate(15); |
| 36 | + private static final String MOCK_USERNAME = RANDOM_STRING_GENERATOR.generate(10).toLowerCase() + "@cloudbees.com"; |
| 37 | + |
| 38 | + @ClassRule |
| 39 | + public static WireMockClassRule zendeskApiClass = new WireMockClassRule(options() |
| 40 | + .dynamicPort() |
| 41 | + .dynamicHttpsPort() |
| 42 | + ); |
| 43 | + |
| 44 | + @Rule |
| 45 | + public WireMockClassRule zendeskApiMock = zendeskApiClass; |
| 46 | + |
| 47 | + private Zendesk client; |
| 48 | + //use a mapper that is identical to what the client will use |
| 49 | + private ObjectMapper objectMapper = Zendesk.createMapper(); |
| 50 | + |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() throws Exception { |
| 54 | + int ephemeralPort = zendeskApiMock.port(); |
| 55 | + |
| 56 | + String hostname = String.format(MOCK_URL_FORMATTED_STRING, ephemeralPort); |
| 57 | + |
| 58 | + client = new Zendesk.Builder(hostname) |
| 59 | + .setUsername(MOCK_USERNAME) |
| 60 | + .setToken(MOCK_API_TOKEN) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + @After |
| 65 | + public void closeClient() { |
| 66 | + if (client != null) { |
| 67 | + client.close(); |
| 68 | + } |
| 69 | + client = null; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Test |
| 74 | + public void mergeUsers() throws JsonProcessingException { |
| 75 | + |
| 76 | + long userThatWillBeMerged = 1234L; |
| 77 | + long userThatWillRemain = 2345L; |
| 78 | + |
| 79 | + |
| 80 | + User userObjectInRequest = new User(); |
| 81 | + userObjectInRequest.setId(userThatWillRemain); |
| 82 | + String expectedJsonPayload = objectMapper.writeValueAsString(Collections.singletonMap("user", userObjectInRequest)); |
| 83 | + |
| 84 | + User userObjectInResponse = new User(); |
| 85 | + userObjectInResponse.setId(userThatWillRemain); |
| 86 | + userObjectInResponse.setPhone("867-5309"); //a field that wasn't in the request |
| 87 | + String expectedJsonResponse = objectMapper.writeValueAsString(Collections.singletonMap("user", userObjectInResponse)); |
| 88 | + |
| 89 | + zendeskApiMock.stubFor( |
| 90 | + put( |
| 91 | + urlPathEqualTo("/api/v2/users/1234/merge.json")) |
| 92 | + .withRequestBody(equalToJson(expectedJsonPayload)) |
| 93 | + .willReturn(ok() |
| 94 | + .withBody(expectedJsonResponse) |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + User remainingUser = client.mergeUsers(userThatWillRemain, userThatWillBeMerged); |
| 99 | + |
| 100 | + zendeskApiMock.verify(putRequestedFor( |
| 101 | + urlEqualTo("/api/v2/users/1234/merge.json")) |
| 102 | + .withHeader("Content-Type", equalTo(APPLICATION_JSON+"; charset=UTF-8")) |
| 103 | + .withRequestBody(equalToJson(expectedJsonPayload)) |
| 104 | + ); |
| 105 | + |
| 106 | + assertThat(remainingUser).as("result") |
| 107 | + .isNotNull() |
| 108 | + .isEqualToComparingFieldByField(userObjectInResponse); |
| 109 | + |
| 110 | + } |
| 111 | +} |
0 commit comments