internal refactoring : use a generic parser for list of elements

This commit is contained in:
Vincent Behar 2011-07-06 22:58:09 +02:00
parent 2cadc04052
commit 01cd512b0b
10 changed files with 172 additions and 427 deletions

View file

@ -24,19 +24,16 @@ import org.apache.commons.lang.StringUtils;
import org.rundeck.api.RundeckApiException.RundeckApiLoginException;
import org.rundeck.api.domain.RundeckAbort;
import org.rundeck.api.domain.RundeckExecution;
import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
import org.rundeck.api.domain.RundeckJob;
import org.rundeck.api.domain.RundeckNode;
import org.rundeck.api.domain.RundeckProject;
import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
import org.rundeck.api.parser.AbortParser;
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.ListParser;
import org.rundeck.api.parser.NodeParser;
import org.rundeck.api.parser.NodesParser;
import org.rundeck.api.parser.ProjectParser;
import org.rundeck.api.parser.ProjectsParser;
import org.rundeck.api.util.AssertUtil;
import org.rundeck.api.util.ParametersUtil;
@ -120,7 +117,8 @@ public class RundeckClient implements Serializable {
* @throws RundeckApiLoginException if the login failed
*/
public List<RundeckProject> getProjects() throws RundeckApiException, RundeckApiLoginException {
return new ApiCall(this).get(new ApiPathBuilder("/projects"), new ProjectsParser("result/projects/project"));
return new ApiCall(this).get(new ApiPathBuilder("/projects"),
new ListParser<RundeckProject>(new ProjectParser(), "result/projects/project"));
}
/**
@ -193,7 +191,7 @@ public class RundeckClient implements Serializable {
.param("jobFilter", jobFilter)
.param("groupPath", groupPath)
.param("idlist", StringUtils.join(jobIds, ",")),
new JobsParser("result/jobs/job"));
new ListParser<RundeckJob>(new JobParser(), "result/jobs/job"));
}
/**
@ -590,7 +588,8 @@ public class RundeckClient implements Serializable {
RundeckApiLoginException, IllegalArgumentException {
AssertUtil.notBlank(project, "project is mandatory get all running executions !");
return new ApiCall(this).get(new ApiPathBuilder("/executions/running").param("project", project),
new ExecutionsParser("result/executions/execution"));
new ListParser<RundeckExecution>(new ExecutionParser(),
"result/executions/execution"));
}
/**
@ -641,7 +640,8 @@ public class RundeckClient implements Serializable {
status != null ? StringUtils.lowerCase(status.toString()) : null)
.param("max", max)
.param("offset", offset),
new ExecutionsParser("result/executions/execution"));
new ListParser<RundeckExecution>(new ExecutionParser(),
"result/executions/execution"));
}
/**
@ -725,7 +725,7 @@ public class RundeckClient implements Serializable {
AssertUtil.notBlank(project, "project is mandatory to get all nodes !");
return new ApiCall(this).get(new ApiPathBuilder("/resources").param("project", project)
.nodeFilters(nodeFilters),
new NodesParser("project/node"));
new ListParser<RundeckNode>(new NodeParser(), "project/node"));
}
/**

View file

@ -1,55 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rundeck.api.parser;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Node;
import org.rundeck.api.domain.RundeckExecution;
/**
* Parser for a {@link List} of {@link RundeckExecution}
*
* @author Vincent Behar
*/
public class ExecutionsParser implements XmlNodeParser<List<RundeckExecution>> {
private final String xpath;
/**
* @param xpath of the executions elements
*/
public ExecutionsParser(String xpath) {
super();
this.xpath = xpath;
}
@Override
public List<RundeckExecution> parseXmlNode(Node node) {
List<RundeckExecution> executions = new ArrayList<RundeckExecution>();
@SuppressWarnings("unchecked")
List<Node> execNodes = node.selectNodes(xpath);
for (Node execNode : execNodes) {
RundeckExecution execution = new ExecutionParser().parseXmlNode(execNode);
executions.add(execution);
}
return executions;
}
}

View file

@ -18,38 +18,41 @@ package org.rundeck.api.parser;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Node;
import org.rundeck.api.domain.RundeckJob;
/**
* Parser for a {@link List} of {@link RundeckJob}
* Parser for a {@link List} of elements
*
* @author Vincent Behar
*/
public class JobsParser implements XmlNodeParser<List<RundeckJob>> {
public class ListParser<T> implements XmlNodeParser<List<T>> {
private final XmlNodeParser<T> parser;
private final String xpath;
/**
* @param xpath of the jobs elements
* @param parser for an individual element
* @param xpath of the elements
*/
public JobsParser(String xpath) {
public ListParser(XmlNodeParser<T> parser, String xpath) {
super();
this.parser = parser;
this.xpath = xpath;
}
@Override
public List<RundeckJob> parseXmlNode(Node node) {
List<RundeckJob> jobs = new ArrayList<RundeckJob>();
public List<T> parseXmlNode(Node node) {
List<T> elements = new ArrayList<T>();
@SuppressWarnings("unchecked")
List<Node> jobNodes = node.selectNodes(xpath);
List<Node> elementNodes = node.selectNodes(xpath);
for (Node jobNode : jobNodes) {
RundeckJob job = new JobParser().parseXmlNode(jobNode);
jobs.add(job);
for (Node elementNode : elementNodes) {
T element = parser.parseXmlNode(elementNode);
elements.add(element);
}
return jobs;
return elements;
}
}

View file

@ -1,55 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rundeck.api.parser;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Node;
import org.rundeck.api.domain.RundeckNode;
/**
* Parser for a {@link List} of {@link RundeckNode}
*
* @author Vincent Behar
*/
public class NodesParser implements XmlNodeParser<List<RundeckNode>> {
private final String xpath;
/**
* @param xpath of the rundeck-nodes elements
*/
public NodesParser(String xpath) {
super();
this.xpath = xpath;
}
@Override
public List<RundeckNode> parseXmlNode(Node node) {
List<RundeckNode> rundeckNodes = new ArrayList<RundeckNode>();
@SuppressWarnings("unchecked")
List<Node> rundeckNodeNodes = node.selectNodes(xpath);
for (Node rundeckNodeNode : rundeckNodeNodes) {
RundeckNode rundeckNode = new NodeParser().parseXmlNode(rundeckNodeNode);
rundeckNodes.add(rundeckNode);
}
return rundeckNodes;
}
}

View file

@ -1,55 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 XmlNodeParser<List<RundeckProject>> {
private final String xpath;
/**
* @param xpath of the projects elements
*/
public ProjectsParser(String xpath) {
super();
this.xpath = xpath;
}
@Override
public List<RundeckProject> parseXmlNode(Node node) {
List<RundeckProject> projects = new ArrayList<RundeckProject>();
@SuppressWarnings("unchecked")
List<Node> projectNodes = node.selectNodes(xpath);
for (Node projectNode : projectNodes) {
RundeckProject project = new ProjectParser().parseXmlNode(projectNode);
projects.add(project);
}
return projects;
}
}

View file

@ -1,80 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rundeck.api.parser;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import org.dom4j.Document;
import org.junit.Assert;
import org.junit.Test;
import org.rundeck.api.domain.RundeckExecution;
import org.rundeck.api.domain.RundeckJob;
import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
/**
* Test the {@link ExecutionsParser}
*
* @author Vincent Behar
*/
public class ExecutionsParserTest {
@Test
public void parseExecutions() throws Exception {
InputStream input = getClass().getResourceAsStream("executions.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckExecution> executions = new ExecutionsParser("result/executions/execution").parseXmlNode(document);
Assert.assertEquals(2, executions.size());
RundeckExecution exec1 = executions.get(0);
Assert.assertEquals(new Long(1), exec1.getId());
Assert.assertEquals("http://localhost:4440/execution/follow/1", exec1.getUrl());
Assert.assertEquals(ExecutionStatus.SUCCEEDED, exec1.getStatus());
Assert.assertEquals("admin", exec1.getStartedBy());
Assert.assertEquals(new Date(1308322895104L), exec1.getStartedAt());
Assert.assertEquals(new Date(1308322959420L), exec1.getEndedAt());
Assert.assertEquals(null, exec1.getAbortedBy());
Assert.assertEquals("ls ${option.dir}", exec1.getDescription());
RundeckExecution exec2 = executions.get(1);
Assert.assertEquals(new Long(2), exec2.getId());
Assert.assertEquals("http://localhost:4440/execution/follow/2", exec2.getUrl());
Assert.assertEquals(ExecutionStatus.SUCCEEDED, exec2.getStatus());
Assert.assertEquals("admin", exec2.getStartedBy());
Assert.assertEquals(new Date(1309524165388L), exec2.getStartedAt());
Assert.assertEquals(new Date(1309524174635L), exec2.getEndedAt());
Assert.assertEquals(null, exec2.getAbortedBy());
Assert.assertEquals("ls ${option.dir}", exec2.getDescription());
RundeckJob job1 = exec1.getJob();
Assert.assertEquals("1", job1.getId());
Assert.assertEquals("ls", job1.getName());
Assert.assertEquals("system", job1.getGroup());
Assert.assertEquals("test", job1.getProject());
Assert.assertEquals("list files", job1.getDescription());
RundeckJob job2 = exec2.getJob();
Assert.assertEquals("1", job2.getId());
Assert.assertEquals("ls", job2.getName());
Assert.assertEquals("system", job2.getGroup());
Assert.assertEquals("test", job2.getProject());
Assert.assertEquals("list files", job2.getDescription());
Assert.assertEquals(job1, job2);
}
}

View file

@ -1,55 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.RundeckJob;
/**
* Test the {@link JobsParser}
*
* @author Vincent Behar
*/
public class JobsParserTest {
@Test
public void parseJobs() throws Exception {
InputStream input = getClass().getResourceAsStream("jobs.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckJob> jobs = new JobsParser("result/jobs/job").parseXmlNode(document);
Assert.assertEquals(2, jobs.size());
RundeckJob job1 = jobs.get(0);
Assert.assertEquals("1", job1.getId());
Assert.assertEquals("ls", job1.getName());
Assert.assertEquals("list files", job1.getDescription());
Assert.assertEquals("system", job1.getGroup());
Assert.assertEquals("test", job1.getProject());
RundeckJob job2 = jobs.get(1);
Assert.assertEquals("2", job2.getId());
Assert.assertEquals("ps", job2.getName());
Assert.assertEquals("list processes", job2.getDescription());
Assert.assertEquals("system", job2.getGroup());
Assert.assertEquals("test", job2.getProject());
}
}

View file

@ -0,0 +1,147 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rundeck.api.parser;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.dom4j.Document;
import org.junit.Assert;
import org.junit.Test;
import org.rundeck.api.domain.RundeckExecution;
import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
import org.rundeck.api.domain.RundeckJob;
import org.rundeck.api.domain.RundeckNode;
import org.rundeck.api.domain.RundeckProject;
/**
* Test the {@link ListParser}
*
* @author Vincent Behar
*/
public class ListParserTest {
@Test
public void parseExecutions() throws Exception {
InputStream input = getClass().getResourceAsStream("executions.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckExecution> executions = new ListParser<RundeckExecution>(new ExecutionParser(),
"result/executions/execution").parseXmlNode(document);
Assert.assertEquals(2, executions.size());
RundeckExecution exec1 = executions.get(0);
Assert.assertEquals(new Long(1), exec1.getId());
Assert.assertEquals("http://localhost:4440/execution/follow/1", exec1.getUrl());
Assert.assertEquals(ExecutionStatus.SUCCEEDED, exec1.getStatus());
Assert.assertEquals("admin", exec1.getStartedBy());
Assert.assertEquals(new Date(1308322895104L), exec1.getStartedAt());
Assert.assertEquals(new Date(1308322959420L), exec1.getEndedAt());
Assert.assertEquals(null, exec1.getAbortedBy());
Assert.assertEquals("ls ${option.dir}", exec1.getDescription());
RundeckExecution exec2 = executions.get(1);
Assert.assertEquals(new Long(2), exec2.getId());
Assert.assertEquals("http://localhost:4440/execution/follow/2", exec2.getUrl());
Assert.assertEquals(ExecutionStatus.SUCCEEDED, exec2.getStatus());
Assert.assertEquals("admin", exec2.getStartedBy());
Assert.assertEquals(new Date(1309524165388L), exec2.getStartedAt());
Assert.assertEquals(new Date(1309524174635L), exec2.getEndedAt());
Assert.assertEquals(null, exec2.getAbortedBy());
Assert.assertEquals("ls ${option.dir}", exec2.getDescription());
RundeckJob job1 = exec1.getJob();
Assert.assertEquals("1", job1.getId());
Assert.assertEquals("ls", job1.getName());
Assert.assertEquals("system", job1.getGroup());
Assert.assertEquals("test", job1.getProject());
Assert.assertEquals("list files", job1.getDescription());
RundeckJob job2 = exec2.getJob();
Assert.assertEquals("1", job2.getId());
Assert.assertEquals("ls", job2.getName());
Assert.assertEquals("system", job2.getGroup());
Assert.assertEquals("test", job2.getProject());
Assert.assertEquals("list files", job2.getDescription());
Assert.assertEquals(job1, job2);
}
@Test
public void parseJobs() throws Exception {
InputStream input = getClass().getResourceAsStream("jobs.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckJob> jobs = new ListParser<RundeckJob>(new JobParser(), "result/jobs/job").parseXmlNode(document);
Assert.assertEquals(2, jobs.size());
RundeckJob job1 = jobs.get(0);
Assert.assertEquals("1", job1.getId());
Assert.assertEquals("ls", job1.getName());
Assert.assertEquals("list files", job1.getDescription());
Assert.assertEquals("system", job1.getGroup());
Assert.assertEquals("test", job1.getProject());
RundeckJob job2 = jobs.get(1);
Assert.assertEquals("2", job2.getId());
Assert.assertEquals("ps", job2.getName());
Assert.assertEquals("list processes", job2.getDescription());
Assert.assertEquals("system", job2.getGroup());
Assert.assertEquals("test", job2.getProject());
}
@Test
public void parseNodes() throws Exception {
InputStream input = getClass().getResourceAsStream("resources.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckNode> nodes = new ListParser<RundeckNode>(new NodeParser(), "project/node").parseXmlNode(document);
Assert.assertEquals(1, nodes.size());
RundeckNode node1 = nodes.get(0);
Assert.assertEquals("strongbad", node1.getName());
Assert.assertEquals("Node", node1.getType());
Assert.assertEquals("a development host", node1.getDescription());
Assert.assertEquals(Arrays.asList("dev"), node1.getTags());
Assert.assertEquals("strongbad.local", node1.getHostname());
Assert.assertEquals("i386", node1.getOsArch());
Assert.assertEquals("unix", node1.getOsFamily());
Assert.assertEquals("Linux", node1.getOsName());
Assert.assertEquals("2.6.35-30-generic-pae", node1.getOsVersion());
Assert.assertEquals("rundeck", node1.getUsername());
Assert.assertEquals(null, node1.getEditUrl());
Assert.assertEquals(null, node1.getRemoteUrl());
}
@Test
public void parseProjects() throws Exception {
InputStream input = getClass().getResourceAsStream("projects.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckProject> projects = new ListParser<RundeckProject>(new ProjectParser(), "result/projects/project").parseXmlNode(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());
}
}

View file

@ -1,56 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rundeck.api.parser;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import org.dom4j.Document;
import org.junit.Assert;
import org.junit.Test;
import org.rundeck.api.domain.RundeckNode;
/**
* Test the {@link NodesParser}
*
* @author Vincent Behar
*/
public class NodesParserTest {
@Test
public void parseNodes() throws Exception {
InputStream input = getClass().getResourceAsStream("resources.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckNode> nodes = new NodesParser("project/node").parseXmlNode(document);
Assert.assertEquals(1, nodes.size());
RundeckNode node1 = nodes.get(0);
Assert.assertEquals("strongbad", node1.getName());
Assert.assertEquals("Node", node1.getType());
Assert.assertEquals("a development host", node1.getDescription());
Assert.assertEquals(Arrays.asList("dev"), node1.getTags());
Assert.assertEquals("strongbad.local", node1.getHostname());
Assert.assertEquals("i386", node1.getOsArch());
Assert.assertEquals("unix", node1.getOsFamily());
Assert.assertEquals("Linux", node1.getOsName());
Assert.assertEquals("2.6.35-30-generic-pae", node1.getOsVersion());
Assert.assertEquals("rundeck", node1.getUsername());
Assert.assertEquals(null, node1.getEditUrl());
Assert.assertEquals(null, node1.getRemoteUrl());
}
}

View file

@ -1,49 +0,0 @@
/*
* Copyright 2011 Vincent Behar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 parseProjects() throws Exception {
InputStream input = getClass().getResourceAsStream("projects.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckProject> projects = new ProjectsParser("result/projects/project").parseXmlNode(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());
}
}