Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test refactoring, pull common utilities into test base class.
- Loading branch information
Ryan Morgan
committed
Sep 24, 2009
1 parent
a9cbe46
commit 11adffb
Showing
5 changed files
with
210 additions
and
107 deletions.
There are no files selected for viewing
86 changes: 22 additions & 64 deletions
86
src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters