[BUG] Fix error when adding valid_values constraint 12/134712/8
authoreschcam <cameron.scholes@est.tech>
Mon, 29 May 2023 15:45:58 +0000 (16:45 +0100)
committerMichael Morris <michael.morris@est.tech>
Tue, 13 Jun 2023 10:47:13 +0000 (10:47 +0000)
Issue-ID: SDC-4515
Signed-off-by: eschcam <cameron.scholes@est.tech>
Change-Id: Id8f951053581337504b8d75c76d2409ebd71940e

catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java

index ea7642b..569294d 100644 (file)
@@ -120,12 +120,27 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
         try {
             Collection<Object> valuesToValidate;
             if (ToscaType.LIST == toscaType) {
-                valuesToValidate = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
+                if (propertyDefinition.getValue() != null) {
+                    valuesToValidate = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
+                } else {
+                    valuesToValidate = ConstraintUtil.parseToCollection(propertyDefinition.getDefaultValue(), new TypeReference<>() {});
+                }
             } else if (ToscaType.MAP == toscaType) {
-                final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
+                Map<String, Object> map;
+
+                if (propertyDefinition.getValue() != null) {
+                    map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
+                } else {
+                    map = ConstraintUtil.parseToCollection(propertyDefinition.getDefaultValue(), new TypeReference<>() {});
+                }
+
                 valuesToValidate = map.values();
             } else {
-                valuesToValidate = Collections.singleton(propertyDefinition.getValue());
+                if (propertyDefinition.getValue() != null) {
+                    valuesToValidate = Collections.singleton(propertyDefinition.getValue());
+                } else {
+                    valuesToValidate = Collections.singleton(propertyDefinition.getDefaultValue());
+                }
             }
             if (propertyDefinition.getSubPropertyToscaFunctions() != null) {
                 propertyDefinition.getSubPropertyToscaFunctions().forEach(subPropToscaFunction -> {
@@ -134,7 +149,9 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
             }
             ToscaType valuesType = getValuesType(toscaType, propertyDefinition.getSchema());
             for (final Object value: valuesToValidate) {
-                validate(valuesType, value.toString());
+                if (value != null) {
+                    validate(valuesType, value.toString());
+                }
             }
         } catch (ConstraintValueDoNotMatchPropertyTypeException exception) {
             throw new ConstraintViolationException("Value cannot be parsed to a list", exception);
index 1e03513..f6c63ad 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.openecomp.sdc.be.model.tosca.constraints;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -28,7 +29,11 @@ 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.datatypes.elements.SchemaDefinition;
+import org.openecomp.sdc.be.model.PropertyDefinition;
+import org.openecomp.sdc.be.model.tosca.ToscaType;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
 
 class ValidValuesConstraintTest {
 
@@ -125,4 +130,164 @@ class ValidValuesConstraintTest {
 
         result.forEach(value -> assertTrue(value instanceof String));
     }
+
+    @Test
+    void testValidateConstraintWithoutDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setType(ToscaType.INTEGER.getType());
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
+
+    @Test
+    void testValidateConstraintWithDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setDefaultValue("2");
+        prop.setType(ToscaType.INTEGER.getType());
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
+    @Test
+    void testValidateConstraintWithValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setValue("2");
+        prop.setType(ToscaType.INTEGER.getType());
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
+
+    @Test
+    void testValidateConstraintWithIncorrectDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setDefaultValue("1000");
+        prop.setType(ToscaType.INTEGER.getType());
+
+        assertThrows(ConstraintViolationException.class, () -> {
+            constraint.validate(prop);
+        });
+    }
+
+    @Test
+    void testValidateConstraintWithInvalidDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setDefaultValue("A String");
+        prop.setType(ToscaType.INTEGER.getType());
+
+        assertThrows(NumberFormatException.class, () -> {
+            constraint.validate(prop);
+        });
+    }
+
+    @Test
+    void testValidateConstraintWithNullProp() {
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+
+        assertThrows(NullPointerException.class, () -> {
+            constraint.validate(null);
+        });
+    }
+
+    @Test
+    void testValidateConstraintWithListPropWithDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        PropertyDefinition intProp = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setDefaultValue("[\"2\"]");
+        prop.setType(ToscaType.LIST.getType());
+
+        intProp.setType(ToscaType.INTEGER.getType());
+        schemaDefinition.setProperty(intProp);
+        prop.setSchema(schemaDefinition);
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
+    @Test
+    void testValidateConstraintWithListPropWithValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        PropertyDefinition intProp = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setValue("[\"2\"]");
+        prop.setType(ToscaType.LIST.getType());
+
+        intProp.setType(ToscaType.INTEGER.getType());
+        schemaDefinition.setProperty(intProp);
+        prop.setSchema(schemaDefinition);
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
+
+    @Test
+    void testValidateConstraintWithMapPropWithDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        PropertyDefinition intProp = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setDefaultValue("{\"key\": \"2\"}");
+        prop.setType(ToscaType.MAP.getType());
+
+        intProp.setType(ToscaType.INTEGER.getType());
+        schemaDefinition.setProperty(intProp);
+        prop.setSchema(schemaDefinition);
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
+    @Test
+    void testValidateConstraintWithMapPropWithValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+        PropertyDefinition prop = new PropertyDefinition();
+        PropertyDefinition intProp = new PropertyDefinition();
+        ValidValuesConstraint constraint = createIntegerTestSubject();
+        SchemaDefinition schemaDefinition = new SchemaDefinition();
+        constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+        prop.setValue("{\"key\": \"2\"}");
+        prop.setType(ToscaType.MAP.getType());
+
+        intProp.setType(ToscaType.INTEGER.getType());
+        schemaDefinition.setProperty(intProp);
+        prop.setSchema(schemaDefinition);
+
+        assertDoesNotThrow(() -> {
+            constraint.validate(prop);
+        });
+    }
 }