From 11adffbd5c010764d5fc44d968706e6dfb27facb Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 11:29:10 -0700 Subject: [PATCH] Test refactoring, pull common utilities into test base class. --- .../hqapi1/test/ApplicationCreate_test.java | 86 +++--------- .../hqapi1/test/ApplicationDelete_test.java | 17 +-- .../hq/hqapi1/test/ApplicationList_test.java | 42 ++++-- .../hq/hqapi1/test/ApplicationTestBase.java | 131 ++++++++++++++++++ .../hqapi1/test/ApplicationUpdate_test.java | 41 +++--- 5 files changed, 210 insertions(+), 107 deletions(-) create mode 100644 src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 7193d38b..787d17a7 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/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 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 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); } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java index 650f171c..b955fbb8 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java @@ -3,9 +3,8 @@ 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); @@ -13,19 +12,10 @@ public ApplicationDelete_test(String 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 { @@ -33,6 +23,5 @@ public void testDeleteNonExistingApp() throws Exception { StatusResponse response = api.deleteApplication(Integer.MAX_VALUE); hqAssertFailureObjectNotFound(response); - } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 35c5421e..06f7b04a 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -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 apps = new HashMap(); + 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()); + } } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java new file mode 100644 index 00000000..cd0e5372 --- /dev/null +++ b/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 services, + List 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(); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 99ffd26c..18592ac4 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -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); } }