Skip to content

Commit 0e07f21

Browse files
wstrzalkapnathan
authored andcommitted
Unit tests refactored to check for multiple jobs with names that differ in lower/upper case only
1 parent 0d617dd commit 0e07f21

1 file changed

Lines changed: 94 additions & 3 deletions

File tree

src/test/java/com/offbytwo/jenkins/JenkinsServerTest.java

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.IOException;
1919
import java.net.URISyntaxException;
20+
import java.util.ArrayList;
21+
import java.util.List;
2022
import java.util.Map;
2123
import java.util.UUID;
2224

@@ -149,7 +151,7 @@ public void testGetFolderJobInvalidFolder() throws Exception {
149151

150152
given(folderJob.isFolder()).willReturn(false);
151153
given(client.get(eq(path), eq(FolderJob.class))).willReturn(folderJob);
152-
154+
153155
// when
154156
Optional<FolderJob> oj = server.getFolderJob(someJob);
155157

@@ -181,7 +183,7 @@ public void testCreateSubFolderJob() throws Exception {
181183
// then
182184
verify(client).post_form(eq(path + "createItem?"), anyMap(), eq(false));
183185
}
184-
186+
185187
@Test
186188
public void testUpdateJobXml() throws Exception {
187189
// given
@@ -242,7 +244,7 @@ public void testQuietDown() throws IOException {
242244
@Test
243245
public void testScriptPosts() throws IOException, URISyntaxException {
244246
given(client.post_text("/scriptText", "script=script", ContentType.APPLICATION_FORM_URLENCODED, false))
245-
.willReturn("result");
247+
.willReturn("result");
246248
String result = server.runScript("script");
247249
verify(client).post_text("/scriptText", "script=script", ContentType.APPLICATION_FORM_URLENCODED, false);
248250
assertEquals("result", result);
@@ -253,4 +255,93 @@ public void testJenkinsPathEncoding() throws IOException {
253255

254256
assertEquals("<xml>not a real response</xml>", server.getJobXml("encoded/properly?"));
255257
}
258+
259+
private MainView createTestView(List<Job> jobs) {
260+
return new MainView(jobs.toArray(new Job[0]));
261+
}
262+
263+
private MainView createTestView(String baseUrl, String... jobNames) {
264+
return createTestView(createTestJobs(baseUrl, jobNames));
265+
}
266+
267+
private List<Job> createTestJobs(String baseUrl, String... jobNames) {
268+
List<Job> jobs = new ArrayList<Job>();
269+
for(String name: jobNames) {
270+
jobs.add(new Job(name, baseUrl+name));
271+
}
272+
273+
return jobs;
274+
}
275+
276+
277+
@Test
278+
public void testReturnSingleJob() throws Exception {
279+
shouldReturnListOfJobs("hello");
280+
}
281+
282+
@Test
283+
public void testReturnListOfJobs() throws Exception {
284+
shouldReturnListOfJobs("hello", "Hello", "HeLLo");
285+
}
286+
287+
@Test
288+
public void testFolderGetSingleJob() throws Exception {
289+
shouldGetFolderJobs("jobname");
290+
}
291+
292+
293+
private void shouldReturnListOfJobs(String...jobNames) throws Exception {
294+
MainView mainView = createTestView("http://localhost/job/", jobNames);
295+
given(client.get("/", MainView.class)).willReturn(mainView);
296+
Map<String, Job> jobs = server.getJobs();
297+
for(String name : jobNames)
298+
assertTrue(jobs.containsKey(name));
299+
300+
assertEquals(jobNames.length, jobs.size());
301+
}
302+
303+
@Test
304+
public void testGetJobXmls() throws Exception {
305+
306+
shouldGetJobXml("pr");
307+
shouldGetJobXml("hello");
308+
shouldGetJobXml("Hello");
309+
shouldGetJobXml("HeLLo");
310+
}
311+
312+
private void shouldGetFolderJobs(String... jobNames) throws IOException {
313+
// given
314+
String path = "http://localhost/jobs/someFolder/";
315+
FolderJob folderJob = new FolderJob("someFolder", path);
316+
317+
List<Job> someJobs = createTestJobs(path, jobNames);
318+
MainView mv = createTestView(someJobs);
319+
320+
given(client.get(eq(path), eq(MainView.class))).willReturn(mv);
321+
322+
// when
323+
Map<String, Job> map = server.getJobs(folderJob);
324+
325+
// then
326+
verify(client).get(path, MainView.class);
327+
328+
for(String name : jobNames)
329+
assertTrue(someJobs.contains(map.get(name)));
330+
331+
assertEquals(jobNames.length, map.size());
332+
}
333+
private void shouldGetJobXml(String jobName) throws Exception {
334+
// given
335+
String xmlString = "<xml>some xml goes here</xml>";
336+
337+
given(client.get(anyString())).willReturn(xmlString);
338+
339+
// when
340+
String xmlReturn = server.getJobXml(jobName);
341+
342+
// then
343+
verify(client).get("/job/"+jobName+"/config.xml");
344+
verify(client).get("/job/"+jobName+"/config.xml");
345+
assertEquals(xmlString, xmlReturn);
346+
}
256347
}

0 commit comments

Comments
 (0)