Skip to content

Commit

Permalink
Fixed #53
Browse files Browse the repository at this point in the history
 Added methods to get the existing views.
  • Loading branch information
khmarbaise committed Aug 13, 2015
1 parent 822c654 commit f7614d3
Showing 5 changed files with 102 additions and 23 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -145,6 +145,12 @@
<version>${jenkins-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
19 changes: 19 additions & 0 deletions src/main/java/com/offbytwo/jenkins/JenkinsServer.java
Original file line number Diff line number Diff line change
@@ -97,6 +97,25 @@ public String apply(Job job) {
});
}

/**
* Get a list of all the defined views on the server (at the summary level)
*
* @return list of defined views
* @throws IOException
*/
public Map<String, View> getViews() throws IOException {
List<View> views = client.get("/", MainView.class).getViews();
return Maps.uniqueIndex(views, new Function<View, String>() {
@Override
public String apply(View view) {
view.setClient(client);
//return view.getName().toLowerCase();
return view.getName();
}
});
}


/**
* Get a single view object from the server
*
20 changes: 15 additions & 5 deletions src/main/java/com/offbytwo/jenkins/model/MainView.java
Original file line number Diff line number Diff line change
@@ -6,26 +6,28 @@

package com.offbytwo.jenkins.model;

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;

public class MainView extends BaseModel {

private List<Job> jobs;
private List<View> views;

/* default constructor needed for Jackson */
public MainView() {
this(Lists.<Job>newArrayList());
this(Lists.<Job> newArrayList(), Lists.<View> newArrayList());
}

public MainView(List<Job> jobs) {
public MainView(List<Job> jobs, List<View> views) {
this.jobs = jobs;
this.views = views;
}

public MainView(Job... jobs) {
this(Arrays.asList(jobs));
this(Arrays.asList(jobs), Lists.<View> newArrayList());
}

public List<Job> getJobs() {
@@ -35,4 +37,12 @@ public List<Job> getJobs() {
public void setJobs(List<Job> jobs) {
this.jobs = jobs;
}

public List<View> getViews() {
return views;
}

public void setViews(List<View> views) {
this.views = views;
}
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;

@@ -13,17 +15,29 @@
import com.offbytwo.jenkins.model.BuildChangeSetItem;
import com.offbytwo.jenkins.model.BuildChangeSetPath;
import com.offbytwo.jenkins.model.BuildWithDetails;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.MavenJobWithDetails;
import com.offbytwo.jenkins.model.View;

public class JenkinsChangeSetTest {

@Test
public void shouldAddStringParamToAnExistingJob() throws IOException {
JenkinsServer js = new
JenkinsServer(URI.create("http://localhost:10090/"));
// JenkinsServer js = new JenkinsServer(URI.create("http://ci.soebes.de:8080/"));
MavenJobWithDetails mavenJob = js.getMavenJob("javaee");
// MavenJobWithDetails mavenJob = js.getMavenJob("appassembler-maven-plugin");
// JenkinsServer js = new
// JenkinsServer(URI.create("http://localhost:10090/"));
JenkinsServer js = new JenkinsServer(URI.create("http://ci.soebes.de:8080/"));
Map<String, View> views = js.getViews();
for (Entry<String, View> view : views.entrySet()) {
System.out.println("view: " +view.getKey() + " " + view.getValue().getName());
View selectedView = js.getView(view.getKey());
System.out.println(" Selected View: " + selectedView.getName());
List<Job> jobs = selectedView.getJobs();
for (Job job : jobs) {
System.out.println(" Selected View job: " + job.getName());
}
}
// MavenJobWithDetails mavenJob = js.getMavenJob("javaee");
MavenJobWithDetails mavenJob = js.getMavenJob("appassembler-maven-plugin");

BuildWithDetails details = mavenJob.getLastBuild().details();
// BuildWithDetails details = mavenJob.getBuilds().get(10).details();
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.offbytwo.jenkins.integration;

import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.TopLevelItem;
import org.dom4j.DocumentException;
import org.junit.Test;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import static org.assertj.core.api.Assertions.assertThat;

import javax.servlet.ServletException;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Map;

import javax.servlet.ServletException;
import javax.xml.bind.JAXBException;

import org.dom4j.DocumentException;
import org.junit.Test;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import com.offbytwo.jenkins.model.View;

import static org.junit.Assert.assertNotNull;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.ListView;
import hudson.model.TopLevelItem;

public class JenkinsClientViewIT extends BaseForIntegrationTests {

@@ -30,7 +36,29 @@ public void shouldObtainView() throws URISyntaxException, IOException, JAXBExcep
com.offbytwo.jenkins.model.View testView = jenkinsServer.getView(TEST_VIEW);

// then
assertNotNull(testView);
assertThat(testView).isNotNull();
}

@Test
public void shouldGetAllViews() throws URISyntaxException, IOException, JAXBException, DocumentException {
ListView createdFirstView = new ListView("FirstView");
ListView createdSecondView = new ListView("SecondView");
// given
jenkinsRule.getInstance().addView(createdFirstView);
jenkinsRule.getInstance().addView(createdSecondView);

// when
Map<String, View> testView = jenkinsServer.getViews();

assertThat(testView).hasSize(3);
//TODO: Check why this does not work?
// // then
// View first = new View();
// first.setName("FirstView");
// assertThat(testView.get("FirstView")).isEqualTo(first);
// View second = new View();
// second.setName("SecondView");
// assertThat(testView.get("SecondView")).isEqualTo(second);
}

private static class TestView extends hudson.model.View implements Serializable {
@@ -54,12 +82,14 @@ public boolean contains(TopLevelItem topLevelItem) {
}

@Override
protected void submit(StaplerRequest staplerRequest) throws IOException, ServletException, Descriptor.FormException {
protected void submit(StaplerRequest staplerRequest)
throws IOException, ServletException, Descriptor.FormException {

}

@Override
public Item doCreateItem(StaplerRequest staplerRequest, StaplerResponse staplerResponse) throws IOException, ServletException {
public Item doCreateItem(StaplerRequest staplerRequest, StaplerResponse staplerResponse)
throws IOException, ServletException {
return null;
}
}

0 comments on commit f7614d3

Please sign in to comment.