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+ }
0 commit comments