|
| 1 | +package hudson.plugins.jira; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.hamcrest.Matchers.hasItem; |
| 6 | +import static org.hamcrest.Matchers.hasProperty; |
| 7 | +import static org.mockito.Mockito.spy; |
| 8 | +import static org.mockito.Mockito.times; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import com.atlassian.jira.rest.client.api.domain.Issue; |
| 13 | +import com.atlassian.jira.rest.client.api.domain.Version; |
| 14 | +import hudson.plugins.jira.extension.ExtendedVersion; |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URISyntaxException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.ThreadLocalRandom; |
| 22 | +import java.util.concurrent.TimeoutException; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.mockito.ArgumentCaptor; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class JiraSessionTest { |
| 32 | + |
| 33 | + private static final String PROJECT_KEY = "myKey"; |
| 34 | + private static final String QUERY = "query"; |
| 35 | + |
| 36 | + private JiraSession jiraSession = null; |
| 37 | + |
| 38 | + @Mock |
| 39 | + private JiraSite site; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private JiraRestService service = null; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void prepareMocks() throws IOException, InterruptedException { |
| 46 | + jiraSession = spy(new JiraSession(site, service)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void replaceWithFixVersionByRegex() throws URISyntaxException, TimeoutException { |
| 51 | + final ExtendedVersion newVersion = |
| 52 | + new ExtendedVersion(new URI("self"), 3L, "v3.0", null, false, false, null, null); |
| 53 | + List<ExtendedVersion> myVersions = new ArrayList<>(); |
| 54 | + myVersions.add(newVersion); |
| 55 | + when(jiraSession.getVersions(PROJECT_KEY)).thenReturn(myVersions); |
| 56 | + |
| 57 | + ArrayList<Issue> issues = new ArrayList<>(); |
| 58 | + issues.add(getIssue(Arrays.asList("v1.0"), 1L)); |
| 59 | + issues.add(getIssue(Arrays.asList("v1.0", "v2.0", "v2.0.0"), 2L)); |
| 60 | + when(service.getIssuesFromJqlSearch(QUERY, JiraSession.MAX_ISSUES)).thenReturn(issues); |
| 61 | + |
| 62 | + jiraSession.replaceFixVersion(PROJECT_KEY, "/v1.*/", newVersion.getName(), QUERY); |
| 63 | + |
| 64 | + ArgumentCaptor<String> issueKeys = ArgumentCaptor.forClass(String.class); |
| 65 | + ArgumentCaptor<List> versionList = ArgumentCaptor.forClass(List.class); |
| 66 | + verify(service, times(2)).updateIssue(issueKeys.capture(), versionList.capture()); |
| 67 | + |
| 68 | + // First Issue, current FixVersion replaced by new one |
| 69 | + assertThat(issueKeys.getAllValues().get(0), equalTo(issues.get(0).getKey())); |
| 70 | + List<ExtendedVersion> firstIssueUpdatedFixVersions = |
| 71 | + versionList.getAllValues().get(0); |
| 72 | + assertThat(firstIssueUpdatedFixVersions.size(), equalTo(1)); |
| 73 | + assertThat(firstIssueUpdatedFixVersions.get(0).getName(), equalTo(newVersion.getName())); |
| 74 | + |
| 75 | + // Second Issue, current FixVersion stays, new fixVersion added. |
| 76 | + assertThat(issueKeys.getAllValues().get(1), equalTo(issues.get(1).getKey())); |
| 77 | + List<ExtendedVersion> secondIssueUpdatedFixVersions = |
| 78 | + versionList.getAllValues().get(1); |
| 79 | + assertThat(secondIssueUpdatedFixVersions.size(), equalTo(3)); |
| 80 | + |
| 81 | + // Check that the collection contains versions with these names in any order |
| 82 | + assertThat(secondIssueUpdatedFixVersions, hasItem(hasProperty("name", equalTo(newVersion.getName())))); |
| 83 | + assertThat(secondIssueUpdatedFixVersions, hasItem(hasProperty("name", equalTo("v2.0")))); |
| 84 | + assertThat(secondIssueUpdatedFixVersions, hasItem(hasProperty("name", equalTo("v2.0.0")))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void replaceFixVersion() throws URISyntaxException, TimeoutException { |
| 89 | + final ExtendedVersion newVersion = |
| 90 | + new ExtendedVersion(new URI("self"), 3L, "v3.0", null, false, false, null, null); |
| 91 | + List<ExtendedVersion> myVersions = new ArrayList<>(); |
| 92 | + myVersions.add(newVersion); |
| 93 | + when(jiraSession.getVersions(PROJECT_KEY)).thenReturn(myVersions); |
| 94 | + |
| 95 | + ArrayList<Issue> issues = new ArrayList<>(); |
| 96 | + issues.add(getIssue(Arrays.asList("v1.0"), 1L)); |
| 97 | + issues.add(getIssue(Arrays.asList("v1.0", "v1.0.0", "v2.0.0"), 2L)); |
| 98 | + when(service.getIssuesFromJqlSearch(QUERY, JiraSession.MAX_ISSUES)).thenReturn(issues); |
| 99 | + |
| 100 | + jiraSession.replaceFixVersion(PROJECT_KEY, "v1.0", newVersion.getName(), QUERY); |
| 101 | + |
| 102 | + ArgumentCaptor<String> issueKeys = ArgumentCaptor.forClass(String.class); |
| 103 | + ArgumentCaptor<List> versionList = ArgumentCaptor.forClass(List.class); |
| 104 | + verify(service, times(2)).updateIssue(issueKeys.capture(), versionList.capture()); |
| 105 | + |
| 106 | + // First Issue, current FixVersion replaced by new one |
| 107 | + assertThat(issueKeys.getAllValues().get(0), equalTo(issues.get(0).getKey())); |
| 108 | + List<Version> firstIssueUpdatedFixVersions = versionList.getAllValues().get(0); |
| 109 | + assertThat(firstIssueUpdatedFixVersions.size(), equalTo(1)); |
| 110 | + assertThat(firstIssueUpdatedFixVersions.get(0), equalTo(newVersion)); |
| 111 | + |
| 112 | + // Second Issue, current FixVersion stays, new fixVersion added. |
| 113 | + assertThat(issueKeys.getAllValues().get(1), equalTo(issues.get(1).getKey())); |
| 114 | + List<Version> secondIssueUpdatedFixVersions = versionList.getAllValues().get(1); |
| 115 | + assertThat(secondIssueUpdatedFixVersions.size(), equalTo(3)); |
| 116 | + assertThat(secondIssueUpdatedFixVersions, hasItem(hasProperty("name", equalTo(newVersion.getName())))); |
| 117 | + assertThat(secondIssueUpdatedFixVersions, hasItem(hasProperty("name", equalTo("v1.0.0")))); |
| 118 | + assertThat(secondIssueUpdatedFixVersions, hasItem(hasProperty("name", equalTo("v2.0.0")))); |
| 119 | + } |
| 120 | + |
| 121 | + private Issue getIssue(List<String> versions, long id) throws URISyntaxException { |
| 122 | + List<Version> fixVersions = new ArrayList<>(); |
| 123 | + if (versions != null) { |
| 124 | + for (String fixVersion : versions) { |
| 125 | + fixVersions.add(new Version( |
| 126 | + new URI("self"), ThreadLocalRandom.current().nextLong(), fixVersion, null, false, false, null)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return new Issue( |
| 131 | + "", |
| 132 | + new URI(""), |
| 133 | + PROJECT_KEY + id, |
| 134 | + ThreadLocalRandom.current().nextLong(), |
| 135 | + null, |
| 136 | + null, |
| 137 | + null, |
| 138 | + null, |
| 139 | + null, |
| 140 | + null, |
| 141 | + null, |
| 142 | + null, |
| 143 | + null, |
| 144 | + null, |
| 145 | + null, |
| 146 | + null, |
| 147 | + null, |
| 148 | + fixVersions, |
| 149 | + null, |
| 150 | + null, |
| 151 | + null, |
| 152 | + null, |
| 153 | + null, |
| 154 | + null, |
| 155 | + null, |
| 156 | + null, |
| 157 | + null, |
| 158 | + null, |
| 159 | + null, |
| 160 | + null, |
| 161 | + null, |
| 162 | + null, |
| 163 | + null); |
| 164 | + } |
| 165 | +} |
0 commit comments