Fix error validation equals constraint 40/133340/3
authorMichaelMorris <michael.morris@est.tech>
Fri, 17 Feb 2023 16:55:53 +0000 (16:55 +0000)
committerJEFF VAN DAM <jeff.van.dam@est.tech>
Wed, 22 Feb 2023 14:22:15 +0000 (14:22 +0000)
Error thrown when validating value for list property with an equal constraint, even when valid valid is given

Signed-off-by: MichaelMorris <michael.morris@est.tech>
Issue-ID: SDC-4399
Change-Id: Iec1746561351acc026050bf8571c060cc5705c2e

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/constraints/ConstraintUtil.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java

index 8756ac4..2d88f38 100644 (file)
@@ -2503,6 +2503,12 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
                 }
             } else if (jsonElement.getNodeType().equals(JsonNodeType.BOOLEAN)) {
                 return jsonElement.asBoolean();
+            } else if (jsonElement.getNodeType().equals(JsonNodeType.ARRAY)) {
+                List<Object> listValues = new ArrayList<>();
+                for (JsonNode jsonArrayElement : jsonElement) {
+                    listValues.add(convertToType(jsonArrayElement));
+                }
+                return listValues;
             } else {
                 return jsonElement.asText();
             }
index fee828c..83b3ab8 100644 (file)
@@ -53,16 +53,8 @@ public final class ConstraintUtil {
             throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid property type <" + propertyType.toString() + ">");
         }
     }
-
-    /**
-     * Verify that the given tosca type is supported for comparison
-     *
-     * @param propertyType the tosca type to check
-     * @throws ConstraintValueDoNotMatchPropertyTypeException if the property type cannot be compared
-     */
-    public static void checkComparableType(final ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        // The validity of the value is already assured by us with our ToscaType.convert() method
-        // here we just want to check that the constraint is not used on unsupported type as boolean
+    
+    public static boolean isComparableType(final ToscaType propertyType)  {
         final ToscaType toscaType = ToscaType.getToscaType(propertyType.getType());
         switch (toscaType) {
             case FLOAT:
@@ -76,12 +68,21 @@ public final class ConstraintUtil {
             case SCALAR_UNIT_TIME:
             case SCALAR_UNIT_BITRATE:
             case SCALAR_UNIT_FREQUENCY:
-                break;
-            case BOOLEAN:
-            case SCALAR_UNIT:
-                throw new ConstraintValueDoNotMatchPropertyTypeException("Constraint is invalid for property type <" + propertyType.getType() + ">");
+                return true;
             default:
-                throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid property type <" + propertyType.getType() + ">");
+                return false;
+        }
+    }
+
+    /**
+     * Verify that the given tosca type is supported for comparison
+     *
+     * @param propertyType the tosca type to check
+     * @throws ConstraintValueDoNotMatchPropertyTypeException if the property type cannot be compared
+     */
+    public static void checkComparableType(final ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+        if (!isComparableType(propertyType)) {
+            throw new ConstraintValueDoNotMatchPropertyTypeException("Constraint is invalid for property type <" + propertyType.getType() + ">");
         }
     }
 
index 3017a05..a565580 100644 (file)
@@ -20,6 +20,8 @@
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
+import java.util.List;
+
 import javax.validation.constraints.NotNull;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
@@ -52,10 +54,9 @@ public class EqualConstraint extends AbstractComparablePropertyConstraint {
     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
         if (propertyType.isValidValue(String.valueOf(equal))) {
             typed = propertyType.convert(String.valueOf(equal));
-            if (propertyType.equals(ToscaType.BOOLEAN)) {
-                return;
+            if (ConstraintUtil.isComparableType(propertyType)) {
+                initialize(String.valueOf(equal), propertyType);
             }
-            initialize(String.valueOf(equal), propertyType);
         } else {
             throw new ConstraintValueDoNotMatchPropertyTypeException(
                 "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">");
@@ -104,11 +105,15 @@ public class EqualConstraint extends AbstractComparablePropertyConstraint {
         if (propertyValue == null) {
             throw new ConstraintViolationException("Value to check is null");
         }
-        if (!(propertyValue instanceof Boolean)) {
+        if (isComparableValue(propertyValue)) {
             super.validate(propertyValue);
         }
         doValidate(propertyValue);
     }
+    
+    private boolean isComparableValue(Object propertyValue) {
+        return Comparable.class.isAssignableFrom(propertyValue.getClass());
+    }
 
     public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
         ToscaType toscaType = ToscaType.getToscaType(propertyType);