modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBean.java | 31 ++ modules/enterprise/server/jar/src/test/groovy/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBeanTest.groovy | 118 +++++++--- 2 files changed, 116 insertions(+), 33 deletions(-)
New commits: commit aa3d4cd81b3854ca37f7235103807fedd6bde5d7 Author: John Sanda jsanda@redhat.com Date: Fri Oct 29 14:59:29 2010 -0400
Add logic to delete bundles and bundle types
ResourceMetadataManagerBean lacks support for deleting content-related stuff. With this commit, it now deletes bundles and bundle types that are associated with the resource type being deleted. I have also added tests to verify that bundles and bundle types are deleted.
I had to add some additional set up code to start the master (server) plugin container. When I first wrote my test and a call to BundleManagerBean, an exception was thrown complaining about the master plugin contain not running.
diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBean.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBean.java index ff81a6e..d02195b 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBean.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBean.java @@ -56,6 +56,7 @@ import org.rhq.core.clientapi.descriptor.AgentPluginDescriptorUtil; import org.rhq.core.clientapi.descriptor.plugin.PluginDescriptor; import org.rhq.core.domain.auth.Subject; import org.rhq.core.domain.authz.Permission; +import org.rhq.core.domain.bundle.Bundle; import org.rhq.core.domain.bundle.BundleType; import org.rhq.core.domain.configuration.Configuration; import org.rhq.core.domain.configuration.Property; @@ -63,6 +64,7 @@ import org.rhq.core.domain.configuration.definition.ConfigurationDefinition; import org.rhq.core.domain.configuration.definition.ConfigurationTemplate; import org.rhq.core.domain.configuration.definition.PropertyDefinition; import org.rhq.core.domain.content.PackageType; +import org.rhq.core.domain.criteria.BundleCriteria; import org.rhq.core.domain.criteria.ResourceCriteria; import org.rhq.core.domain.event.EventDefinition; import org.rhq.core.domain.measurement.MeasurementDefinition; @@ -79,6 +81,7 @@ import org.rhq.core.util.jdbc.JDBCUtil; import org.rhq.enterprise.server.RHQConstants; import org.rhq.enterprise.server.auth.SubjectManagerLocal; import org.rhq.enterprise.server.authz.RequiredPermission; +import org.rhq.enterprise.server.bundle.BundleManagerLocal; import org.rhq.enterprise.server.configuration.metadata.ConfigurationDefinitionUpdateReport; import org.rhq.enterprise.server.configuration.metadata.ConfigurationMetadataManagerLocal; import org.rhq.enterprise.server.event.EventManagerLocal; @@ -138,6 +141,9 @@ public class ResourceMetadataManagerBean implements ResourceMetadataManagerLocal @EJB private ResourceMetadataManagerLocal resourceMetadataManager; // self
+ @EJB + private BundleManagerLocal bundleManager; + @SuppressWarnings("unchecked") public List<Plugin> getAllPluginsById(List<Integer> pluginIds) { if (pluginIds == null || pluginIds.size() == 0) { @@ -591,6 +597,15 @@ public class ResourceMetadataManagerBean implements ResourceMetadataManagerLocal removeFromChildren(existingType); entityManager.merge(existingType);
+ try { + deleteBundles(subject, existingType); + } catch (Exception e) { + throw new RuntimeException("Bundle deletion failed. Cannot finish deleting " + existingType, e); + } + + entityManager.flush(); + existingType = entityManager.find(existingType.getClass(), existingType.getId()); + // Remove all compatible groups that are of the type. List<ResourceGroup> compatGroups = existingType.getResourceGroups(); if (compatGroups != null) { @@ -634,6 +649,22 @@ public class ResourceMetadataManagerBean implements ResourceMetadataManagerLocal entityManager.flush(); }
+ private void deleteBundles(Subject subject, ResourceType resourceType) throws Exception { + BundleType bundleType = resourceType.getBundleType(); + + if (bundleType == null) { + return; + } + + BundleCriteria criteria = new BundleCriteria(); + criteria.addFilterBundleTypeId(bundleType.getId()); + + List<Bundle> bundles = bundleManager.findBundlesByCriteria(subject, criteria); + for (Bundle bundle : bundles) { + bundleManager.deleteBundle(subject, bundle.getId()); + } + } + private void removeFromParents(ResourceType typeToBeRemoved) { // Wrap in new HashSet to avoid ConcurrentModificationExceptions. Set<ResourceType> parentTypes = new HashSet<ResourceType>(typeToBeRemoved.getParentResourceTypes()); diff --git a/modules/enterprise/server/jar/src/test/groovy/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBeanTest.groovy b/modules/enterprise/server/jar/src/test/groovy/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBeanTest.groovy index 584683c..a92c639 100644 --- a/modules/enterprise/server/jar/src/test/groovy/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBeanTest.groovy +++ b/modules/enterprise/server/jar/src/test/groovy/org/rhq/enterprise/server/resource/metadata/ResourceMetadataManagerBeanTest.groovy @@ -19,11 +19,20 @@ import org.hibernate.Session import org.rhq.core.domain.shared.ResourceBuilder import org.rhq.core.domain.criteria.ResourceCriteria import org.rhq.core.domain.resource.InventoryStatus +import org.testng.annotations.BeforeClass +import org.rhq.enterprise.server.bundle.TestBundleServerPluginService
class ResourceMetadataManagerBeanTest extends AbstractEJB3Test {
def plugins = []
+ @BeforeClass + void startMBeanServer() { + def bundleService = new TestBundleServerPluginService(); + prepareCustomServerPluginService(bundleService) + bundleService.startMasterPluginContainerWithoutSchedulingJobs() + } + @AfterClass void removePluginsFromDB() { transaction { @@ -286,6 +295,8 @@ class ResourceMetadataManagerBeanTest extends AbstractEJB3Test { <subcategory name="ServerC.Category2"/> </subcategories>
+ <bundle type="Test Bundle"/> + <process-scan name="scan1" query="process|basename|match=^java.*,arg|org.rhq.serverC1|match=.*"/> <process-scan name="scan2" query="process|basename|match=^java.*,arg|org.rhq.serverC2|match=.*"/>
@@ -312,6 +323,7 @@ class ResourceMetadataManagerBeanTest extends AbstractEJB3Test { createPlugin 'remove-types-plugin', '1.0', originalDescriptor
createResources(3, 'RemoveTypesPlugin', 'ServerC') + createBundle("test-bundle-1", "Test Bundle", "ServerC", "RemoveTypesPlugin")
def updatedDescriptor = """ <plugin name="RemoveTypesPlugin" displayName="Remove Types Plugin" package="org.rhq.plugins.test" @@ -327,31 +339,6 @@ class ResourceMetadataManagerBeanTest extends AbstractEJB3Test { createPlugin 'remove-types-plugin', '2.0', updatedDescriptor }
- def createResources(Integer count, String pluginName, String resourceTypeName) { - def resourceTypeMgr = LookupUtil.resourceTypeManager - def resourceType = resourceTypeMgr.getResourceTypeByNameAndPlugin(resourceTypeName, pluginName) - - assertNotNull( - "Cannot create resources. Unable to find resource type for [name: $resourceTypeName, plugin: $pluginName]", - resourceType - ) - - def resources = [] - count.times { - resources << new ResourceBuilder() - .createServer() - .withResourceType(resourceType) - .withName("${resourceType.name}-$it") - .withUuid("$resourceType.name:") - .withRandomResourceKey("${resourceType.name}-$it") - .build() - } - - transaction { - resources.each { resource -> entityManager.persist(resource) } - } - } - @Test(dependsOnMethods = ['upgradePluginWithTypesRemoved'], groups = ['RemoveTypes']) void deleteOperationDefsForRemovedType() { def operationMgr = LookupUtil.operationManager @@ -452,14 +439,22 @@ class ResourceMetadataManagerBeanTest extends AbstractEJB3Test { } }
- def transaction(work) { - try { - transactionManager.begin() - work() - transactionManager.commit() - } catch (Throwable t) { - transactionManager.rollback() - } + @Test(dependsOnMethods = ['upgradePluginWithTypesRemoved'], groups = ['RemoveTypes']) + void deleteBundles() { + def bundles = entityManager.createQuery("from Bundle b where b.bundleType.name = :name") + .setParameter("name", "Test Bundle") + .getResultList() + + assertEquals("Failed to delete the bundles", 0, bundles.size()) + } + + @Test(dependsOnMethods = ['upgradePluginWithTypesRemoved'], groups = ['RemoveTypes']) + void deleteBundleTypes() { + def bundleTypes = entityManager.createQuery("from BundleType b where b.name = :name") + .setParameter("name", "Test Bundle") + .getResultList() + + assertEquals("The bundle type should have been deleted", 0, bundleTypes.size()) }
/** @@ -541,6 +536,63 @@ class ResourceMetadataManagerBeanTest extends AbstractEJB3Test { return pluginDescriptor.ampsVersion }
+ def createResources(Integer count, String pluginName, String resourceTypeName) { + def resourceTypeMgr = LookupUtil.resourceTypeManager + def resourceType = resourceTypeMgr.getResourceTypeByNameAndPlugin(resourceTypeName, pluginName) + + assertNotNull( + "Cannot create resources. Unable to find resource type for [name: $resourceTypeName, plugin: $pluginName]", + resourceType + ) + + def resources = [] + count.times { + resources << new ResourceBuilder() + .createServer() + .withResourceType(resourceType) + .withName("${resourceType.name}-$it") + .withUuid("$resourceType.name:") + .withRandomResourceKey("${resourceType.name}-$it") + .build() + } + + transaction { + resources.each { resource -> entityManager.persist(resource) } + } + } + + def createBundle(bundleName, bundleTypeName, resourceTypeName, pluginName) { + def subjectMgr = LookupUtil.subjectManager + def bundleMgr = LookupUtil.bundleManager + def resourceTypeMgr = LookupUtil.resourceTypeManager + def resourceType = resourceTypeMgr.getResourceTypeByNameAndPlugin(resourceTypeName, pluginName) + + assertNotNull( + "Cannot create bundle. Unable to find resource type for [name: $resourceTypeName, plugin: $pluginName]", + resourceType + ) + + def bundleType = bundleMgr.getBundleType(subjectMgr.overlord, bundleTypeName) + + assertNotNull("Cannot create bundle. Unable to find bundle type for [name: $bundleTypeName]") + + def bundle = bundleMgr.createBundle(subjectMgr.overlord, bundleName, "test bundle: $bundleName", bundleType.id) + + assertNotNull("Failed create bundle for [name: $bundleName]", bundle) + + return bundle + } + + def transaction(work) { + try { + transactionManager.begin() + work() + transactionManager.commit() + } catch (Throwable t) { + transactionManager.rollback() + } + } + void assertTypesPersisted(msg, types, plugin) { def typesNotFound = [] def resourceTypeMgr = LookupUtil.resourceTypeManager
rhq-commits@lists.fedorahosted.org