-
-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathJenkinsServerTest.java
More file actions
422 lines (325 loc) · 13.8 KB
/
Copy pathJenkinsServerTest.java
File metadata and controls
422 lines (325 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright (c) 2013 Cosmin Stejerean, Karl Heinz Marbaise, and contributors.
*
* Distributed under the MIT license: http://opensource.org/licenses/MIT
*/
package com.offbytwo.jenkins;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.http.entity.ContentType;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import com.google.common.base.Optional;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.BuildWithDetails;
import com.offbytwo.jenkins.model.FolderJob;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.MainView;
import com.offbytwo.jenkins.model.View;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpResponseException;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class JenkinsServerTest extends BaseUnitTest {
private JenkinsHttpClient client = mock(JenkinsHttpClient.class);
private JenkinsServer server = new JenkinsServer(client);
private MainView mainView = new MainView(new Job("Hello", "http://localhost/job/Hello/"));
@Before
public void setUp() throws Exception {
given(client.get("/", MainView.class)).willReturn(mainView);
}
@Test
public void shouldReturnListOfJobs() throws Exception {
assertTrue(server.getJobs().containsKey("Hello"));
}
@Test
public void testGetJobXml() throws Exception {
// given
String xmlString = "<xml>some xml goes here</xml>";
String jobName = "pr";
given(client.get(anyString())).willReturn(xmlString);
// when
String xmlReturn = server.getJobXml(jobName);
// then
verify(client).get("/job/pr/config.xml");
assertEquals(xmlString, xmlReturn);
}
@Test
public void testFolderGetJobs() throws Exception {
String[] jobNames = { "job-the-first", "Job-The-Next", "Job-the-Next" };
// given
String path = "http://localhost/jobs/someFolder/";
Job someJob = new Job("jobname", path + "jobname");
FolderJob folderJob = new FolderJob("someFolder", path);
List<Job> someJobs = createTestJobs(path, jobNames);
MainView mv = createTestView(someJobs);
given(client.get(eq(path), eq(MainView.class))).willReturn(mv);
// when
Map<String, Job> map = server.getJobs(folderJob);
// then
verify(client).get(path, MainView.class);
for (String name : jobNames)
assertTrue(someJobs.contains(map.get(name)));
assertEquals(jobNames.length, map.size());
}
@Test
public void testFolderGetJob() throws Exception {
// given
String path = "http://localhost/jobs/someFolder/";
JobWithDetails someJob = mock(JobWithDetails.class);
FolderJob folderJob = new FolderJob("someFolder", path);
given(client.get(eq(path + "job/jobname"), eq(JobWithDetails.class))).willReturn(someJob);
// when
Job jobResult = server.getJob(folderJob, "jobname");
// then
verify(client).get(path + "job/jobname", JobWithDetails.class);
assertEquals(someJob, jobResult);
}
@Test
public void testFolderGetView() throws Exception {
// given
String path = "http://localhost/jobs/someFolder/";
FolderJob folderJob = new FolderJob("someFolder", path);
View someView = mock(View.class);
given(client.get(eq(path + "view/viewname/"), eq(View.class))).willReturn(someView);
// when
View viewResult = server.getView(folderJob, "viewname");
// then
verify(client).get(path + "view/viewname/", View.class);
assertEquals(someView, viewResult);
}
@Test
public void testGetFolderJob() throws Exception {
// given
String path = "http://localhost/jobs/someFolder/";
Job someJob = new Job("someFolder", path);
FolderJob folderJob = mock(FolderJob.class);
given(folderJob.isFolder()).willReturn(true);
given(client.get(eq(path), eq(FolderJob.class))).willReturn(folderJob);
// when
Optional<FolderJob> oj = server.getFolderJob(someJob);
// then
verify(client).get(path, FolderJob.class);
assertEquals(folderJob, oj.get());
}
@Test
public void testGetFolderJobInvalidFolder() throws Exception {
// given
String path = "http://localhost/jobs/someFolder/";
Job someJob = new Job("someFolder", path);
FolderJob folderJob = mock(FolderJob.class);
given(folderJob.isFolder()).willReturn(false);
given(client.get(eq(path), eq(FolderJob.class))).willReturn(folderJob);
// when
Optional<FolderJob> oj = server.getFolderJob(someJob);
// then
verify(client).get(path, FolderJob.class);
assertEquals(false, oj.isPresent());
}
@Test
public void testCreateFolderJob() throws Exception {
// when
server.createFolder("someFolder");
// then
verify(client).post_form(eq("/createItem?"), anyMap(), eq(false));
}
@Test
public void testCreateSubFolderJob() throws Exception {
// given
String path = "http://localhost/jobs/someFolder/";
FolderJob folderJob = mock(FolderJob.class);
given(folderJob.getUrl()).willReturn(path);
// when
server.createFolder(folderJob, "someFolder");
// then
verify(client).post_form(eq(path + "createItem?"), anyMap(), eq(false));
}
@Test
public void testUpdateJobXml() throws Exception {
// given
String jobName = "pr";
String xmlString = "<xml>some xml goes here</xml>";
given(client.post_xml(anyString(), eq(xmlString))).willReturn(xmlString);
// when
server.updateJob(jobName, xmlString);
// then
ArgumentCaptor<String> captureString = ArgumentCaptor.forClass(String.class);
verify(client).post_xml(eq("/job/pr/config.xml"), captureString.capture(), eq(true));
assertEquals(xmlString, captureString.getValue());
}
@Test
public void testCreateJob() throws Exception {
// given
String jobName = "test-job-" + UUID.randomUUID().toString();
String xmlString = "<xml>some xml goes here</xml>";
// when
server.createJob(jobName, xmlString);
// then
ArgumentCaptor<String> captureString = ArgumentCaptor.forClass(String.class);
verify(client).post_xml(eq("/createItem?name=" + jobName), captureString.capture(), eq(false));
String xmlReturn = captureString.getValue();
assertEquals(xmlReturn, xmlString);
}
@Test
public void testJenkinsConnectivity() throws IOException {
// given
given(client.get("/")).willReturn("<xml>not a real response</xml>");
// then
assertEquals(server.isRunning(), true);
}
@Test
public void testJenkinsConnectivityBroken() throws IOException {
// given
given(client.get("/")).willThrow(IOException.class);
// then
assertEquals(server.isRunning(), false);
}
@Test
public void testQuietDown() throws IOException {
server.quietDown();
server.cancelQuietDown();
}
@Test
public void testScriptPosts() throws IOException, URISyntaxException {
given(client.post_text("/scriptText", "script=script", ContentType.APPLICATION_FORM_URLENCODED, false))
.willReturn("result");
String result = server.runScript("script");
verify(client).post_text("/scriptText", "script=script", ContentType.APPLICATION_FORM_URLENCODED, false);
assertEquals("result", result);
}
public void testJenkinsPathEncoding() throws IOException {
given(client.get("/job/encoded%2Fproperly%3F/config.xml")).willReturn("<xml>not a real response</xml>");
assertEquals("<xml>not a real response</xml>", server.getJobXml("encoded/properly?"));
}
private MainView createTestView(List<Job> jobs) {
return new MainView(jobs.toArray(new Job[0]));
}
private MainView createTestView(String baseUrl, String... jobNames) {
return createTestView(createTestJobs(baseUrl, jobNames));
}
private List<Job> createTestJobs(String baseUrl, String... jobNames) {
List<Job> jobs = new ArrayList<Job>();
for (String name : jobNames) {
jobs.add(new Job(name, baseUrl + name));
}
return jobs;
}
@Test
public void testReturnSingleJob() throws Exception {
shouldReturnListOfJobs("hello");
}
@Test
public void testReturnListOfJobs() throws Exception {
shouldReturnListOfJobs("hello", "Hello", "HeLLo");
}
@Test
public void testFolderGetSingleJob() throws Exception {
shouldGetFolderJobs("jobname");
}
private void shouldReturnListOfJobs(String... jobNames) throws Exception {
MainView mainView = createTestView("http://localhost/job/", jobNames);
given(client.get("/", MainView.class)).willReturn(mainView);
Map<String, Job> jobs = server.getJobs();
for (String name : jobNames)
assertTrue(jobs.containsKey(name));
assertEquals(jobNames.length, jobs.size());
}
@Test
public void testGetJobXmls() throws Exception {
shouldGetJobXml("pr");
shouldGetJobXml("hello");
shouldGetJobXml("Hello");
shouldGetJobXml("HeLLo");
}
@Test
public void getVersionShouldNotFailWithNPE()
throws Exception
{
when (client.get( "/" )).thenReturn( "TheAnswer");
when (client.getJenkinsVersion()).thenReturn( "1.23");
JenkinsServer server = new JenkinsServer( client);
server.getVersion();
verify( client, times( 1 )).isJenkinsVersionSet();
verify( client, times( 1 )).get( "/" );
verify( client, times( 1 )).getJenkinsVersion();
}
@Test
public void testGetBuildDetails_NotFound() throws IOException {
final HttpResponseException ex = new HttpResponseException(HttpStatus.SC_NOT_FOUND,"");
given(client.get(anyString(), eq(BuildWithDetails.class))).willThrow(ex);
assertNull(server.getBuildDetails("someJob", 1));
}
@Test(expected=HttpResponseException.class)
public void testGetBuildDetails_ResponseException() throws IOException {
final HttpResponseException ex = new HttpResponseException(HttpStatus.SC_BAD_REQUEST,"");
given(client.get(anyString(), eq(BuildWithDetails.class))).willThrow(ex);
server.getBuildDetails("someJob", 1);
}
@Test
public void testGetBuildDetails_WithFolderJob() throws Exception {
final String expectedPath = "http://localhost/jobs/someFolder/job/someJob/1";
final FolderJob folderJob = new FolderJob("someFolder", "http://localhost/jobs/someFolder/");
final BuildWithDetails expected = new BuildWithDetails();
given(client.get(eq(expectedPath), eq(BuildWithDetails.class))).willReturn(expected);
final BuildWithDetails details = server.getBuildDetails(folderJob, "someJob", 1);
assertTrue(details == expected);
}
@Test
public void testGetBuildDetails_WithOutFolderJob() throws Exception {
final String expectedPath = "/job/someJob/1";
final BuildWithDetails expected = new BuildWithDetails();
given(client.get(eq(expectedPath), eq(BuildWithDetails.class))).willReturn(expected);
final BuildWithDetails details = server.getBuildDetails("someJob", 1);
assertTrue(details == expected);
}
@Test
public void testGetBuildDetails_WithTreeProperties() throws Exception {
final String expectedPath = "/job/someJob/1?tree=building%2Cculprits%5BfullName%5D";
final BuildWithDetails expected = new BuildWithDetails();
given(client.get(eq(expectedPath), eq(BuildWithDetails.class))).willReturn(expected);
final String[] treeProps = {"building", "culprits[fullName]"};
final BuildWithDetails details = server.getBuildDetails("someJob", 1, treeProps);
assertTrue(details == expected);
}
private void shouldGetFolderJobs(String... jobNames) throws IOException {
// given
String path = "http://localhost/jobs/someFolder/";
FolderJob folderJob = new FolderJob("someFolder", path);
List<Job> someJobs = createTestJobs(path, jobNames);
MainView mv = createTestView(someJobs);
given(client.get(eq(path), eq(MainView.class))).willReturn(mv);
// when
Map<String, Job> map = server.getJobs(folderJob);
// then
verify(client).get(path, MainView.class);
for (String name : jobNames)
assertTrue(someJobs.contains(map.get(name)));
assertEquals(jobNames.length, map.size());
}
private void shouldGetJobXml(String jobName) throws Exception {
// given
String xmlString = "<xml>some xml goes here</xml>";
given(client.get(anyString())).willReturn(xmlString);
// when
String xmlReturn = server.getJobXml(jobName);
// then
verify(client).get("/job/" + jobName + "/config.xml");
verify(client).get("/job/" + jobName + "/config.xml");
assertEquals(xmlString, xmlReturn);
}
}