Fix numeric constraint values generated as strings 80/132380/7
authorJvD_Ericsson <jeff.van.dam@est.tech>
Mon, 21 Nov 2022 09:55:26 +0000 (09:55 +0000)
committerMichael Morris <michael.morris@est.tech>
Fri, 2 Dec 2022 09:44:21 +0000 (09:44 +0000)
When importing a vfc the property constraints are now
checked to see if they are the same type as the property
If not it will attempt to convert to that type

Issue-ID: SDC-4274
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: I32c1d930166d10142ad9ca6914550c7946e9128c

32 files changed:
catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImpl.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java
catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperationTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraintTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java [new file with mode: 0644]
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraintTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraintTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraintTest.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java [new file with mode: 0644]
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java

index 9572c44..eb601d8 100644 (file)
@@ -151,12 +151,12 @@ public class NsDescriptorGeneratorImpl implements NsDescriptorGenerator {
             return returnValueOnError;
         }
         final ToscaPropertyConstraintValidValues validValuesConstraint = (ToscaPropertyConstraintValidValues) toscaPropertyConstraint;
-        final List<String> validValues = validValuesConstraint.getValidValues();
+        final List<Object> validValues = validValuesConstraint.getValidValues();
         if (CollectionUtils.isEmpty(validValues)) {
             LOGGER.error(errorMsg);
             return returnValueOnError;
         }
