Lior Vernia has uploaded a new change for review. Change subject: core: Render CustomPropertiesUtils usable by frontend ......................................................................
core: Render CustomPropertiesUtils usable by frontend Removed usage of Pattern/Matcher and of StringUtils where it wasn't necessary, as well as removed new generic instantiation syntax. Change-Id: I7926222061b88325ea0df35e39ad40ec8cce235d Signed-off-by: Lior Vernia <lver...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetDeviceCustomPropertiesQuery.java M backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/StringHelper.java M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtils.java M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtils.java M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/VmPropertiesUtils.java M backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtilsTest.java M backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtilsTest.java M backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/PropertiesUtilsTestHelper.java 9 files changed, 80 insertions(+), 136 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/83/27383/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetDeviceCustomPropertiesQuery.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetDeviceCustomPropertiesQuery.java index 51b5f76..fe9bf23 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetDeviceCustomPropertiesQuery.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetDeviceCustomPropertiesQuery.java @@ -23,6 +23,6 @@ @Override protected void executeQueryCommand() { getQueryReturnValue().setReturnValue(DevicePropertiesUtils.getInstance() - .getRawDeviceProperties(getParameters().getVersion(), getParameters().getDeviceType())); + .getDeviceProperties(getParameters().getVersion(), getParameters().getDeviceType())); } } diff --git a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/StringHelper.java b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/StringHelper.java index 277ef4e..28e9cd7 100644 --- a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/StringHelper.java +++ b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/StringHelper.java @@ -164,4 +164,11 @@ return sb.toString(); } + /** + * Replaces a null String with "", otherwise returns the original String. + */ + public static String defaultString(String s) { + return s == null ? "" : s; + } + } diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtils.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtils.java index 67cfc44..048f93c 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtils.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtils.java @@ -1,5 +1,6 @@ package org.ovirt.engine.core.utils.customprop; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -8,13 +9,11 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; -import org.apache.commons.lang.StringUtils; import org.ovirt.engine.core.common.config.Config; import org.ovirt.engine.core.common.config.ConfigValues; +import org.ovirt.engine.core.compat.StringHelper; import org.ovirt.engine.core.compat.Version; -import org.ovirt.engine.core.utils.collections.MultiValueMapUtils; /** * Abstract class to ease custom properties handling @@ -32,19 +31,14 @@ protected final static String KEY_VALUE_DELIMETER = "="; /** - * Pattern to separate property definition - */ - protected final Pattern semicolonPattern; - - /** * Regex describing legitimate characters for property name - alphanumeric characters and underscore */ protected final static String LEGITIMATE_CHARACTER_FOR_KEY = "[a-z_A-Z0-9]"; /** - * Pattern to validate property name + * Regex describing property name */ - protected final Pattern keyPattern; + protected final static String KEY_REGEX = "(" + LEGITIMATE_CHARACTER_FOR_KEY + ")+"; /** * Regex describing legitimate characters for property value - all except {@code PROPERTIES_DELIMITER} @@ -52,9 +46,9 @@ protected final static String LEGITIMATE_CHARACTER_FOR_VALUE = "[^" + PROPERTIES_DELIMETER + "]"; /** - * Pattern to validate property value + * Regex describing property value */ - protected final Pattern valuePattern; + protected final static String VALUE_REGEX = "(" + LEGITIMATE_CHARACTER_FOR_VALUE + ")*"; /** * Regex describing property definition - key=value @@ -69,11 +63,6 @@ protected static final String VALIDATION_STR = "(" + KEY_VALUE_REGEX_STR + "(;" + KEY_VALUE_REGEX_STR + ")*;?)?"; /** - * Pattern to validation properties definition - */ - protected final Pattern validationPattern; - - /** * List defining syntax error during properties validation */ protected final List<ValidationError> invalidSyntaxValidationError; @@ -82,10 +71,6 @@ * Constructor has package access to enable testing, but class cannot be instantiated outside package */ CustomPropertiesUtils() { - semicolonPattern = Pattern.compile(PROPERTIES_DELIMETER); - keyPattern = Pattern.compile("(" + LEGITIMATE_CHARACTER_FOR_KEY + ")+"); - valuePattern = Pattern.compile("(" + LEGITIMATE_CHARACTER_FOR_VALUE + ")*"); - validationPattern = Pattern.compile(VALIDATION_STR); invalidSyntaxValidationError = Arrays.asList(new ValidationError(ValidationFailureReason.SYNTAX_ERROR, "")); } @@ -107,7 +92,7 @@ * @return returns {@code true} if custom properties contains syntax error, otherwise {@code false} */ public boolean syntaxErrorInProperties(String properties) { - return !validationPattern.matcher(properties).matches(); + return properties != null && !properties.matches(VALIDATION_STR); } /** @@ -122,13 +107,13 @@ if (properties != null && !properties.isEmpty()) { for (Map.Entry<String, String> e : properties.entrySet()) { String key = e.getKey(); - if (key == null || !keyPattern.matcher(key).matches()) { + if (key == null || !key.matches(KEY_REGEX)) { // syntax error in property name error = true; break; } - if (!valuePattern.matcher(StringUtils.defaultString(e.getValue())).matches()) { + if (!StringHelper.defaultString(e.getValue()).matches(VALUE_REGEX)) { // syntax error in property value error = true; break; @@ -141,24 +126,24 @@ /** * Converts properties specification from {@code String} to {@code Map<String, Pattern} */ - protected void parsePropertiesRegex(String properties, Map<String, Pattern> keysToRegex) { - if (StringUtils.isEmpty(properties)) { + protected void parsePropertiesRegex(String properties, Map<String, String> keysToRegex) { + if (StringHelper.isNullOrEmpty(properties)) { return; } - String[] propertiesStrs = semicolonPattern.split(properties); + String[] propertiesStrs = properties.split(PROPERTIES_DELIMETER); // Property is in the form of key=regex for (String property : propertiesStrs) { - Pattern pattern = null; + String pattern = null; String[] propertyParts = property.split(KEY_VALUE_DELIMETER, 2); if (propertyParts.length == 1) { // there is no value(regex) for the property - we assume in that case that any value is allowed except // for the properties delimiter - pattern = valuePattern; + pattern = VALUE_REGEX; } else { - pattern = Pattern.compile(propertyParts[1]); + pattern = propertyParts[1]; } keysToRegex.put(propertyParts[0], pattern); @@ -179,7 +164,12 @@ } for (ValidationError error : errorsList) { - MultiValueMapUtils.addToMap(error.getReason(), error, resultMap); + List<ValidationError> errorsForReason = resultMap.get(error.getReason()); + if (errorsForReason == null) { + errorsForReason = new ArrayList<ValidationError>(); + resultMap.put(error.getReason(), errorsForReason); + } + errorsForReason.add(error); } } @@ -259,8 +249,8 @@ } Map<String, String> map = new LinkedHashMap<String, String>(); - if (StringUtils.isNotEmpty(properties)) { - String keyValuePairs[] = semicolonPattern.split(properties); + if (!StringHelper.isNullOrEmpty(properties)) { + String keyValuePairs[] = properties.split(PROPERTIES_DELIMETER); for (String keyValuePairStr : keyValuePairs) { String[] pairParts = keyValuePairStr.split(KEY_VALUE_DELIMETER, 2); String key = pairParts[0]; @@ -291,7 +281,7 @@ for (Map.Entry<String, String> e : properties.entrySet()) { sb.append(e.getKey()); sb.append(KEY_VALUE_DELIMETER); - sb.append(StringUtils.defaultString(e.getValue())); + sb.append(StringHelper.defaultString(e.getValue())); sb.append(PROPERTIES_DELIMETER); } // remove last PROPERTIES_DELIMETER diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtils.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtils.java index f1f6920..d112342 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtils.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtils.java @@ -86,7 +86,7 @@ /** * Map of device custom properties for each version and device type */ - private Map<Version, EnumMap<VmDeviceGeneralType, Map<String, Pattern>>> deviceProperties; + private Map<Version, EnumMap<VmDeviceGeneralType, Map<String, String>>> deviceProperties; /** * List of device types for which custom properties can be set @@ -188,7 +188,7 @@ devicePropertiesStr = getCustomDeviceProperties(version); if (FeatureSupported.deviceCustomProperties(version)) { deviceProperties.put(version, - new EnumMap<VmDeviceGeneralType, Map<String, Pattern>>(VmDeviceGeneralType.class)); + new EnumMap<VmDeviceGeneralType, Map<String, String>>(VmDeviceGeneralType.class)); Matcher typeMatcher = devicePropSplitPattern.matcher(devicePropertiesStr); while (typeMatcher.find()) { String dcpStr = typeMatcher.group(); @@ -204,7 +204,7 @@ VmDeviceGeneralType type = VmDeviceGeneralType.forValue(dcpStr.substring(0, idx)); // properties definition for device starts with ";prop={" String propStr = dcpStr.substring(idx + PROP_PREFIX_LEN, dcpStr.length() - 1); - Map<String, Pattern> props = new HashMap<String, Pattern>(); + Map<String, String> props = new HashMap<String, String>(); parsePropertiesRegex(propStr, props); deviceProperties.get(version).put(type, props); } @@ -226,7 +226,7 @@ return Collections.emptySet(); } - EnumMap<VmDeviceGeneralType, Map<String, Pattern>> map = deviceProperties.get(version); + EnumMap<VmDeviceGeneralType, Map<String, String>> map = deviceProperties.get(version); if (map.isEmpty()) { // no device type has any properties return Collections.emptySet(); @@ -246,12 +246,12 @@ * specified device type * @return map of device properties */ - public Map<String, Pattern> getDeviceProperties(Version version, VmDeviceGeneralType type) { + public Map<String, String> getDeviceProperties(Version version, VmDeviceGeneralType type) { if (!FeatureSupported.deviceCustomProperties(version)) { return Collections.emptyMap(); } - Map<String, Pattern> map = deviceProperties.get(version).get(type); + Map<String, String> map = deviceProperties.get(version).get(type); if (map == null) { // no defined properties for specified type map = Collections.emptyMap(); @@ -260,31 +260,6 @@ map = Collections.unmodifiableMap(map); } return map; - } - - /** - * Returns map of device properties <property name, regex to validate property value> for specified version and - * device type. Method should be used only for GWT, because {@code java.util.regex.Pattern} cannot be used for GWT - * - * @param version - * version of the cluster that the VM containing the device is in - * @param type - * specified device type - * @return map of device properties - */ - public Map<String, String> getRawDeviceProperties(Version version, VmDeviceGeneralType type) { - Map<String, Pattern> map = getDeviceProperties(version, type); - - if (map.isEmpty()) { - return Collections.emptyMap(); - } - - // convert to Map<String,String> - Map<String, String> result = new HashMap<>(); - for (Map.Entry<String, Pattern> e : map.entrySet()) { - result.put(e.getKey(), e.getValue().pattern()); - } - return result; } /** @@ -336,7 +311,7 @@ } String value = StringUtils.defaultString(e.getValue()); - if (!deviceProperties.get(version).get(type).get(key).matcher(value).matches()) { + if (!value.matches(deviceProperties.get(version).get(type).get(key))) { errorsSet.add(new ValidationError(ValidationFailureReason.INCORRECT_VALUE, key)); continue; } diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java index 3b92939..3e1fe53 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java @@ -2,15 +2,13 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.regex.Pattern; -import org.apache.commons.lang.StringUtils; +import org.ovirt.engine.core.compat.StringHelper; public class SimpleCustomPropertiesUtil extends CustomPropertiesUtils { @@ -41,12 +39,6 @@ } Set<ValidationError> errorsSet = new HashSet<ValidationError>(); - Map<String, Pattern> keysToRegex = new HashMap<String, Pattern>(); - for (Entry<String, String> property : regExMap.entrySet()) { - Pattern pattern = Pattern.compile(property.getValue()); - keysToRegex.put(property.getKey(), pattern); - } - for (Entry<String, String> e : properties.entrySet()) { String key = e.getKey(); if (key == null || !regExMap.containsKey(key)) { @@ -54,7 +46,7 @@ continue; } - if (!keysToRegex.get(key).matcher(StringUtils.defaultString(e.getValue())).matches()) { + if (!StringHelper.defaultString(e.getValue()).matches(regExMap.get(key))) { errorsSet.add(new ValidationError(ValidationFailureReason.INCORRECT_VALUE, key)); continue; } diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/VmPropertiesUtils.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/VmPropertiesUtils.java index 26ac04b..d2f44e3 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/VmPropertiesUtils.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/VmPropertiesUtils.java @@ -9,12 +9,11 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.regex.Pattern; -import org.apache.commons.lang.StringUtils; import org.ovirt.engine.core.common.businessentities.VmStatic; import org.ovirt.engine.core.common.config.Config; import org.ovirt.engine.core.common.config.ConfigValues; +import org.ovirt.engine.core.compat.StringHelper; import org.ovirt.engine.core.compat.Version; import org.ovirt.engine.core.utils.exceptions.InitializationException; @@ -31,8 +30,8 @@ vmPropertiesUtils = new VmPropertiesUtils(); } - private Map<Version, Map<String, Pattern>> predefinedProperties; - private Map<Version, Map<String, Pattern>> userdefinedProperties; + private Map<Version, Map<String, String>> predefinedProperties; + private Map<Version, Map<String, String>> userdefinedProperties; private Map<Version, String> allVmProperties; public static VmPropertiesUtils getInstance() { @@ -41,8 +40,8 @@ public void init() throws InitializationException { try { - predefinedProperties = new HashMap<Version, Map<String, Pattern>>(); - userdefinedProperties = new HashMap<Version, Map<String, Pattern>>(); + predefinedProperties = new HashMap<Version, Map<String, String>>(); + userdefinedProperties = new HashMap<Version, Map<String, String>>(); allVmProperties = new HashMap<Version, String>(); Set<Version> versions = getSupportedClusterLevels(); String predefinedVMPropertiesStr, userDefinedVMPropertiesStr; @@ -58,8 +57,8 @@ sb.append(userDefinedVMPropertiesStr); allVmProperties.put(version, sb.toString()); - predefinedProperties.put(version, new HashMap<String, Pattern>()); - userdefinedProperties.put(version, new HashMap<String, Pattern>()); + predefinedProperties.put(version, new HashMap<String, String>()); + userdefinedProperties.put(version, new HashMap<String, String>()); parsePropertiesRegex(predefinedVMPropertiesStr, predefinedProperties.get(version)); parsePropertiesRegex(userDefinedVMPropertiesStr, userdefinedProperties.get(version)); } @@ -118,7 +117,7 @@ * @return a list of validation errors. if there are no errors - the list will be empty */ public List<ValidationError> validateVmProperties(Version version, String properties) { - if (StringUtils.isEmpty(properties)) { // No errors in case of empty value + if (StringHelper.isNullOrEmpty(properties)) { // No errors in case of empty value return Collections.emptyList(); } if (syntaxErrorInProperties(properties)) { @@ -136,11 +135,11 @@ private boolean isValueValid(Version version, String key, String value) { // Checks that the value for the given property is valid by running by trying to perform // regex validation - Pattern userDefinedPattern = userdefinedProperties.get(version).get(key); - Pattern predefinedPattern = predefinedProperties.get(version).get(key); + String userDefinedPattern = userdefinedProperties.get(version).get(key); + String predefinedPattern = predefinedProperties.get(version).get(key); - return (userDefinedPattern != null && userDefinedPattern.matcher(value).matches()) - || (predefinedPattern != null && predefinedPattern.matcher(value).matches()); + return (userDefinedPattern != null && value.matches(userDefinedPattern)) + || (predefinedPattern != null && value.matches(predefinedPattern)); } /** @@ -207,7 +206,7 @@ */ public void getVMProperties(Version version, Map<String, String> propertiesMap, String vmPropertiesFieldValue) { // format of properties is key1=val1,key2=val2,key3=val3,key4=val4 - if (StringUtils.isEmpty(vmPropertiesFieldValue)) { + if (StringHelper.isNullOrEmpty(vmPropertiesFieldValue)) { return; } @@ -228,13 +227,13 @@ Map<String, String> propertiesMap) { Set<ValidationError> errorsSet = new HashSet<ValidationError>(); List<ValidationError> results = new ArrayList<ValidationError>(); - if (!StringUtils.isEmpty(vmPropertiesFieldValue)) { - String keyValuePairs[] = semicolonPattern.split(vmPropertiesFieldValue); + if (!StringHelper.isNullOrEmpty(vmPropertiesFieldValue)) { + String keyValuePairs[] = vmPropertiesFieldValue.split(PROPERTIES_DELIMETER); for (String keyValuePairStr : keyValuePairs) { String[] pairParts = keyValuePairStr.split(KEY_VALUE_DELIMETER, 2); String key = pairParts[0]; - String value = StringUtils.defaultString(pairParts[1]); + String value = StringHelper.defaultString(pairParts[1]); if (propertiesMap.containsKey(key)) { errorsSet.add(new ValidationError(ValidationFailureReason.DUPLICATE_KEY, key)); continue; @@ -256,7 +255,7 @@ return results; } - protected boolean keyExistsInVersion(Map<Version, Map<String, Pattern>> propertiesMap, Version version, String key) { + protected boolean keyExistsInVersion(Map<Version, Map<String, String>> propertiesMap, Version version, String key) { return propertiesMap.get(version).containsKey(key); } @@ -271,14 +270,14 @@ Entry<String, String> entry = iterator.next(); result.append(entry.getKey()) .append("=") - .append(StringUtils.defaultString(entry.getValue())); + .append(StringHelper.defaultString(entry.getValue())); while (iterator.hasNext()) { result.append(";"); entry = iterator.next(); if (entry != null) { result.append(entry.getKey()) .append("=") - .append(StringUtils.defaultString(entry.getValue())); + .append(StringHelper.defaultString(entry.getValue())); } } return result.toString(); @@ -299,7 +298,7 @@ Set<String> userdefinedPropertiesKeys = userdefinedProperties.get(version).keySet(); for (Entry<String, String> propertiesEntry : propertiesEntries) { String propertyKey = propertiesEntry.getKey(); - String propertyValue = StringUtils.defaultString(propertiesEntry.getValue()); + String propertyValue = StringHelper.defaultString(propertiesEntry.getValue()); if (predefinedPropertiesKeys.contains(propertyKey)) { predefinedPropertiesMap.put(propertyKey, propertyValue); } @@ -319,9 +318,9 @@ */ public String customProperties(String predefinedProperties, String userDefinedProperties) { StringBuilder result = new StringBuilder(); - result.append((StringUtils.isEmpty(predefinedProperties)) ? "" : predefinedProperties); + result.append(predefinedProperties == null ? "" : predefinedProperties); result.append((result.length() == 0) ? "" : ";"); - result.append((StringUtils.isEmpty(userDefinedProperties)) ? "" : userDefinedProperties); + result.append(userDefinedProperties == null ? "" : userDefinedProperties); return result.toString(); } diff --git a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtilsTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtilsTest.java index c0553f6..8ebc88e 100644 --- a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtilsTest.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/CustomPropertiesUtilsTest.java @@ -34,7 +34,7 @@ @Test public void mapPropertiesSyntaxNullValue() { CustomPropertiesUtils utils = new CustomPropertiesUtils(); - Map<String, String> propMap = new LinkedHashMap<>(); + Map<String, String> propMap = new LinkedHashMap<String, String>(); propMap.put("speed", "1024"); propMap.put("duplex", null); propMap.put("debug", ""); @@ -63,7 +63,7 @@ @Test public void convertValidPropertiesToString() { CustomPropertiesUtils utils = new CustomPropertiesUtils(); - Map<String, String> propMap = new LinkedHashMap<>(); + Map<String, String> propMap = new LinkedHashMap<String, String>(); propMap.put("speed", "1024"); propMap.put("duplex", "half"); propMap.put("debug", null); diff --git a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtilsTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtilsTest.java index 13005ad..3f759d31 100644 --- a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtilsTest.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/DevicePropertiesUtilsTest.java @@ -9,7 +9,6 @@ import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig; import static org.ovirt.engine.core.utils.customprop.PropertiesUtilsTestHelper.validateFailure; import static org.ovirt.engine.core.utils.customprop.PropertiesUtilsTestHelper.validatePropertyMap; -import static org.ovirt.engine.core.utils.customprop.PropertiesUtilsTestHelper.validatePropertyPattern; import static org.ovirt.engine.core.utils.customprop.PropertiesUtilsTestHelper.validatePropertyValue; import java.util.Arrays; @@ -17,7 +16,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; import org.junit.ClassRule; import org.junit.Test; @@ -292,7 +290,7 @@ */ @Test public void parseValidCustomDevPropSpec() { - Map<String, Pattern> devProp; + Map<String, String> devProp; String customDevPropSpec = "{type=disk;prop={bootable=^(true|false)$}};" + "{type=interface;prop={speed=[0-9]{1,5};duplex=^(full|half)$}};" @@ -317,54 +315,54 @@ // test disk properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.DISK); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "bootable", "^(true|false)$"); + validatePropertyValue(devProp, "bootable", "^(true|false)$"); // test interface properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.INTERFACE); validatePropertyMap(devProp, 2); - validatePropertyPattern(devProp, "speed", "[0-9]{1,5}"); - validatePropertyPattern(devProp, "duplex", "^(full|half)$"); + validatePropertyValue(devProp, "speed", "[0-9]{1,5}"); + validatePropertyValue(devProp, "duplex", "^(full|half)$"); // test video properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.VIDEO); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "turned_on", "^(true|false)$"); + validatePropertyValue(devProp, "turned_on", "^(true|false)$"); // test sound properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.SOUND); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "volume", "[0-9]{1,2}"); + validatePropertyValue(devProp, "volume", "[0-9]{1,2}"); // test video properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.CONTROLLER); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "hotplug", "^(true|false)$"); + validatePropertyValue(devProp, "hotplug", "^(true|false)$"); // test balloon properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.BALLOON); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "max_size", "[0-9]{1,15}"); + validatePropertyValue(devProp, "max_size", "[0-9]{1,15}"); // test channel properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.CHANNEL); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "auth_type", "^(plain|md5|kerberos)$"); + validatePropertyValue(devProp, "auth_type", "^(plain|md5|kerberos)$"); // test redir properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.REDIR); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "max_len", "[0-9]{1,15}"); + validatePropertyValue(devProp, "max_len", "[0-9]{1,15}"); // test console properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.CONSOLE); validatePropertyMap(devProp, 2); - validatePropertyPattern(devProp, "type", "^(text|vnc)$"); - validatePropertyPattern(devProp, "prop", "\\{\\}"); + validatePropertyValue(devProp, "type", "^(text|vnc)$"); + validatePropertyValue(devProp, "prop", "\\{\\}"); // test smartcard properties devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.SMARTCARD); validatePropertyMap(devProp, 1); - validatePropertyPattern(devProp, "spec_chars", "[\\@\\#\\$\\%\\^\\&\\*\\(\\)\\{\\}\\:\\<\\>\\,\\.\\?\\[\\]]?"); + validatePropertyValue(devProp, "spec_chars", "[\\@\\#\\$\\%\\^\\&\\*\\(\\)\\{\\}\\:\\<\\>\\,\\.\\?\\[\\]]?"); } /** @@ -387,12 +385,12 @@ assertEquals(2, utils.getDeviceTypesWithProperties(Version.v3_3).size()); // test disk properties - devProp = utils.getRawDeviceProperties(Version.v3_3, VmDeviceGeneralType.DISK); + devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.DISK); validatePropertyMap(devProp, 1); validatePropertyValue(devProp, "bootable", "^(true|false)$"); // test interface properties - devProp = utils.getRawDeviceProperties(Version.v3_3, VmDeviceGeneralType.INTERFACE); + devProp = utils.getDeviceProperties(Version.v3_3, VmDeviceGeneralType.INTERFACE); validatePropertyMap(devProp, 2); validatePropertyValue(devProp, "speed", "[0-9]{1,5}"); validatePropertyValue(devProp, "duplex", "^(full|half)$"); diff --git a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/PropertiesUtilsTestHelper.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/PropertiesUtilsTestHelper.java index ae403bd..0b4b0f5 100644 --- a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/PropertiesUtilsTestHelper.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/customprop/PropertiesUtilsTestHelper.java @@ -6,7 +6,6 @@ import java.util.List; import java.util.Map; -import java.util.regex.Pattern; /** * Class with helper methods to ease properties testing @@ -52,21 +51,5 @@ public static void validateFailure(List<ValidationError> errors, ValidationFailureReason reason) { assertFalse(errors.isEmpty()); assertEquals(reason, errors.get(0).getReason()); - } - - /** - * Validates if property exists and its pattern matches the specified value - * - * @param map - * properties map - * @param key - * property name - * @param value - * property pattern value - */ - public static void validatePropertyPattern(Map<String, Pattern> map, String key, String value) { - Pattern pat = map.get(key); - assertNotNull(pat); - assertEquals(value, pat.pattern()); } } -- To view, visit http://gerrit.ovirt.org/27383 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7926222061b88325ea0df35e39ad40ec8cce235d Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Lior Vernia <lver...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches