Skip to content

Commit a02382c

Browse files
Copilotrantoniuk
andcommitted
Move pure Mockito tests to unit package and create split test files
Co-authored-by: rantoniuk <521838+rantoniuk@users.noreply.github.com>
1 parent 12a87bc commit a02382c

12 files changed

+1885
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package hudson.plugins.jira.integration;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.Mockito.*;
7+
8+
import com.atlassian.jira.rest.client.api.domain.Component;
9+
import com.atlassian.jira.rest.client.api.domain.IssueType;
10+
import com.atlassian.jira.rest.client.api.domain.Priority;
11+
import com.cloudbees.hudson.plugins.folder.Folder;
12+
import com.cloudbees.plugins.credentials.CredentialsScope;
13+
import com.cloudbees.plugins.credentials.CredentialsStore;
14+
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
15+
import com.cloudbees.plugins.credentials.domains.Domain;
16+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
17+
import hudson.model.FreeStyleProject;
18+
import hudson.plugins.jira.JiraCreateIssueNotifier;
19+
import hudson.plugins.jira.JiraFolderProperty;
20+
import hudson.plugins.jira.JiraFolderPropertyTest;
21+
import hudson.plugins.jira.JiraGlobalConfiguration;
22+
import hudson.plugins.jira.JiraSession;
23+
import hudson.plugins.jira.JiraSite;
24+
import hudson.util.ListBoxModel;
25+
import java.net.URL;
26+
import java.util.Collections;
27+
import org.hamcrest.Matchers;
28+
import org.junit.jupiter.api.Test;
29+
import org.jvnet.hudson.test.JenkinsRule;
30+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
31+
32+
@WithJenkins
33+
public class JiraCreateIssueNotifierIntegrationTest {
34+
35+
@Test
36+
void doFillPriorityIdItems(JenkinsRule j) throws Exception {
37+
38+
String credId_1 = "cred-1-id";
39+
String credId_2 = "cred-2-id";
40+
41+
String pwd1 = "pwd1";
42+
String pwd2 = "pwd2";
43+
44+
UsernamePasswordCredentialsImpl cred1 =
45+
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credId_1, null, "user1", pwd1);
46+
UsernamePasswordCredentialsImpl cred2 =
47+
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credId_2, null, "user2", pwd2);
48+
49+
SystemCredentialsProvider systemProvider = SystemCredentialsProvider.getInstance();
50+
systemProvider.getCredentials().add(cred1);
51+
systemProvider.save();
52+
53+
{ // test at project level
54+
URL url = new URL("https://pacific-ale.com.au");
55+
JiraSite jiraSite = mock(JiraSite.class);
56+
when(jiraSite.getUrl()).thenReturn(url);
57+
when(jiraSite.getCredentialsId()).thenReturn(credId_1);
58+
when(jiraSite.getName()).thenReturn(url.toExternalForm());
59+
JiraSession jiraSession = mock(JiraSession.class);
60+
when(jiraSession.getPriorities())
61+
.thenReturn(Collections.singletonList(new Priority(null, 2L, "priority-1", null, null, null)));
62+
when(jiraSite.getSession(any())).thenReturn(jiraSession);
63+
64+
JiraGlobalConfiguration.get().setSites(Collections.singletonList(jiraSite));
65+
66+
FreeStyleProject p = j.jenkins.createProject(
67+
FreeStyleProject.class, "p" + j.jenkins.getItems().size());
68+
ListBoxModel options = JiraCreateIssueNotifier.DESCRIPTOR.doFillPriorityIdItems(p);
69+
assertNotNull(options);
70+
assertThat(options.size(), Matchers.equalTo(2));
71+
assertThat(options.get(1).value, Matchers.equalTo("2"));
72+
assertThat(options.get(1).name, Matchers.containsString("priority-1"));
73+
assertThat(options.get(1).name, Matchers.containsString("https://pacific-ale.com.au"));
74+
}
75+
76+
{ // test at folder level
77+
Folder folder = j.jenkins.createProject(
78+
Folder.class, "folder" + j.jenkins.getItems().size());
79+
80+
CredentialsStore folderStore = JiraFolderPropertyTest.getFolderStore(folder);
81+
folderStore.addCredentials(Domain.global(), cred2);
82+
83+
JiraFolderProperty foo = new JiraFolderProperty();
84+
85+
JiraSite jiraSite = mock(JiraSite.class);
86+
URL url = new URL("https://pale-ale.com.au");
87+
when(jiraSite.getUrl()).thenReturn(url);
88+
when(jiraSite.getCredentialsId()).thenReturn(credId_2);
89+
when(jiraSite.getName()).thenReturn(url.toExternalForm());
90+
JiraSession jiraSession = mock(JiraSession.class);
91+
when(jiraSession.getPriorities())
92+
.thenReturn(Collections.singletonList(new Priority(null, 3L, "priority-2", null, null, null)));
93+
when(jiraSite.getSession(any())).thenReturn(jiraSession);
94+
95+
foo.setSites(Collections.singletonList(jiraSite));
96+
folder.getProperties().add(foo);
97+
98+
ListBoxModel options = JiraCreateIssueNotifier.DESCRIPTOR.doFillPriorityIdItems(folder);
99+
assertNotNull(options);
100+
assertEquals(2, options.size());
101+
assertEquals("3", options.get(1).value);
102+
assertTrue(options.get(1).name.contains("priority-2"));
103+
assertTrue(options.get(1).name.contains("https://pale-ale.com.au"));
104+
}
105+
}
106+
107+
@Test
108+
void doFillTypeItems(JenkinsRule j) throws Exception {
109+
110+
String credId_1 = "cred-1-id";
111+
String credId_2 = "cred-2-id";
112+
113+
String pwd1 = "pwd1";
114+
String pwd2 = "pwd2";
115+
116+
UsernamePasswordCredentialsImpl cred1 =
117+
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credId_1, null, "user1", pwd1);
118+
UsernamePasswordCredentialsImpl cred2 =
119+
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credId_2, null, "user2", pwd2);
120+
121+
SystemCredentialsProvider systemProvider = SystemCredentialsProvider.getInstance();
122+
systemProvider.getCredentials().add(cred1);
123+
systemProvider.save();
124+
125+
{ // test at project level
126+
URL url = new URL("https://pacific-ale.com.au");
127+
JiraSite jiraSite = mock(JiraSite.class);
128+
when(jiraSite.getUrl()).thenReturn(url);
129+
when(jiraSite.getCredentialsId()).thenReturn(credId_1);
130+
when(jiraSite.getName()).thenReturn(url.toExternalForm());
131+
JiraSession jiraSession = mock(JiraSession.class);
132+
when(jiraSession.getIssueTypes())
133+
.thenReturn(Collections.singletonList(new IssueType(null, 4L, "issue-type-1", false, null, null)));
134+
when(jiraSite.getSession(any())).thenReturn(jiraSession);
135+
136+
JiraGlobalConfiguration.get().setSites(Collections.singletonList(jiraSite));
137+
138+
FreeStyleProject p = j.jenkins.createProject(
139+
FreeStyleProject.class, "p" + j.jenkins.getItems().size());
140+
ListBoxModel options = JiraCreateIssueNotifier.DESCRIPTOR.doFillTypeItems(p);
141+
assertNotNull(options);
142+
assertThat(options.size(), Matchers.equalTo(2));
143+
assertThat(options.get(1).value, Matchers.equalTo("4"));
144+
assertThat(options.get(1).name, Matchers.containsString("issue-type-1"));
145+
assertThat(options.get(1).name, Matchers.containsString("https://pacific-ale.com.au"));
146+
}
147+
148+
{ // test at folder level
149+
Folder folder = j.jenkins.createProject(
150+
Folder.class, "folder" + j.jenkins.getItems().size());
151+
152+
CredentialsStore folderStore = JiraFolderPropertyTest.getFolderStore(folder);
153+
folderStore.addCredentials(Domain.global(), cred2);
154+
155+
JiraFolderProperty foo = new JiraFolderProperty();
156+
157+
JiraSite jiraSite = mock(JiraSite.class);
158+
URL url = new URL("https://pale-ale.com.au");
159+
when(jiraSite.getUrl()).thenReturn(url);
160+
when(jiraSite.getCredentialsId()).thenReturn(credId_2);
161+
when(jiraSite.getName()).thenReturn(url.toExternalForm());
162+
JiraSession jiraSession = mock(JiraSession.class);
163+
when(jiraSession.getIssueTypes())
164+
.thenReturn(Collections.singletonList(new IssueType(null, 5L, "issue-type-2", false, null, null)));
165+
when(jiraSite.getSession(any())).thenReturn(jiraSession);
166+
167+
foo.setSites(Collections.singletonList(jiraSite));
168+
folder.getProperties().add(foo);
169+
170+
ListBoxModel options = JiraCreateIssueNotifier.DESCRIPTOR.doFillTypeItems(folder);
171+
assertNotNull(options);
172+
assertEquals(2, options.size());
173+
assertEquals("5", options.get(1).value);
174+
assertTrue(options.get(1).name.contains("issue-type-2"));
175+
assertTrue(options.get(1).name.contains("https://pale-ale.com.au"));
176+
}
177+
}
178+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package hudson.plugins.jira.integration;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.thoughtworks.xstream.XStream;
6+
import hudson.plugins.jira.JiraGlobalConfiguration;
7+
import hudson.plugins.jira.JiraProjectProperty.DescriptorImpl;
8+
import hudson.plugins.jira.JiraSite;
9+
import hudson.util.XStream2;
10+
import java.io.InputStream;
11+
import org.junit.jupiter.api.Test;
12+
import org.jvnet.hudson.test.JenkinsRule;
13+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
14+
15+
@WithJenkins
16+
class JiraGlobalConfigurationTest {
17+
18+
@Test
19+
void migrateOldConfiguration(JenkinsRule r) throws Exception {
20+
String url = "https://backwardsCompatURL.com/";
21+
XStream xstream = new XStream2();
22+
JiraSite expected = new JiraSite(url);
23+
InputStream resource = getClass().getResourceAsStream("oldJiraProjectProperty.xml");
24+
DescriptorImpl instance = (DescriptorImpl) xstream.fromXML(resource);
25+
assertNotNull(instance);
26+
assertNull(instance.sites);
27+
JiraSite actual = JiraGlobalConfiguration.get().getSites().get(0);
28+
assertEquals(url, actual.getName());
29+
r.assertEqualDataBoundBeans(expected, actual);
30+
}
31+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package hudson.plugins.jira.integration;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.cloudbees.hudson.plugins.folder.Folder;
6+
import hudson.model.FreeStyleProject;
7+
import hudson.plugins.jira.JiraFolderProperty;
8+
import hudson.plugins.jira.JiraProjectProperty;
9+
import hudson.plugins.jira.JiraSite;
10+
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
11+
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
12+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.jvnet.hudson.test.JenkinsRule;
18+
19+
@WithJenkinsConfiguredWithCode
20+
class JiraProjectPropertyTest {
21+
22+
private JenkinsRule r;
23+
24+
private FreeStyleProject freeStyleProject;
25+
private Folder folder;
26+
private List<JiraSite> firstList;
27+
28+
@BeforeEach
29+
void initialize(JenkinsConfiguredWithCodeRule r) throws Exception {
30+
this.r = r;
31+
folder = r.jenkins.createProject(Folder.class, "first");
32+
JiraFolderProperty jiraFolderProperty = new JiraFolderProperty();
33+
firstList = new ArrayList<>();
34+
firstList.add(new JiraSite("https://first.com/"));
35+
jiraFolderProperty.setSites(firstList);
36+
folder.getProperties().add(jiraFolderProperty);
37+
}
38+
39+
@Test
40+
void getSitesNullWithoutFolder() throws Exception {
41+
FreeStyleProject freeStyleProject = r.createFreeStyleProject();
42+
JiraProjectProperty prop = new JiraProjectProperty(null);
43+
freeStyleProject.addProperty(prop);
44+
JiraProjectProperty actual = freeStyleProject.getProperty(JiraProjectProperty.class);
45+
assertNotNull(actual);
46+
assertNull(actual.getSite());
47+
}
48+
49+
@Test
50+
void getSitesNullWithFolder() throws Exception {
51+
freeStyleProject = folder.createProject(FreeStyleProject.class, "something");
52+
JiraProjectProperty prop = new JiraProjectProperty(null);
53+
freeStyleProject.addProperty(prop);
54+
JiraProjectProperty property = freeStyleProject.getProperty(JiraProjectProperty.class);
55+
assertNotNull(property);
56+
assertNull(property.getSite());
57+
}
58+
59+
@Test
60+
@ConfiguredWithCode("single-site.yml")
61+
void getSiteFromProjectProperty() {
62+
JiraProjectProperty prop = new JiraProjectProperty(null);
63+
JiraSite site = prop.getSite();
64+
@SuppressWarnings("ConstantConditions")
65+
String actual = site.getUrl().toExternalForm();
66+
assertEquals("https://jira.com/", actual);
67+
}
68+
69+
@Test
70+
@ConfiguredWithCode("single-site.yml")
71+
void getSiteFromSingleEntry() throws Exception {
72+
freeStyleProject = r.createFreeStyleProject();
73+
JiraSite expected = JiraGlobalConfiguration.get().getSites().get(0);
74+
JiraProjectProperty prop = new JiraProjectProperty(null);
75+
freeStyleProject.addProperty(prop);
76+
JiraProjectProperty property = freeStyleProject.getProperty(JiraProjectProperty.class);
77+
assertNotNull(property);
78+
assertNotNull(property.getSite());
79+
assertEquals(expected.getName(), property.siteName);
80+
r.assertEqualDataBoundBeans(expected, property.getSite());
81+
}
82+
83+
@Test
84+
@ConfiguredWithCode("multiple-sites.yml")
85+
void getSiteFromFirstGlobalMultipleEntryMultipleSites() throws Exception {
86+
freeStyleProject = r.createFreeStyleProject();
87+
JiraSite expected = JiraGlobalConfiguration.get().getSites().get(0);
88+
JiraProjectProperty prop = new JiraProjectProperty(null);
89+
freeStyleProject.addProperty(prop);
90+
JiraProjectProperty property = freeStyleProject.getProperty(JiraProjectProperty.class);
91+
assertNotNull(property);
92+
assertNotNull(property.getSite());
93+
assertEquals(expected.getName(), property.siteName);
94+
r.assertEqualDataBoundBeans(expected, property.getSite());
95+
}
96+
97+
@Test
98+
@ConfiguredWithCode("multiple-sites.yml")
99+
void getSiteFromSecondGlobalEntryMultipleSites() throws Exception {
100+
freeStyleProject = r.createFreeStyleProject();
101+
JiraSite expected = new JiraSite("https://jira.com/");
102+
JiraProjectProperty prop = new JiraProjectProperty(expected.getName());
103+
freeStyleProject.addProperty(prop);
104+
JiraProjectProperty property = freeStyleProject.getProperty(JiraProjectProperty.class);
105+
assertNotNull(property);
106+
assertNotNull(property.getSite());
107+
assertEquals(expected.getName(), property.siteName);
108+
r.assertEqualDataBoundBeans(expected, property.getSite());
109+
}
110+
111+
@Test
112+
@ConfiguredWithCode("single-site.yml")
113+
void getSiteFromFirstFolderLayer() throws Exception {
114+
freeStyleProject = folder.createProject(FreeStyleProject.class, "something");
115+
JiraSite expected = firstList.get(0);
116+
JiraProjectProperty prop = new JiraProjectProperty(expected.getName());
117+
freeStyleProject.addProperty(prop);
118+
JiraProjectProperty property = freeStyleProject.getProperty(JiraProjectProperty.class);
119+
assertNotNull(property);
120+
assertNotNull(property.getSite());
121+
assertEquals(expected.getName(), property.siteName);
122+
r.assertEqualDataBoundBeans(expected, property.getSite());
123+
}
124+
125+
@Test
126+
@ConfiguredWithCode("single-site.yml")
127+
void getSiteFromNestedFolderLayer() throws Exception {
128+
Folder secondFolder = folder.createProject(Folder.class, "second");
129+
freeStyleProject = secondFolder.createProject(FreeStyleProject.class, "something");
130+
// testing we can get value from folder above.
131+
JiraSite expected = firstList.get(0);
132+
JiraProjectProperty prop = new JiraProjectProperty(expected.getName());
133+
freeStyleProject.addProperty(prop);
134+
JiraProjectProperty property = freeStyleProject.getProperty(JiraProjectProperty.class);
135+
assertNotNull(property);
136+
assertNotNull(property.getSite());
137+
assertEquals(expected.getName(), property.siteName);
138+
r.assertEqualDataBoundBeans(expected, property.getSite());
139+
}
140+
}

0 commit comments

Comments
 (0)