Skip to content

Commit

Permalink
alm client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okean committed Jul 30, 2016
1 parent 6cf967e commit 47f140e
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 41 deletions.
12 changes: 5 additions & 7 deletions 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.

Expand All @@ -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
Expand Down
62 changes: 57 additions & 5 deletions src/java/org/alm/Client.java
Expand Up @@ -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()));
Expand All @@ -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));
Expand All @@ -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()));
Expand All @@ -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()));
Expand All @@ -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");
Expand All @@ -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()));
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/java/org/alm/Config.java
Expand Up @@ -19,16 +19,21 @@ 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())
{
throw new Exception("Invalid ALM config.");
}
}

public Config(String content) throws Exception
{
this(createPropertyFile(content));
}

public String host()
{
return properties.getProperty("host", "");
Expand Down
7 changes: 5 additions & 2 deletions src/java/org/alm/model/Entities.java
Expand Up @@ -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<T extends Entity>
{
@XmlElement(name = "Entity")
@XmlElementRefs({
@XmlElementRef(name="Entity", type=TestInstance.class),
@XmlElementRef(name="Entity", type=RunStep.class)})
private List<T> entities;

public Entities()
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/alm/AlmApiStub.java
Expand Up @@ -166,6 +166,8 @@ public Attachment createAttachments(
@Produces("application/xml")
public Run createRun(Run run)
{
run.id("1");

return run;
}

Expand Down
102 changes: 102 additions & 0 deletions 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();
}
}
30 changes: 7 additions & 23 deletions 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;

Expand All @@ -20,8 +19,6 @@

public class TestDao
{
private final boolean useStub;

private final String host;
private final String port;
private final String domain;
Expand All @@ -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();
}
Expand Down Expand Up @@ -166,18 +158,19 @@ public void createRun() throws Exception

Run actual = Dao.createRun(expected);

expected.id(actual.id());

EntityAssert.assertEquals(actual, expected);
}

@Test(groups = { "crud", "enity" })
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);
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 47f140e

Please sign in to comment.