From 47f140e5a29fb0d15b78b38910d72021bfbc67fa Mon Sep 17 00:00:00 2001 From: Oleg Kean Date: Sat, 30 Jul 2016 18:20:27 +0300 Subject: [PATCH] alm client tests --- README.md | 12 ++- src/java/org/alm/Client.java | 62 ++++++++++++++-- src/java/org/alm/Config.java | 9 ++- src/java/org/alm/model/Entities.java | 7 +- src/test/java/org/alm/AlmApiStub.java | 2 + src/test/java/org/alm/TestClient.java | 102 ++++++++++++++++++++++++++ src/test/java/org/alm/TestDao.java | 30 ++------ src/test/java/org/alm/Util.java | 24 +++++- src/test/resources/alm.properties | 1 - 9 files changed, 208 insertions(+), 41 deletions(-) create mode 100644 src/test/java/org/alm/TestClient.java diff --git a/README.md b/README.md index 8a5bc80..214962c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ ## ALM REST API Integration [![Build Status](https://travis-ci.org/okean/alm-rest-api.svg?branch=master)](https://travis-ci.org/okean/alm-rest-api) [![Coverage Status](https://coveralls.io/repos/github/okean/alm-rest-api/badge.svg?branch=master)](https://coveralls.io/github/okean/alm-rest-api?branch=master) -[![Gitter](https://badges.gitter.im/okean/alm-rest-api.svg)](https://gitter.im/okean/alm-rest-api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) A simple Java API client to connect to HP ALM using REST service. @@ -16,15 +15,14 @@ mvn clean install ### Examples Here is a simple example that login, returns a test entity and logout from ALM server. ```java -// Init rest connector instance to authenticate your session -RestConnector.instance().init("localhost", "8181", "b2b", "rx"); +Client client = new Client(new Config("alm.properties")); -Dao.login("admin", "admin"); +client.login(); -// Get the test with specified ID -Test test = Dao.readTest("1"); +// Get the test set with specified ID +TestSet testSet = client.loadTestSet("1"); -Dao.logout(); +client.logout(); ``` ### REST API Overview diff --git a/src/java/org/alm/Client.java b/src/java/org/alm/Client.java index d686202..1df736e 100644 --- a/src/java/org/alm/Client.java +++ b/src/java/org/alm/Client.java @@ -23,6 +23,11 @@ public Client(Config almConfig) config.host(), config.port(), config.domain(), config.project()); } + /** + * Login to HP ALM platform using basic authentication. + * + * @throws Exception + */ public void login() throws Exception { Log.info(String.format("Logging in as '%s' ...", config.username())); @@ -32,11 +37,25 @@ public void login() throws Exception Log.info(String.format("Successfully authenticated as '%s'", config.username())); } + /** + * Close session on server and clean session cookies on client. + * + * @throws Exception + */ public void logout() throws Exception { Dao.logout(); + + Log.info("Successfully logout"); } + /** + * Read test set entity. + * + * @param testSetId + * @return + * @throws Exception + */ public TestSet loadTestSet(String testSetId) throws Exception { Log.info(String.format("Loading TestSet ... (test-set-id = %s)", testSetId)); @@ -48,6 +67,13 @@ public TestSet loadTestSet(String testSetId) throws Exception return testSet; } + /** + * Read test instance entities + * + * @param testSet + * @return + * @throws Exception + */ public TestInstances loadTestInstances(TestSet testSet) throws Exception { Log.info(String.format("Loading TestInstances ... (test-set-id = %s)", testSet.id())); @@ -59,6 +85,13 @@ public TestInstances loadTestInstances(TestSet testSet) throws Exception return testInstances; } + /** + * Read test entity. + * + * @param testInstance + * @return + * @throws Exception + */ public Test loadTest(TestInstance testInstance) throws Exception { Log.info(String.format("Loading Test ... (test-id = %s)", testInstance.testId())); @@ -70,6 +103,14 @@ public Test loadTest(TestInstance testInstance) throws Exception return test; } + /** + * Create run entity. + * + * @param testInstance + * @param test + * @return + * @throws Exception + */ public Run createRun(TestInstance testInstance, Test test) throws Exception { final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @@ -95,20 +136,31 @@ public Run createRun(TestInstance testInstance, Test test) throws Exception return run; } + /** + * Create run step entities. + * + * @param run + * @param runSteps + * @throws Exception + */ public void createRunSteps(Run run, RunSteps runSteps) throws Exception { Log.info("Creating RunSteps ..."); - for(RunStep runStep : runSteps.entities()) + for (RunStep runStep : runSteps.entities()) { - runStep.runId(run.id()); + RunStep prepRunStep = new RunStep(); + + prepRunStep.runId(run.id()); + prepRunStep.name(runStep.name()); + prepRunStep.status(runStep.status()); - Log.info(String.format("Creating RunStep ... ('%s', %s)", runStep.name(), runStep.status())); + Log.info(String.format("Creating RunStep ... ('%s', %s)", prepRunStep.name(), prepRunStep.status())); - Dao.createRunStep(runStep); + Dao.createRunStep(prepRunStep); Log.info(String.format("RunStep has been created (run-step-id = %s, '%s', %s)", - runStep.id(), runStep.name(),runStep.status())); + prepRunStep.id(), prepRunStep.name(),prepRunStep.status())); } } diff --git a/src/java/org/alm/Config.java b/src/java/org/alm/Config.java index 1e161cf..c1912d5 100644 --- a/src/java/org/alm/Config.java +++ b/src/java/org/alm/Config.java @@ -19,9 +19,9 @@ public class Config { private Properties properties; - public Config(String content) throws Exception + public Config(Properties properties) throws Exception { - properties = createPropertyFile(content); + this.properties = properties; if (!check()) { @@ -29,6 +29,11 @@ public Config(String content) throws Exception } } + public Config(String content) throws Exception + { + this(createPropertyFile(content)); + } + public String host() { return properties.getProperty("host", ""); diff --git a/src/java/org/alm/model/Entities.java b/src/java/org/alm/model/Entities.java index 8f202a8..5c5c96b 100644 --- a/src/java/org/alm/model/Entities.java +++ b/src/java/org/alm/model/Entities.java @@ -6,14 +6,17 @@ import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementRef; +import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Entities") @XmlAccessorType(XmlAccessType.FIELD) public class Entities { - @XmlElement(name = "Entity") + @XmlElementRefs({ + @XmlElementRef(name="Entity", type=TestInstance.class), + @XmlElementRef(name="Entity", type=RunStep.class)}) private List entities; public Entities() diff --git a/src/test/java/org/alm/AlmApiStub.java b/src/test/java/org/alm/AlmApiStub.java index 7405847..744fecc 100644 --- a/src/test/java/org/alm/AlmApiStub.java +++ b/src/test/java/org/alm/AlmApiStub.java @@ -166,6 +166,8 @@ public Attachment createAttachments( @Produces("application/xml") public Run createRun(Run run) { + run.id("1"); + return run; } diff --git a/src/test/java/org/alm/TestClient.java b/src/test/java/org/alm/TestClient.java new file mode 100644 index 0000000..25d2116 --- /dev/null +++ b/src/test/java/org/alm/TestClient.java @@ -0,0 +1,102 @@ +package org.alm; + +import static org.alm.Util.readAlmProperties; + +import java.net.URI; + +import junit.framework.Assert; + +import org.alm.model.*; +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.server.ResourceConfig; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class TestClient +{ + private HttpServer server; + + private Config config; + private Client client; + private TestSet testSet; + private TestInstances testInstances; + private org.alm.model.Test test; + private Run run; + + public TestClient() throws Exception + { + config = new Config(readAlmProperties()); + client = new Client(config); + } + + @BeforeClass + public void setUp() throws Exception + { + + final ResourceConfig rc = new ResourceConfig(AlmApiStub.class); + + server = GrizzlyHttpServerFactory.createHttpServer( + URI.create(String.format("http://%s:%s/", config.host(), config.port())), rc); + } + + @AfterClass + public void tearDown() throws Exception + { + if (server != null) + { + server.shutdownNow(); + } + } + + @Test + public void login() throws Exception + { + client.login(); + } + + @Test(dependsOnMethods = { "login" }) + public void loadTestSet() throws Exception + { + testSet = client.loadTestSet("1"); + + Assert.assertNotNull(testSet); + } + + @Test(dependsOnMethods = { "loadTestSet" }) + public void loadTestInstances() throws Exception + { + testInstances = client.loadTestInstances(testSet); + + Assert.assertNotNull(testInstances); + } + + @Test(dependsOnMethods = { "loadTestInstances" }) + public void loadTest() throws Exception + { + test = client.loadTest(testInstances.entities().get(0)); + + Assert.assertNotNull(test); + } + + @Test(dependsOnMethods = { "loadTest" }) + public void createRun() throws Exception + { + run = client.createRun(testInstances.entities().get(0), test); + + Assert.assertNotNull(run); + } + + @Test(dependsOnMethods = { "createRun" }) + public void createRunSteps() throws Exception + { + client.createRunSteps(run, Util.createRunStepEntities()); + } + + @Test(dependsOnMethods = { "createRunSteps" }) + public void logout() throws Exception + { + client.logout(); + } +} diff --git a/src/test/java/org/alm/TestDao.java b/src/test/java/org/alm/TestDao.java index a3dbb0f..f68ba8f 100644 --- a/src/test/java/org/alm/TestDao.java +++ b/src/test/java/org/alm/TestDao.java @@ -1,7 +1,6 @@ package org.alm; import java.io.IOException; -import java.io.InputStream; import java.net.URI; import java.util.Properties; @@ -20,8 +19,6 @@ public class TestDao { - private final boolean useStub; - private final String host; private final String port; private final String domain; @@ -38,27 +35,22 @@ public TestDao() throws IOException domain = almProperty.getProperty("domain"); project = almProperty.getProperty("project"); - useStub = Boolean.parseBoolean(almProperty.getProperty("use-stub")); - RestConnector.instance().init(host, port, domain, project); } @BeforeClass public void setUp() throws Exception { - if (useStub) - { - final ResourceConfig rc = new ResourceConfig(AlmApiStub.class); + final ResourceConfig rc = new ResourceConfig(AlmApiStub.class); - server = GrizzlyHttpServerFactory.createHttpServer( - URI.create(String.format("http://%s:%s/", host, port)), rc); - } + server = GrizzlyHttpServerFactory.createHttpServer( + URI.create(String.format("http://%s:%s/", host, port)), rc); } @AfterClass public void tearDown() throws Exception { - if (useStub && server != null) + if (server != null) { server.shutdownNow(); } @@ -166,6 +158,8 @@ public void createRun() throws Exception Run actual = Dao.createRun(expected); + expected.id(actual.id()); + EntityAssert.assertEquals(actual, expected); } @@ -173,11 +167,10 @@ public void createRun() throws Exception public void updateRunEntity() throws Exception { Run expected = createRunEntity(); - String id = expected.id(); Run actual = Dao.updateRun(expected); - expected.id(id); + expected.id(actual.id()); EntityAssert.assertEquals(actual, expected); } @@ -223,13 +216,4 @@ public void updateRunStep() throws Exception EntityAssert.assertEquals(actual, expected); } - - private static Properties readAlmProperties() throws IOException - { - Properties almProperties = new Properties(); - InputStream in = TestDao.class.getResourceAsStream("/alm.properties"); - almProperties.load(in); - - return almProperties; - } } diff --git a/src/test/java/org/alm/Util.java b/src/test/java/org/alm/Util.java index de69961..52ded5f 100644 --- a/src/test/java/org/alm/Util.java +++ b/src/test/java/org/alm/Util.java @@ -1,7 +1,10 @@ package org.alm; +import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import java.util.List; +import java.util.Properties; import org.alm.model.*; import org.apache.commons.lang.StringEscapeUtils; @@ -73,7 +76,6 @@ public static Run createRunEntity() { Run run = new Run(); - run.id("2"); run.testInstanceId("42"); run.testSetId("7"); run.testId("133"); @@ -94,6 +96,7 @@ public static RunStep createRunStepEntity(String runId, String id) runStep.id(id); runStep.runId(runId); + runStep.name("Login#HomePage"); runStep.description("Navigat to HomePage"); runStep.status("Failed"); runStep.testId("136"); @@ -104,6 +107,16 @@ public static RunStep createRunStepEntity(String runId, String id) return runStep; } + public static RunSteps createRunStepEntities() + { + RunSteps runSteps = new RunSteps(); + + runSteps.addEntity(createRunStepEntity("", "1")); + runSteps.addEntity(createRunStepEntity("", "2")); + + return runSteps; + } + public static String wrapToHtml(String line) { return wrapToHtml(Arrays.asList(line)); @@ -125,4 +138,13 @@ public static String wrapToHtml(List lines) return sb.toString(); } + + public static Properties readAlmProperties() throws IOException + { + Properties almProperties = new Properties(); + InputStream in = TestDao.class.getResourceAsStream("/alm.properties"); + almProperties.load(in); + + return almProperties; + } } diff --git a/src/test/resources/alm.properties b/src/test/resources/alm.properties index 33fe937..e031c8c 100644 --- a/src/test/resources/alm.properties +++ b/src/test/resources/alm.properties @@ -6,4 +6,3 @@ domain=b2b project=rx username=admin password=admin -use-stub=true