|
| 1 | +package se.bjurr.gitchangelog.internal.integrations.jira; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 6 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | + |
| 9 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 10 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 11 | +import java.util.Optional; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import se.bjurr.gitchangelog.api.exceptions.GitChangelogIntegrationException; |
| 16 | + |
| 17 | +public class JiraClientWireMockTest { |
| 18 | + |
| 19 | + private WireMockServer wireMockServer; |
| 20 | + private DefaultJiraClient jiraClient; |
| 21 | + private String baseUrl; |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + public void setUp() { |
| 25 | + wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort()); |
| 26 | + wireMockServer.start(); |
| 27 | + baseUrl = "http://localhost:" + wireMockServer.port(); |
| 28 | + jiraClient = new DefaultJiraClient(baseUrl); |
| 29 | + } |
| 30 | + |
| 31 | + @AfterEach |
| 32 | + public void tearDown() { |
| 33 | + if (wireMockServer != null) { |
| 34 | + wireMockServer.stop(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testGetIssueSuccess() throws GitChangelogIntegrationException { |
| 40 | + String mockResponse = |
| 41 | + """ |
| 42 | + { |
| 43 | + "key": "TEST-123", |
| 44 | + "fields": { |
| 45 | + "summary": "Test Summary", |
| 46 | + "description": "Test Description", |
| 47 | + "issuetype": {"name": "Bug"}, |
| 48 | + "labels": ["test"], |
| 49 | + "parent": null, |
| 50 | + "issuelinks": [] |
| 51 | + } |
| 52 | + } |
| 53 | + """; |
| 54 | + |
| 55 | + wireMockServer.stubFor( |
| 56 | + get(urlPathEqualTo("/rest/api/2/issue/TEST-123")) |
| 57 | + .withQueryParam( |
| 58 | + "fields", equalTo("parent,summary,issuetype,labels,description,issuelinks")) |
| 59 | + .willReturn( |
| 60 | + aResponse() |
| 61 | + .withStatus(200) |
| 62 | + .withHeader("Content-Type", "application/json") |
| 63 | + .withBody(mockResponse))); |
| 64 | + |
| 65 | + Optional<JiraIssue> result = jiraClient.getIssue("TEST-123"); |
| 66 | + |
| 67 | + assertThat(result).isPresent(); |
| 68 | + JiraIssue issue = result.get(); |
| 69 | + assertThat(issue.getIssue()).isEqualTo("TEST-123"); |
| 70 | + assertThat(issue.getTitle()).isEqualTo("Test Summary"); |
| 71 | + assertThat(issue.getDescription()).isEqualTo("Test Description"); |
| 72 | + assertThat(issue.getIssueType()).isEqualTo("Bug"); |
| 73 | + assertThat(issue.getLink()).isEqualTo(baseUrl + "/browse/TEST-123"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetIssueNotFound() throws GitChangelogIntegrationException { |
| 78 | + wireMockServer.stubFor( |
| 79 | + get(urlPathEqualTo("/rest/api/2/issue/NOTFOUND-123")) |
| 80 | + .willReturn(aResponse().withStatus(404).withBody("Issue not found"))); |
| 81 | + |
| 82 | + Optional<JiraIssue> result = jiraClient.getIssue("NOTFOUND-123"); |
| 83 | + |
| 84 | + assertThat(result).isEmpty(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testGetIssueServerError() throws GitChangelogIntegrationException { |
| 89 | + wireMockServer.stubFor( |
| 90 | + get(urlPathEqualTo("/rest/api/2/issue/ERROR-123")) |
| 91 | + .willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); |
| 92 | + |
| 93 | + Optional<JiraIssue> result = jiraClient.getIssue("ERROR-123"); |
| 94 | + |
| 95 | + assertThat(result).isEmpty(); |
| 96 | + } |
| 97 | +} |
0 commit comments