Navigation Menu

Skip to content

Commit

Permalink
Test refactoring, pull common utilities into test base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Sep 24, 2009
1 parent a9cbe46 commit 11adffb
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 107 deletions.
86 changes: 22 additions & 64 deletions src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java
@@ -1,87 +1,45 @@
package org.hyperic.hq.hqapi1.test;

import org.hyperic.hq.hqapi1.ApplicationApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.ResourceApi;
import org.hyperic.hq.hqapi1.GroupApi;
import org.hyperic.hq.hqapi1.AgentApi;
import org.hyperic.hq.hqapi1.types.*;

import java.util.List;
import java.util.ArrayList;

public class ApplicationCreate_test extends HQApiTestBase {
public class ApplicationCreate_test extends ApplicationTestBase {

public ApplicationCreate_test(String name) {
super(name);
}

// TODO: Stub
public void testApplicationCreate() throws Exception {

public void testApplicationCreateNoServices() throws Exception {
ApplicationApi api = getApi().getApplicationApi();
Application a = createTestApplication(null, null);

Application a = new Application();
a.setName("A new app");
a.setLocation("Tahiti");
a.setDescription("A test app created using the API");
a.setEngContact("the Engineer");
a.setBizContact("the Businessman");
a.setOpsContact("the Ops Man");

Agent agent = getRunningAgent();

ResourceApi resourceApi = getApi().getResourceApi();
AgentApi agentApi = getApi().getAgentApi();
GroupApi groupApi = getApi().getGroupApi();

GroupsResponse grpResponse = groupApi.getMixedGroups();
List<Group> groups = a.getGroup();
for (Group g : grpResponse.getGroup()) {
System.out.println("GROUP: " + g.getDescription() + " :: " + g.getResource());
// TODO: need a better way of finding App groups - maybe create one?
if (g.getDescription().contains("Application")) {
System.out.println("+> " + g.getId() + " " + g.getName());
groups.add(g);
}
}

ResourcePrototypeResponse rp = resourceApi.getResourcePrototype("");
ResourcesResponse findResponse = resourceApi.getResources(agent, true, true);
hqAssertSuccess(findResponse);

assertTrue("Found 0 platform resources for agent " + agent.getId(),
findResponse.getResource().size() > 0);
StatusResponse response = api.deleteApplication(a.getId());
hqAssertSuccess(response);
}

Resource toAdd1 = null;
Resource toAdd2 = null;
for (Resource r : findResponse.getResource().get(0).getResource()) {
boolean done = false;
System.out.println("RESOURCE: " + r.getDescription() + " :: " + r.getResourcePrototype().getName());
for (Resource r2 : r.getResource()) {
System.out.println("+> " + r2.getId() + " " + r2.getName());
if (toAdd1 == null) {
toAdd1 = r2;
}
else {
toAdd2 = r2;
break;
}
}
if (toAdd2 != null) {
break;
}
}
assertNotNull("Found 0 services to add", toAdd1);
public void testApplicationCreateWithServices() throws Exception {
HQApi api = getApi();
ResourceApi rApi = api.getResourceApi();
ApplicationApi appApi = api.getApplicationApi();

List<Resource> resources = a.getResource();
resources.add(toAdd1);
if (toAdd2 != null) {
resources.add(toAdd2);
}
ResourcePrototypeResponse protoResponse =
rApi.getResourcePrototype("CPU");
hqAssertSuccess(protoResponse);

ApplicationResponse response = api.createApplication(a);
ResourcesResponse cpusResponse =
rApi.getResources(protoResponse.getResourcePrototype(),
false, false);
hqAssertSuccess(cpusResponse);

hqAssertSuccess(response);
Application a = createTestApplication(cpusResponse.getResource(), null);

api.deleteApplication(response.getApplication().getId());
StatusResponse deleteResponse = appApi.deleteApplication(a.getId());
hqAssertSuccess(deleteResponse);
}
}
17 changes: 3 additions & 14 deletions src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java
Expand Up @@ -3,36 +3,25 @@
import org.hyperic.hq.hqapi1.ApplicationApi;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.Application;
import org.hyperic.hq.hqapi1.types.ApplicationResponse;

public class ApplicationDelete_test extends HQApiTestBase {
public class ApplicationDelete_test extends ApplicationTestBase {

public ApplicationDelete_test(String name) {
super(name);
}

public void testDeleteExistingApp() throws Exception {
ApplicationApi api = getApi().getApplicationApi();
Application a = createTestApplication(null, null);

Application a = new Application();
a.setName("App to delete");
a.setLocation("Hawaii");
a.setDescription("A test app created using the API");
a.setEngContact("the Engineer");
a.setBizContact("the Businessman");
a.setOpsContact("the Ops Man");
ApplicationResponse appResponse = api.createApplication(a);

StatusResponse response = api.deleteApplication(appResponse.getApplication().getId());
StatusResponse response = api.deleteApplication(a.getId());
hqAssertSuccess(response);

}

public void testDeleteNonExistingApp() throws Exception {
ApplicationApi api = getApi().getApplicationApi();

StatusResponse response = api.deleteApplication(Integer.MAX_VALUE);
hqAssertFailureObjectNotFound(response);

}
}
42 changes: 30 additions & 12 deletions src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java
Expand Up @@ -5,28 +5,46 @@
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.Application;

public class ApplicationList_test extends HQApiTestBase {
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class ApplicationList_test extends ApplicationTestBase {

public ApplicationList_test(String name) {
super(name);
}

// TODO: Stub
public void testList() throws Exception {
ApplicationApi api = getApi().getApplicationApi();

ApplicationsResponse response = api.listApplications();
// TODO: remove debugging lines
System.out.println("TEST " + response);
System.out.println("TEST - Apps: " + response.getApplication().size());
for (Application app : response.getApplication()) {
Integer id = app.getId();
System.out.println("APP " + id + " name: " + app.getName());
System.out.println(" location: " + app.getLocation());
System.out.println(" desription: " + app.getDescription());
System.out.println(" Resources: " + app.getResource().size());
System.out.println(" Groups: " + app.getGroup().size());
hqAssertSuccess(response);
}

public void testListWithApplications() throws Exception {
ApplicationApi api = getApi().getApplicationApi();

int num = 5;
Map<Integer,Application> apps = new HashMap<Integer,Application>();
for (int i = 0; i < num; i++) {
Application a = createTestApplication(null, null);
apps.put(a.getId(), a);
}

ApplicationsResponse response = api.listApplications();
hqAssertSuccess(response);

for (Application a : response.getApplication()) {
apps.remove(a.getId());
}

assertTrue("Not all created applications were found during listing!",
apps.isEmpty());

for (Application a : response.getApplication()) {
api.deleteApplication(a.getId());
}
}
}
131 changes: 131 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java
@@ -0,0 +1,131 @@
package org.hyperic.hq.hqapi1.test;

import org.hyperic.hq.hqapi1.types.Application;
import org.hyperic.hq.hqapi1.types.ApplicationResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse;
import org.hyperic.hq.hqapi1.types.ResourcesResponse;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.ApplicationApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.GroupApi;
import org.hyperic.hq.hqapi1.ResourceApi;

import java.util.Random;
import java.util.List;

public abstract class ApplicationTestBase extends HQApiTestBase {

protected static final String APP_NAME = "Test Application";
protected static final String APP_LOCATION = "SFO";
protected static final String APP_DESC = "Test Application Description";
protected static final String APP_ENG_CONTACT = "415-555-5555";
protected static final String APP_BIZ_CONTACT = "212-555-5555";
protected static final String APP_OPS_CONTACT = "510-555-5555";

static final String GROUP_NAME = "API Test Group";
static final String GROUP_LOCATION = "API Test Group Location";
static final String GROUP_DESCRIPTION = "API Test Group Description";

public ApplicationTestBase(String name) {
super(name);
}

/**
* Create an Application with no groups or services associated.
*
* @param services A list of Resources to add to the Application or null
* if no Resources should be added.
* @param groups A list of Groups to add to the Application or null if no
* Groups should be added.
* @return The created Application
* @throws Exception If an error occurs.
*/
protected Application createTestApplication(List<Resource> services,
List<Group> groups) throws Exception {
ApplicationApi api = getApi().getApplicationApi();

Random r = new Random();
Application a = new Application();

String name = APP_NAME + r.nextInt();
a.setName(name);
a.setLocation(APP_LOCATION);
a.setDescription(APP_DESC);
a.setEngContact(APP_ENG_CONTACT);
a.setBizContact(APP_BIZ_CONTACT);
a.setOpsContact(APP_OPS_CONTACT);

if (services != null) {
a.getResource().addAll(services);
}

if (groups != null) {
a.getGroup().addAll(groups);
}

ApplicationResponse response = api.createApplication(a);
hqAssertSuccess(response);

Application createdApp = response.getApplication();

assertNotNull("Application id was null!", createdApp.getId());
assertEquals(createdApp.getName(), name);
assertEquals(createdApp.getLocation(), APP_LOCATION);
assertEquals(createdApp.getDescription(), APP_DESC);
assertEquals(createdApp.getEngContact(), APP_ENG_CONTACT);
assertEquals(createdApp.getBizContact(), APP_BIZ_CONTACT);
assertEquals(createdApp.getOpsContact(), APP_OPS_CONTACT);

if (services != null) {
assertEquals(createdApp.getResource().size(), services.size());
}

if (groups != null) {
assertEquals(createdApp.getGroup().size(), groups.size());
}

return response.getApplication();
}

/**
* Create a compabile group of the given prototype that includes all
* resources of that type in the inventory.
*
* @param prototype The type of compatible group to create.
* @return The created Group
* @throws Exception If an error occurs creating the group
*/
protected Group createTestCompatibleGroup(String prototype) throws Exception {
HQApi api = getApi();
GroupApi groupApi = api.getGroupApi();
ResourceApi rApi = api.getResourceApi();

ResourcePrototypeResponse protoResponse =
rApi.getResourcePrototype(prototype);
hqAssertSuccess(protoResponse);

ResourcesResponse resourcesResponse =
rApi.getResources(protoResponse.getResourcePrototype(),
false, false);
hqAssertSuccess(resourcesResponse);

assertTrue("No resources of type " + prototype + " found!",
resourcesResponse.getResource().size() > 0);

Group g = new Group();
Random r = new Random();

g.setName(GROUP_NAME + r.nextInt());
g.setDescription(GROUP_DESCRIPTION);
g.setLocation(GROUP_LOCATION);
g.setResourcePrototype(protoResponse.getResourcePrototype());
g.getResource().addAll(resourcesResponse.getResource());

GroupResponse groupResponse = groupApi.createGroup(g);
hqAssertSuccess(groupResponse);

return groupResponse.getGroup();
}
}
41 changes: 24 additions & 17 deletions src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java
Expand Up @@ -3,34 +3,41 @@
import org.hyperic.hq.hqapi1.ApplicationApi;
import org.hyperic.hq.hqapi1.types.ApplicationResponse;
import org.hyperic.hq.hqapi1.types.Application;
import org.hyperic.hq.hqapi1.types.StatusResponse;

public class ApplicationUpdate_test extends HQApiTestBase {
public class ApplicationUpdate_test extends ApplicationTestBase {

private static final String UPDATE_PREFIX = "UPDATED-";

public ApplicationUpdate_test(String name) {
super(name);
}

// TODO: Stub
public void testUpdate() throws Exception {
public void testUpdateNoServices() throws Exception {
ApplicationApi api = getApi().getApplicationApi();

Application a = new Application();
a.setName("A new app");
a.setLocation("Tahiti");
a.setDescription("A test app created using the API");
a.setEngContact("the Engineer");
a.setBizContact("the Businessman");
a.setOpsContact("the Ops Man");
Application a = createTestApplication(null, null);

a.setName(UPDATE_PREFIX + a.getName());
a.setDescription(UPDATE_PREFIX + a.getDescription());
a.setLocation(UPDATE_PREFIX + a.getLocation());
a.setOpsContact(UPDATE_PREFIX + a.getOpsContact());
a.setBizContact(UPDATE_PREFIX + a.getBizContact());
a.setEngContact(UPDATE_PREFIX + a.getEngContact());

ApplicationResponse newResponse = api.createApplication(a);
ApplicationResponse updateResponse = api.updateApplication(a);
hqAssertSuccess(updateResponse);

Application a2 = newResponse.getApplication();
a2.setBizContact("new biz contact");
Application updatedApp = updateResponse.getApplication();

ApplicationResponse response = api.updateApplication(a2);
hqAssertSuccess(response);
assertEquals("new biz contact", newResponse.getApplication().getBizContact());
assertEquals(a.getName(), updatedApp.getName());
assertEquals(a.getDescription(), updatedApp.getDescription());
assertEquals(a.getLocation(), updatedApp.getLocation());
assertEquals(a.getOpsContact(), updatedApp.getOpsContact());
assertEquals(a.getBizContact(), updatedApp.getBizContact());
assertEquals(a.getEngContact(), updatedApp.getEngContact());

api.deleteApplication(newResponse.getApplication().getId());
StatusResponse deleteResponse = api.deleteApplication(updatedApp.getId());
hqAssertSuccess(deleteResponse);
}
}

0 comments on commit 11adffb

Please sign in to comment.