From 1dfaa8555ace5590ab4f6ecfe1f9127de0c62149 Mon Sep 17 00:00:00 2001 From: Vincent Behar Date: Fri, 1 Jul 2011 18:18:26 +0200 Subject: [PATCH] add mappings for /projects and /project/NAME --- .../java/org/rundeck/api/RundeckClient.java | 29 ++++++++ .../rundeck/api/domain/RundeckProject.java | 70 +++++++++++++++++++ .../org/rundeck/api/parser/ProjectParser.java | 40 +++++++++++ .../rundeck/api/parser/ProjectsParser.java | 40 +++++++++++ .../rundeck/api/parser/ProjectParserTest.java | 27 +++++++ .../api/parser/ProjectsParserTest.java | 34 +++++++++ .../org/rundeck/api/parser/project.xml | 1 + .../org/rundeck/api/parser/projects.xml | 1 + 8 files changed, 242 insertions(+) create mode 100644 src/main/java/org/rundeck/api/domain/RundeckProject.java create mode 100644 src/main/java/org/rundeck/api/parser/ProjectParser.java create mode 100644 src/main/java/org/rundeck/api/parser/ProjectsParser.java create mode 100644 src/test/java/org/rundeck/api/parser/ProjectParserTest.java create mode 100644 src/test/java/org/rundeck/api/parser/ProjectsParserTest.java create mode 100644 src/test/resources/org/rundeck/api/parser/project.xml create mode 100644 src/test/resources/org/rundeck/api/parser/projects.xml diff --git a/src/main/java/org/rundeck/api/RundeckClient.java b/src/main/java/org/rundeck/api/RundeckClient.java index e1a9abd..9285c5a 100644 --- a/src/main/java/org/rundeck/api/RundeckClient.java +++ b/src/main/java/org/rundeck/api/RundeckClient.java @@ -7,10 +7,13 @@ import org.apache.commons.lang.StringUtils; import org.rundeck.api.RundeckApiException.RundeckApiLoginException; import org.rundeck.api.domain.RundeckExecution; import org.rundeck.api.domain.RundeckJob; +import org.rundeck.api.domain.RundeckProject; import org.rundeck.api.parser.ExecutionParser; import org.rundeck.api.parser.ExecutionsParser; import org.rundeck.api.parser.JobParser; import org.rundeck.api.parser.JobsParser; +import org.rundeck.api.parser.ProjectParser; +import org.rundeck.api.parser.ProjectsParser; import org.rundeck.api.util.ArgsUtil; import org.rundeck.api.util.AssertUtil; @@ -174,6 +177,32 @@ public class RundeckClient implements Serializable { return new ApiCall(this).get(apiPath, new ExecutionParser("result/executions/execution")); } + /** + * List all projects + * + * @return a {@link List} of {@link RundeckProject} : might be empty, but won't be null + * @throws RundeckApiException in case of error when calling the API + * @throws RundeckApiLoginException if the login failed + */ + public List getProjects() throws RundeckApiException, RundeckApiLoginException { + return new ApiCall(this).get("/projects", new ProjectsParser("result/projects/project")); + } + + /** + * Get the definition of a single project, identified by the given name + * + * @param projectName name of the project - mandatory + * @return a {@link RundeckProject} instance + * @throws RundeckApiException in case of error when calling the API + * @throws RundeckApiLoginException if the login failed + * @throws IllegalArgumentException if the projectName is blank (null, empty or whitespace) + */ + public RundeckProject getProject(String projectName) throws RundeckApiException, RundeckApiLoginException, + IllegalArgumentException { + AssertUtil.notBlank(projectName, "projectName is mandatory to get the details of a project !"); + return new ApiCall(this).get("/project/" + projectName, new ProjectParser("result/projects/project")); + } + public String getUrl() { return url; } diff --git a/src/main/java/org/rundeck/api/domain/RundeckProject.java b/src/main/java/org/rundeck/api/domain/RundeckProject.java new file mode 100644 index 0000000..6ea6d50 --- /dev/null +++ b/src/main/java/org/rundeck/api/domain/RundeckProject.java @@ -0,0 +1,70 @@ +package org.rundeck.api.domain; + +import java.io.Serializable; + +/** + * Represents a RunDeck project + * + * @author Vincent Behar + */ +public class RundeckProject implements Serializable { + + private static final long serialVersionUID = 1L; + + private String name; + + private String description; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "RundeckProject [name=" + name + ", description=" + description + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + RundeckProject other = (RundeckProject) obj; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/src/main/java/org/rundeck/api/parser/ProjectParser.java b/src/main/java/org/rundeck/api/parser/ProjectParser.java new file mode 100644 index 0000000..2b27a72 --- /dev/null +++ b/src/main/java/org/rundeck/api/parser/ProjectParser.java @@ -0,0 +1,40 @@ +package org.rundeck.api.parser; + +import org.apache.commons.lang.StringUtils; +import org.dom4j.Node; +import org.rundeck.api.domain.RundeckProject; + +/** + * Parser for a single {@link RundeckProject} + * + * @author Vincent Behar + */ +public class ProjectParser implements NodeParser { + + private String xpath; + + public ProjectParser() { + super(); + } + + /** + * @param xpath of the project element if it is not the root node + */ + public ProjectParser(String xpath) { + super(); + this.xpath = xpath; + } + + @Override + public RundeckProject parseNode(Node node) { + Node projectNode = xpath != null ? node.selectSingleNode(xpath) : node; + + RundeckProject project = new RundeckProject(); + + project.setName(StringUtils.trimToNull(projectNode.valueOf("name"))); + project.setDescription(StringUtils.trimToNull(projectNode.valueOf("description"))); + + return project; + } + +} diff --git a/src/main/java/org/rundeck/api/parser/ProjectsParser.java b/src/main/java/org/rundeck/api/parser/ProjectsParser.java new file mode 100644 index 0000000..9a9a619 --- /dev/null +++ b/src/main/java/org/rundeck/api/parser/ProjectsParser.java @@ -0,0 +1,40 @@ +package org.rundeck.api.parser; + +import java.util.ArrayList; +import java.util.List; +import org.dom4j.Node; +import org.rundeck.api.domain.RundeckProject; + +/** + * Parser for a {@link List} of {@link RundeckProject} + * + * @author Vincent Behar + */ +public class ProjectsParser implements NodeParser> { + + private final String xpath; + + /** + * @param xpath of the projects elements + */ + public ProjectsParser(String xpath) { + super(); + this.xpath = xpath; + } + + @Override + public List parseNode(Node node) { + List projects = new ArrayList(); + + @SuppressWarnings("unchecked") + List projectNodes = node.selectNodes(xpath); + + for (Node projectNode : projectNodes) { + RundeckProject project = new ProjectParser().parseNode(projectNode); + projects.add(project); + } + + return projects; + } + +} diff --git a/src/test/java/org/rundeck/api/parser/ProjectParserTest.java b/src/test/java/org/rundeck/api/parser/ProjectParserTest.java new file mode 100644 index 0000000..a123765 --- /dev/null +++ b/src/test/java/org/rundeck/api/parser/ProjectParserTest.java @@ -0,0 +1,27 @@ +package org.rundeck.api.parser; + +import java.io.InputStream; +import org.dom4j.Document; +import org.junit.Assert; +import org.junit.Test; +import org.rundeck.api.domain.RundeckProject; + +/** + * Test the {@link ProjectParser} + * + * @author Vincent Behar + */ +public class ProjectParserTest { + + @Test + public void parseNode() throws Exception { + InputStream input = getClass().getResourceAsStream("project.xml"); + Document document = ParserHelper.loadDocument(input); + + RundeckProject project = new ProjectParser("result/projects/project").parseNode(document); + + Assert.assertEquals("test", project.getName()); + Assert.assertEquals("test project", project.getDescription()); + } + +} diff --git a/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java b/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java new file mode 100644 index 0000000..627ec79 --- /dev/null +++ b/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java @@ -0,0 +1,34 @@ +package org.rundeck.api.parser; + +import java.io.InputStream; +import java.util.List; +import org.dom4j.Document; +import org.junit.Assert; +import org.junit.Test; +import org.rundeck.api.domain.RundeckProject; + +/** + * Test the {@link ProjectsParser} + * + * @author Vincent Behar + */ +public class ProjectsParserTest { + + @Test + public void parseNode() throws Exception { + InputStream input = getClass().getResourceAsStream("projects.xml"); + Document document = ParserHelper.loadDocument(input); + + List projects = new ProjectsParser("result/projects/project").parseNode(document); + Assert.assertEquals(2, projects.size()); + + RundeckProject project1 = projects.get(0); + Assert.assertEquals("test", project1.getName()); + Assert.assertEquals("test project", project1.getDescription()); + + RundeckProject project2 = projects.get(1); + Assert.assertEquals("other", project2.getName()); + Assert.assertEquals(null, project2.getDescription()); + } + +} diff --git a/src/test/resources/org/rundeck/api/parser/project.xml b/src/test/resources/org/rundeck/api/parser/project.xml new file mode 100644 index 0000000..18030d9 --- /dev/null +++ b/src/test/resources/org/rundeck/api/parser/project.xml @@ -0,0 +1 @@ +testtest project \ No newline at end of file diff --git a/src/test/resources/org/rundeck/api/parser/projects.xml b/src/test/resources/org/rundeck/api/parser/projects.xml new file mode 100644 index 0000000..fb08df7 --- /dev/null +++ b/src/test/resources/org/rundeck/api/parser/projects.xml @@ -0,0 +1 @@ +testtest projectother \ No newline at end of file