From 35b5687d60558bac72c1e53ddd9044584964a3d8 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Tue, 1 Oct 2019 16:47:26 +0100 Subject: Fix for scalar-unit type value. Issue-ID: SDC-323 Change-Id: I7f19a7356e1cd34deca0a168b3cb707ef657b9cb Signed-off-by: aribeiro --- common/onap-tosca-datatype/pom.xml | 6 ++ .../sdc/tosca/datatypes/model/PropertyType.java | 41 ++++++------ .../tosca/datatypes/model/ScalarUnitValidator.java | 74 ++++++++++++++++++++++ 3 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java (limited to 'common') diff --git a/common/onap-tosca-datatype/pom.xml b/common/onap-tosca-datatype/pom.xml index bd46d62518..c99be321f2 100644 --- a/common/onap-tosca-datatype/pom.xml +++ b/common/onap-tosca-datatype/pom.xml @@ -75,6 +75,12 @@ slf4j-api ${slf4j.version} + + org.apache.commons + commons-lang3 + ${lang3.version} + provided + com.google.code.bean-matchers bean-matchers diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java index facdb2a7b1..1b91ca96d2 100644 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/PropertyType.java @@ -15,8 +15,17 @@ */ package org.onap.sdc.tosca.datatypes.model; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter public enum PropertyType { STRING("string"), @@ -28,27 +37,21 @@ public enum PropertyType { MAP("map"), LIST("list"), SCALAR_UNIT_SIZE("scalar-unit.size"), + SCALAR_UNIT_TIME("scalar-unit.time"), SCALAR_UNIT_FREQUENCY("scalar-unit.frequency"); - private static final Map M_MAP = - Collections.unmodifiableMap(initializeMapping()); - private static final Set SIMPLE_PROPERTY_TYPES = - Collections.unmodifiableSet(initializeSimplePropertyTypes()); + private static final Map M_MAP = Collections.unmodifiableMap(initializeMapping()); + private static final Set SIMPLE_PROPERTY_TYPES = Collections.unmodifiableSet(initializeSimplePropertyTypes()); private String displayName; - PropertyType(String displayName) { - - this.displayName = displayName; - } - /** * Initilize property type display name mapping. * @return Map */ public static Map initializeMapping() { - Map typeMap = new HashMap<>(); - for (PropertyType v : PropertyType.values()) { - typeMap.put(v.displayName, v); + final Map typeMap = new HashMap<>(); + for (final PropertyType propertyType : PropertyType.values()) { + typeMap.put(propertyType.displayName, propertyType); } return typeMap; } @@ -58,7 +61,7 @@ public enum PropertyType { * @param displayName * @return PropertyType */ - public static PropertyType getPropertyTypeByDisplayName(String displayName) { + public static PropertyType getPropertyTypeByDisplayName(final String displayName) { if (M_MAP.containsKey(displayName)) { return M_MAP.get(displayName); } @@ -66,12 +69,14 @@ public enum PropertyType { } private static Set initializeSimplePropertyTypes() { - final int setSize = 4; - Set simplePropertyTypes = new HashSet<>(setSize); + final Set simplePropertyTypes = new HashSet<>(); simplePropertyTypes.add(STRING.getDisplayName().toLowerCase()); simplePropertyTypes.add(INTEGER.getDisplayName().toLowerCase()); simplePropertyTypes.add(FLOAT.getDisplayName().toLowerCase()); simplePropertyTypes.add(BOOLEAN.getDisplayName().toLowerCase()); + simplePropertyTypes.add(SCALAR_UNIT_SIZE.getDisplayName().toLowerCase()); + simplePropertyTypes.add(SCALAR_UNIT_TIME.getDisplayName().toLowerCase()); + simplePropertyTypes.add(SCALAR_UNIT_FREQUENCY.getDisplayName().toLowerCase()); return simplePropertyTypes; } @@ -79,8 +84,4 @@ public enum PropertyType { return SIMPLE_PROPERTY_TYPES; } - public String getDisplayName() { - return displayName; - } - } diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java new file mode 100644 index 0000000000..d7e538e0a7 --- /dev/null +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.tosca.datatypes.model; + +import java.util.Arrays; +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; + +/** + * This Class is responsible for validating if a given value is a valid Tosca Scalar Unit Type. + */ +public class ScalarUnitValidator { + + private static ScalarUnitValidator scalarUnitValidator = new ScalarUnitValidator(); + + /** + * Tosca Scalar Unit Types structure. + */ + private final Pattern pattern = Pattern.compile("\\d+\\s*[a-zA-Z]{1,3}"); + + private ScalarUnitValidator() { + } + + public static ScalarUnitValidator getInstance() { + return scalarUnitValidator; + } + + /** + * Validates if the given String matches with the Tosca Scalar Unit Types structure. + * + * @param value String to be validated. + * @return an {@Boolean} + */ + public boolean isScalarUnit(final String value) { + if (value == null || value.isEmpty()) { + return true; + } + + return pattern.matcher(value).matches(); + } + + /** + * Validates if the given String has a Recognized Tosca unit. + * + * @param value String to be validated + * @param enumClass Enum that represents a Tosca Scalar Unit Type. + * @param + * @return an Enum that represents the Tosca Scalar Unit Type. + */ + public > boolean isValueScalarUnit(final Object value, final Class enumClass) { + final String stringToValidate = String.valueOf(value); + return isScalarUnit(stringToValidate) && Arrays.stream(StringUtils.split(stringToValidate)) + .anyMatch(strValue -> Arrays.stream(enumClass.getEnumConstants()) + .anyMatch(scalarUnit -> + scalarUnit.name().equalsIgnoreCase(strValue) || strValue.endsWith(scalarUnit.name()))); + } +} -- cgit 1.2.3-korg