|
| 1 | +package hudson.plugins.jira.pipeline; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.containsString; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertSame; |
| 7 | +import static org.mockito.ArgumentMatchers.any; |
| 8 | +import static org.mockito.ArgumentMatchers.anyList; |
| 9 | +import static org.mockito.ArgumentMatchers.anyString; |
| 10 | +import static org.mockito.Mockito.doAnswer; |
| 11 | +import static org.mockito.Mockito.doReturn; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.spy; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import hudson.EnvVars; |
| 17 | +import hudson.Launcher; |
| 18 | +import hudson.model.AbstractBuild; |
| 19 | +import hudson.model.AbstractProject; |
| 20 | +import hudson.model.BuildListener; |
| 21 | +import hudson.model.Job; |
| 22 | +import hudson.model.Result; |
| 23 | +import hudson.plugins.jira.JiraGlobalConfiguration; |
| 24 | +import hudson.plugins.jira.JiraSession; |
| 25 | +import hudson.plugins.jira.JiraSite; |
| 26 | +import hudson.plugins.jira.model.JiraIssueField; |
| 27 | +import hudson.plugins.jira.selector.ExplicitIssueSelector; |
| 28 | +import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.PrintStream; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Random; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Rule; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.jvnet.hudson.test.JenkinsRule; |
| 41 | +import org.mockito.Mock; |
| 42 | +import org.mockito.junit.MockitoJUnitRunner; |
| 43 | +import org.mockito.stubbing.Answer; |
| 44 | + |
| 45 | +/** @author Dmitry Frolov tekillaz.dev@gmail.com */ |
| 46 | +@RunWith(MockitoJUnitRunner.class) |
| 47 | +public class IssueFieldUpdateStepTest { |
| 48 | + |
| 49 | + private static final String BUILD_NUMBER_VAR = "${BUILD_NUMBER}"; |
| 50 | + |
| 51 | + @Rule public JenkinsRule r = new JenkinsConfiguredWithCodeRule(); |
| 52 | + |
| 53 | + @Mock JiraSite site; |
| 54 | + @Mock JiraSession session; |
| 55 | + @Mock PrintStream logger; |
| 56 | + @Mock AbstractBuild build; |
| 57 | + @Mock Launcher launcher; |
| 58 | + @Mock BuildListener listener; |
| 59 | + @Mock AbstractProject project; |
| 60 | + @Mock Job job; |
| 61 | + |
| 62 | + @Before |
| 63 | + public void before() { |
| 64 | + when(build.getParent()).thenReturn(project); |
| 65 | + when(build.getProject()).thenReturn(project); |
| 66 | + when(listener.getLogger()).thenReturn(logger); |
| 67 | + doReturn(session).when(site).getSession(any()); |
| 68 | + JiraGlobalConfiguration.get().setSites(Collections.singletonList(site)); |
| 69 | + // when(jiraGlobalConfiguration.getSites()).thenReturn(sites); |
| 70 | + // .getSession(build.getParent())).thenReturn(session); |
| 71 | + when(job.getBuild(any())).thenReturn(build); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void checkPrepareFieldId() { |
| 76 | + |
| 77 | + List<String> field_test = Arrays.asList("10100", "customfield_10100", "field_10100"); |
| 78 | + |
| 79 | + List<String> field_after = |
| 80 | + Arrays.asList("customfield_10100", "customfield_10100", "customfield_field_10100"); |
| 81 | + |
| 82 | + IssueFieldUpdateStep jifu = new IssueFieldUpdateStep(null, null, ""); |
| 83 | + for (int i = 0; i < field_test.size(); i++) |
| 84 | + assertEquals( |
| 85 | + "Check field id conversion #" + i, |
| 86 | + jifu.prepareFieldId(field_test.get(i)), |
| 87 | + field_after.get(i)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test(expected = IOException.class) |
| 91 | + public void shouldFailIfSelectorIsNull() throws InterruptedException, IOException { |
| 92 | + IssueFieldUpdateStep jifu = spy(new IssueFieldUpdateStep(null, "", "")); |
| 93 | + jifu.perform(build, null, launcher, listener); |
| 94 | + assertSame("Check selector is null", Result.FAILURE, build.getResult()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + // @ConfiguredWithCode("single-site.yml") |
| 99 | + public void checkSubmit() throws InterruptedException, IOException { |
| 100 | + Random random = new Random(); |
| 101 | + Integer randomBuildNumber = random.nextInt(85) + 15; // random number 15 < r < 99 |
| 102 | + String issueId = "ISSUE-" + random.nextInt(1000) + 999; |
| 103 | + String beforeFieldid = "field" + random.nextInt(100) + 99; |
| 104 | + String beforeFieldValue = "Some comment, build #${BUILD_NUMBER}"; |
| 105 | + |
| 106 | + EnvVars env = new EnvVars(); |
| 107 | + env.put("BUILD_NUMBER", randomBuildNumber.toString()); |
| 108 | + when(build.getEnvironment(listener)).thenReturn(env); |
| 109 | + |
| 110 | + final ExplicitIssueSelector issueSelector = new ExplicitIssueSelector(issueId); |
| 111 | + final List<String> issuesAfter = new ArrayList<String>(); |
| 112 | + final List<JiraIssueField> fieldsAfter = new ArrayList<JiraIssueField>(); |
| 113 | + |
| 114 | + doAnswer( |
| 115 | + invocation -> { |
| 116 | + String id = (String) invocation.getArguments()[0]; |
| 117 | + List<JiraIssueField> jif = (List<JiraIssueField>) invocation.getArguments()[1]; |
| 118 | + issuesAfter.add(id); |
| 119 | + fieldsAfter.addAll(jif); |
| 120 | + return null; |
| 121 | + }) |
| 122 | + .when(session) |
| 123 | + .addFields(anyString(), anyList()); |
| 124 | + |
| 125 | + IssueFieldUpdateStep jifu = spy(new IssueFieldUpdateStep(issueSelector, beforeFieldid, beforeFieldValue)); |
| 126 | + jifu.perform(build, null, launcher, listener); |
| 127 | + |
| 128 | + assertEquals("Check issue value", issuesAfter.get(0), issueId); |
| 129 | + assertEquals("customfield_" + beforeFieldid, fieldsAfter.get(0).getId()); |
| 130 | + assertThat( |
| 131 | + fieldsAfter.get(0).getValue().toString(), |
| 132 | + containsString("build #" + randomBuildNumber.toString())); |
| 133 | + |
| 134 | + jifu.perform(build, null, env, launcher, listener); |
| 135 | + assertThat( |
| 136 | + fieldsAfter.get(1).getValue().toString(), |
| 137 | + containsString("build #" + randomBuildNumber.toString())); |
| 138 | + } |
| 139 | +} |
0 commit comments