Fix valid values for list and map 85/133385/3
authorMichaelMorris <michael.morris@est.tech>
Wed, 22 Feb 2023 14:33:47 +0000 (14:33 +0000)
committerFrancisco Javier Paradela Vila <javier.paradela.vila@est.tech>
Fri, 24 Feb 2023 11:31:10 +0000 (11:31 +0000)
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Issue-ID: SDC-4412
Change-Id: I672193784c51250173b7e5c4c43354a0b9852c21

catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.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/constraints/AbstractPropertyConstraint.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/tosca/constraints/AbstractStringPropertyConstraintTest.java

index 4893c02..247fff9 100644 (file)
@@ -144,7 +144,7 @@ public class PropertyValueConstraintValidationUtil {
             for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
                 try {
                     propertyConstraint.initialize(toscaType, propertyDefinition.getSchema());
-                    propertyConstraint.validate(toscaType, propertyDefinition.getSchema(), propertyDefinition.getValue());
+                    propertyConstraint.validate(propertyDefinition);
                 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
                     errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
                 } catch (IllegalArgumentException ie) {
index df961ac..51f0dbc 100644 (file)
@@ -34,7 +34,7 @@ public interface PropertyConstraint {
     
     void validate(Object propertyValue) throws ConstraintViolationException;
 
-    void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException;
+    void validate(PropertyDefinition property) throws ConstraintViolationException;
 
     @JsonIgnore
     ConstraintType getConstraintType();
index 2d88f38..dbe11a8 100644 (file)
@@ -2464,8 +2464,8 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
                 if (rangeArray.size() != 2) {
                     log.error("The range constraint content is invalid. value = {}", value);
                 } else {
-                    final String minValue = rangeArray.get(0).asText();
-                    final String maxValue = rangeArray.get(1).asText();
+                    final Object minValue = convertToType(rangeArray.get(0));
+                    final Object maxValue = convertToType(rangeArray.get(1));
                     final Comparable min = ConstraintUtil.convertToComparable(
                         ToscaType.getToscaType(minValue.getClass().getSimpleName().toLowerCase()), String.valueOf(minValue));
                     final Comparable max = ConstraintUtil.convertToComparable(
index b8e2e26..8e09648 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Arrays;
 
 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
 import org.openecomp.sdc.be.model.PropertyConstraint;
+import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
@@ -34,8 +35,8 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint {
     private static final String INVALID_VALUE_ERROR_MESSAGE = "Unsupported value provided for %s property supported value type is %s.";
 
     @Override
-    public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException {
-        validate(toscaType, propertyTextValue);
+    public void validate(PropertyDefinition property) throws ConstraintViolationException {
+        validate(ToscaType.isValidType(property.getType()), property.getValue());
     }
     
     protected void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException {
index 4970808..ea7642b 100644 (file)
@@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.tosca.constraints;
 
 import static java.util.stream.Collectors.toList;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 
@@ -38,6 +39,7 @@ import org.openecomp.sdc.be.dao.api.ActionStatus;
 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
+import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.tosca.ToscaType;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
@@ -113,18 +115,24 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint {
     }
     
     @Override
-    public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException {
+    public void validate(PropertyDefinition propertyDefinition) throws ConstraintViolationException {
+        ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType());
         try {
             Collection<Object> valuesToValidate;
             if (ToscaType.LIST == toscaType) {
-                valuesToValidate = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {});
+                valuesToValidate = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
             } else if (ToscaType.MAP == toscaType) {
-                final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {});
+                final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
                 valuesToValidate = map.values();
             } else {
-                valuesToValidate = Collections.singleton(propertyTextValue);
+                valuesToValidate = Collections.singleton(propertyDefinition.getValue());
             }
-            ToscaType valuesType = getValuesType(toscaType, schema);
+            if (propertyDefinition.getSubPropertyToscaFunctions() != null) {
+                propertyDefinition.getSubPropertyToscaFunctions().forEach(subPropToscaFunction -> {
+                    valuesToValidate.remove(subPropToscaFunction.getToscaFunction().getJsonObjectValue());
+                });
+            }
+            ToscaType valuesType = getValuesType(toscaType, propertyDefinition.getSchema());
             for (final Object value: valuesToValidate) {
                 validate(valuesType, value.toString());
             }
index 595437f..be814af 100644 (file)
@@ -62,7 +62,7 @@ public class AbstractStringPropertyConstraintTest {
     @Test(expected = ConstraintViolationException.class)
     public void testValidateNull() throws ConstraintViolationException {
         // when
-        constraint.validate(null);
+        constraint.validate((String)null);
     }
 
     @Test(expected = ConstraintViolationException.class)