diff --git a/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobAsyncClient.java b/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobAsyncClient.java index 110bda6405..45b612aa5d 100644 --- a/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobAsyncClient.java +++ b/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobAsyncClient.java @@ -73,4 +73,20 @@ public interface JobAsyncClient { @ExceptionParser(ReturnVoidOn302Or404.class) ListenableFuture delete(@PathParam("displayName") String displayName); + /** + * @see JobClient#buildJob + */ + @POST + @Path("/job/{displayName}/build") + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture build(@PathParam("displayName") String displayName); + + /** + * @see JobClient#fetchConfigXML + */ + @GET + @Path("/job/{displayName}/config.xml") + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture fetchConfigXML(@PathParam("displayName") String displayName); + } diff --git a/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobClient.java b/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobClient.java index 45e0af6f9d..cce44bcdfc 100644 --- a/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobClient.java +++ b/labs/jenkins/src/main/java/org/jclouds/jenkins/v1/features/JobClient.java @@ -44,5 +44,17 @@ public interface JobClient { JobDetails get(String displayName); void delete(String displayName); - + + /** + * Build a job via API + * + * If security is enabled, provide username/password of an account with build permission in the request. + * Another alternative (but deprecated) is to configure the 'Trigger builds remotely' section in the job configuration. + * + * @param displayName + */ + void build(String displayName); + + String fetchConfigXML(String displayName); + } diff --git a/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientExpectTest.java b/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientExpectTest.java index f6eb16b7fb..e41d1b6bf6 100644 --- a/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientExpectTest.java +++ b/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientExpectTest.java @@ -41,7 +41,6 @@ import com.google.common.collect.ImmutableMultimap; @Test(groups = "unit", testName = "JobClientExpectTest") public class JobClientExpectTest extends BaseJenkinsClientExpectTest { - public void testCreateJobStringWhenResponseIs2xx() throws IOException { HttpRequest createJob = HttpRequest.builder() .method("POST") @@ -97,22 +96,57 @@ public class JobClientExpectTest extends BaseJenkinsClientExpectTest { .build(); public void testGetJobWhenResponseIs2xx() { - HttpResponse getJobResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/job.json")).build(); - JenkinsClient clientWhenJobExists = requestSendsResponse(getJob, getJobResponse); - assertEquals(clientWhenJobExists.getJobClient().get("ddd").toString(), new ParseJobDetailsTest().expected().toString()); } public void testGetJobWhenResponseIs404() { - HttpResponse getJobResponse = HttpResponse.builder().statusCode(404).build(); - JenkinsClient getJobWhenGetd = requestSendsResponse(getJob, getJobResponse); - assertNull(getJobWhenGetd.getJobClient().get("ddd")); } + + HttpRequest buildJob = HttpRequest.builder() + .method("POST") + .endpoint(URI.create("http://localhost:8080/job/ddd/build")) + .headers(ImmutableMultimap. builder() + .put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()) + .build(); + + public void testBuildJobWhenResponseIs2xx() { + HttpResponse buildJobResponse = HttpResponse.builder().statusCode(200).build(); + JenkinsClient clientWhenJobExists = requestSendsResponse(buildJob, buildJobResponse); + clientWhenJobExists.getJobClient().build("ddd"); + } + + public void testBuildJobWhenResponseIs404() { + HttpResponse getJobResponse = HttpResponse.builder().statusCode(404).build(); + JenkinsClient getJobWhenGetd = requestSendsResponse(buildJob, getJobResponse); + getJobWhenGetd.getJobClient().build("ddd"); + } + + HttpRequest fetchConfig = HttpRequest.builder() + .method("GET") + .endpoint(URI.create("http://localhost:8080/job/ddd/config.xml")) + .headers(ImmutableMultimap. builder() + .put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()) + .build(); + + public void testFetchConfigXMLWhenResponseIs2xx() { + HttpResponse fetchConfigResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/sample_job.xml", "text/xml")).build(); + JenkinsClient clientWhenJobExists = requestSendsResponse(fetchConfig, fetchConfigResponse); + String configXML = clientWhenJobExists.getJobClient().fetchConfigXML("ddd"); + //TODO enable this assertion + //assertEquals(configXML, Strings2.toStringAndClose(getClass().getResourceAsStream("/sample_job.xml"))); + } + + public void testFetchConfigXMLWhenResponseIs404() { + HttpResponse fetchConfigResponse = HttpResponse.builder().statusCode(404).build(); + JenkinsClient getJobWhenGetd = requestSendsResponse(fetchConfig, fetchConfigResponse); + getJobWhenGetd.getJobClient().fetchConfigXML("ddd"); + } } diff --git a/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientLiveTest.java b/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientLiveTest.java index 6ed48d54b7..ae7218b056 100644 --- a/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientLiveTest.java +++ b/labs/jenkins/src/test/java/org/jclouds/jenkins/v1/features/JobClientLiveTest.java @@ -40,8 +40,16 @@ public class JobClientLiveTest extends BaseJenkinsClientLiveTest { getClient().delete("blagoo"); getClient().createFromXML("blagoo", Strings2.toStringAndClose(getClass().getResourceAsStream("/sample_job.xml"))); } - + @Test(dependsOnMethods = "testCreateJob") + public void testFetchConfigXML() throws IOException { + String configXML = getClient().fetchConfigXML("blagoo"); + assertNotNull(configXML); + //TODO enable this assertion + //assertEquals(configXML, Strings2.toStringAndClose(getClass().getResourceAsStream("/sample_job.xml"))); + } + + @Test(dependsOnMethods = "testFetchConfigXML") public void testGetJob() throws IOException { JobDetails job = getClient().get("blagoo"); assertNotNull(job); @@ -49,6 +57,11 @@ public class JobClientLiveTest extends BaseJenkinsClientLiveTest { } @Test(dependsOnMethods = "testGetJob") + public void testBuildJob() throws IOException { + getClient().build("blagoo"); + } + + @Test(dependsOnMethods = "testBuildJob") public void testDeleteJob() { getClient().delete("blagoo"); } @@ -57,6 +70,7 @@ public class JobClientLiveTest extends BaseJenkinsClientLiveTest { @Override protected void tearDownContext() { getClient().delete("blagoo"); + getClient().delete("blagooCopy"); super.tearDownContext(); }