modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/ResourceManagerBean.java | 42 - modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/measurement/test/MeasurementDataManagerTest.java | 231 ++++++++++ 2 files changed, 253 insertions(+), 20 deletions(-)
New commits: commit d35402236a2c8e63abd5c110cf575b7ca1c84044 Author: Heiko W. Rupp hwr@redhat.com Date: Tue Dec 14 12:41:38 2010 +0100
New test class around measurement handling. First case is bz 658491
diff --git a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/measurement/test/MeasurementDataManagerTest.java b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/measurement/test/MeasurementDataManagerTest.java new file mode 100644 index 0000000..53e21c9 --- /dev/null +++ b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/measurement/test/MeasurementDataManagerTest.java @@ -0,0 +1,231 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2010 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package org.rhq.enterprise.server.measurement.test; + +import java.util.Date; +import java.util.Random; + +import javax.persistence.EntityManager; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import org.rhq.core.domain.auth.Subject; +import org.rhq.core.domain.measurement.DataType; +import org.rhq.core.domain.measurement.DisplayType; +import org.rhq.core.domain.measurement.MeasurementCategory; +import org.rhq.core.domain.measurement.MeasurementDefinition; +import org.rhq.core.domain.measurement.MeasurementReport; +import org.rhq.core.domain.measurement.MeasurementSchedule; +import org.rhq.core.domain.measurement.MeasurementScheduleRequest; +import org.rhq.core.domain.measurement.MeasurementUnits; +import org.rhq.core.domain.measurement.calltime.CallTimeData; +import org.rhq.core.domain.measurement.calltime.CallTimeDataComposite; +import org.rhq.core.domain.resource.Agent; +import org.rhq.core.domain.resource.Resource; +import org.rhq.core.domain.resource.ResourceCategory; +import org.rhq.core.domain.resource.ResourceType; +import org.rhq.core.domain.util.PageControl; +import org.rhq.core.domain.util.PageList; +import org.rhq.enterprise.server.measurement.CallTimeDataManagerLocal; +import org.rhq.enterprise.server.measurement.MeasurementDataManagerLocal; +import org.rhq.enterprise.server.resource.ResourceManagerLocal; +import org.rhq.enterprise.server.test.AbstractEJB3Test; +import org.rhq.enterprise.server.util.LookupUtil; + +/** + * Test some measurement subsystem functionality + * @author Heiko W. Rupp + */ + +public class MeasurementDataManagerTest extends AbstractEJB3Test { + + private final Log log = LogFactory.getLog(MeasurementDataManagerTest.class); + + private ResourceManagerLocal resourceManager; + private MeasurementDataManagerLocal measurementDataManager; + private CallTimeDataManagerLocal callTimeDataManager; + + private Subject overlord; + + private Resource resource1,resource2; + private ResourceType theResourceType; + private Agent theAgent; + private MeasurementDefinition theDefinition; + + @BeforeMethod + public void beforeMethod() { + try { + prepareScheduler(); + + this.resourceManager = LookupUtil.getResourceManager(); + this.measurementDataManager = LookupUtil.getMeasurementDataManager(); + this.callTimeDataManager = LookupUtil.getCallTimeDataManager(); + this.overlord = LookupUtil.getSubjectManager().getOverlord(); + } catch (Throwable t) { + // Catch RuntimeExceptions and Errors and dump their stack trace, because Surefire will completely swallow them + // and throw a cryptic NPE (see http://jira.codehaus.org/browse/SUREFIRE-157)! + t.printStackTrace(); + throw new RuntimeException(t); + } + } + + @AfterMethod + public void afterMethod() throws Exception { + try { + EntityManager em = getEntityManager(); + getTransactionManager().begin(); + + MeasurementSchedule schedule1 = resource1.getSchedules().iterator().next(); + MeasurementSchedule schedule2 = resource2.getSchedules().iterator().next(); + schedule1 = em.find(MeasurementSchedule.class,schedule1.getId()); + schedule2 = em.find(MeasurementSchedule.class,schedule2.getId()); + em.remove(schedule1); + em.remove(schedule2); + resource1 = em.find(Resource.class,resource1.getId()); + resource2 = em.find(Resource.class,resource2.getId()); + em.remove(resource1); + em.remove(resource2); + theDefinition = em.find(MeasurementDefinition.class,theDefinition.getId()); + em.remove(theDefinition); + theResourceType = em.find(ResourceType.class,theResourceType.getId()); + em.remove(theResourceType); + theAgent = em.find(Agent.class,theAgent.getId()); + em.remove(theAgent); + + getTransactionManager().commit(); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + + finally { + unprepareScheduler(); + } + } + + + @Test + public void bz658491() throws Exception { + EntityManager em = beginTx(); + + try { + setupResources(em); + commitAndClose(em); + + MeasurementSchedule schedule1 = resource1.getSchedules().iterator().next(); + MeasurementSchedule schedule2 = resource2.getSchedules().iterator().next(); + MeasurementScheduleRequest request1 = new MeasurementScheduleRequest(schedule1); + MeasurementScheduleRequest request2 = new MeasurementScheduleRequest(schedule2); + + CallTimeData data1 = new CallTimeData(request1); + CallTimeData data2 = new CallTimeData(request2); + + data1.addCallData("/foo", new Date(),100); + data2.addCallData("/bar", new Date(),200); + + MeasurementReport report = new MeasurementReport(); + report.addData(data1); + report.addData(data2); + + measurementDataManager.mergeMeasurementReport(report); + + + PageList<CallTimeDataComposite> list1 = callTimeDataManager.findCallTimeDataForResource(overlord,schedule1.getId(), + 0,System.currentTimeMillis(),new PageControl()); + PageList<CallTimeDataComposite> list2 = callTimeDataManager.findCallTimeDataForResource(overlord,schedule2.getId(), + 0,System.currentTimeMillis(),new PageControl()); + + assert list1 != null; + assert list2 != null; + + assert list1.size() == 1 : "List 1 returned " + list1.size() + " entries, expected was 1"; + assert list2.size() == 1 : "List 2 returned " + list2.size() + " entries, expected was 1"; + + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + + finally { + unprepareScheduler(); + } + } + + + /** + * Just set up a resource where we can attach the availabilities to + * + * @param em The EntityManager to use + * + * @return A Resource ready to use + */ + private void setupResources(EntityManager em) { + theAgent = new Agent("testagent", "localhost", 1234, "", "randomToken"); + em.persist(theAgent); + + theResourceType = new ResourceType("test-plat", "test-plugin", ResourceCategory.PLATFORM, null); + em.persist(theResourceType); + + theDefinition = new MeasurementDefinition("CT-Def", MeasurementCategory.PERFORMANCE, + MeasurementUnits.MILLISECONDS, DataType.CALLTIME,true,60000, DisplayType.SUMMARY); + theDefinition.setResourceType(theResourceType); + em.persist(theDefinition); + + + resource1 = new Resource("test-platform-key1", "test-platform-name", theResourceType); + resource1.setUuid("" + new Random().nextInt()); + resource1.setAgent(theAgent); + em.persist(resource1); + resource2 = new Resource("test-platform-key2", "test-platform-name", theResourceType); + resource2.setUuid("" + new Random().nextInt()); + resource2.setAgent(theAgent); + em.persist(resource2); + + MeasurementSchedule schedule1 = new MeasurementSchedule(theDefinition,resource1); + em.persist(schedule1); + theDefinition.addSchedule(schedule1); + resource1.addSchedule(schedule1); + + MeasurementSchedule schedule2 = new MeasurementSchedule(theDefinition,resource2); + em.persist(schedule2); + theDefinition.addSchedule(schedule2); + resource2.addSchedule(schedule2); + + em.flush(); + } + + private EntityManager beginTx() throws Exception { + getTransactionManager().begin(); + EntityManager em = getEntityManager(); + return em; + } + + private void commitAndClose(EntityManager em) throws Exception { + getTransactionManager().commit(); + em.close(); + } + + +}
commit b1bb8f858e37c8b6f6d9a99f3d1f7afb28927528 Author: Heiko W. Rupp hwr@redhat.com Date: Tue Dec 14 12:40:55 2010 +0100
Catch a possible NPE
diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/ResourceManagerBean.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/ResourceManagerBean.java index e61fc0c..a5b5a38 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/ResourceManagerBean.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/ResourceManagerBean.java @@ -403,21 +403,23 @@ public class ResourceManagerBean implements ResourceManagerLocal, ResourceManage }
// one more thing, delete any autogroup backing groups - List<ResourceGroup> backingGroups = attachedResource.getAutoGroupBackingGroups(); - if (null != backingGroups && !backingGroups.isEmpty()) { - int size = backingGroups.size(); - int[] backingGroupIds = new int[size]; - for (int i = 0; (i < size); ++i) { - backingGroupIds[i] = backingGroups.get(i).getId(); - } - try { - resourceGroupManager.deleteResourceGroups(user, backingGroupIds); - } catch (Throwable t) { - if (log.isDebugEnabled()) { - log.error("Bulk delete error for autogroup backing group deletion for " + backingGroupIds, t); - } else { - log.error("Bulk delete error for autogroup backing group deletion for " + backingGroupIds + ": " - + t.getMessage()); + if (attachedResource!=null) { + List<ResourceGroup> backingGroups = attachedResource.getAutoGroupBackingGroups(); + if (null != backingGroups && !backingGroups.isEmpty()) { + int size = backingGroups.size(); + int[] backingGroupIds = new int[size]; + for (int i = 0; (i < size); ++i) { + backingGroupIds[i] = backingGroups.get(i).getId(); + } + try { + resourceGroupManager.deleteResourceGroups(user, backingGroupIds); + } catch (Throwable t) { + if (log.isDebugEnabled()) { + log.error("Bulk delete error for autogroup backing group deletion for " + backingGroupIds, t); + } else { + log.error("Bulk delete error for autogroup backing group deletion for " + backingGroupIds + ": " + + t.getMessage()); + } } } } @@ -742,7 +744,7 @@ public class ResourceManagerBean implements ResourceManagerLocal, ResourceManage resource.getId()))); }
- // fill out the tree, including only the direct ancestors and all viewable relations + // fill out the tree, including only the direct ancestors and all viewable relations List<ResourceLineageComposite> result = new LinkedList<ResourceLineageComposite>();
for (ResourceLineageComposite ancestor : resourceLineage) { @@ -750,14 +752,14 @@ public class ResourceManagerBean implements ResourceManagerLocal, ResourceManage result.add(ancestor);
// if the ancestor is not locked, include relevant children. Also, always show viewable - // siblings of the target resource. + // siblings of the target resource. if (!ancestor.isLocked() || ancestor.getResource() == parent) {
// get children Set<Resource> children = ancestor.getResource().getChildResources(); // remove any that are in the ancestry to avoid repeated handling children.removeAll(rawResourceLineage); - // only add the viewable children + // only add the viewable children List<Resource> viewableChildren = new ArrayList<Resource>(children.size()); for (Resource child : children) { boolean isCommitted = (child.getInventoryStatus() == InventoryStatus.COMMITTED); @@ -788,7 +790,7 @@ public class ResourceManagerBean implements ResourceManagerLocal, ResourceManage if (isCommitted && isViewable) { // if not a direct ancestor add to the list (direct ancestors already added by default) if (!rawResourceLineage.contains(grandChild)) { - // ensure we have the parent in the entity + // ensure we have the parent in the entity grandChild.getParentResource().getId(); ResourceLineageComposite composite = new ResourceLineageComposite(grandChild, false); result.add(composite); @@ -1827,7 +1829,7 @@ public class ResourceManagerBean implements ResourceManagerLocal, ResourceManage if (resourceError.getErrorType() == ResourceErrorType.INVALID_PLUGIN_CONFIGURATION || resourceError.getErrorType() == ResourceErrorType.AVAILABILITY_CHECK || resourceError.getErrorType() == ResourceErrorType.UPGRADE) { - // there should be at most one invalid plugin configuration error, availability check + // there should be at most one invalid plugin configuration error, availability check // or upgrade error per resource, so delete any currently existing ones before we add this new one List<ResourceError> doomedErrors = resource.getResourceErrors(resourceError.getErrorType());
rhq-commits@lists.fedorahosted.org