Skip to content

Commit

Permalink
Issue #43. RealBuilds.parameters() was implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
aistomin committed Feb 25, 2016
1 parent cc1b4fb commit 136b7fa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/main/java/com/github/aistomin/jenkins/real/RealJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
package com.github.aistomin.jenkins.real;

import com.github.aistomin.http.PostRequest;
import com.github.aistomin.iterators.EntityIterator;
import com.github.aistomin.iterators.Transformation;
import com.github.aistomin.jenkins.Builds;
import com.github.aistomin.jenkins.Credentials;
import com.github.aistomin.jenkins.Job;
import com.github.aistomin.jenkins.JobDetails;
import com.github.aistomin.jenkins.JobParameter;
import com.github.aistomin.xml.XmlString;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.net.URLEncoder;
import java.util.Iterator;
import org.apache.commons.lang3.NotImplementedException;

/**
* Jenkins' job.
Expand Down Expand Up @@ -111,14 +113,12 @@ public Builds builds() throws Exception {
*
* @return Job's parameters.
* @throws Exception If something goes wrong.
* @todo: Let's implement this method and solve Issue #43.
*/
public Iterator<JobParameter> parameters() throws Exception {
throw new NotImplementedException(
String.format(
"parameters() method is not implemented for %s.",
this.getClass().getCanonicalName()
)
return new EntityIterator<JobParameter, XML>(
new XMLDocument(
this.xml("action/parameterDefinition&wrapper=parameters")
).nodes("/parameters/*").iterator(), new ParamTransformer()
);
}

Expand All @@ -132,6 +132,21 @@ public String xml() throws Exception {
return new PostRequest(this.request(), this.creds.headers()).execute();
}

/**
* Get XML for path.
*
* @param path API path.
* @return XML's string.
* @throws Exception If something goes wrong.
*/
private String xml(final String path) throws Exception {
return new PostRequest(
String.format(
"%s/%s", this.request(), path
), this.creds.headers()
).execute();
}

/**
* Creates API URL to request Jenkins' job data.
*
Expand Down Expand Up @@ -186,4 +201,24 @@ public Job transform(final String source) {
return new RealJob(source, this.api, this.creds);
}
}

/**
* JobParameter transformer.
*/
private static final class ParamTransformer implements
Transformation<JobParameter, XML> {

/**
* Transform XML to JobParameter.
*
* @param source Source object.
* @return JobParameter.
* @checkstyle NonStaticMethodCheck (500 lines)
*/
public JobParameter transform(final XML source) {
return new RealJobParameter(
new XmlString(source.toString())
);
}
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/github/aistomin/jenkins/real/ITRealJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.github.aistomin.jenkins.Job;
import com.github.aistomin.jenkins.JobDetails;
import com.github.aistomin.jenkins.JobParameter;
import java.util.Iterator;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
Expand Down Expand Up @@ -104,4 +105,26 @@ public void testCanReadUrl() throws Exception {
)
);
}

/**
* Can read job's parameters.
*
* @throws Exception If something goes wrong.
*/
@Test
public void testCanReadParameters() throws Exception {
final Iterator<Job> jobs = new TestJenkins().jobs().findByName(
"test-parametrised-job"
);
final Iterator<JobParameter> params = jobs.next().parameters();
MatcherAssert.assertThat(params.hasNext(), new IsEqual<Boolean>(true));
MatcherAssert.assertThat(
params.next().name(), new IsEqual<String>("stringParameter")
);
MatcherAssert.assertThat(params.hasNext(), new IsEqual<Boolean>(true));
MatcherAssert.assertThat(
params.next().name(), new IsEqual<String>("boolParam")
);
MatcherAssert.assertThat(params.hasNext(), new IsEqual<Boolean>(false));
}
}

0 comments on commit 136b7fa

Please sign in to comment.