Skip to content

Commit

Permalink
Validate that resources added to an Application are Services.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Sep 24, 2009
1 parent ebae45d commit 6348179
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
16 changes: 16 additions & 0 deletions hqu/hqapi1/app/ApplicationController.groovy
Expand Up @@ -68,6 +68,22 @@ class ApplicationController extends ApiController {
return
}

// Validate Resources
for (xmlResource in xmlApplication['Resource']) {
def rid = xmlResource.'@id'?.toInteger()
def resource = resourceHelper.findById(rid)
if (!resource.isService()) {
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Invalid resource passed to create, " +
r.name + " is not a service")
}
}
return
}
}

def appName = xmlApplication[0].'@name'
def appLoc = xmlApplication[0].'@location'
def appDesc = xmlApplication[0].'@description'
Expand Down
37 changes: 37 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java
Expand Up @@ -38,4 +38,41 @@ public void testApplicationCreateWithServices() throws Exception {
StatusResponse deleteResponse = appApi.deleteApplication(a.getId());
hqAssertSuccess(deleteResponse);
}

public void testApplicationCreateWithServers() throws Exception {
HQApi api = getApi();
ResourceApi rApi = api.getResourceApi();
ApplicationApi appApi = api.getApplicationApi();

ResourcePrototypeResponse protoResponse =
rApi.getResourcePrototype("HQ Agent");
hqAssertSuccess(protoResponse);

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

assertTrue("No HQ Agent resources found in the inventory!",
agentsResponse.getResource().size() > 0);

Application a = generateTestApplication();
a.getResource().addAll(agentsResponse.getResource());

ApplicationResponse response = appApi.createApplication(a);
hqAssertFailureInvalidParameters(response); // Invalid - cannot have servers
}

public void testApplicationCreateWithPlatforms() throws Exception {
HQApi api = getApi();
ApplicationApi appApi = api.getApplicationApi();

Resource platform = getLocalPlatformResource(false, false);

Application a = generateTestApplication();
a.getResource().add(platform);

ApplicationResponse response = appApi.createApplication(a);
hqAssertFailureInvalidParameters(response); // Invalid - cannot have servers
}
}
29 changes: 19 additions & 10 deletions src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java
Expand Up @@ -32,6 +32,23 @@ public ApplicationTestBase(String name) {
super(name);
}

protected Application generateTestApplication()
throws Exception {

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);

return a;
}

/**
* Create an Application with no groups or services associated.
*
Expand All @@ -47,15 +64,7 @@ protected Application createTestApplication(List<Resource> services)
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);
Application a = generateTestApplication();

if (services != null) {
a.getResource().addAll(services);
Expand All @@ -67,7 +76,7 @@ protected Application createTestApplication(List<Resource> services)
Application createdApp = response.getApplication();

assertNotNull("Application id was null!", createdApp.getId());
assertEquals(createdApp.getName(), name);
assertEquals(createdApp.getName(), a.getName());
assertEquals(createdApp.getLocation(), APP_LOCATION);
assertEquals(createdApp.getDescription(), APP_DESC);
assertEquals(createdApp.getEngContact(), APP_ENG_CONTACT);
Expand Down

0 comments on commit 6348179

Please sign in to comment.