-        return validValues.get(0);
+        return String.valueOf(validValues.get(0));
     }
 
     private ToscaTemplate createNetworkServiceDescriptor(final Component component, final List<VnfDescriptor> vnfDescriptorList) throws NsdException {
index 181c860..9c2c070 100644 (file)
@@ -63,6 +63,9 @@ import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
+import org.openecomp.sdc.be.model.tosca.constraints.AbstractComparablePropertyConstraint;
+import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
+import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 import org.openecomp.sdc.be.utils.TypeUtils;
@@ -315,12 +318,46 @@ public final class ImportUtils {
         if (propertyConstraint instanceof ValidValuesConstraint) {
             try {
                 ((ValidValuesConstraint) propertyConstraint).validateType(propertyType);
+                boolean valid = ((ValidValuesConstraint) propertyConstraint).validateValueType(propertyType);
+                if (!valid) {
+                    ((ValidValuesConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
+                }
             } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
                 BeEcompErrorManager.getInstance()
                     .logInternalFlowError("GetInitializedPropertyConstraint", e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR);
                 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.VALID_VALUES.name(),
                     ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType);
             }
+        } else if (propertyConstraint instanceof AbstractComparablePropertyConstraint) {
+            try {
+                boolean valid = ((AbstractComparablePropertyConstraint) propertyConstraint).validateValueType(propertyType);
+                if (!valid) {
+                    ((AbstractComparablePropertyConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
+                }
+            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
+                throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, propertyConstraint.getConstraintType().name(),
+                        ((AbstractComparablePropertyConstraint) propertyConstraint).getConstraintValueAsString(), propertyType);
+            }
+        } else if (propertyConstraint instanceof EqualConstraint) {
+            try {
+                boolean valid = ((EqualConstraint) propertyConstraint).validateValueType(propertyType);
+                if (!valid) {
+                    ((EqualConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
+                }
+            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
+                throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.EQUAL.name(),
+                        String.valueOf(((EqualConstraint) propertyConstraint).getEqual()), propertyType);
+            }
+        } else if (propertyConstraint instanceof InRangeConstraint) {
+            try {
+                boolean valid = ((InRangeConstraint) propertyConstraint).validateValueType(propertyType);
+                if (!valid) {
+                    ((InRangeConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
+                }
+            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
+                throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.IN_RANGE.name(),
+                        String.valueOf(((InRangeConstraint) propertyConstraint).getInRange()), propertyType);
+            }
         }
         return propertyConstraint;
     }
index f1c8a17..2ad6a94 100644 (file)
@@ -158,16 +158,17 @@ public class PropertyConvertor {
             }
             if (constraint instanceof InRangeConstraint) {
                 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
-                List<String> range = new ArrayList<>();
+                List<Object> range = new ArrayList<>();
                 range.add(inRangeConstraint.getRangeMinValue());
                 range.add(inRangeConstraint.getRangeMaxValue());
                 convertedConstraints.add(new ToscaPropertyConstraintInRange(range));
             }
             if (constraint instanceof ValidValuesConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintValidValues(((ValidValuesConstraint) constraint).getValidValues()));
+                List validValues = ((ValidValuesConstraint) constraint).getValidValues();
+                convertedConstraints.add(new ToscaPropertyConstraintValidValues(validValues));
             }
             if (constraint instanceof LengthConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength().toString()));
+                convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength()));
             }
             if (constraint instanceof MinLengthConstraint) {
                 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
index 532cb8c..e52f8df 100644 (file)
@@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintEqual implements ToscaPropertyConstraint {
 
-    private String equal;
+    private Object equal;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.EQUAL;
 
     @Override
index fcc9dcc..89c563b 100644 (file)
@@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintGreaterOrEqual implements ToscaPropertyConstraint {
 
-    private String greaterOrEqual;
+    private Object greaterOrEqual;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.GREATER_OR_EQUAL;
 
     @Override
index 6f2268b..2168cf4 100644 (file)
@@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintGreaterThan implements ToscaPropertyConstraint {
 
-    private String greaterThan;
+    private Object greaterThan;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.GREATER_THAN;
 
     @Override
index 6c8cfba..91e1f55 100644 (file)
@@ -32,7 +32,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintInRange implements ToscaPropertyConstraint {
 
-    private List<String> inRange;
+    private List<Object> inRange;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.IN_RANGE;
 
     @Override
@@ -40,7 +40,6 @@ public class ToscaPropertyConstraintInRange implements ToscaPropertyConstraint {
         if ("inRange".equals(attributeName)) {
             return CONSTRAINT_TYPE.getType();
         }
-        
         return attributeName;
     }
 
index 67aaca0..998a728 100644 (file)
@@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintLength implements ToscaPropertyConstraint {
 
-    private String length;
+    private Integer length;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LENGTH;
 
     @Override
index 3d51ec9..75e661d 100644 (file)
@@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintLessOrEqual implements ToscaPropertyConstraint {
 
-    private String lessOrEqual;
+    private Object lessOrEqual;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LESS_OR_EQUAL;
 
     @Override
index ad51f66..a64382d 100644 (file)
@@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintLessThan implements ToscaPropertyConstraint {
 
-    private String lessThan;
+    private Object lessThan;
     private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LESS_THAN;
 
     @Override
index c23be57..4f9a65c 100644 (file)
@@ -32,7 +32,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 @AllArgsConstructor
 public class ToscaPropertyConstraintValidValues implements ToscaPropertyConstraint {
 
-    private List<String> validValues;
+    private List<Object> validValues;
 
     @Override
     public String getEntryToscaName(final String attributeName) {
index b7559e3..8fcc06b 100644 (file)
@@ -329,7 +329,7 @@ public class ImportUtilsTest {
         assertTrue(property.getConstraints() != null && property.getConstraints().size() == 1);
         assertTrue(property.getConstraints().get(0) instanceof ValidValuesConstraint);
         assertNotNull(((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues());
-        List<String> validValues = ((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues();
+        List<Object> validValues = ((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues();
         assertTrue(validValues.containsAll(Lists.newArrayList("firewall", "analyzer", "source-nat", "loadbalancer")));
 
         assertTrue(properties.containsKey("service_interface_type_list"));
index dc97e25..9e06b42 100644 (file)
@@ -2139,20 +2139,20 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
             JsonArray jsonArray = new JsonArray();
             if (src instanceof InRangeConstraint) {
                 InRangeConstraint rangeConstraint = (InRangeConstraint) src;
-                jsonArray.add(JsonParser.parseString(rangeConstraint.getRangeMinValue()));
-                jsonArray.add(JsonParser.parseString(rangeConstraint.getRangeMaxValue()));
+                jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getRangeMinValue())));
+                jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getRangeMaxValue())));
                 result.add("inRange", jsonArray);
             } else if (src instanceof GreaterThanConstraint) {
                 GreaterThanConstraint greaterThanConstraint = (GreaterThanConstraint) src;
-                jsonArray.add(JsonParser.parseString(greaterThanConstraint.getGreaterThan()));
+                jsonArray.add(JsonParser.parseString(String.valueOf(greaterThanConstraint.getGreaterThan())));
                 result.add("greaterThan", jsonArray);
             } else if (src instanceof LessThanConstraint) {
                 LessThanConstraint lessThanConstraint = (LessThanConstraint) src;
-                jsonArray.add(JsonParser.parseString(lessThanConstraint.getLessThan()));
+                jsonArray.add(JsonParser.parseString(String.valueOf(lessThanConstraint.getLessThan())));
                 result.add("lessThan", jsonArray);
             } else if (src instanceof LessOrEqualConstraint) {
                 LessOrEqualConstraint lessOrEqualConstraint = (LessOrEqualConstraint) src;
-                jsonArray.add(JsonParser.parseString(lessOrEqualConstraint.getLessOrEqual()));
+                jsonArray.add(JsonParser.parseString(String.valueOf(lessOrEqualConstraint.getLessOrEqual())));
                 result.add("lessOrEqual", jsonArray);
             } else {
                 log.warn("PropertyConstraint {} is not supported. Ignored.", src.getClass().getName());
@@ -2173,38 +2173,32 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
                 Entry<String, JsonElement> element = set.iterator().next();
                 String key = element.getKey();
                 JsonElement value = element.getValue();
+                Object typedValue = getTypedValue(element.getValue());
                 ConstraintType constraintType = ConstraintType.findByType(key).orElse(null);
                 if (constraintType == null) {
                     log.warn("ConstraintType was not found for constraint name:{}", key);
                 } else {
                     switch (constraintType) {
                         case EQUAL:
-                            if (value != null) {
-                                String asString = value.getAsString();
-                                log.debug("Before adding value to EqualConstraint object. value = {}", asString);
-                                propertyConstraint = new EqualConstraint(asString);
+                            if ( typedValue != null) {
+                                log.debug("Before adding value to EqualConstraint object. value = {}", typedValue);
+                                propertyConstraint = new EqualConstraint(typedValue);
                                 break;
                             } else {
                                 log.warn("The value of equal constraint is null");
                             }
                             break;
                         case IN_RANGE:
-                            if (value != null) {
-                                if (value instanceof JsonArray) {
-                                    JsonArray rangeArray = (JsonArray) value;
-                                    if (rangeArray.size() != 2 || rangeArray.contains(new JsonPrimitive(""))) {
-                                        log.error("The range constraint content is invalid. value = {}", value);
+                            if (typedValue != null) {
+                                if (typedValue instanceof ArrayList) {
+                                    ArrayList rangeArray = (ArrayList) typedValue;
+                                    if (rangeArray.size() != 2 || rangeArray.contains("")) {
+                                        log.error("The range constraint content is invalid. value = {}", typedValue);
                                         throw new JsonSyntaxException("The range constraint content is invalid");
                                     } else {
                                         InRangeConstraint rangeConstraint = new InRangeConstraint();
-                                        String minValue = rangeArray.get(0).getAsString();
-                                        String maxValue;
-                                        JsonElement maxElement = rangeArray.get(1);
-                                        if (maxElement.isJsonNull()) {
-                                            maxValue = String.valueOf(maxElement.getAsJsonNull());
-                                        } else {
-                                            maxValue = maxElement.getAsString();
-                                        }
+                                        Object minValue = rangeArray.get(0);
+                                        Object maxValue = rangeArray.get(1);
                                         rangeConstraint.setRangeMinValue(minValue);
                                         rangeConstraint.setRangeMaxValue(maxValue);
                                         propertyConstraint = rangeConstraint;
@@ -2215,58 +2209,49 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
                             }
                             break;
                         case GREATER_THAN:
-                            if (value != null) {
-                                String asString = value.getAsString();
-                                log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString);
-                                propertyConstraint = new GreaterThanConstraint(asString);
+                            if (typedValue != null) {
+                                log.debug("Before adding value to GreaterThanConstraint object. value = {}", typedValue);
+                                propertyConstraint = new GreaterThanConstraint(typedValue);
                                 break;
                             } else {
                                 log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
                             }
                             break;
                         case LESS_THAN:
-                            if (value != null) {
-                                String asString = value.getAsString();
-                                log.debug("Before adding value to LessThanConstraint object. value = {}", asString);
-                                propertyConstraint = new LessThanConstraint(asString);
+                            if (typedValue != null) {
+                                log.debug("Before adding value to LessThanConstraint object. value = {}", typedValue);
+                                propertyConstraint = new LessThanConstraint(typedValue);
                                 break;
                             } else {
                                 log.warn("The value of LessThanConstraint is null");
                             }
                             break;
                         case GREATER_OR_EQUAL:
-                            if (value != null) {
-                                String asString = value.getAsString();
-                                log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString);
-                                propertyConstraint = new GreaterOrEqualConstraint(asString);
+                            if (typedValue != null) {
+                                log.debug("Before adding value to GreaterThanConstraint object. value = {}", typedValue);
+                                propertyConstraint = new GreaterOrEqualConstraint(typedValue);
                                 break;
                             } else {
                                 log.warn("The value of GreaterOrEqualConstraint is null");
                             }
                             break;
                         case LESS_OR_EQUAL:
-                            if (value != null) {
-                                String asString = value.getAsString();
-                                log.debug("Before adding value to LessOrEqualConstraint object. value = {}", asString);
-                                propertyConstraint = new LessOrEqualConstraint(asString);
+                            if (typedValue != null) {
+                                log.debug("Before adding value to LessOrEqualConstraint object. value = {}", typedValue);
+                                propertyConstraint = new LessOrEqualConstraint(typedValue);
                             } else {
                                 log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
                             }
                             break;
                         case VALID_VALUES:
-                            if (value != null) {
-                                JsonArray rangeArray = (JsonArray) value;
-                                if (rangeArray.size() == 0 || rangeArray.contains(new JsonPrimitive(""))) {
-                                    log.error("The valid values constraint content is invalid. value = {}", value);
+                            if (typedValue != null) {
+                                ArrayList validValuesArray = (ArrayList)typedValue;
+                                if (validValuesArray.size() == 0 || validValuesArray.contains("")) {
+                                    log.error("The valid values constraint content is invalid. value = {}", typedValue);
                                     throw new JsonSyntaxException("The valid values constraint content is invalid");
                                 } else {
                                     ValidValuesConstraint vvConstraint = new ValidValuesConstraint();
-                                    List<String> validValues = new ArrayList<>();
-                                    for (JsonElement jsonElement : rangeArray) {
-                                        String item = jsonElement.getAsString();
-                                        validValues.add(item);
-                                    }
-                                    vvConstraint.setValidValues(validValues);
+                                    vvConstraint.setValidValues(validValuesArray);
                                     propertyConstraint = vvConstraint;
                                 }
                             }
@@ -2318,6 +2303,37 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
             }
             return propertyConstraint;
         }
+
+        private Object getTypedValue(JsonElement je) {
+            if (je.isJsonNull())
+                return null;
+            if (je.isJsonPrimitive()) {
+                return getJsonPrimitive(je.getAsJsonPrimitive());
+            }
+            if (je.isJsonArray()) {
+                ArrayList<Object> array = new ArrayList<>();
+                for (JsonElement e : je.getAsJsonArray()) {
+                    array.add(getJsonPrimitive(e.getAsJsonPrimitive()));
+                }
+                return array;
+            }
+            return je;
+        }
+
+        private Object getJsonPrimitive(JsonPrimitive je) {
+            if (je.isBoolean())
+                return je.getAsBoolean();
+            if (je.isString())
+                return je.getAsString();
+            if (je.isNumber()){
+                double number = je.getAsNumber().floatValue();
+                if ((number % 1) == 0) {
+                    return je.getAsNumber().intValue();
+                }
+                return number;
+            }
+            return null;
+        }
     }
 
     public static class PropertyConstraintJacksonDeserializer extends com.fasterxml.jackson.databind.JsonDeserializer<PropertyConstraint> {
@@ -2388,7 +2404,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
             String asString = value.asText();
             log.debug("Before adding value to {} object. value = {}", constraintClass, asString);
             try {
-                return constraintClass.getConstructor(String.class).newInstance(asString);
+                return constraintClass.getConstructor(Object.class).newInstance(asString);
             } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
                      | SecurityException exception) {
                 log.error("Error deserializing constraint", exception);
@@ -2437,7 +2453,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
                 log.error("The valid values constraint content is invalid. value = {}", value);
             } else {
                 ValidValuesConstraint vvConstraint = new ValidValuesConstraint();
-                List<String> validValues = new ArrayList<>();
+                List<Object> validValues = new ArrayList<>();
                 for (JsonNode jsonElement : rangeArray) {
                     String item = jsonElement.asText();
                     validValues.add(item);
index 8575870..f59ebe6 100644 (file)
@@ -49,6 +49,7 @@ public enum ToscaType {
        VERSION("version"),
        LIST("list"),
        MAP("map"),
+    RANGE("range"),
        SCALAR_UNIT("scalar-unit"),
        SCALAR_UNIT_SIZE("scalar-unit.size"),
        SCALAR_UNIT_TIME("scalar-unit.time"),
@@ -93,6 +94,31 @@ public enum ToscaType {
         return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type);
     }
 
+    public Boolean isValueTypeValid(Object value) {
+        switch (this) {
+            case BOOLEAN:
+                return value.equals(true) || value.equals(false);
+            case FLOAT:
+                return value instanceof Float;
+            case INTEGER:
+            case RANGE:
+                return value instanceof Integer;
+            case STRING:
+            case SCALAR_UNIT:
+            case SCALAR_UNIT_SIZE:
+            case SCALAR_UNIT_TIME:
+            case SCALAR_UNIT_FREQUENCY:
+            case TIMESTAMP:
+            case VERSION:
+                return value instanceof String;
+            case LIST:
+            case MAP:
+                return true;
+            default:
+                return false;
+        }
+    }
+
     public boolean isValidValue(String value) {
         switch (this) {
             case BOOLEAN:
@@ -172,6 +198,7 @@ public enum ToscaType {
                 return Boolean.valueOf(value);
             case FLOAT:
                 return Float.valueOf(value);
+            case RANGE:
             case INTEGER:
                 return Long.valueOf(value);
             case TIMESTAMP:
index 42fb1eb..e7730bd 100644 (file)
@@ -50,6 +50,12 @@ public abstract class AbstractComparablePropertyConstraint extends AbstractPrope
 
     protected abstract void doValidate(Object propertyValue) throws ConstraintViolationException;
 
+    public abstract boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException;
+
+    public abstract String getConstraintValueAsString();
+
+    public abstract void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException;
+
     @Override
     public void validate(Object propertyValue) throws ConstraintViolationException {
         if (propertyValue == null) {
index 0fdc570..9e86573 100644 (file)
@@ -21,6 +21,8 @@ package org.openecomp.sdc.be.model.tosca.constraints;
 
 import java.io.Serializable;
 import javax.validation.constraints.NotNull;
+import lombok.Setter;
+import lombok.Getter;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
@@ -28,25 +30,25 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunction
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
-import lombok.Getter;
 
 @SuppressWarnings("serial")
 public class EqualConstraint extends AbstractPropertyConstraint implements Serializable {
 
-    @NotNull
     @Getter
-    private String equal;
+    @Setter
+    @NotNull
+    private Object equal;
     private Object typed;
 
-    public EqualConstraint(String equal) {
+    public EqualConstraint(Object equal) {
         super();
         this.equal = equal;
     }
 
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        if (propertyType.isValidValue(equal)) {
-            typed = propertyType.convert(equal);
+        if (propertyType.isValidValue(String.valueOf(equal))) {
+            typed = propertyType.convert(String.valueOf(equal));
         } else {
             throw new ConstraintValueDoNotMatchPropertyTypeException(
                 "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">");
@@ -82,6 +84,29 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria
 
     @Override
     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
-        return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", equal);
+        return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", String.valueOf(equal));
+    }
+
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">");
+        }
+        if (equal == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "equal constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        return toscaType.isValueTypeValid(equal);
+    }
+
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            equal = toscaType.convert(String.valueOf(equal));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">");
+        }
     }
 }
index 00a8461..b45cf97 100644 (file)
@@ -37,11 +37,11 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraint
 public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstraint {
 
     @NotNull
-    private String greaterOrEqual;
+    private Object greaterOrEqual;
 
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        initialize(greaterOrEqual, propertyType);
+        initialize(String.valueOf(greaterOrEqual), propertyType);
     }
 
     @Override
@@ -62,6 +62,36 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai
 
     @Override
     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
-        return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", greaterOrEqual);
+        return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", String.valueOf(greaterOrEqual));
+    }
+
+    @Override
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">");
+        }
+        if (greaterOrEqual == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "greaterOrEqual constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        return toscaType.isValueTypeValid(greaterOrEqual);
+    }
+
+    @Override
+    public String getConstraintValueAsString() {
+        return String.valueOf(greaterOrEqual);
+    }
+
+    @Override
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            greaterOrEqual = toscaType.convert(String.valueOf(greaterOrEqual));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">");
+        }
     }
 }
index a819a8e..9d638cf 100644 (file)
@@ -20,6 +20,9 @@
 package org.openecomp.sdc.be.model.tosca.constraints;
 
 import javax.validation.constraints.NotNull;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
@@ -28,18 +31,17 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
 
-public class GreaterThanConstraint extends AbstractComparablePropertyConstraint {
+@Getter
+@Setter
+@AllArgsConstructor
+public class GreaterThanConstraint<T> extends AbstractComparablePropertyConstraint {
 
     @NotNull
-    private String greaterThan;
-
-    public GreaterThanConstraint(String greaterThan) {
-        this.greaterThan = greaterThan;
-    }
+    private T greaterThan;
 
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        initialize(greaterThan, propertyType);
+        initialize(String.valueOf(greaterThan), propertyType);
     }
 
     @Override
@@ -58,16 +60,38 @@ public class GreaterThanConstraint extends AbstractComparablePropertyConstraint
         }
     }
 
-    public String getGreaterThan() {
-        return greaterThan;
+    @Override
+    public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
+        return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", String.valueOf(greaterThan));
+    }
+
+    @Override
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "greaterThan constraint has invalid values <" + greaterThan.toString() + "> property type is <" + propertyType + ">");
+        }
+        if (greaterThan == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "greaterThan constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        return toscaType.isValueTypeValid(greaterThan);
     }
 
-    public void setGreaterThan(String greaterThan) {
-        this.greaterThan = greaterThan;
+    @Override
+    public String getConstraintValueAsString() {
+        return String.valueOf(greaterThan);
     }
 
     @Override
-    public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
-        return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", greaterThan);
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            greaterThan = (T) toscaType.convert(String.valueOf(greaterThan));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                "greaterThan constraint has invalid values <" + greaterThan.toString() + "> property type is <" + propertyType + ">");
+        }
     }
 }
index ad871e5..2256f7d 100644 (file)
@@ -22,6 +22,10 @@ package org.openecomp.sdc.be.model.tosca.constraints;
 import com.google.common.collect.Lists;
 import java.util.List;
 import javax.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.NonNull;
+import lombok.Setter;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
@@ -30,19 +34,20 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
 
+@NoArgsConstructor
+@Getter
+@Setter
 public class InRangeConstraint extends AbstractPropertyConstraint {
 
-    private List<String> inRange;
+    @NonNull
+    private List<Object> inRange;
     private Comparable min;
     private Comparable max;
 
-    public InRangeConstraint(List<String> inRange) {
+    public InRangeConstraint(List<Object> inRange) {
         this.inRange = inRange;
     }
 
-    public InRangeConstraint() {
-    }
-
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
         // Perform verification that the property type is supported for
@@ -52,8 +57,8 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
         if (inRange == null || inRange.size() != 2) {
             throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
         }
-        String minRawText = inRange.get(0);
-        String maxRawText = inRange.get(1);
+        String minRawText = String.valueOf(inRange.get(0));
+        String maxRawText = String.valueOf(inRange.get(1));
         if (!propertyType.isValidValue(minRawText)) {
             throw new ConstraintValueDoNotMatchPropertyTypeException(
                 "Invalid min value for in range constraint [" + minRawText + "] as it does not follow the property type [" + propertyType + "]");
@@ -90,7 +95,7 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
     }
 
     @NotNull
-    public String getRangeMinValue() {
+    public Object getRangeMinValue() {
         if (inRange != null) {
             return inRange.get(0);
         } else {
@@ -98,16 +103,16 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
         }
     }
 
-    public void setRangeMinValue(String minValue) {
+    public void setRangeMinValue(Object minValue) {
         if (inRange == null) {
-            inRange = Lists.newArrayList(minValue, "");
+            inRange = Lists.newArrayList(minValue, null);
         } else {
             inRange.set(0, minValue);
         }
     }
 
     @NotNull
-    public String getRangeMaxValue() {
+    public Object getRangeMaxValue() {
         if (inRange != null) {
             return inRange.get(1);
         } else {
@@ -115,9 +120,9 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
         }
     }
 
-    public void setRangeMaxValue(String maxValue) {
+    public void setRangeMaxValue(Object maxValue) {
         if (inRange == null) {
-            inRange = Lists.newArrayList("", maxValue);
+            inRange = Lists.newArrayList(null, maxValue);
         } else {
             inRange.set(1, maxValue);
         }
@@ -128,4 +133,32 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
         return getErrorMessage(toscaType, e, propertyName, "%s property value must be between >= [%s] and <= [%s]", String.valueOf(min),
             String.valueOf(max));
     }
+
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");
+        }
+        if (inRange == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                "inRange constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        for (Object value : inRange) {
+            if (!toscaType.isValueTypeValid(value)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            inRange.replaceAll(obj -> toscaType.convert(String.valueOf(obj)));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");
+        }
+    }
 }
index 86c6383..0f64a4f 100644 (file)
@@ -20,6 +20,9 @@
 package org.openecomp.sdc.be.model.tosca.constraints;
 
 import javax.validation.constraints.NotNull;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
@@ -28,18 +31,17 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
 
-public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint {
+@Getter
+@Setter
+@AllArgsConstructor
+public class LessOrEqualConstraint<T> extends AbstractComparablePropertyConstraint {
 
     @NotNull
-    private String lessOrEqual;
-
-    public LessOrEqualConstraint(String lessOrEqual) {
-        this.lessOrEqual = lessOrEqual;
-    }
+    private T lessOrEqual;
 
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        initialize(lessOrEqual, propertyType);
+        initialize(String.valueOf(lessOrEqual), propertyType);
     }
 
     @Override
@@ -58,16 +60,38 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint
         }
     }
 
-    public String getLessOrEqual() {
-        return lessOrEqual;
+    @Override
+    public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
+        return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", String.valueOf(lessOrEqual));
+    }
+
+    @Override
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "lessOrEqual constraint has invalid values <" + lessOrEqual.toString() + "> property type is <" + propertyType + ">");
+        }
+        if (lessOrEqual == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "lessOrEqual constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        return toscaType.isValueTypeValid(lessOrEqual);
     }
 
-    public void setLessOrEqual(String lessOrEqual) {
-        this.lessOrEqual = lessOrEqual;
+    @Override
+    public String getConstraintValueAsString() {
+        return String.valueOf(lessOrEqual);
     }
 
     @Override
-    public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
-        return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", lessOrEqual);
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            lessOrEqual = (T) toscaType.convert(String.valueOf(lessOrEqual));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                "lessOrEqual constraint has invalid values <" + lessOrEqual.toString() + "> property type is <" + propertyType + ">");
+        }
     }
 }
index 740ee8b..92ab927 100644 (file)
@@ -22,6 +22,7 @@ package org.openecomp.sdc.be.model.tosca.constraints;
 import javax.validation.constraints.NotNull;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
+import lombok.Setter;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
@@ -31,15 +32,16 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolatio
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
 
 @Getter
+@Setter
 @AllArgsConstructor
 public class LessThanConstraint extends AbstractComparablePropertyConstraint {
 
     @NotNull
-    private String lessThan;
+    private Object lessThan;
 
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        initialize(lessThan, propertyType);
+        initialize(String.valueOf(lessThan), propertyType);
     }
 
     @Override
@@ -60,6 +62,36 @@ public class LessThanConstraint extends AbstractComparablePropertyConstraint {
 
     @Override
     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
-        return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", lessThan);
+        return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", String.valueOf(lessThan));
+    }
+
+    @Override
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">");
+        }
+        if (lessThan == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "lessThan constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        return toscaType.isValueTypeValid(lessThan);
+    }
+
+    @Override
+    public String getConstraintValueAsString() {
+        return String.valueOf(lessThan);
+    }
+
+    @Override
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            lessThan = toscaType.convert(String.valueOf(lessThan));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">");
+        }
     }
 }
index 1cafc21..f6bc589 100644 (file)
@@ -25,6 +25,10 @@ import com.google.common.collect.Sets;
 import java.util.List;
 import java.util.Set;
 import javax.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
 import org.openecomp.sdc.be.dao.api.ActionStatus;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
@@ -34,20 +38,20 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
 
+@NoArgsConstructor
 public class ValidValuesConstraint extends AbstractPropertyConstraint {
 
     private static final String PROPERTY_TYPE_IS = "> property type is <";
+    @Getter
+    @Setter
     @NotNull
-    private List<String> validValues;
+    private List<Object> validValues;
     private Set<Object> validValuesTyped;
 
-    public ValidValuesConstraint(List<String> validValues) {
+    public ValidValuesConstraint(List<Object> validValues) {
         this.validValues = validValues;
     }
 
-    public ValidValuesConstraint() {
-    }
-
     @Override
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
         validValuesTyped = Sets.newHashSet();
@@ -55,12 +59,12 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
             throw new ConstraintValueDoNotMatchPropertyTypeException(
                 "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">");
         }
-        for (String value : validValues) {
-            if (!propertyType.isValidValue(value)) {
+        for (Object value : validValues) {
+            if (!propertyType.isValidValue(String.valueOf(value))) {
                 throw new ConstraintValueDoNotMatchPropertyTypeException(
                     "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">");
             } else {
-                validValuesTyped.add(propertyType.convert(value));
+                validValuesTyped.add(propertyType.convert(String.valueOf(value)));
             }
         }
     }
@@ -75,8 +79,8 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
             throw new ConstraintValueDoNotMatchPropertyTypeException(
                 "validValues constraint has invalid value <> property type is <" + propertyType + ">");
         }
-        for (String value : validValues) {
-            if (!toscaType.isValidValue(value)) {
+        for (Object value : validValues) {
+            if (!toscaType.isValidValue(String.valueOf(value))) {
                 throw new ConstraintValueDoNotMatchPropertyTypeException(
                     "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType + ">");
             }
@@ -105,14 +109,6 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
         }
     }
 
-    public List<String> getValidValues() {
-        return validValues;
-    }
-
-    public void setValidValues(List<String> validValues) {
-        this.validValues = validValues;
-    }
-
     @Override
     public ConstraintType getConstraintType() {
         return ConstraintType.VALID_VALUES;
@@ -120,6 +116,34 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
 
     @Override
     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
-        return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", validValues));
+        return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", String.valueOf(validValues)));
+    }
+
+    public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        if (toscaType == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "validValues constraint has invalid values <" + validValues + "> property type is <" + propertyType + ">");
+        }
+        if (validValues == null) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "validValues constraint has invalid value <> property type is <" + propertyType + ">");
+        }
+        for (Object value : validValues) {
+            if (!toscaType.isValueTypeValid(value)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        ToscaType toscaType = ToscaType.getToscaType(propertyType);
+        try {
+            validValues.replaceAll(obj -> toscaType.convert(String.valueOf(obj)));
+        } catch (Exception e) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException(
+                    "validValues constraint has invalid values <" + validValues + "> property type is <" + propertyType + ">");
+        }
     }
 }
index 8b8e813..39f9118 100644 (file)
@@ -523,7 +523,7 @@ public class CapabilityTypeOperationTest extends ModelTestBase {
         property2.setDescription("Number of (actual or virtual) CPUs associated with the Compute node.");
         property2.setType(ToscaType.INTEGER.name().toLowerCase());
         List<PropertyConstraint> constraints3 = new ArrayList<>();
-        List<String> range = new ArrayList<>();
+        List<Object> range = new ArrayList<>();
         range.add("1");
         range.add("4");
 
index 33322ec..3f78ff0 100644 (file)
@@ -166,17 +166,17 @@ public class PropertyOperationTest extends ModelTestBase {
         constraints.add(propertyConstraint10);
         return constraints;
     }
-    
+
     private InRangeConstraint buildInRangeConstraint() {
-        List<String> range = new ArrayList<>();
+        List<Object> range = new ArrayList<>();
         range.add("23");
         range.add("67");
         InRangeConstraint inRangeConstraint = new InRangeConstraint(range);
         return inRangeConstraint;
     }
-    
+
     private ValidValuesConstraint buildValidValuesConstraint() {
-        List<String> validValues = new ArrayList<>();
+        List<Object> validValues = new ArrayList<>();
         validValues.add("abc");
         validValues.add("def");
         validValues.add("fhi");
index 73a6007..5f8bde8 100644 (file)
@@ -302,7 +302,7 @@ public class RelationshipTypeOperationTest extends ModelTestBase {
         property2.setDescription("Number of (actual or virtual) CPUs associated with the Compute node.");
         property2.setType(ToscaType.INTEGER.name().toLowerCase());
         List<PropertyConstraint> constraints3 = new ArrayList<>();
-        List<String> range = new ArrayList<>();
+        List<Object> range = new ArrayList<>();
         range.add("4");
         range.add("1");
         InRangeConstraint propertyConstraint3 = new InRangeConstraint(range);
@@ -593,7 +593,7 @@ public class RelationshipTypeOperationTest extends ModelTestBase {
         propertyDefinition.setDescription(PROP + "_" + value);
         propertyDefinition.setType(ToscaType.INTEGER.name().toLowerCase());
         List<PropertyConstraint> constraints = new ArrayList<>();
-        List<String> range = new ArrayList<>();
+        List<Object> range = new ArrayList<>();
         range.add("1");
         range.add("4");
         InRangeConstraint propertyConstraint = new InRangeConstraint(range);
index 6d9a4c4..7f35aff 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 
 public class EqualConstraintTest {
 
-       private EqualConstraint createTestSubject() {
-               return new EqualConstraint("");
-       }
+    private EqualConstraint createStringTestSubject() {
+        return new EqualConstraint("test");
+    }
 
-       
+    private EqualConstraint createIntegerTestSubject() {
+        return new EqualConstraint(418);
+    }
 
-       
-       @Test
-       public void testValidate() throws Exception {
-               EqualConstraint testSubject;
-               Object propertyValue = null;
+    @Test
+    public void testGetEqual() {
+        EqualConstraint testSubject = createStringTestSubject();
+        Object result = testSubject.getEqual();
 
-               // test 1
-               testSubject = createTestSubject();
-               propertyValue = null;
-               testSubject.validate(propertyValue);
-       }
+        assertEquals("test", result);
+    }
 
-       
+    @Test
+    public void testSetEqual() {
+        EqualConstraint testSubject = createStringTestSubject();
+        testSubject.setEqual("test2");
+        Object result = testSubject.getEqual();
 
+        assertEquals("test2", result);
+    }
+
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        EqualConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        EqualConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        EqualConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        EqualConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        EqualConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
+
+        String expectedMessage =
+            "equal constraint has invalid values <" + testSubject.getEqual() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
+
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
+
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        EqualConstraint testSubject = createIntegerTestSubject();
+
+        testSubject.changeConstraintValueTypeTo("string");
+        Object result = testSubject.getEqual();
+
+        assertTrue(result instanceof String);
+    }
 }
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java
new file mode 100644 (file)
index 0000000..d5d8fa7
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 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.openecomp.sdc.be.model.tosca.constraints;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
+
+public class GreaterOrEqualConstraintTest {
+
+    private GreaterOrEqualConstraint createStringTestSubject() {
+        return new GreaterOrEqualConstraint("test");
+    }
+
+    private GreaterOrEqualConstraint createIntegerTestSubject() {
+        return new GreaterOrEqualConstraint(418);
+    }
+
+    @Test
+    public void testGetGreaterOrEqualThan() {
+        GreaterOrEqualConstraint testSubject = createStringTestSubject();
+        Object result = testSubject.getGreaterOrEqual();
+
+        assertEquals("test", result);
+    }
+
+    @Test
+    public void testSetGreaterOrEqual() {
+        GreaterOrEqualConstraint testSubject = createStringTestSubject();
+        testSubject.setGreaterOrEqual("test2");
+        Object result = testSubject.getGreaterOrEqual();
+
+        assertEquals("test2", result);
+    }
+
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterOrEqualConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterOrEqualConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterOrEqualConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterOrEqualConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        GreaterOrEqualConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
+
+        String expectedMessage =
+            "greaterOrEqual constraint has invalid values <" + testSubject.getGreaterOrEqual() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
+
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
+
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterOrEqualConstraint testSubject = createIntegerTestSubject();
+
+        testSubject.changeConstraintValueTypeTo("string");
+        Object result = testSubject.getGreaterOrEqual();
+
+        assertTrue(result instanceof String);
+    }
+}
index dd79d80..41bc94f 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 
 public class GreaterThanConstraintTest {
 
-       private GreaterThanConstraint createTestSubject() {
-               return new GreaterThanConstraint("");
-       }
+    private GreaterThanConstraint createStringTestSubject() {
+        return new GreaterThanConstraint("test");
+    }
+
+    private GreaterThanConstraint createIntegerTestSubject() {
+        return new GreaterThanConstraint(418);
+    }
+
+    @Test
+    public void testGetGreaterThan() {
+        GreaterThanConstraint testSubject = createStringTestSubject();
+        Object result = testSubject.getGreaterThan();
+
+        assertEquals("test", result);
+    }
+
+    @Test
+    public void testSetGreaterThan() {
+        GreaterThanConstraint testSubject = createStringTestSubject();
+        testSubject.setGreaterThan("test2");
+        Object result = testSubject.getGreaterThan();
+
+        assertEquals("test2", result);
+    }
+
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterThanConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterThanConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterThanConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertTrue(validTypes);
+    }
 
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterThanConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertFalse(validTypes);
+    }
 
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        GreaterThanConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
 
-       
+        String expectedMessage =
+            "greaterThan constraint has invalid values <" + testSubject.getGreaterThan() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
 
-       
-       @Test
-       public void testGetGreaterThan() throws Exception {
-               GreaterThanConstraint testSubject;
-               String result;
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
 
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getGreaterThan();
-       }
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        GreaterThanConstraint testSubject = createIntegerTestSubject();
 
-       
-       @Test
-       public void testSetGreaterThan() throws Exception {
-               GreaterThanConstraint testSubject;
-               String greaterThan = "";
+        testSubject.changeConstraintValueTypeTo("string");
+        Object result = testSubject.getGreaterThan();
 
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setGreaterThan(greaterThan);
-       }
+        assertTrue(result instanceof String);
+    }
 }
index 9d14bfc..8eff6f7 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 
 public class InRangeConstraintTest {
 
-       private InRangeConstraint createTestSubject() {
-               return new InRangeConstraint(null);
-       }
-
-       
-
-
-       
-       @Test
-       public void testGetRangeMinValue() throws Exception {
-               InRangeConstraint testSubject;
-               String result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getRangeMinValue();
-       }
-
-       
-       @Test
-       public void testSetRangeMinValue() throws Exception {
-               InRangeConstraint testSubject;
-               String minValue = "";
-
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setRangeMinValue(minValue);
-       }
-
-       
-       @Test
-       public void testGetRangeMaxValue() throws Exception {
-               InRangeConstraint testSubject;
-               String result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getRangeMaxValue();
-       }
-
-       
-       @Test
-       public void testSetRangeMaxValue() throws Exception {
-               InRangeConstraint testSubject;
-               String maxValue = "";
-
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setRangeMaxValue(maxValue);
-       }
+    private InRangeConstraint createStringTestSubject() {
+        List<Object> validValues = new ArrayList<>();
+        validValues.add("test1");
+        validValues.add("test10");
+        return new InRangeConstraint(validValues);
+    }
+
+    private InRangeConstraint createIntegerTestSubject() {
+        List<Object> validValues = new ArrayList<>();
+        validValues.add(1);
+        validValues.add(10);
+        return new InRangeConstraint(validValues);
+    }
+
+    @Test
+    public void testGetInRange() {
+        InRangeConstraint testSubject = createStringTestSubject();
+        List<Object> result = testSubject.getInRange();
+
+        assertFalse(result.isEmpty());
+        assertEquals("test1", result.get(0));
+        assertEquals("test10", result.get(1));
+    }
+
+    @Test
+    public void testSetInRange() {
+        InRangeConstraint testSubject = createStringTestSubject();
+        List<Object> validValues = new ArrayList<>();
+        validValues.add("test21");
+        validValues.add("test30");
+        testSubject.setInRange(validValues);
+        List<Object> result = testSubject.getInRange();
+
+        assertEquals(2, result.size());
+        assertEquals("test21", result.get(0));
+        assertEquals("test30", result.get(1));
+    }
+
+    @Test
+    public void testGetRangeMinValue() throws Exception {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        Object result = testSubject.getRangeMinValue();
+
+        assertEquals(1, result);
+    }
+
+    @Test
+    public void testSetRangeMinValue() throws Exception {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        testSubject.setRangeMinValue(21);
+
+        Object result = testSubject.getRangeMinValue();
+
+        assertEquals(21, result);
+    }
+
+    @Test
+    public void testGetRangeMaxValue() throws Exception {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        Object result = testSubject.getRangeMaxValue();
+
+        assertEquals(10, result);
+    }
+
+    @Test
+    public void testSetRangeMaxValue() throws Exception {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        testSubject.setRangeMaxValue(30);
+
+        Object result = testSubject.getRangeMaxValue();
+
+        assertEquals(30, result);
+    }
+
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        InRangeConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        InRangeConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        InRangeConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
+        String expectedMessage =
+            "inRange constraint has invalid values <" + testSubject.getInRange() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
+
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
+
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        InRangeConstraint testSubject = createIntegerTestSubject();
+        testSubject.changeConstraintValueTypeTo("string");
+        List<Object> result = testSubject.getInRange();
+
+        result.forEach(value -> assertTrue(value instanceof String));
+    }
 }
index ef567d9..aae21f0 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 
 public class LessOrEqualConstraintTest {
 
-       private LessOrEqualConstraint createTestSubject() {
-               return new LessOrEqualConstraint("");
-       }
+    private LessOrEqualConstraint createStringTestSubject() {
+        return new LessOrEqualConstraint("test");
+    }
 
-       
+    private LessOrEqualConstraint createIntegerTestSubject() {
+        return new LessOrEqualConstraint(418);
+    }
 
-       
+    @Test
+    public void testGetLessOrEqualThan() {
+        LessOrEqualConstraint testSubject = createStringTestSubject();
+        Object result = testSubject.getLessOrEqual();
 
+        assertEquals("test", result);
+    }
 
-       
-       @Test
-       public void testGetLessOrEqual() throws Exception {
-               LessOrEqualConstraint testSubject;
-               String result;
+    @Test
+    public void testSetLessOrEqual() {
+        LessOrEqualConstraint testSubject = createStringTestSubject();
+        testSubject.setLessOrEqual("test2");
+        Object result = testSubject.getLessOrEqual();
 
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getLessOrEqual();
-       }
+        assertEquals("test2", result);
+    }
 
-       
-       @Test
-       public void testSetLessOrEqual() throws Exception {
-               LessOrEqualConstraint testSubject;
-               String lessOrEqual = "";
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessOrEqualConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertTrue(validTypes);
+    }
 
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setLessOrEqual(lessOrEqual);
-       }
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessOrEqualConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessOrEqualConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessOrEqualConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        LessOrEqualConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
+
+        String expectedMessage =
+            "lessOrEqual constraint has invalid values <" + testSubject.getLessOrEqual() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
+
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
+
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessOrEqualConstraint testSubject = createIntegerTestSubject();
+
+        testSubject.changeConstraintValueTypeTo("string");
+        Object result = testSubject.getLessOrEqual();
+
+        assertTrue(result instanceof String);
+    }
 }
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java
new file mode 100644 (file)
index 0000000..6736452
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 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.openecomp.sdc.be.model.tosca.constraints;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
+
+public class LessThanConstraintTest {
+
+    private LessThanConstraint createStringTestSubject() {
+        return new LessThanConstraint("test");
+    }
+
+    private LessThanConstraint createIntegerTestSubject() {
+        return new LessThanConstraint(418);
+    }
+
+    @Test
+    public void testGetLessThan() {
+        LessThanConstraint testSubject = createStringTestSubject();
+        Object result = testSubject.getLessThan();
+
+        assertEquals("test", result);
+    }
+
+    @Test
+    public void testSetLessThan() {
+        LessThanConstraint testSubject = createStringTestSubject();
+        testSubject.setLessThan("test2");
+        Object result = testSubject.getLessThan();
+
+        assertEquals("test2", result);
+    }
+
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessThanConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessThanConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessThanConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessThanConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        LessThanConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
+
+        String expectedMessage =
+            "lessThan constraint has invalid values <" + testSubject.getLessThan() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
+
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
+
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        LessThanConstraint testSubject = createIntegerTestSubject();
+
+        testSubject.changeConstraintValueTypeTo("string");
+        Object result = testSubject.getLessThan();
+
+        assertTrue(result instanceof String);
+    }
+}
index 69f67b3..01450e2 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
-import java.util.List;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import org.junit.Test;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 
 
 public class ValidValuesConstraintTest {
 
-       private ValidValuesConstraint createTestSubject() {
-               return new ValidValuesConstraint(null);
-       }
+    private ValidValuesConstraint createStringTestSubject() {
+        List<Object> validValues = new ArrayList<>();
+        validValues.add("test1");
+        validValues.add("test2");
+        validValues.add("test3");
+        validValues.add("test4");
+        return new ValidValuesConstraint(validValues);
+    }
+
+    private ValidValuesConstraint createIntegerTestSubject() {
+        List<Object> validValues = new ArrayList<>();
+        validValues.add(1);
+        validValues.add(2);
+        validValues.add(3);
+        validValues.add(4);
+        return new ValidValuesConstraint(validValues);
+    }
+
+    @Test
+    public void testGetValidValues() {
+        ValidValuesConstraint testSubject = createStringTestSubject();
+        List<Object> result = testSubject.getValidValues();
+
+        assertFalse(result.isEmpty());
+        assertEquals("test1", result.get(0));
+        assertEquals("test4", result.get(3));
+    }
+
+    @Test
+    public void testSetValidValues() {
+        ValidValuesConstraint testSubject = createStringTestSubject();
+        List<Object> validValues = new ArrayList<>();
+        validValues.add("test5");
+        validValues.add("test6");
+        validValues.add("test7");
+        validValues.add("test8");
+        testSubject.setValidValues(validValues);
+
+        List<Object> result = testSubject.getValidValues();
+
+        assertEquals(4, result.size());
+        assertEquals("test5", result.get(0));
+        assertEquals("test8", result.get(3));
+    }
+
+    @Test
+    public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        ValidValuesConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertTrue(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        ValidValuesConstraint testSubject = createStringTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertFalse(validTypes);
+    }
+
+    @Test
+    public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        ValidValuesConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("integer");
+        assertTrue(validTypes);
+    }
 
-       
+    @Test
+    public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+        ValidValuesConstraint testSubject = createIntegerTestSubject();
+        Boolean validTypes = testSubject.validateValueType("string");
+        assertFalse(validTypes);
+    }
 
-       
+    @Test
+    public void testChangeStringConstraintValueTypeToIntegerThrow() {
+        String propertyType = "integer";
+        ValidValuesConstraint testSubject = createStringTestSubject();
+        Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
+            testSubject.changeConstraintValueTypeTo(propertyType);
+        });
 
+        String expectedMessage =
+            "validValues constraint has invalid values <" + testSubject.getValidValues() + "> property type is <" + propertyType + ">";
+        String actualMessage = exception.getMessage();
 
-       
-       @Test
-       public void testGetValidValues() throws Exception {
-               ValidValuesConstraint testSubject;
-               List<String> result;
+        assertTrue(actualMessage.contains(expectedMessage));
+    }
 
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getValidValues();
-       }
+    @Test
+    public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+        ValidValuesConstraint testSubject = createIntegerTestSubject();
 
-       
-       @Test
-       public void testSetValidValues() throws Exception {
-               ValidValuesConstraint testSubject;
-               List<String> validValues = null;
+        testSubject.changeConstraintValueTypeTo("string");
+        List<Object> result = testSubject.getValidValues();
 
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setValidValues(validValues);
-       }
+        result.forEach(value -> assertTrue(value instanceof String));
+    }
 }