Skip to content

Commit

Permalink
Add tests for add/remove application services.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Sep 24, 2009
1 parent 28d3f7a commit 5f642a4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 49 deletions.
88 changes: 39 additions & 49 deletions hqu/hqapi1/app/ApplicationController.groovy
Expand Up @@ -98,15 +98,12 @@ class ApplicationController extends ApiController {
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz

Application newApp = null;
def newApp;

try {
applicationValue.applicationType = appMan.findApplicationType(1)
newApp = appMan.createApplication( user, applicationValue, new ArrayList())
//def sessionId = SessionManager.instance.put(user)
//def newAppValue = aBoss.createApplication( sessionId, applicationValue, new ArrayList(), new ConfigResponse())
//newApp = appMan.findApplicationById(user, newAppValue.id)
// had to initialize appServices to avoid NPE
newApp = appMan.createApplication( user, applicationValue, new ArrayList())
// Initialize appServices to avoid NPE
newApp.appServices = new ArrayList()
} catch (Exception e) {
renderXml() {
Expand All @@ -118,14 +115,9 @@ class ApplicationController extends ApiController {
return
}

def resource = xmlApplication['Resource']
if (resource) {
updateAppServices(newApp, resource)
}
def group = xmlApplication['Group']
if (group) {
updateAppGroups(newApp, group)
}
def resources = xmlApplication['Resource']
def groups = xmlApplication['Group']
updateAppServices(newApp, resources, groups)

renderXml() {
ApplicationResponse() {
Expand Down Expand Up @@ -165,11 +157,11 @@ class ApplicationController extends ApiController {
def appOps = xmlApplication[0].'@opsContact'
def appBiz = xmlApplication[0].'@bizContact'

def updateApp = null
def updateApp
try {
updateApp = appMan.findApplicationById(user, appId)
} catch (Exception e) {
log.error("ERROR: " + e)
log.error("Error finding application" + e)
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND)
Expand All @@ -179,18 +171,15 @@ class ApplicationController extends ApiController {
}

def applicationValue = updateApp.getApplicationValue()

applicationValue.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz

def retAppValue = null;

try {
retAppValue = appMan.updateApplication(user, applicationValue)
appMan.updateApplication(user, applicationValue)
} catch (Exception e) {
renderXml() {
log.error("Error updating application", e)
Expand All @@ -201,19 +190,15 @@ class ApplicationController extends ApiController {
return
}

def resource = xmlApplication['Resource']
if (resource) {
updateAppServices(updateApp, resource)
}
def group = xmlApplication['Group']
if (group) {
updateAppGroups(updateApp, group)
}
def resources = xmlApplication['Resource']
def groups = xmlApplication['Group']
updateAppServices(updateApp, resources, groups)

renderXml() {
ApplicationResponse() {
out << getSuccessXML()
out << getApplicationXML(retAppValue)
// Must relookup the app to get updated services
out << getApplicationXML(applicationValue)
}
}
}
Expand Down Expand Up @@ -261,35 +246,40 @@ class ApplicationController extends ApiController {
}
}

private updateAppServices(app, resources) {
def svcList = new ArrayList()
resources.each { res ->
def rid = res.'@id'?.toInteger()
def sid = resMan.findResourceById(rid)?.instanceId
def svcAeid = AppdefEntityID.newServiceID(sid)
svcList.add(svcAeid)
private updateAppServices(app, resources, groups) {
def svcList = []

if (resources) {
resources.each { res ->
def rid = res.'@id'?.toInteger()
def sid = resMan.findResourceById(rid)?.instanceId
def svcAeid = AppdefEntityID.newServiceID(sid)
svcList.add(svcAeid)
}
}

appMan.setApplicationServices(user, app.id, svcList)

}

private updateAppGroups(app, groups) {
def grpList = new ArrayList()
groups.each { grp ->
def gid = grp.'@id'?.toInteger()
def groupAeid = AppdefEntityID.newGroupID(gid)
grpList.add(groupAeid)
if (groups) {
groups.each { grp ->
def gid = grp.'@id'?.toInteger()
def groupAeid = AppdefEntityID.newGroupID(gid)
svcList.add(groupAeid)
}
}

appMan.setApplicationServices(user, app.id, grpList)
log.info("Updating " + app.name + " to have " + svcList.size() + " app services!")

appMan.setApplicationServices(user, app.id, svcList)

// Setting the application services does not remove any app services
// that may have been removed from the application. We must look up
// the tree and remove one-by-one.
// TODO: Fix me
}

private getApplication(id) {
try {
return appMan.findApplicationById(user, id)
}
catch (Exception e) {
} catch (Exception e) {
return null
}
}
Expand Down
124 changes: 124 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java
@@ -1,9 +1,18 @@
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.types.ApplicationResponse;
import org.hyperic.hq.hqapi1.types.Application;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse;
import org.hyperic.hq.hqapi1.types.ResourcesResponse;
import org.hyperic.hq.hqapi1.types.Group;

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

public class ApplicationUpdate_test extends ApplicationTestBase {

Expand Down Expand Up @@ -40,4 +49,119 @@ public void testUpdateNoServices() throws Exception {
StatusResponse deleteResponse = api.deleteApplication(updatedApp.getId());
hqAssertSuccess(deleteResponse);
}

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

ResourcePrototypeResponse protoResponse =
rApi.getResourcePrototype("CPU");
hqAssertSuccess(protoResponse);

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

Application a = createTestApplication(null, null);

a.getResource().addAll(cpusResponse.getResource());

ApplicationResponse updateResponse = appApi.updateApplication(a);
hqAssertSuccess(updateResponse);

Application updatedApplication = updateResponse.getApplication();

assertEquals(cpusResponse.getResource().size(),
updatedApplication.getResource().size());

StatusResponse deleteResponse =
appApi.deleteApplication(updatedApplication.getId());
hqAssertSuccess(deleteResponse);
}

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

ResourcePrototypeResponse protoResponse =
rApi.getResourcePrototype("CPU");
hqAssertSuccess(protoResponse);

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

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

a.getResource().clear();

ApplicationResponse updateResponse = appApi.updateApplication(a);
hqAssertSuccess(updateResponse);

Application updatedApplication = updateResponse.getApplication();

assertEquals(0, updatedApplication.getResource().size());

StatusResponse deleteResponse =
appApi.deleteApplication(updatedApplication.getId());
hqAssertSuccess(deleteResponse);
}

public void testUpdateAddGroups() throws Exception {
HQApi api = getApi();
GroupApi gApi = api.getGroupApi();
ApplicationApi appApi = api.getApplicationApi();

Application a = createTestApplication(null, null);

Group g = createTestCompatibleGroup("CPU");
List<Group> groups = new ArrayList<Group>();
groups.add(g);

a.getGroup().addAll(groups);

ApplicationResponse updateResponse = appApi.updateApplication(a);
hqAssertSuccess(updateResponse);

Application updatedApplication = updateResponse.getApplication();

assertEquals(groups.size(), updatedApplication.getGroup().size());

StatusResponse deleteResponse = gApi.deleteGroup(g.getId());
hqAssertSuccess(deleteResponse);

deleteResponse = appApi.deleteApplication(updatedApplication.getId());
hqAssertSuccess(deleteResponse);
}

public void testUpdateRemoveGroups() throws Exception {
HQApi api = getApi();
GroupApi gApi = api.getGroupApi();
ApplicationApi appApi = api.getApplicationApi();

Group g = createTestCompatibleGroup("CPU");
List<Group> groups = new ArrayList<Group>();
groups.add(g);

Application a = createTestApplication(null, groups);

a.getGroup().clear();

ApplicationResponse updateResponse = appApi.updateApplication(a);
hqAssertSuccess(updateResponse);

Application updatedApplication = updateResponse.getApplication();

assertEquals(0, updatedApplication.getGroup().size());

StatusResponse deleteResponse = gApi.deleteGroup(g.getId());
hqAssertSuccess(deleteResponse);

deleteResponse = appApi.deleteApplication(updatedApplication.getId());
hqAssertSuccess(deleteResponse);
}
}

0 comments on commit 5f642a4

Please sign in to comment.