modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtility.java
| 244 ------
modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtilityTest.java
| 322 --------
modules/core/domain/src/main/java/org/rhq/core/domain/configuration/ConfigurationUtility.java
| 334 +++++++++
modules/core/domain/src/test/java/org/rhq/core/domain/configuration/ConfigurationUtilityTest.java
| 363 ++++++++++
modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/bundle/deploy/GetDeploymentConfigStep.java
| 12
5 files changed, 721 insertions(+), 554 deletions(-)
New commits:
commit a5ef337f5921c5cc52fd6a96c084925c46b8f4a1
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Wed Jun 20 17:26:41 2012 -0400
[BZ 828843] fix bundle deploy wizard's configuration editor initialization so
required properties that didn't exist in the live deployment config are set to their
default values
diff --git
a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtility.java
b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtility.java
index 85ae0bb..6420978 100644
---
a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtility.java
+++
b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtility.java
@@ -22,31 +22,20 @@
*/
package org.rhq.core.clientapi.agent.configuration;
-import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.rhq.core.domain.configuration.AbstractPropertyMap;
import org.rhq.core.domain.configuration.Configuration;
-import org.rhq.core.domain.configuration.Property;
-import org.rhq.core.domain.configuration.PropertyDefinitionDynamic;
-import org.rhq.core.domain.configuration.PropertyList;
-import org.rhq.core.domain.configuration.PropertyMap;
-import org.rhq.core.domain.configuration.PropertySimple;
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.configuration.definition.PropertyDefinitionList;
-import org.rhq.core.domain.configuration.definition.PropertyDefinitionMap;
-import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
/**
* Utility methods for working with {@link Configuration}s.
*
* @author Ian Springer
+ *
+ * @deprecated use the utility found in the domain module
-org.rhq.core.domain.configuration.ConfigurationUtility
*/
public abstract class ConfigurationUtility {
@@ -60,21 +49,11 @@ public abstract class ConfigurationUtility {
* for it, this method is a no-op and will return immediately.
*
* @param configDef the configuration definition whose default template is to be
created and set
+ *
+ * @deprecated use the utility found in the domain module
-org.rhq.core.domain.configuration.ConfigurationUtility
*/
public static void initializeDefaultTemplate(ConfigurationDefinition configDef) {
- ConfigurationTemplate defaultTemplate = configDef.getDefaultTemplate();
- if (defaultTemplate == null) {
- Configuration defaultConfig = createDefaultConfiguration(configDef);
- // not everything should have a default template - only stuff that has
default values
- if (!defaultConfig.getProperties().isEmpty()) {
- defaultTemplate = new
ConfigurationTemplate(ConfigurationTemplate.DEFAULT_TEMPLATE_NAME,
- ConfigurationTemplate.DEFAULT_TEMPLATE_NAME);
- defaultTemplate.setDefault(true);
- defaultTemplate.setConfiguration(defaultConfig);
- configDef.putTemplate(defaultTemplate);
- }
- }
- return;
+
org.rhq.core.domain.configuration.ConfigurationUtility.initializeDefaultTemplate(configDef);
}
/**
@@ -87,17 +66,12 @@ public abstract class ConfigurationUtility {
*
* @param configurationDefinition the configuration definition whose default
configuration is to be created
* @return configuration the default configuration
+ *
+ * @deprecated use the utility found in the domain module
-org.rhq.core.domain.configuration.ConfigurationUtility
*/
public static Configuration createDefaultConfiguration(ConfigurationDefinition
configurationDefinition) {
- if (configurationDefinition == null) {
- throw new IllegalArgumentException("configurationDefinition ==
null");
- }
- Configuration defaultConfig = new Configuration();
- Map<String, PropertyDefinition> childPropertyDefinitions =
configurationDefinition.getPropertyDefinitions();
- for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
- createDefaultProperty(childPropertyDefinition, defaultConfig);
- }
- return defaultConfig;
+ return org.rhq.core.domain.configuration.ConfigurationUtility
+ .createDefaultConfiguration(configurationDefinition);
}
/**
@@ -107,19 +81,13 @@ public abstract class ConfigurationUtility {
*
* @param configuration the configuration to be normalized
* @param configurationDefinition the configuration definition to normalize the
configuration against
+ *
+ * @deprecated use the utility found in the domain module
-org.rhq.core.domain.configuration.ConfigurationUtility
*/
public static void normalizeConfiguration(@NotNull Configuration configuration,
@Nullable ConfigurationDefinition configurationDefinition) {
- //noinspection ConstantConditions
- if (configuration == null) {
- throw new IllegalArgumentException("Configuration parameter is
null.");
- }
- if (configurationDefinition != null) {
- Map<String, PropertyDefinition> childPropertyDefinitions =
configurationDefinition.getPropertyDefinitions();
- for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
- normalizeProperty(childPropertyDefinition, configuration);
- }
- }
+
org.rhq.core.domain.configuration.ConfigurationUtility.normalizeConfiguration(configuration,
+ configurationDefinition, false, false);
}
/**
@@ -132,191 +100,13 @@ public abstract class ConfigurationUtility {
* @param configurationDefinition the configuration definition to validate the
configuration against
*
* @return a list of messages describing any errors that were found
+ *
+ * @deprecated use the utility found in the domain module
-org.rhq.core.domain.configuration.ConfigurationUtility
*/
@NotNull
public static List<String> validateConfiguration(@NotNull Configuration
configuration,
@Nullable ConfigurationDefinition configurationDefinition) {
- List<String> errorMessages = new ArrayList<String>();
- if (configurationDefinition != null) {
- Map<String, PropertyDefinition> childPropertyDefinitions =
configurationDefinition.getPropertyDefinitions();
- for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
- validateProperty(childPropertyDefinition, configuration, errorMessages);
- }
- }
- return errorMessages;
- }
-
- private static void createDefaultProperty(PropertyDefinition propertyDefinition,
- AbstractPropertyMap parentPropertyMap) {
-
- Property property = null;
-
- if (propertyDefinition instanceof PropertyDefinitionSimple) {
- String defaultValue = ((PropertyDefinitionSimple)
propertyDefinition).getDefaultValue();
- if (defaultValue != null || propertyDefinition.isRequired()) {
- property = new PropertySimple(propertyDefinition.getName(),
defaultValue);
- }
- } else if (propertyDefinition.isRequired()) {
- if (propertyDefinition instanceof PropertyDefinitionMap) {
- property = new PropertyMap(propertyDefinition.getName());
- Map<String, PropertyDefinition> childPropertyDefinitions =
((PropertyDefinitionMap) propertyDefinition)
- .getMap();
- for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
- createDefaultProperty(childPropertyDefinition, (PropertyMap)
property);
- }
- } else if (propertyDefinition instanceof PropertyDefinitionList) {
- property = new PropertyList(propertyDefinition.getName());
- PropertyDefinition listMemberPropertyDefinition =
((PropertyDefinitionList) propertyDefinition)
- .getMemberDefinition();
- if (listMemberPropertyDefinition.isRequired()) {
- if (listMemberPropertyDefinition instanceof PropertyDefinitionMap) {
- // member property is a list-o-maps, create a default child map
if appropriate
- PropertyDefinitionMap listMemberDefinitionMap =
(PropertyDefinitionMap) listMemberPropertyDefinition;
- PropertyMap listMap = new
PropertyMap(listMemberDefinitionMap.getName());
- createDefaultProperty(listMemberDefinitionMap, listMap);
- ((PropertyList) property).add(listMap);
- } else if (listMemberPropertyDefinition instanceof
PropertyDefinitionSimple) {
- // member property is a simple, create a single list entry as its
default if appropriate
- PropertyDefinitionSimple listMemberDefinitionSimple =
(PropertyDefinitionSimple) listMemberPropertyDefinition;
- String defaultValue =
listMemberDefinitionSimple.getDefaultValue();
- if (defaultValue != null ||
listMemberDefinitionSimple.isRequired()) {
- PropertySimple listSimple = new
PropertySimple(listMemberDefinitionSimple.getName(),
- defaultValue);
- ((PropertyList) property).add(listSimple);
- }
- }
- }
- } else if (propertyDefinition instanceof PropertyDefinitionDynamic) {
- // Dynamic property values should simply be stored as simple
- property = new PropertySimple(propertyDefinition.getName(), null);
- } else {
- throw new IllegalStateException("Unsupported PropertyDefinition
subclass: "
- + propertyDefinition.getClass().getName());
- }
- }
-
- if (property != null) {
- parentPropertyMap.put(property);
- }
- }
-
- private static void normalizeProperty(PropertyDefinition propertyDefinition,
AbstractPropertyMap parentPropertyMap) {
- if (parentPropertyMap.getMap().keySet().contains(propertyDefinition.getName()))
// property is already set
- {
- if (propertyDefinition instanceof PropertyDefinitionSimple) {
- PropertySimple propertySimple =
parentPropertyMap.getSimple(propertyDefinition.getName());
- String value = propertySimple.getStringValue();
- if (value != null) {
- if (value.equals("")) {
- // Normalize "" to null, since Oracle will do the same
upon persistence.
- propertySimple.setStringValue(null);
- } else if (value.length() > PropertySimple.MAX_VALUE_LENGTH) {
- // Truncate the value to the max length allowed by the DB
schema.
- propertySimple.setStringValue(value.substring(0,
PropertySimple.MAX_VALUE_LENGTH));
- }
- }
- }
-
- // If the property is a Map, recurse into it and normalize its child
properties.
- else if (propertyDefinition instanceof PropertyDefinitionMap) {
- PropertyMap propertyMap =
parentPropertyMap.getMap(propertyDefinition.getName());
- PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
propertyDefinition;
- normalizePropertyMap(propertyMap, propertyDefinitionMap);
- } else if (propertyDefinition instanceof PropertyDefinitionList) {
- PropertyDefinitionList propertyDefinitionList = (PropertyDefinitionList)
propertyDefinition;
- PropertyDefinition listMemberPropertyDefinition =
propertyDefinitionList.getMemberDefinition();
-
- // If the property is a List of Maps, iterate the list, and recurse into
each Map and verify its child
- // properties.
- if (listMemberPropertyDefinition instanceof PropertyDefinitionMap) {
- PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
listMemberPropertyDefinition;
- PropertyList propertyList =
parentPropertyMap.getList(propertyDefinition.getName());
- for (Property property : propertyList.getList()) {
- PropertyMap propertyMap = (PropertyMap) property;
- normalizePropertyMap(propertyMap, propertyDefinitionMap);
- }
- }
- }
- } else // property is not set yet
- {
- Property property;
- if (propertyDefinition instanceof PropertyDefinitionSimple) {
- property = new PropertySimple(propertyDefinition.getName(), null);
- } else if (propertyDefinition instanceof PropertyDefinitionMap) {
- property = new PropertyMap(propertyDefinition.getName());
- } else if (propertyDefinition instanceof PropertyDefinitionList) {
- property = new PropertyList(propertyDefinition.getName());
- } else if (propertyDefinition instanceof PropertyDefinitionDynamic) {
- // Dynamic property values should simply be stored as simple
- property = new PropertySimple(propertyDefinition.getName(), null);
- } else {
- throw new IllegalStateException("Unsupported PropertyDefinition
subclass: "
- + propertyDefinition.getClass().getName());
- }
-
- parentPropertyMap.put(property);
- }
- }
-
- private static void normalizePropertyMap(AbstractPropertyMap propertyMap,
- PropertyDefinitionMap propertyDefinitionMap) {
- Map<String, PropertyDefinition> childPropertyDefinitions =
propertyDefinitionMap.getMap();
- for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
- normalizeProperty(childPropertyDefinition, propertyMap);
- }
- }
-
- private static void validateProperty(PropertyDefinition propertyDefinition,
AbstractPropertyMap parentPropertyMap,
- List<String> errorMessages) {
- if (parentPropertyMap.getMap().keySet().contains(propertyDefinition.getName()))
// property is already set
- {
- if (propertyDefinition instanceof PropertyDefinitionSimple) {
- PropertySimple propertySimple =
parentPropertyMap.getSimple(propertyDefinition.getName());
- if (propertyDefinition.isRequired() &&
(propertySimple.getStringValue() == null)) {
- errorMessages.add("Required property '" +
propertyDefinition.getName() + "' has a null value in "
- + parentPropertyMap + ".");
- propertySimple.setStringValue("");
- }
- }
-
- // If the property is a Map, recurse into it and validate its child
properties.
- else if (propertyDefinition instanceof PropertyDefinitionMap) {
- PropertyMap propertyMap =
parentPropertyMap.getMap(propertyDefinition.getName());
- PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
propertyDefinition;
- validatePropertyMap(propertyMap, propertyDefinitionMap, errorMessages);
- } else if (propertyDefinition instanceof PropertyDefinitionList) {
- PropertyDefinitionList propertyDefinitionList = (PropertyDefinitionList)
propertyDefinition;
- PropertyDefinition listMemberPropertyDefinition =
propertyDefinitionList.getMemberDefinition();
-
- // If the property is a List of Maps, iterate the list, and recurse into
each Map and validate its child
- // properties.
- if (listMemberPropertyDefinition instanceof PropertyDefinitionMap) {
- PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
listMemberPropertyDefinition;
- PropertyList propertyList =
parentPropertyMap.getList(propertyDefinition.getName());
- for (Property property : propertyList.getList()) {
- PropertyMap propertyMap = (PropertyMap) property;
- validatePropertyMap(propertyMap, propertyDefinitionMap,
errorMessages);
- }
- }
- }
- } else // property is not set yet
- {
- if (propertyDefinition.isRequired()) {
- errorMessages.add("Required property '" +
propertyDefinition.getName() + "' was not set in "
- + parentPropertyMap + ".");
- if (propertyDefinition instanceof PropertyDefinitionSimple) {
- PropertySimple propertySimple = new
PropertySimple(propertyDefinition.getName(), "");
- parentPropertyMap.put(propertySimple);
- }
- }
- }
- }
-
- private static void validatePropertyMap(AbstractPropertyMap propertyMap,
- PropertyDefinitionMap propertyDefinitionMap, List<String> errorMessages) {
- Map<String, PropertyDefinition> childPropertyDefinitions =
propertyDefinitionMap.getMap();
- for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
- validateProperty(childPropertyDefinition, propertyMap, errorMessages);
- }
+ return
org.rhq.core.domain.configuration.ConfigurationUtility.validateConfiguration(configuration,
+ configurationDefinition);
}
}
\ No newline at end of file
diff --git
a/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtilityTest.java
b/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtilityTest.java
deleted file mode 100644
index 3f3dccd..0000000
---
a/modules/core/client-api/src/test/java/org/rhq/core/clientapi/agent/configuration/ConfigurationUtilityTest.java
+++ /dev/null
@@ -1,322 +0,0 @@
-/*
- * 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, 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.clientapi.agent.configuration;
-
-import org.testng.annotations.Test;
-
-import org.rhq.core.domain.configuration.Configuration;
-import org.rhq.core.domain.configuration.PropertyList;
-import org.rhq.core.domain.configuration.PropertyMap;
-import org.rhq.core.domain.configuration.PropertySimple;
-import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
-import org.rhq.core.domain.configuration.definition.PropertyDefinitionList;
-import org.rhq.core.domain.configuration.definition.PropertyDefinitionMap;
-import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
-import org.rhq.core.domain.configuration.definition.PropertySimpleType;
-
-@Test
-public class ConfigurationUtilityTest {
- public void testCreateDefaultNone() {
- // no defaults, no required props - returned config should be empty
- ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
-
- PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
- PropertyDefinitionMap map = new PropertyDefinitionMap("map", null,
false, simple);
- PropertyDefinitionList list = new PropertyDefinitionList("list", null,
false, simple);
-
- configDef.put(simple);
- configDef.put(map);
- configDef.put(list);
-
- Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
- assert config != null;
- assert config.getProperties().size() == 0;
- }
-
- public void testCreateDefaultSimple() {
- ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
-
- PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
- PropertyDefinitionSimple simpleRequired = new
PropertyDefinitionSimple("simpleRequired", null, true,
- PropertySimpleType.STRING);
- PropertyDefinitionSimple simpleDefault = new
PropertyDefinitionSimple("simpleDefault", null, false,
- PropertySimpleType.STRING);
- PropertyDefinitionSimple simpleRequiredDefault = new
PropertyDefinitionSimple("simpleRequiredDefault", null,
- true, PropertySimpleType.STRING);
-
- simpleDefault.setDefaultValue("!!simpleDefaultValue!!");
-
simpleRequiredDefault.setDefaultValue("!!simpleRequiredDefaultValue!!");
-
- configDef.put(simple);
- configDef.put(simpleRequired);
- configDef.put(simpleDefault);
- configDef.put(simpleRequiredDefault);
-
- Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
- assert config != null;
- assert config.getProperties().size() == 3; // simple is not required with no
default - its not in the config
- assert config.getSimple(simple.getName()) == null;
- assert config.getSimpleValue(simpleRequired.getName(), null) == null;
- assert config.getSimpleValue(simpleDefault.getName(),
null).equals("!!simpleDefaultValue!!");
- assert config.getSimpleValue(simpleRequiredDefault.getName(),
null).equals("!!simpleRequiredDefaultValue!!");
- }
-
- public void testCreateDefaultMap() {
- ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
-
- // a=not required, no default; c=not required, has default; d=required, has
default
- PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple b = new PropertyDefinitionSimple("b", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
- b.setDefaultValue("!!bDefaultValue!!");
- c.setDefaultValue("!!cDefaultValue!!");
-
- PropertyDefinitionMap map1 = new PropertyDefinitionMap("map1", null,
false, a, b, c);
- PropertyDefinitionMap mapRequired1 = new
PropertyDefinitionMap("mapRequired1", null, true, a, b, c);
- PropertyDefinitionMap mapRequiredDefault1 = new
PropertyDefinitionMap("mapRequiredDefault1", null, true, a, b,
- c);
-
- configDef.put(map1);
- configDef.put(mapRequired1);
- configDef.put(mapRequiredDefault1);
-
- Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
- assert config != null;
- assert config.getProperties().size() == 2; // map is not required with no default
- its not in the config
- assert config.getMap(map1.getName()) == null;
-
- // the two required maps have the same definitions - a,b,c as above. since a
isn't required with no default, its not there
- PropertyMap mapProp1 = config.getMap(mapRequired1.getName());
- assert mapProp1 != null;
- assert mapProp1.getSimple(a.getName()) == null;
- assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
- assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
-
- mapProp1 = config.getMap(mapRequiredDefault1.getName());
- assert mapProp1 != null;
- assert mapProp1.getSimple(a.getName()) == null;
- assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
- assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
- }
-
- public void testCreateDefaultList() {
- ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
-
- // a=not required, no default, b=required, has default
- PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
- c.setDefaultValue("!!cDefaultValue!!");
-
- PropertyDefinitionList list1 = new PropertyDefinitionList("list1",
null, false, a);
- PropertyDefinitionList listRequired1 = new
PropertyDefinitionList("listRequired1", null, true, a);
- PropertyDefinitionList listRequiredDefault1 = new
PropertyDefinitionList("listRequiredDefault1", null, true, c);
-
- configDef.put(list1);
- configDef.put(listRequired1);
- configDef.put(listRequiredDefault1);
-
- Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
- assert config != null;
- assert config.getProperties().size() == 2; // list is not required with no
default - its not in the config
- assert config.getList(list1.getName()) == null;
-
- PropertyList listProp1 = config.getList(listRequired1.getName());
- assert listProp1 != null;
- assert listProp1.getList().isEmpty(); // has "a" definition, which is
not required and has no default
-
- listProp1 = config.getList(listRequiredDefault1.getName());
- assert listProp1 != null;
- assert listProp1.getList().get(0).getName().equals(c.getName());
- assert ((PropertySimple)
listProp1.getList().get(0)).getStringValue().equals("!!cDefaultValue!!");
- }
-
- public void testCreateDefaultListOMaps() {
- ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
-
- // a=not required, no default; c=not required, has default; d=required, has
default
- PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple b = new PropertyDefinitionSimple("b", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
- b.setDefaultValue("!!bDefaultValue!!");
- c.setDefaultValue("!!cDefaultValue!!");
-
- PropertyDefinitionMap map2 = new PropertyDefinitionMap("map2", null,
false, a, b, c);
- PropertyDefinitionMap mapRequired2 = new
PropertyDefinitionMap("mapRequired2", null, true, a, b, c);
- PropertyDefinitionMap mapRequiredDefault2 = new
PropertyDefinitionMap("mapRequiredDefault2", null, true, a, b,
- c);
-
- PropertyDefinitionList listX = new PropertyDefinitionList("listX",
null, true, map2);
- PropertyDefinitionList listY = new PropertyDefinitionList("listY",
null, true, mapRequired2);
- PropertyDefinitionList listZ = new PropertyDefinitionList("listZ",
null, true, mapRequiredDefault2);
-
- configDef.put(listX);
- configDef.put(listY);
- configDef.put(listZ);
-
- Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
-
- assert config != null;
- assert config.getProperties().size() == 3;
-
- PropertyList listPropTest = config.getList(listX.getName());
- assert listPropTest != null;
- assert listPropTest.getList().isEmpty();
-
- listPropTest = config.getList(listY.getName());
- assert listPropTest != null;
- PropertyMap childMap2 = (PropertyMap) listPropTest.getList().get(0);
-
- listPropTest = config.getList(listZ.getName());
- assert listPropTest != null;
- PropertyMap childMap3 = (PropertyMap) listPropTest.getList().get(0);
-
- assert childMap2.getName().equals(mapRequired2.getName());
- assert childMap3.getName().equals(mapRequiredDefault2.getName());
- }
-
- public void testCreateDefaultAllSimpleMapList() {
- // tests a big config def that has simples, lists, maps - combination of the
other individual tests
- ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
-
- // SETUP SIMPLE
-
- PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
- PropertyDefinitionSimple simpleRequired = new
PropertyDefinitionSimple("simpleRequired", null, true,
- PropertySimpleType.STRING);
- PropertyDefinitionSimple simpleDefault = new
PropertyDefinitionSimple("simpleDefault", null, false,
- PropertySimpleType.STRING);
- PropertyDefinitionSimple simpleRequiredDefault = new
PropertyDefinitionSimple("simpleRequiredDefault", null,
- true, PropertySimpleType.STRING);
-
- simpleDefault.setDefaultValue("!!simpleDefaultValue!!");
-
simpleRequiredDefault.setDefaultValue("!!simpleRequiredDefaultValue!!");
-
- configDef.put(simple);
- configDef.put(simpleRequired);
- configDef.put(simpleDefault);
- configDef.put(simpleRequiredDefault);
-
- // SETUP MAP
-
- // a=not required, no default; c=not required, has default; d=required, has
default
- PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple b = new PropertyDefinitionSimple("b", null,
false, PropertySimpleType.STRING);
- PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
- b.setDefaultValue("!!bDefaultValue!!");
- c.setDefaultValue("!!cDefaultValue!!");
-
- PropertyDefinitionMap map1 = new PropertyDefinitionMap("map1", null,
false, a, b, c);
- PropertyDefinitionMap mapRequired1 = new
PropertyDefinitionMap("mapRequired1", null, true, a, b, c);
- PropertyDefinitionMap mapRequiredDefault1 = new
PropertyDefinitionMap("mapRequiredDefault1", null, true, a, b,
- c);
-
- configDef.put(map1);
- configDef.put(mapRequired1);
- configDef.put(mapRequiredDefault1);
-
- // SETUP LIST
-
- PropertyDefinitionList list1 = new PropertyDefinitionList("list1",
null, false, a);
- PropertyDefinitionList listRequired1 = new
PropertyDefinitionList("listRequired1", null, true, a);
- PropertyDefinitionList listRequiredDefault1 = new
PropertyDefinitionList("listRequiredDefault1", null, true, c);
-
- configDef.put(list1);
- configDef.put(listRequired1);
- configDef.put(listRequiredDefault1);
-
- // SETUP LIST-O-MAPS
-
- PropertyDefinitionMap map2 = new PropertyDefinitionMap("map2", null,
false, a, b, c);
- PropertyDefinitionMap mapRequired2 = new
PropertyDefinitionMap("mapRequired2", null, true, a, b, c);
- PropertyDefinitionMap mapRequiredDefault2 = new
PropertyDefinitionMap("mapRequiredDefault2", null, true, a, b,
- c);
-
- PropertyDefinitionList listX = new PropertyDefinitionList("listX",
null, true, map2);
- PropertyDefinitionList listY = new PropertyDefinitionList("listY",
null, true, mapRequired2);
- PropertyDefinitionList listZ = new PropertyDefinitionList("listZ",
null, true, mapRequiredDefault2);
-
- configDef.put(listX);
- configDef.put(listY);
- configDef.put(listZ);
-
- // get the default config
- Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
- assert config != null;
- assert config.getProperties().size() == 10;
-
- // ASSERT SIMPLE
-
- assert config.getSimple(simple.getName()) == null;
- assert config.getSimpleValue(simpleRequired.getName(), null) == null;
- assert config.getSimpleValue(simpleDefault.getName(),
null).equals("!!simpleDefaultValue!!");
- assert config.getSimpleValue(simpleRequiredDefault.getName(),
null).equals("!!simpleRequiredDefaultValue!!");
-
- // ASSERT MAP
-
- assert config.getMap(map1.getName()) == null;
-
- // the two required maps have the same definitions - a,b,c as above. since a
isn't required with no default, its not there
- PropertyMap mapProp1 = config.getMap(mapRequired1.getName());
- assert mapProp1 != null;
- assert mapProp1.getSimple(a.getName()) == null;
- assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
- assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
-
- mapProp1 = config.getMap(mapRequiredDefault1.getName());
- assert mapProp1 != null;
- assert mapProp1.getSimple(a.getName()) == null;
- assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
- assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
-
- // ASSERT LIST
-
- assert config.getList(list1.getName()) == null;
-
- PropertyList listProp1 = config.getList(listRequired1.getName());
- assert listProp1 != null;
- assert listProp1.getList().isEmpty(); // has "a" definition, which is
not required and has no default
-
- listProp1 = config.getList(listRequiredDefault1.getName());
- assert listProp1 != null;
- assert listProp1.getList().get(0).getName().equals(c.getName());
- assert ((PropertySimple)
listProp1.getList().get(0)).getStringValue().equals("!!cDefaultValue!!");
-
- // ASSERT LIST-O-MAPS
-
- PropertyList listPropTest = config.getList(listX.getName());
- assert listPropTest != null;
- assert listPropTest.getList().isEmpty();
-
- listPropTest = config.getList(listY.getName());
- assert listPropTest != null;
- PropertyMap childMap2 = (PropertyMap) listPropTest.getList().get(0);
-
- listPropTest = config.getList(listZ.getName());
- assert listPropTest != null;
- PropertyMap childMap3 = (PropertyMap) listPropTest.getList().get(0);
-
- assert childMap2.getName().equals(mapRequired2.getName());
- assert childMap3.getName().equals(mapRequiredDefault2.getName());
- }
-}
diff --git
a/modules/core/domain/src/main/java/org/rhq/core/domain/configuration/ConfigurationUtility.java
b/modules/core/domain/src/main/java/org/rhq/core/domain/configuration/ConfigurationUtility.java
new file mode 100644
index 0000000..1c32526
--- /dev/null
+++
b/modules/core/domain/src/main/java/org/rhq/core/domain/configuration/ConfigurationUtility.java
@@ -0,0 +1,334 @@
+/*
+ * 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.configuration;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+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.configuration.definition.PropertyDefinitionList;
+import org.rhq.core.domain.configuration.definition.PropertyDefinitionMap;
+import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
+
+/**
+ * Utility methods for working with {@link Configuration}s.
+ *
+ * @author Ian Springer
+ */
+public abstract class ConfigurationUtility {
+
+ /**
+ * This will populate the given configuration definition with a default template.
+ * A default template will only be created if one or more properties are required
+ * or have default values. If no property definition is required or has a default
value,
+ * the default template will remain <code>null</code> in the given config
definition.
+ *
+ * Note that if the given configuration definition already has a default template
defined
+ * for it, this method is a no-op and will return immediately.
+ *
+ * @param configDef the configuration definition whose default template is to be
created and set
+ */
+ public static void initializeDefaultTemplate(ConfigurationDefinition configDef) {
+ ConfigurationTemplate defaultTemplate = configDef.getDefaultTemplate();
+ if (defaultTemplate == null) {
+ Configuration defaultConfig = createDefaultConfiguration(configDef);
+ // not everything should have a default template - only stuff that has
default values
+ if (!defaultConfig.getProperties().isEmpty()) {
+ defaultTemplate = new
ConfigurationTemplate(ConfigurationTemplate.DEFAULT_TEMPLATE_NAME,
+ ConfigurationTemplate.DEFAULT_TEMPLATE_NAME);
+ defaultTemplate.setDefault(true);
+ defaultTemplate.setConfiguration(defaultConfig);
+ configDef.putTemplate(defaultTemplate);
+ }
+ }
+ return;
+ }
+
+ /**
+ * Given a configuration definition, this will build and return a "default
configuration" that
+ * can be validated with the definition. All required properties are set and all
properties
+ * that define a default value are also set. If a required property does not have a
default
+ * value defined in the definition, the property value will be set to
<code>null</code>.
+ *
+ * Use this to help create the definition's default template.
+ *
+ * @param configurationDefinition the configuration definition whose default
configuration is to be created
+ * @return configuration the default configuration
+ */
+ public static Configuration createDefaultConfiguration(ConfigurationDefinition
configurationDefinition) {
+ if (configurationDefinition == null) {
+ throw new IllegalArgumentException("configurationDefinition ==
null");
+ }
+ Configuration defaultConfig = new Configuration();
+ Map<String, PropertyDefinition> childPropertyDefinitions =
configurationDefinition.getPropertyDefinitions();
+ for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
+ createDefaultProperty(childPropertyDefinition, defaultConfig);
+ }
+ return defaultConfig;
+ }
+
+ /**
+ * "Normalize" the given configuration according to the given configuration
definition. That is, for any optional
+ * properties that are not defined in the top-level configuration Map or any
sub-Maps, set them.
+ * Map properties are set with an empty Map, and List properties with an empty List.
By default, simple
+ * properties that are missing will be created and set with a null value. However, if
normalizeRequiredDefaults is true,
+ * and a simple property is required with a default, this will set the required
property to that default value.
+ * If normalizeOptionalDefaults is true, and a simple property is not required but
has a default, this will set the
+ * optional property to that default. If a simple property does not have a default
defined, no matter what those
+ * "normalize" booleans are, the simple property will still be set to null
since this method won't know what value
+ * to set it to anyway.
+ *
+ * @param configuration the configuration to be normalized (must not be
null)
+ * @param configurationDefinition the configuration definition to normalize the
configuration against (may be null)
+ * @param normalizeRequiredDefaults if true, and a property is required, its default
will be set as that property's value
+ * @param normalizeOptionalDefaults if true, and a property is optional, its default
will be set as that property's value
+ */
+ public static void normalizeConfiguration(Configuration configuration,
+ ConfigurationDefinition configurationDefinition, boolean
normalizeRequiredDefaults,
+ boolean normalizeOptionalDefaults) {
+ if (configuration == null) {
+ throw new IllegalArgumentException("Configuration parameter is
null.");
+ }
+ if (configurationDefinition != null) {
+ Map<String, PropertyDefinition> childPropertyDefinitions =
configurationDefinition.getPropertyDefinitions();
+ for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
+ normalizeProperty(childPropertyDefinition, configuration,
normalizeRequiredDefaults,
+ normalizeOptionalDefaults);
+ }
+ }
+ }
+
+ /**
+ * Validate the given configuration according to the given configuration definition.
That is, check that any
+ * required properties in the top-level configuration Map or any sub-Maps, are
defined and, in the case of simple
+ * properties, check that they have a non-null value. A list of messages describing
any errors that were found is
+ * returned. Additionally, any undefined or null simple properties will be assigned a
value of "".
+ *
+ * @param configuration the configuration to be validated (must not be
null)
+ * @param configurationDefinition the configuration definition to validate the
configuration against (may be null)
+ *
+ * @return a list of messages describing any errors that were found - will be empty
if there are no messagse
+ */
+ public static List<String> validateConfiguration(Configuration configuration,
+ ConfigurationDefinition configurationDefinition) {
+ List<String> errorMessages = new ArrayList<String>();
+ if (configurationDefinition != null) {
+ Map<String, PropertyDefinition> childPropertyDefinitions =
configurationDefinition.getPropertyDefinitions();
+ for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
+ validateProperty(childPropertyDefinition, configuration, errorMessages);
+ }
+ }
+ return errorMessages;
+ }
+
+ private static void createDefaultProperty(PropertyDefinition propertyDefinition,
+ AbstractPropertyMap parentPropertyMap) {
+
+ Property property = null;
+
+ if (propertyDefinition instanceof PropertyDefinitionSimple) {
+ String defaultValue = ((PropertyDefinitionSimple)
propertyDefinition).getDefaultValue();
+ if (defaultValue != null || propertyDefinition.isRequired()) {
+ property = new PropertySimple(propertyDefinition.getName(),
defaultValue);
+ }
+ } else if (propertyDefinition.isRequired()) {
+ if (propertyDefinition instanceof PropertyDefinitionMap) {
+ property = new PropertyMap(propertyDefinition.getName());
+ Map<String, PropertyDefinition> childPropertyDefinitions =
((PropertyDefinitionMap) propertyDefinition)
+ .getMap();
+ for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
+ createDefaultProperty(childPropertyDefinition, (PropertyMap)
property);
+ }
+ } else if (propertyDefinition instanceof PropertyDefinitionList) {
+ property = new PropertyList(propertyDefinition.getName());
+ PropertyDefinition listMemberPropertyDefinition =
((PropertyDefinitionList) propertyDefinition)
+ .getMemberDefinition();
+ if (listMemberPropertyDefinition.isRequired()) {
+ if (listMemberPropertyDefinition instanceof PropertyDefinitionMap) {
+ // member property is a list-o-maps, create a default child map
if appropriate
+ PropertyDefinitionMap listMemberDefinitionMap =
(PropertyDefinitionMap) listMemberPropertyDefinition;
+ PropertyMap listMap = new
PropertyMap(listMemberDefinitionMap.getName());
+ createDefaultProperty(listMemberDefinitionMap, listMap);
+ ((PropertyList) property).add(listMap);
+ } else if (listMemberPropertyDefinition instanceof
PropertyDefinitionSimple) {
+ // member property is a simple, create a single list entry as its
default if appropriate
+ PropertyDefinitionSimple listMemberDefinitionSimple =
(PropertyDefinitionSimple) listMemberPropertyDefinition;
+ String defaultValue =
listMemberDefinitionSimple.getDefaultValue();
+ if (defaultValue != null ||
listMemberDefinitionSimple.isRequired()) {
+ PropertySimple listSimple = new
PropertySimple(listMemberDefinitionSimple.getName(),
+ defaultValue);
+ ((PropertyList) property).add(listSimple);
+ }
+ }
+ }
+ } else if (propertyDefinition instanceof PropertyDefinitionDynamic) {
+ // Dynamic property values should simply be stored as simple
+ property = new PropertySimple(propertyDefinition.getName(), null);
+ } else {
+ throw new IllegalStateException("Unsupported PropertyDefinition
subclass: "
+ + propertyDefinition.getClass().getName());
+ }
+ }
+
+ if (property != null) {
+ parentPropertyMap.put(property);
+ }
+ }
+
+ private static void normalizeProperty(PropertyDefinition propertyDefinition,
AbstractPropertyMap parentPropertyMap,
+ boolean normalizeRequiredDefaults, boolean normalizeOptionalDefaults) {
+ if (parentPropertyMap.getMap().keySet().contains(propertyDefinition.getName()))
// property is already set
+ {
+ if (propertyDefinition instanceof PropertyDefinitionSimple) {
+ PropertySimple propertySimple =
parentPropertyMap.getSimple(propertyDefinition.getName());
+ String value = propertySimple.getStringValue();
+ if (value != null) {
+ if (value.equals("")) {
+ // Normalize "" to null, since Oracle will do the same
upon persistence.
+ propertySimple.setStringValue(null);
+ } else if (value.length() > PropertySimple.MAX_VALUE_LENGTH) {
+ // Truncate the value to the max length allowed by the DB
schema.
+ propertySimple.setStringValue(value.substring(0,
PropertySimple.MAX_VALUE_LENGTH));
+ }
+ }
+ }
+
+ // If property is a Map, recurse into it and normalize its child properties.
+ else if (propertyDefinition instanceof PropertyDefinitionMap) {
+ PropertyMap propertyMap =
parentPropertyMap.getMap(propertyDefinition.getName());
+ PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
propertyDefinition;
+ normalizePropertyMap(propertyMap, propertyDefinitionMap, false, false);
// TODO do we want to pass normalizeRequired/OptionalDefaults?
+ } else if (propertyDefinition instanceof PropertyDefinitionList) {
+ PropertyDefinitionList propertyDefinitionList = (PropertyDefinitionList)
propertyDefinition;
+ PropertyDefinition listMemberPropertyDefinition =
propertyDefinitionList.getMemberDefinition();
+
+ // If property is a List of Maps, iterate the list and recurse into each
Map and normalize its child properties.
+ if (listMemberPropertyDefinition instanceof PropertyDefinitionMap) {
+ PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
listMemberPropertyDefinition;
+ PropertyList propertyList =
parentPropertyMap.getList(propertyDefinition.getName());
+ for (Property property : propertyList.getList()) {
+ PropertyMap propertyMap = (PropertyMap) property;
+ normalizePropertyMap(propertyMap, propertyDefinitionMap, false,
false); // TODO do we want to pass normalizeRequired/OptionalDefaults?
+ }
+ }
+ }
+ } else // property is not set yet
+ {
+ Property property;
+ if (propertyDefinition instanceof PropertyDefinitionSimple) {
+ String value = null;
+ if (normalizeRequiredDefaults || normalizeOptionalDefaults) {
+ if (propertyDefinition.isRequired()) {
+ if (normalizeRequiredDefaults) {
+ value = ((PropertyDefinitionSimple)
propertyDefinition).getDefaultValue();
+ }
+ } else {
+ if (normalizeOptionalDefaults) {
+ value = ((PropertyDefinitionSimple)
propertyDefinition).getDefaultValue();
+ }
+ }
+ }
+ property = new PropertySimple(propertyDefinition.getName(), value);
+ } else if (propertyDefinition instanceof PropertyDefinitionMap) {
+ property = new PropertyMap(propertyDefinition.getName());
+ } else if (propertyDefinition instanceof PropertyDefinitionList) {
+ property = new PropertyList(propertyDefinition.getName());
+ } else if (propertyDefinition instanceof PropertyDefinitionDynamic) {
+ // Dynamic property values should simply be stored as simple
+ property = new PropertySimple(propertyDefinition.getName(), null);
+ } else {
+ throw new IllegalStateException("Unsupported PropertyDefinition
subclass: "
+ + propertyDefinition.getClass().getName());
+ }
+
+ parentPropertyMap.put(property);
+ }
+ }
+
+ private static void normalizePropertyMap(AbstractPropertyMap propertyMap,
+ PropertyDefinitionMap propertyDefinitionMap, boolean normalizeRequiredDefaults,
+ boolean normalizeOptionalDefaults) {
+ Map<String, PropertyDefinition> childPropertyDefinitions =
propertyDefinitionMap.getMap();
+ for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
+ normalizeProperty(childPropertyDefinition, propertyMap,
normalizeRequiredDefaults,
+ normalizeOptionalDefaults);
+ }
+ }
+
+ private static void validateProperty(PropertyDefinition propertyDefinition,
AbstractPropertyMap parentPropertyMap,
+ List<String> errorMessages) {
+ if (parentPropertyMap.getMap().keySet().contains(propertyDefinition.getName()))
// property is already set
+ {
+ if (propertyDefinition instanceof PropertyDefinitionSimple) {
+ PropertySimple propertySimple =
parentPropertyMap.getSimple(propertyDefinition.getName());
+ if (propertyDefinition.isRequired() &&
(propertySimple.getStringValue() == null)) {
+ errorMessages.add("Required property '" +
propertyDefinition.getName() + "' has a null value in "
+ + parentPropertyMap + ".");
+ propertySimple.setStringValue("");
+ }
+ }
+
+ // If the property is a Map, recurse into it and validate its child
properties.
+ else if (propertyDefinition instanceof PropertyDefinitionMap) {
+ PropertyMap propertyMap =
parentPropertyMap.getMap(propertyDefinition.getName());
+ PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
propertyDefinition;
+ validatePropertyMap(propertyMap, propertyDefinitionMap, errorMessages);
+ } else if (propertyDefinition instanceof PropertyDefinitionList) {
+ PropertyDefinitionList propertyDefinitionList = (PropertyDefinitionList)
propertyDefinition;
+ PropertyDefinition listMemberPropertyDefinition =
propertyDefinitionList.getMemberDefinition();
+
+ // If the property is a List of Maps, iterate the list, and recurse into
each Map and validate its child
+ // properties.
+ if (listMemberPropertyDefinition instanceof PropertyDefinitionMap) {
+ PropertyDefinitionMap propertyDefinitionMap = (PropertyDefinitionMap)
listMemberPropertyDefinition;
+ PropertyList propertyList =
parentPropertyMap.getList(propertyDefinition.getName());
+ for (Property property : propertyList.getList()) {
+ PropertyMap propertyMap = (PropertyMap) property;
+ validatePropertyMap(propertyMap, propertyDefinitionMap,
errorMessages);
+ }
+ }
+ }
+ } else // property is not set yet
+ {
+ if (propertyDefinition.isRequired()) {
+ errorMessages.add("Required property '" +
propertyDefinition.getName() + "' was not set in "
+ + parentPropertyMap + ".");
+ if (propertyDefinition instanceof PropertyDefinitionSimple) {
+ PropertySimple propertySimple = new
PropertySimple(propertyDefinition.getName(), "");
+ parentPropertyMap.put(propertySimple);
+ }
+ }
+ }
+ }
+
+ private static void validatePropertyMap(AbstractPropertyMap propertyMap,
+ PropertyDefinitionMap propertyDefinitionMap, List<String> errorMessages) {
+ Map<String, PropertyDefinition> childPropertyDefinitions =
propertyDefinitionMap.getMap();
+ for (PropertyDefinition childPropertyDefinition :
childPropertyDefinitions.values()) {
+ validateProperty(childPropertyDefinition, propertyMap, errorMessages);
+ }
+ }
+}
\ No newline at end of file
diff --git
a/modules/core/domain/src/test/java/org/rhq/core/domain/configuration/ConfigurationUtilityTest.java
b/modules/core/domain/src/test/java/org/rhq/core/domain/configuration/ConfigurationUtilityTest.java
new file mode 100644
index 0000000..7c49296
--- /dev/null
+++
b/modules/core/domain/src/test/java/org/rhq/core/domain/configuration/ConfigurationUtilityTest.java
@@ -0,0 +1,363 @@
+/*
+ * 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, 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.configuration;
+
+import org.testng.annotations.Test;
+
+import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
+import org.rhq.core.domain.configuration.definition.PropertyDefinitionList;
+import org.rhq.core.domain.configuration.definition.PropertyDefinitionMap;
+import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
+import org.rhq.core.domain.configuration.definition.PropertySimpleType;
+
+@Test
+public class ConfigurationUtilityTest {
+ public void testNormalizeDefaultSimple() {
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleRequired = new
PropertyDefinitionSimple("simpleRequired", null, true,
+ PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleDefault = new
PropertyDefinitionSimple("simpleDefault", null, false,
+ PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleRequiredDefault = new
PropertyDefinitionSimple("simpleRequiredDefault", null,
+ true, PropertySimpleType.STRING);
+
+ simpleDefault.setDefaultValue("!!simpleDefaultValue!!");
+
simpleRequiredDefault.setDefaultValue("!!simpleRequiredDefaultValue!!");
+
+ configDef.put(simple);
+ configDef.put(simpleRequired);
+ configDef.put(simpleDefault);
+ configDef.put(simpleRequiredDefault);
+
+ // test normalization
+ Configuration config = new Configuration();
+ ConfigurationUtility.normalizeConfiguration(config, configDef, false, false);
+ assert config.getProperties().size() == 4;
+ assert config.getSimpleValue(simple.getName(), null) == null;
+ assert config.getSimpleValue(simpleRequired.getName(), null) == null;
+ assert config.getSimpleValue(simpleDefault.getName(), null) == null;
+ assert config.getSimpleValue(simpleRequiredDefault.getName(), null) == null;
+
+ config = new Configuration();
+ ConfigurationUtility.normalizeConfiguration(config, configDef, true, false);
+ assert config.getProperties().size() == 4;
+ assert config.getSimpleValue(simple.getName(), null) == null;
+ assert config.getSimpleValue(simpleRequired.getName(), null) == null; // there is
no default, so nothing to set
+ assert config.getSimpleValue(simpleDefault.getName(), null) == null;
+ assert config.getSimpleValue(simpleRequiredDefault.getName(),
null).equals("!!simpleRequiredDefaultValue!!");
+
+ config = new Configuration();
+ ConfigurationUtility.normalizeConfiguration(config, configDef, false, true);
+ assert config.getProperties().size() == 4;
+ assert config.getSimpleValue(simple.getName(), null) == null; // there is no
default, so nothing to set
+ assert config.getSimpleValue(simpleRequired.getName(), null) == null;
+ assert config.getSimpleValue(simpleDefault.getName(),
null).equals("!!simpleDefaultValue!!");
+ assert config.getSimpleValue(simpleRequiredDefault.getName(), null) == null;
+ }
+
+ public void testCreateDefaultNone() {
+ // no defaults, no required props - returned config should be empty
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
+ PropertyDefinitionMap map = new PropertyDefinitionMap("map", null,
false, simple);
+ PropertyDefinitionList list = new PropertyDefinitionList("list", null,
false, simple);
+
+ configDef.put(simple);
+ configDef.put(map);
+ configDef.put(list);
+
+ Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
+ assert config != null;
+ assert config.getProperties().size() == 0;
+ }
+
+ public void testCreateDefaultSimple() {
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleRequired = new
PropertyDefinitionSimple("simpleRequired", null, true,
+ PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleDefault = new
PropertyDefinitionSimple("simpleDefault", null, false,
+ PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleRequiredDefault = new
PropertyDefinitionSimple("simpleRequiredDefault", null,
+ true, PropertySimpleType.STRING);
+
+ simpleDefault.setDefaultValue("!!simpleDefaultValue!!");
+
simpleRequiredDefault.setDefaultValue("!!simpleRequiredDefaultValue!!");
+
+ configDef.put(simple);
+ configDef.put(simpleRequired);
+ configDef.put(simpleDefault);
+ configDef.put(simpleRequiredDefault);
+
+ Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
+ assert config != null;
+ assert config.getProperties().size() == 3; // simple is not required with no
default - its not in the config
+ assert config.getSimple(simple.getName()) == null;
+ assert config.getSimpleValue(simpleRequired.getName(), null) == null;
+ assert config.getSimpleValue(simpleDefault.getName(),
null).equals("!!simpleDefaultValue!!");
+ assert config.getSimpleValue(simpleRequiredDefault.getName(),
null).equals("!!simpleRequiredDefaultValue!!");
+ }
+
+ public void testCreateDefaultMap() {
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ // a=not required, no default; c=not required, has default; d=required, has
default
+ PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple b = new PropertyDefinitionSimple("b", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
+ b.setDefaultValue("!!bDefaultValue!!");
+ c.setDefaultValue("!!cDefaultValue!!");
+
+ PropertyDefinitionMap map1 = new PropertyDefinitionMap("map1", null,
false, a, b, c);
+ PropertyDefinitionMap mapRequired1 = new
PropertyDefinitionMap("mapRequired1", null, true, a, b, c);
+ PropertyDefinitionMap mapRequiredDefault1 = new
PropertyDefinitionMap("mapRequiredDefault1", null, true, a, b,
+ c);
+
+ configDef.put(map1);
+ configDef.put(mapRequired1);
+ configDef.put(mapRequiredDefault1);
+
+ Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
+ assert config != null;
+ assert config.getProperties().size() == 2; // map is not required with no default
- its not in the config
+ assert config.getMap(map1.getName()) == null;
+
+ // the two required maps have the same definitions - a,b,c as above. since a
isn't required with no default, its not there
+ PropertyMap mapProp1 = config.getMap(mapRequired1.getName());
+ assert mapProp1 != null;
+ assert mapProp1.getSimple(a.getName()) == null;
+ assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
+ assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
+
+ mapProp1 = config.getMap(mapRequiredDefault1.getName());
+ assert mapProp1 != null;
+ assert mapProp1.getSimple(a.getName()) == null;
+ assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
+ assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
+ }
+
+ public void testCreateDefaultList() {
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ // a=not required, no default, b=required, has default
+ PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
+ c.setDefaultValue("!!cDefaultValue!!");
+
+ PropertyDefinitionList list1 = new PropertyDefinitionList("list1",
null, false, a);
+ PropertyDefinitionList listRequired1 = new
PropertyDefinitionList("listRequired1", null, true, a);
+ PropertyDefinitionList listRequiredDefault1 = new
PropertyDefinitionList("listRequiredDefault1", null, true, c);
+
+ configDef.put(list1);
+ configDef.put(listRequired1);
+ configDef.put(listRequiredDefault1);
+
+ Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
+ assert config != null;
+ assert config.getProperties().size() == 2; // list is not required with no
default - its not in the config
+ assert config.getList(list1.getName()) == null;
+
+ PropertyList listProp1 = config.getList(listRequired1.getName());
+ assert listProp1 != null;
+ assert listProp1.getList().isEmpty(); // has "a" definition, which is
not required and has no default
+
+ listProp1 = config.getList(listRequiredDefault1.getName());
+ assert listProp1 != null;
+ assert listProp1.getList().get(0).getName().equals(c.getName());
+ assert ((PropertySimple)
listProp1.getList().get(0)).getStringValue().equals("!!cDefaultValue!!");
+ }
+
+ public void testCreateDefaultListOMaps() {
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ // a=not required, no default; c=not required, has default; d=required, has
default
+ PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple b = new PropertyDefinitionSimple("b", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
+ b.setDefaultValue("!!bDefaultValue!!");
+ c.setDefaultValue("!!cDefaultValue!!");
+
+ PropertyDefinitionMap map2 = new PropertyDefinitionMap("map2", null,
false, a, b, c);
+ PropertyDefinitionMap mapRequired2 = new
PropertyDefinitionMap("mapRequired2", null, true, a, b, c);
+ PropertyDefinitionMap mapRequiredDefault2 = new
PropertyDefinitionMap("mapRequiredDefault2", null, true, a, b,
+ c);
+
+ PropertyDefinitionList listX = new PropertyDefinitionList("listX",
null, true, map2);
+ PropertyDefinitionList listY = new PropertyDefinitionList("listY",
null, true, mapRequired2);
+ PropertyDefinitionList listZ = new PropertyDefinitionList("listZ",
null, true, mapRequiredDefault2);
+
+ configDef.put(listX);
+ configDef.put(listY);
+ configDef.put(listZ);
+
+ Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
+
+ assert config != null;
+ assert config.getProperties().size() == 3;
+
+ PropertyList listPropTest = config.getList(listX.getName());
+ assert listPropTest != null;
+ assert listPropTest.getList().isEmpty();
+
+ listPropTest = config.getList(listY.getName());
+ assert listPropTest != null;
+ PropertyMap childMap2 = (PropertyMap) listPropTest.getList().get(0);
+
+ listPropTest = config.getList(listZ.getName());
+ assert listPropTest != null;
+ PropertyMap childMap3 = (PropertyMap) listPropTest.getList().get(0);
+
+ assert childMap2.getName().equals(mapRequired2.getName());
+ assert childMap3.getName().equals(mapRequiredDefault2.getName());
+ }
+
+ public void testCreateDefaultAllSimpleMapList() {
+ // tests a big config def that has simples, lists, maps - combination of the
other individual tests
+ ConfigurationDefinition configDef = new ConfigurationDefinition("foo",
null);
+
+ // SETUP SIMPLE
+
+ PropertyDefinitionSimple simple = new
PropertyDefinitionSimple("simple", null, false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleRequired = new
PropertyDefinitionSimple("simpleRequired", null, true,
+ PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleDefault = new
PropertyDefinitionSimple("simpleDefault", null, false,
+ PropertySimpleType.STRING);
+ PropertyDefinitionSimple simpleRequiredDefault = new
PropertyDefinitionSimple("simpleRequiredDefault", null,
+ true, PropertySimpleType.STRING);
+
+ simpleDefault.setDefaultValue("!!simpleDefaultValue!!");
+
simpleRequiredDefault.setDefaultValue("!!simpleRequiredDefaultValue!!");
+
+ configDef.put(simple);
+ configDef.put(simpleRequired);
+ configDef.put(simpleDefault);
+ configDef.put(simpleRequiredDefault);
+
+ // SETUP MAP
+
+ // a=not required, no default; c=not required, has default; d=required, has
default
+ PropertyDefinitionSimple a = new PropertyDefinitionSimple("a", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple b = new PropertyDefinitionSimple("b", null,
false, PropertySimpleType.STRING);
+ PropertyDefinitionSimple c = new PropertyDefinitionSimple("c", null,
true, PropertySimpleType.STRING);
+ b.setDefaultValue("!!bDefaultValue!!");
+ c.setDefaultValue("!!cDefaultValue!!");
+
+ PropertyDefinitionMap map1 = new PropertyDefinitionMap("map1", null,
false, a, b, c);
+ PropertyDefinitionMap mapRequired1 = new
PropertyDefinitionMap("mapRequired1", null, true, a, b, c);
+ PropertyDefinitionMap mapRequiredDefault1 = new
PropertyDefinitionMap("mapRequiredDefault1", null, true, a, b,
+ c);
+
+ configDef.put(map1);
+ configDef.put(mapRequired1);
+ configDef.put(mapRequiredDefault1);
+
+ // SETUP LIST
+
+ PropertyDefinitionList list1 = new PropertyDefinitionList("list1",
null, false, a);
+ PropertyDefinitionList listRequired1 = new
PropertyDefinitionList("listRequired1", null, true, a);
+ PropertyDefinitionList listRequiredDefault1 = new
PropertyDefinitionList("listRequiredDefault1", null, true, c);
+
+ configDef.put(list1);
+ configDef.put(listRequired1);
+ configDef.put(listRequiredDefault1);
+
+ // SETUP LIST-O-MAPS
+
+ PropertyDefinitionMap map2 = new PropertyDefinitionMap("map2", null,
false, a, b, c);
+ PropertyDefinitionMap mapRequired2 = new
PropertyDefinitionMap("mapRequired2", null, true, a, b, c);
+ PropertyDefinitionMap mapRequiredDefault2 = new
PropertyDefinitionMap("mapRequiredDefault2", null, true, a, b,
+ c);
+
+ PropertyDefinitionList listX = new PropertyDefinitionList("listX",
null, true, map2);
+ PropertyDefinitionList listY = new PropertyDefinitionList("listY",
null, true, mapRequired2);
+ PropertyDefinitionList listZ = new PropertyDefinitionList("listZ",
null, true, mapRequiredDefault2);
+
+ configDef.put(listX);
+ configDef.put(listY);
+ configDef.put(listZ);
+
+ // get the default config
+ Configuration config =
ConfigurationUtility.createDefaultConfiguration(configDef);
+ assert config != null;
+ assert config.getProperties().size() == 10;
+
+ // ASSERT SIMPLE
+
+ assert config.getSimple(simple.getName()) == null;
+ assert config.getSimpleValue(simpleRequired.getName(), null) == null;
+ assert config.getSimpleValue(simpleDefault.getName(),
null).equals("!!simpleDefaultValue!!");
+ assert config.getSimpleValue(simpleRequiredDefault.getName(),
null).equals("!!simpleRequiredDefaultValue!!");
+
+ // ASSERT MAP
+
+ assert config.getMap(map1.getName()) == null;
+
+ // the two required maps have the same definitions - a,b,c as above. since a
isn't required with no default, its not there
+ PropertyMap mapProp1 = config.getMap(mapRequired1.getName());
+ assert mapProp1 != null;
+ assert mapProp1.getSimple(a.getName()) == null;
+ assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
+ assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
+
+ mapProp1 = config.getMap(mapRequiredDefault1.getName());
+ assert mapProp1 != null;
+ assert mapProp1.getSimple(a.getName()) == null;
+ assert mapProp1.getSimpleValue(b.getName(),
null).equals("!!bDefaultValue!!");
+ assert mapProp1.getSimpleValue(c.getName(),
null).equals("!!cDefaultValue!!");
+
+ // ASSERT LIST
+
+ assert config.getList(list1.getName()) == null;
+
+ PropertyList listProp1 = config.getList(listRequired1.getName());
+ assert listProp1 != null;
+ assert listProp1.getList().isEmpty(); // has "a" definition, which is
not required and has no default
+
+ listProp1 = config.getList(listRequiredDefault1.getName());
+ assert listProp1 != null;
+ assert listProp1.getList().get(0).getName().equals(c.getName());
+ assert ((PropertySimple)
listProp1.getList().get(0)).getStringValue().equals("!!cDefaultValue!!");
+
+ // ASSERT LIST-O-MAPS
+
+ PropertyList listPropTest = config.getList(listX.getName());
+ assert listPropTest != null;
+ assert listPropTest.getList().isEmpty();
+
+ listPropTest = config.getList(listY.getName());
+ assert listPropTest != null;
+ PropertyMap childMap2 = (PropertyMap) listPropTest.getList().get(0);
+
+ listPropTest = config.getList(listZ.getName());
+ assert listPropTest != null;
+ PropertyMap childMap3 = (PropertyMap) listPropTest.getList().get(0);
+
+ assert childMap2.getName().equals(mapRequired2.getName());
+ assert childMap3.getName().equals(mapRequiredDefault2.getName());
+ }
+}
diff --git
a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/bundle/deploy/GetDeploymentConfigStep.java
b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/bundle/deploy/GetDeploymentConfigStep.java
index 7fc0363..c295b74 100644
---
a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/bundle/deploy/GetDeploymentConfigStep.java
+++
b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/bundle/deploy/GetDeploymentConfigStep.java
@@ -19,13 +19,14 @@
package org.rhq.enterprise.gui.coregui.client.bundle.deploy;
import com.smartgwt.client.widgets.Canvas;
+import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.layout.VLayout;
import org.rhq.core.domain.bundle.BundleDeployment;
import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.configuration.ConfigurationUtility;
import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
import org.rhq.core.domain.configuration.definition.ConfigurationTemplate;
-import org.rhq.enterprise.gui.coregui.client.components.HeaderLabel;
import
org.rhq.enterprise.gui.coregui.client.components.configuration.ConfigurationEditor;
import org.rhq.enterprise.gui.coregui.client.components.wizard.AbstractWizardStep;
import org.rhq.enterprise.gui.coregui.client.util.selenium.Locatable;
@@ -58,8 +59,11 @@ public class GetDeploymentConfigStep extends AbstractWizardStep {
// This has started behaving badly. Instead of moving ahead let's
give them a message
// and a chance to go back to the previous screen.
// this.wizard.getView().incrementStep();
- HeaderLabel label = new
HeaderLabel(MSG.view_bundle_deployWizard_getConfigSkip());
+ HTMLFlow label = new
HTMLFlow(MSG.view_bundle_deployWizard_getConfigSkip());
label.setWidth100();
+ label.setHeight(50);
+ label.setStylePrimaryName("HeaderLabel");
+ label.setStyleName("HeaderLabel");
editor = new VLayout();
editor.addMember(label);
} else {
@@ -100,9 +104,7 @@ public class GetDeploymentConfigStep extends AbstractWizardStep {
config = new Configuration();
} else {
config = config.deepCopy(false);
- //TODO: get access to this method, may need to add slsb call
- // also, may need to enhance this drop unnecessary config (maybe it can
just stay around)
- //ConfigurationUtility.normalizeConfiguration(config, configDef);
+ ConfigurationUtility.normalizeConfiguration(config, configDef, true, false);
}
return config;