modules/core/domain-test-utils/pom.xml | 36 modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/BuilderException.java | 49 - modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceBuilder.java | 413 --------- modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceTypeBuilder.java | 139 --- modules/core/domain/pom.xml | 17 modules/core/domain/src/test/java/org/rhq/core/domain/shared/BuilderException.java | 49 + modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceBuilder.java | 417 ++++++++++ modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceTypeBuilder.java | 142 +++ modules/core/pom.xml | 1 modules/enterprise/remoting/cli/pom.xml | 4 modules/enterprise/remoting/cli/src/test/java/org/rhq/enterprise/client/TabularWriterTest.java | 2 modules/enterprise/server/jar/mvn.out | 1 modules/enterprise/server/jar/pom.xml | 3 modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/discovery/DeletedResourceTypeFilterTest.java | 7 14 files changed, 634 insertions(+), 646 deletions(-)
New commits: commit 15611a63e0fdf9e8aea9e133c37644d964c5df38 Author: John Sanda jsanda@redhat.com Date: Thu Oct 28 10:43:03 2010 -0400
Removing domain-test-utils module to avoid potential circular dependencies
domain-test-utils had a depdendency on core/domain, tests in core/domain might very well want to use the classes in domain-test-utils which would result in a circular dependency. The classes in domain-test-utils have been moved into core/domain and are made available to other modules as a test jar.
diff --git a/modules/core/domain-test-utils/pom.xml b/modules/core/domain-test-utils/pom.xml deleted file mode 100644 index 97cb509..0000000 --- a/modules/core/domain-test-utils/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - - <modelVersion>4.0.0</modelVersion> - - <parent> - <groupId>org.rhq</groupId> - <artifactId>rhq-core-parent</artifactId> - <version>4.0.0-SNAPSHOT</version> - </parent> - <groupId>org.rhq</groupId> - <artifactId>rhq-core-domain-test-utils</artifactId> - <name>RHQ Domain Model Test Utils</name> - - <dependencies> - <dependency> - <groupId>${rhq.groupId}</groupId> - <artifactId>rhq-core-domain</artifactId> - <version>${project.version}</version> - <type>ejb</type> - </dependency> - <dependency> - <groupId>hibernate-annotations</groupId> - <artifactId>hibernate-annotations</artifactId> - </dependency> - <dependency> - <groupId>hibernate-entitymanager</groupId> - <artifactId>hibernate-entitymanager</artifactId> - </dependency> - <dependency> - <groupId>javax.persistence</groupId> - <artifactId>persistence-api</artifactId> - <version>1.0</version> - </dependency> - </dependencies> - -</project> diff --git a/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/BuilderException.java b/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/BuilderException.java deleted file mode 100644 index 7277c3c..0000000 --- a/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/BuilderException.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * RHQ Management Platform - * Copyright (C) 2005-2008 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, version 2, as - * published by the Free Software Foundation, and/or the GNU Lesser - * General Public License, version 2.1, also as published by the Free - * Software Foundation. - * - * 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 and the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License - * and the GNU Lesser General Public License along with this program; - * if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.rhq.core.domain.resource; - -/** - * A builder will throw this exception if it is unable to build the target object. For example, if example if all - * required properites of the target object do not have non-null values, then the builder will throw this exception. - * - * @author John Sanda - */ -public class BuilderException extends RuntimeException { - - public BuilderException() { - super(); - } - - public BuilderException(String message) { - super(message); - } - - public BuilderException(String message, Throwable cause) { - super(message, cause); - } - - public BuilderException(Throwable cause) { - super(cause); - } -} diff --git a/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceBuilder.java b/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceBuilder.java deleted file mode 100644 index c065d1f..0000000 --- a/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceBuilder.java +++ /dev/null @@ -1,413 +0,0 @@ -/* - * RHQ Management Platform - * Copyright (C) 2005-2008 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, version 2, as - * published by the Free Software Foundation, and/or the GNU Lesser - * General Public License, version 2.1, also as published by the Free - * Software Foundation. - * - * 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 and the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License - * and the GNU Lesser General Public License along with this program; - * if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.rhq.core.domain.resource; - -import org.rhq.core.domain.measurement.AvailabilityType; -import org.rhq.core.domain.measurement.ResourceAvailability; - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -/** - * ResoureBuilder is a builder object that creates Resource objects. The builder ensures that a Resource is created - * in a valid state, specifically, fields that are not nullable are required to have non-null values. Using a builder - * should help make the intent of tests clearer and more self-documenting. - * <br/><br/> - * Note that this class currently does not yet provide support for all Resource fields/properties. - * - * @author John Sanda - */ -public class ResourceBuilder { - - private ResourceBuilder parentBuilder; - - private Resource resource; - - private Random random; - - private boolean useDefaultResourceType; - - private ResourceCategory category; - - private List<ResourceBuilder> childBuilders = new ArrayList<ResourceBuilder>(); - - public static class AssociationBuilder { - private ResourceBuilder resourceBuilder; - - private int count; - - AssociationBuilder(ResourceBuilder builder, int count) { - resourceBuilder = builder; - this.count = count; - } - - public ResourceBuilder randomChildServers() { - for (int i = 0; i < count; ++i) { - ResourceBuilder childBuilder = new ResourceBuilder(ResourceCategory.SERVER, resourceBuilder); - resourceBuilder.childBuilders.add(childBuilder.createRandomServer()); - } - return resourceBuilder; - } - - public ResourceBuilder randomChildServices() { - for (int i = 0; i < count; ++i) { - ResourceBuilder childBuilder = new ResourceBuilder(ResourceCategory.SERVICE, resourceBuilder); - resourceBuilder.childBuilders.add(childBuilder.createRandomService()); - } - return resourceBuilder; - } - } - - public static class ChildrenResourceBuilder { - private ResourceBuilder parentBuilder; - - ChildrenResourceBuilder(ResourceBuilder builder, ResourceCategory category, int numChildren) { - parentBuilder = builder; - parentBuilder.childBuilders = new ArrayList<ResourceBuilder>(numChildren); - for (int i = 0; i < numChildren; ++i) { - parentBuilder.childBuilders.add(new ResourceBuilder(category, parentBuilder).createResource()); - } - } - - public ChildrenResourceBuilder inInventory() { - for (ResourceBuilder childBuilder : parentBuilder.childBuilders) { - childBuilder.inInventory(); - } - return this; - } - - public ChildrenResourceBuilder notInInventory() { - for (ResourceBuilder childBuilder : parentBuilder.childBuilders) { - childBuilder.notInInventory(); - } - return this; - } - - public ResourceBuilder included() { - return parentBuilder; - } - } - - - public ResourceBuilder() { - } - - private ResourceBuilder(ResourceCategory category, ResourceBuilder parentBuilder) { - this.category = category; - this.parentBuilder = parentBuilder; - } - - public ResourceBuilder createResource() { - resource = new Resource(); - random = new Random(); - return this; - } - - public ResourceBuilder createPlatform() { - category = ResourceCategory.PLATFORM; - return createResource(); - } - - public ResourceBuilder createServer() { - category = ResourceCategory.SERVER; - return createResource(); - } - - public ResourceBuilder createRandomServer() { - category = ResourceCategory.SERVER; - createResource(); - withRandomId(); - withRandomName("server:"); - withRandomResourceKey("server:"); - withRandomUuid("server:"); - withDefaultServerResourceType(); - - return this; - } - - public ResourceBuilder createService() { - category = ResourceCategory.SERVICE; - return createResource(); - } - - public ResourceBuilder createRandomService() { - category = ResourceCategory.SERVICE; - createResource(); - withRandomId(); - withRandomName("service:"); - withRandomResourceKey("service:"); - withRandomUuid("service:"); - withDefaultServiceResourceType(); - - return this; - } - - /** - * Using a default resource type results in <code>Resource.resourceType</code> being assigned to a new - * ResourceType object that has some default values applied to it. If the Resource being created is a platform, then - * the ResourceType will be a platform. More specifically, <code>ResourceType.category</code> will be assigned a - * value of {@link ResourceCategory#PLATFORM}. Likewise, if the Resource being created is a server, then its - * ResourceType object will have a category of {@link ResourceCategory#SERVER}. And if the Resource is a service, - * then the ResourceType category will be {@link ResourceCategory#SERVICE}. - * <br/><br/> - * The resource type name defaults to the name of resource. And the plugin name (as specified by - * ResourceType.plugin) defaults to <code>Resource.name + " Plugin"</code> - * <br/><br/> - * When using a default resource type, the resource must be created using one of {@link #createPlatform()}, - * {@link #createServer()}, or {@link #createService()}; otherwise, an exception will be thrown since the builder - * will not have sufficient information to create the resource type. - * <br/><br/> - * Lastly, if you specify that a default resource type by calling this method and also specify the resource tye - * with {@link #withResourceType(ResourceType)}, the latter will be overwritten regardless of when it is called. The - * default will be used instead. - * - * @return The builder - * - */ - public ResourceBuilder usingDefaultResourceType() { - useDefaultResourceType = true; - return this; - } - - public ResourceBuilder withId(int id) { - resource.setId(id); - return this; - } - - public ResourceBuilder withRandomId() { - resource.setId(random.nextInt()); - return this; - } - - public ResourceBuilder withResourceKey(String key) { - resource.setResourceKey(key); - return this; - } - - public ResourceBuilder withRandomResourceKey(String prefix) { - resource.setResourceKey(prefix + randomString()); - return this; - } - - public ResourceBuilder withRandomResourceKey() { - return withRandomResourceKey(""); - } - - public ResourceBuilder withName(String name) { - resource.setName(name); - return this; - } - - public ResourceBuilder withRandomName(String prefix) { - resource.setName(prefix + randomString()); - return this; - } - - public ResourceBuilder withRandomName() { - return withRandomName(""); - } - - public ResourceBuilder withResourceType(ResourceType resourceType) { - resource.setResourceType(resourceType); - return this; - } - - public ResourceBuilder withUuid(String uuid) { - resource.setUuid(uuid); - return this; - } - - public ResourceBuilder withRandomUuid(String prefix) { - resource.setUuid(prefix + randomString()); - return this; - } - - public ResourceBuilder withRandomUuid() { - return withRandomUuid(""); - } - - public ResourceBuilder withVersion(String version) { - resource.setVersion(version); - return this; - } - - public ResourceBuilder withCurrentAvailability(AvailabilityType availabilityType) { - ResourceAvailability availability = new ResourceAvailability(resource, availabilityType); - resource.setCurrentAvailability(availability); - return this; - } - - public ResourceBuilder withInventoryStatus(InventoryStatus inventoryStatus) { - resource.setInventoryStatus(inventoryStatus); - return this; - } - - /** - * Set the <code>inventoryStatus</code> to {@link InventoryStatus#COMMITTED} - * - * @return The builder - */ - public ResourceBuilder inInventory() { - resource.setInventoryStatus(InventoryStatus.COMMITTED); - return this; - } - - /** - * Set the <code>inventoryStatus</code> to {@link InventoryStatus#NEW} - * - * @return The builder - */ - public ResourceBuilder notInInventory() { - resource.setInventoryStatus(InventoryStatus.NEW); - return this; - } - - public AssociationBuilder with(int count) { - return new AssociationBuilder(this, count); - } - - public ResourceBuilder withChildService() { - ResourceBuilder childBuilder = new ResourceBuilder(ResourceCategory.SERVICE, this); - childBuilders.add(childBuilder.createService()); - return childBuilder; - } - - public ResourceBuilder included() { - return this.parentBuilder; - } - - public Resource build() { - String errors = validate(); - if (errors != null) { - throw new BuilderException(errors); - } - - if (useDefaultResourceType) { - withDefaultResourceType(); - } - - for (ResourceBuilder childBuilder : childBuilders) { - resource.addChildResource(childBuilder.build()); - } - - return resource; - } - - private String validate() { - StringBuilder errors = new StringBuilder(); - - if (resource.getUuid() == null) { - // Making uuid required since it is used in equals/hashCode - errors.append("uuid is a required property\n"); - } - - if (resource.getName() == null) { - errors.append("name is a required property\n"); - } - - if (useDefaultResourceType && category == null) { - errors.append("When using default resource type, the resource must be created with one of " + - "createPlatform(), createServer(), or createService()\n"); - } - - // We only care that resourceType is set if we are not using a default type. If we are using a default, - // then the resourceType property will be set after validation, assuming there are no validation errors. - if (!useDefaultResourceType && resource.getResourceType() == null) { - errors.append("resourceType is a required property\n"); - } - - for (ResourceBuilder childBuilder : childBuilders) { - String childErrors = childBuilder.validate(); - if (childErrors != null) { - errors.append("The following child resource errors were found:\n" + childErrors); - } - } - - if (errors.length() == 0) { - return null; - } - - return "Unable to build Resource instance due to the following validation errors:\n" + errors; - } - - private ResourceBuilder withDefaultResourceType() { - switch (category) { - case PLATFORM: return withDefaultPlatformResourceType(); - case SERVER: return withDefaultServerResourceType(); - default: return withDefaultServiceResourceType(); - } - } - - /** - * The default platform resource type is as its name implies a platform type whose name defaults to the resource - * name and the plugin name (i.e., ResourceType.plugin property) defaults to resource name + 'Plugin'. - * - * @return The builder - */ - private ResourceBuilder withDefaultPlatformResourceType() { - resource.setResourceType(new ResourceTypeBuilder().createPlatformResourceType() - .withName(resource.getName()) - .withPlugin(resource.getName() + " Plugin") - .withParentResourceType(resource.getResourceType()) - .build()); - return this; - } - - /** - * The default server resource type is as its name implies a server type whose name defaults to the resource name - * and the plugin name (i.e., ResourceType.plugin property) defaults to resource name + 'Plugin' - * - * @return The builder - */ - private ResourceBuilder withDefaultServerResourceType() { - resource.setResourceType(new ResourceTypeBuilder().createServerResourceType() - .withName(resource.getName()) - .withPlugin(resource.getName() + " Plugin") - .withParentResourceType(resource.getResourceType()) - .build()); - return this; - } - - /** - * The default service resource type is as its name implies a service type whose name defaults to the resource name - * and the plugin name (i.e., ResourceType.plugin property) defaults to resource name + 'Plugin' - * - * @return The builder - */ - private ResourceBuilder withDefaultServiceResourceType() { - resource.setResourceType(new ResourceTypeBuilder().createServerResourceType() - .withName(resource.getName()) - .withPlugin(resource.getName() + " Plugin") - .withParentResourceType(resource.getResourceType()) - .build()); - return this; - } - - private String randomString() { - return new BigInteger(16, random).toString(32); - } - -} diff --git a/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceTypeBuilder.java b/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceTypeBuilder.java deleted file mode 100644 index 2e83fd3..0000000 --- a/modules/core/domain-test-utils/src/main/java/org/rhq/core/domain/resource/ResourceTypeBuilder.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * RHQ Management Platform - * Copyright (C) 2005-2008 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, version 2, as - * published by the Free Software Foundation, and/or the GNU Lesser - * General Public License, version 2.1, also as published by the Free - * Software Foundation. - * - * 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 and the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License - * and the GNU Lesser General Public License along with this program; - * if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.rhq.core.domain.resource; - -import java.util.HashSet; - -/** - * ResourceTypeBuilder is a builder that creates ResourceType objects. The builder ensures that the ResourceType is - * created in a valid state, specifically fields that are not nullable are required to have non-null values. Using the - * builder should help make the intent of tests clearer and more self-documenting. - * <br/><br/> - * Note that this class currently does not yet provide support for all ResourceType fields/properties. - * - * @author John Sanda - */ -public class ResourceTypeBuilder { - - private ResourceType resourceType; - - public ResourceTypeBuilder createResourceType() { - resourceType = new ResourceType(); - resourceType.setParentResourceTypes(new HashSet<ResourceType>()); - resourceType.setChildResourceTypes(new HashSet<ResourceType>()); - - return this; - } - - public ResourceTypeBuilder createPlatformResourceType() { - return createResourceType().withCategory(ResourceCategory.PLATFORM); - } - - public ResourceTypeBuilder createServerResourceType() { - return createResourceType().withCategory(ResourceCategory.SERVER); - } - - public ResourceTypeBuilder createServiceResourceType() { - return createResourceType().withCategory(ResourceCategory.SERVICE); - } - - public ResourceTypeBuilder withId(int id) { - resourceType.setId(id); - return this; - } - - public ResourceTypeBuilder withName(String name) { - resourceType.setName(name); - return this; - } - - public ResourceTypeBuilder withPlugin(String plugin) { - resourceType.setPlugin(plugin); - return this; - } - - public ResourceTypeBuilder withCategory(ResourceCategory category) { - resourceType.setCategory(category); - return this; - } - - public ResourceTypeBuilder thatIsDeleted() { - resourceType.setDeleted(true); - return this; - } - - public ResourceTypeBuilder withParentResourceType(ResourceType parentResourceType) { - if (parentResourceType != null) { - resourceType.addParentResourceType(parentResourceType); - } - return this; - } - - public ResourceTypeBuilder withParentResourceTypes(ResourceType... parentResourceTypes) { - for (ResourceType parent : parentResourceTypes) { - resourceType.addParentResourceType(parent); - } - return this; - } - - public ResourceType build() { - String errors = valdiate(); - if (errors != null) { - throw new BuilderException(errors); - } - - return resourceType; - } - - private String valdiate() { - StringBuilder errors = new StringBuilder(); - - if (resourceType.getName() == null) { - errors.append("name is a required property\n"); - } - - if (resourceType.getCategory() == null) { - errors.append("category is a required property\n"); - } - - if (resourceType.getCreationDataType() == null) { - errors.append("creationDate is a required property\n"); - } - - if (resourceType.getCreateDeletePolicy() == null) { - errors.append("createDeletePolicy is a required property\n"); - } - - if (resourceType.getPlugin() == null) { - errors.append("plugin is a required property\n"); - } - - if (errors.length() == 0) { - return null; - } - - return "Unable to build ResourceType instance due to the following validation errors:\n" + errors; - } - -} diff --git a/modules/core/domain/pom.xml b/modules/core/domain/pom.xml index c7cca99..0fd6b98 100644 --- a/modules/core/domain/pom.xml +++ b/modules/core/domain/pom.xml @@ -288,6 +288,23 @@ </configuration> </plugin>
+ <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>test-jar</goal> + </goals> + <configuration> + <includes> + <include>org/rhq/core/domain/shared/**</include> + </includes> + </configuration> + </execution> + </executions> + </plugin> </plugins> </build>
diff --git a/modules/core/domain/src/test/java/org/rhq/core/domain/shared/BuilderException.java b/modules/core/domain/src/test/java/org/rhq/core/domain/shared/BuilderException.java new file mode 100644 index 0000000..9b0dcea --- /dev/null +++ b/modules/core/domain/src/test/java/org/rhq/core/domain/shared/BuilderException.java @@ -0,0 +1,49 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 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, version 2, as + * published by the Free Software Foundation, and/or the GNU Lesser + * General Public License, version 2.1, also as published by the Free + * Software Foundation. + * + * 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 and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.rhq.core.domain.shared; + +/** + * A builder will throw this exception if it is unable to build the target object. For example, if example if all + * required properites of the target object do not have non-null values, then the builder will throw this exception. + * + * @author John Sanda + */ +public class BuilderException extends RuntimeException { + + public BuilderException() { + super(); + } + + public BuilderException(String message) { + super(message); + } + + public BuilderException(String message, Throwable cause) { + super(message, cause); + } + + public BuilderException(Throwable cause) { + super(cause); + } +} diff --git a/modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceBuilder.java b/modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceBuilder.java new file mode 100644 index 0000000..d60749d --- /dev/null +++ b/modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceBuilder.java @@ -0,0 +1,417 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 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, version 2, as + * published by the Free Software Foundation, and/or the GNU Lesser + * General Public License, version 2.1, also as published by the Free + * Software Foundation. + * + * 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 and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.rhq.core.domain.shared; + +import org.rhq.core.domain.measurement.AvailabilityType; +import org.rhq.core.domain.measurement.ResourceAvailability; +import org.rhq.core.domain.resource.InventoryStatus; +import org.rhq.core.domain.resource.Resource; +import org.rhq.core.domain.resource.ResourceCategory; +import org.rhq.core.domain.resource.ResourceType; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * ResoureBuilder is a builder object that creates Resource objects. The builder ensures that a Resource is created + * in a valid state, specifically, fields that are not nullable are required to have non-null values. Using a builder + * should help make the intent of tests clearer and more self-documenting. + * <br/><br/> + * Note that this class currently does not yet provide support for all Resource fields/properties. + * + * @author John Sanda + */ +public class ResourceBuilder { + + private ResourceBuilder parentBuilder; + + private Resource resource; + + private Random random; + + private boolean useDefaultResourceType; + + private ResourceCategory category; + + private List<ResourceBuilder> childBuilders = new ArrayList<ResourceBuilder>(); + + public static class AssociationBuilder { + private ResourceBuilder resourceBuilder; + + private int count; + + AssociationBuilder(ResourceBuilder builder, int count) { + resourceBuilder = builder; + this.count = count; + } + + public ResourceBuilder randomChildServers() { + for (int i = 0; i < count; ++i) { + ResourceBuilder childBuilder = new ResourceBuilder(ResourceCategory.SERVER, resourceBuilder); + resourceBuilder.childBuilders.add(childBuilder.createRandomServer()); + } + return resourceBuilder; + } + + public ResourceBuilder randomChildServices() { + for (int i = 0; i < count; ++i) { + ResourceBuilder childBuilder = new ResourceBuilder(ResourceCategory.SERVICE, resourceBuilder); + resourceBuilder.childBuilders.add(childBuilder.createRandomService()); + } + return resourceBuilder; + } + } + + public static class ChildrenResourceBuilder { + private ResourceBuilder parentBuilder; + + ChildrenResourceBuilder(ResourceBuilder builder, ResourceCategory category, int numChildren) { + parentBuilder = builder; + parentBuilder.childBuilders = new ArrayList<ResourceBuilder>(numChildren); + for (int i = 0; i < numChildren; ++i) { + parentBuilder.childBuilders.add(new ResourceBuilder(category, parentBuilder).createResource()); + } + } + + public ChildrenResourceBuilder inInventory() { + for (ResourceBuilder childBuilder : parentBuilder.childBuilders) { + childBuilder.inInventory(); + } + return this; + } + + public ChildrenResourceBuilder notInInventory() { + for (ResourceBuilder childBuilder : parentBuilder.childBuilders) { + childBuilder.notInInventory(); + } + return this; + } + + public ResourceBuilder included() { + return parentBuilder; + } + } + + + public ResourceBuilder() { + } + + private ResourceBuilder(ResourceCategory category, ResourceBuilder parentBuilder) { + this.category = category; + this.parentBuilder = parentBuilder; + } + + public ResourceBuilder createResource() { + resource = new Resource(); + random = new Random(); + return this; + } + + public ResourceBuilder createPlatform() { + category = ResourceCategory.PLATFORM; + return createResource(); + } + + public ResourceBuilder createServer() { + category = ResourceCategory.SERVER; + return createResource(); + } + + public ResourceBuilder createRandomServer() { + category = ResourceCategory.SERVER; + createResource(); + withRandomId(); + withRandomName("server:"); + withRandomResourceKey("server:"); + withRandomUuid("server:"); + withDefaultServerResourceType(); + + return this; + } + + public ResourceBuilder createService() { + category = ResourceCategory.SERVICE; + return createResource(); + } + + public ResourceBuilder createRandomService() { + category = ResourceCategory.SERVICE; + createResource(); + withRandomId(); + withRandomName("service:"); + withRandomResourceKey("service:"); + withRandomUuid("service:"); + withDefaultServiceResourceType(); + + return this; + } + + /** + * Using a default resource type results in <code>Resource.resourceType</code> being assigned to a new + * ResourceType object that has some default values applied to it. If the Resource being created is a platform, then + * the ResourceType will be a platform. More specifically, <code>ResourceType.category</code> will be assigned a + * value of {@link ResourceCategory#PLATFORM}. Likewise, if the Resource being created is a server, then its + * ResourceType object will have a category of {@link ResourceCategory#SERVER}. And if the Resource is a service, + * then the ResourceType category will be {@link ResourceCategory#SERVICE}. + * <br/><br/> + * The resource type name defaults to the name of resource. And the plugin name (as specified by + * ResourceType.plugin) defaults to <code>Resource.name + " Plugin"</code> + * <br/><br/> + * When using a default resource type, the resource must be created using one of {@link #createPlatform()}, + * {@link #createServer()}, or {@link #createService()}; otherwise, an exception will be thrown since the builder + * will not have sufficient information to create the resource type. + * <br/><br/> + * Lastly, if you specify that a default resource type by calling this method and also specify the resource tye + * with {@link #withResourceType(org.rhq.core.domain.resource.ResourceType)}, the latter will be overwritten regardless of when it is called. The + * default will be used instead. + * + * @return The builder + * + */ + public ResourceBuilder usingDefaultResourceType() { + useDefaultResourceType = true; + return this; + } + + public ResourceBuilder withId(int id) { + resource.setId(id); + return this; + } + + public ResourceBuilder withRandomId() { + resource.setId(random.nextInt()); + return this; + } + + public ResourceBuilder withResourceKey(String key) { + resource.setResourceKey(key); + return this; + } + + public ResourceBuilder withRandomResourceKey(String prefix) { + resource.setResourceKey(prefix + randomString()); + return this; + } + + public ResourceBuilder withRandomResourceKey() { + return withRandomResourceKey(""); + } + + public ResourceBuilder withName(String name) { + resource.setName(name); + return this; + } + + public ResourceBuilder withRandomName(String prefix) { + resource.setName(prefix + randomString()); + return this; + } + + public ResourceBuilder withRandomName() { + return withRandomName(""); + } + + public ResourceBuilder withResourceType(ResourceType resourceType) { + resource.setResourceType(resourceType); + return this; + } + + public ResourceBuilder withUuid(String uuid) { + resource.setUuid(uuid); + return this; + } + + public ResourceBuilder withRandomUuid(String prefix) { + resource.setUuid(prefix + randomString()); + return this; + } + + public ResourceBuilder withRandomUuid() { + return withRandomUuid(""); + } + + public ResourceBuilder withVersion(String version) { + resource.setVersion(version); + return this; + } + + public ResourceBuilder withCurrentAvailability(AvailabilityType availabilityType) { + ResourceAvailability availability = new ResourceAvailability(resource, availabilityType); + resource.setCurrentAvailability(availability); + return this; + } + + public ResourceBuilder withInventoryStatus(InventoryStatus inventoryStatus) { + resource.setInventoryStatus(inventoryStatus); + return this; + } + + /** + * Set the <code>inventoryStatus</code> to {@link InventoryStatus#COMMITTED} + * + * @return The builder + */ + public ResourceBuilder inInventory() { + resource.setInventoryStatus(InventoryStatus.COMMITTED); + return this; + } + + /** + * Set the <code>inventoryStatus</code> to {@link InventoryStatus#NEW} + * + * @return The builder + */ + public ResourceBuilder notInInventory() { + resource.setInventoryStatus(InventoryStatus.NEW); + return this; + } + + public AssociationBuilder with(int count) { + return new AssociationBuilder(this, count); + } + + public ResourceBuilder withChildService() { + ResourceBuilder childBuilder = new ResourceBuilder(ResourceCategory.SERVICE, this); + childBuilders.add(childBuilder.createService()); + return childBuilder; + } + + public ResourceBuilder included() { + return this.parentBuilder; + } + + public Resource build() { + String errors = validate(); + if (errors != null) { + throw new BuilderException(errors); + } + + if (useDefaultResourceType) { + withDefaultResourceType(); + } + + for (ResourceBuilder childBuilder : childBuilders) { + resource.addChildResource(childBuilder.build()); + } + + return resource; + } + + private String validate() { + StringBuilder errors = new StringBuilder(); + + if (resource.getUuid() == null) { + // Making uuid required since it is used in equals/hashCode + errors.append("uuid is a required property\n"); + } + + if (resource.getName() == null) { + errors.append("name is a required property\n"); + } + + if (useDefaultResourceType && category == null) { + errors.append("When using default resource type, the resource must be created with one of " + + "createPlatform(), createServer(), or createService()\n"); + } + + // We only care that resourceType is set if we are not using a default type. If we are using a default, + // then the resourceType property will be set after validation, assuming there are no validation errors. + if (!useDefaultResourceType && resource.getResourceType() == null) { + errors.append("resourceType is a required property\n"); + } + + for (ResourceBuilder childBuilder : childBuilders) { + String childErrors = childBuilder.validate(); + if (childErrors != null) { + errors.append("The following child resource errors were found:\n" + childErrors); + } + } + + if (errors.length() == 0) { + return null; + } + + return "Unable to build Resource instance due to the following validation errors:\n" + errors; + } + + private ResourceBuilder withDefaultResourceType() { + switch (category) { + case PLATFORM: return withDefaultPlatformResourceType(); + case SERVER: return withDefaultServerResourceType(); + default: return withDefaultServiceResourceType(); + } + } + + /** + * The default platform resource type is as its name implies a platform type whose name defaults to the resource + * name and the plugin name (i.e., ResourceType.plugin property) defaults to resource name + 'Plugin'. + * + * @return The builder + */ + private ResourceBuilder withDefaultPlatformResourceType() { + resource.setResourceType(new ResourceTypeBuilder().createPlatformResourceType() + .withName(resource.getName()) + .withPlugin(resource.getName() + " Plugin") + .withParentResourceType(resource.getResourceType()) + .build()); + return this; + } + + /** + * The default server resource type is as its name implies a server type whose name defaults to the resource name + * and the plugin name (i.e., ResourceType.plugin property) defaults to resource name + 'Plugin' + * + * @return The builder + */ + private ResourceBuilder withDefaultServerResourceType() { + resource.setResourceType(new ResourceTypeBuilder().createServerResourceType() + .withName(resource.getName()) + .withPlugin(resource.getName() + " Plugin") + .withParentResourceType(resource.getResourceType()) + .build()); + return this; + } + + /** + * The default service resource type is as its name implies a service type whose name defaults to the resource name + * and the plugin name (i.e., ResourceType.plugin property) defaults to resource name + 'Plugin' + * + * @return The builder + */ + private ResourceBuilder withDefaultServiceResourceType() { + resource.setResourceType(new ResourceTypeBuilder().createServerResourceType() + .withName(resource.getName()) + .withPlugin(resource.getName() + " Plugin") + .withParentResourceType(resource.getResourceType()) + .build()); + return this; + } + + private String randomString() { + return new BigInteger(16, random).toString(32); + } + +} diff --git a/modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceTypeBuilder.java b/modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceTypeBuilder.java new file mode 100644 index 0000000..f6e55de --- /dev/null +++ b/modules/core/domain/src/test/java/org/rhq/core/domain/shared/ResourceTypeBuilder.java @@ -0,0 +1,142 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 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, version 2, as + * published by the Free Software Foundation, and/or the GNU Lesser + * General Public License, version 2.1, also as published by the Free + * Software Foundation. + * + * 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 and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.rhq.core.domain.shared; + +import java.util.HashSet; + +import org.rhq.core.domain.resource.ResourceCategory; +import org.rhq.core.domain.resource.ResourceType; + +/** + * ResourceTypeBuilder is a builder that creates ResourceType objects. The builder ensures that the ResourceType is + * created in a valid state, specifically fields that are not nullable are required to have non-null values. Using the + * builder should help make the intent of tests clearer and more self-documenting. + * <br/><br/> + * Note that this class currently does not yet provide support for all ResourceType fields/properties. + * + * @author John Sanda + */ +public class ResourceTypeBuilder { + + private ResourceType resourceType; + + public ResourceTypeBuilder createResourceType() { + resourceType = new ResourceType(); + resourceType.setParentResourceTypes(new HashSet<ResourceType>()); + resourceType.setChildResourceTypes(new HashSet<ResourceType>()); + + return this; + } + + public ResourceTypeBuilder createPlatformResourceType() { + return createResourceType().withCategory(ResourceCategory.PLATFORM); + } + + public ResourceTypeBuilder createServerResourceType() { + return createResourceType().withCategory(ResourceCategory.SERVER); + } + + public ResourceTypeBuilder createServiceResourceType() { + return createResourceType().withCategory(ResourceCategory.SERVICE); + } + + public ResourceTypeBuilder withId(int id) { + resourceType.setId(id); + return this; + } + + public ResourceTypeBuilder withName(String name) { + resourceType.setName(name); + return this; + } + + public ResourceTypeBuilder withPlugin(String plugin) { + resourceType.setPlugin(plugin); + return this; + } + + public ResourceTypeBuilder withCategory(ResourceCategory category) { + resourceType.setCategory(category); + return this; + } + + public ResourceTypeBuilder thatIsDeleted() { + resourceType.setDeleted(true); + return this; + } + + public ResourceTypeBuilder withParentResourceType(ResourceType parentResourceType) { + if (parentResourceType != null) { + resourceType.addParentResourceType(parentResourceType); + } + return this; + } + + public ResourceTypeBuilder withParentResourceTypes(ResourceType... parentResourceTypes) { + for (ResourceType parent : parentResourceTypes) { + resourceType.addParentResourceType(parent); + } + return this; + } + + public ResourceType build() { + String errors = valdiate(); + if (errors != null) { + throw new BuilderException(errors); + } + + return resourceType; + } + + private String valdiate() { + StringBuilder errors = new StringBuilder(); + + if (resourceType.getName() == null) { + errors.append("name is a required property\n"); + } + + if (resourceType.getCategory() == null) { + errors.append("category is a required property\n"); + } + + if (resourceType.getCreationDataType() == null) { + errors.append("creationDate is a required property\n"); + } + + if (resourceType.getCreateDeletePolicy() == null) { + errors.append("createDeletePolicy is a required property\n"); + } + + if (resourceType.getPlugin() == null) { + errors.append("plugin is a required property\n"); + } + + if (errors.length() == 0) { + return null; + } + + return "Unable to build ResourceType instance due to the following validation errors:\n" + errors; + } + +} diff --git a/modules/core/pom.xml b/modules/core/pom.xml index b936f26..f6f8289 100644 --- a/modules/core/pom.xml +++ b/modules/core/pom.xml @@ -47,7 +47,6 @@ <module>comm-api</module> <module>dbutils</module> <module>domain</module> - <module>domain-test-utils</module> <module>plugin-api</module> <module>client-api</module> <module>plugin-container</module> diff --git a/modules/enterprise/remoting/cli/pom.xml b/modules/enterprise/remoting/cli/pom.xml index d2c0f16..d67c325 100644 --- a/modules/enterprise/remoting/cli/pom.xml +++ b/modules/enterprise/remoting/cli/pom.xml @@ -35,8 +35,10 @@
<dependency> <groupId>${groupId}</groupId> - <artifactId>rhq-core-domain-test-utils</artifactId> + <artifactId>rhq-core-domain</artifactId> <version>${version}</version> + <type>test-jar</type> + <scope>test</scope> </dependency>
<dependency> diff --git a/modules/enterprise/remoting/cli/src/test/java/org/rhq/enterprise/client/TabularWriterTest.java b/modules/enterprise/remoting/cli/src/test/java/org/rhq/enterprise/client/TabularWriterTest.java index c9d8bb6..665abf0 100644 --- a/modules/enterprise/remoting/cli/src/test/java/org/rhq/enterprise/client/TabularWriterTest.java +++ b/modules/enterprise/remoting/cli/src/test/java/org/rhq/enterprise/client/TabularWriterTest.java @@ -26,7 +26,7 @@ package org.rhq.enterprise.client; import static org.testng.Assert.*;
import org.rhq.core.domain.resource.Resource; -import org.rhq.core.domain.resource.ResourceBuilder; +import org.rhq.core.domain.shared.ResourceBuilder;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; diff --git a/modules/enterprise/server/jar/mvn.out b/modules/enterprise/server/jar/mvn.out new file mode 100644 index 0000000..2b43846 --- /dev/null +++ b/modules/enterprise/server/jar/mvn.out @@ -0,0 +1 @@ +[INFO] Scanning for projects... diff --git a/modules/enterprise/server/jar/pom.xml b/modules/enterprise/server/jar/pom.xml index ac041f1..e33e006 100644 --- a/modules/enterprise/server/jar/pom.xml +++ b/modules/enterprise/server/jar/pom.xml @@ -85,8 +85,9 @@
<dependency> <groupId>org.rhq</groupId> - <artifactId>rhq-core-domain-test-utils</artifactId> + <artifactId>rhq-core-domain</artifactId> <version>${project.version}</version> + <type>test-jar</type> <scope>test</scope> </dependency>
diff --git a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/discovery/DeletedResourceTypeFilterTest.java b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/discovery/DeletedResourceTypeFilterTest.java index 13de583..a3cdcd2 100644 --- a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/discovery/DeletedResourceTypeFilterTest.java +++ b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/discovery/DeletedResourceTypeFilterTest.java @@ -1,7 +1,5 @@ package org.rhq.enterprise.server.discovery;
-import java.util.List; - import org.jmock.Expectations; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -10,10 +8,9 @@ import org.rhq.core.clientapi.server.discovery.InventoryReport; import org.rhq.core.domain.auth.Subject; import org.rhq.core.domain.criteria.ResourceTypeCriteria; import org.rhq.core.domain.resource.Agent; -import org.rhq.core.domain.resource.Resource; -import org.rhq.core.domain.resource.ResourceBuilder; +import org.rhq.core.domain.shared.ResourceBuilder; import org.rhq.core.domain.resource.ResourceType; -import org.rhq.core.domain.resource.ResourceTypeBuilder; +import org.rhq.core.domain.shared.ResourceTypeBuilder; import org.rhq.core.domain.util.PageList; import org.rhq.enterprise.server.auth.SubjectManagerLocal; import org.rhq.enterprise.server.resource.ResourceTypeManagerLocal;
rhq-commits@lists.fedorahosted.org