VFC Property default value enforced forced to comply with restraints 22/134222/5
authorfranciscovila <javier.paradela.vila@est.tech>
Tue, 18 Apr 2023 14:08:07 +0000 (15:08 +0100)
committerMichael Morris <michael.morris@est.tech>
Wed, 26 Apr 2023 07:59:43 +0000 (07:59 +0000)
Issue-ID: SDC-4477
Signed-off-by: franciscovila <javier.paradela.vila@est.tech>
Change-Id: Ib9115aae9019e2d36990147dc0ec9f4058352d88

catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java
catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentPropertyServlet.java
catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ComponentPropertyServletTest.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/ConstraintUtil.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java

index ef7363a..74cf8eb 100644 (file)
@@ -96,7 +96,7 @@ public class PropertyValueConstraintValidationUtil {
         if (propertyDefinition instanceof InputDefinition) {
             return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
         }
-        return StringUtils.isNotEmpty(propertyDefinition.getValue());
+        return StringUtils.isNotEmpty(propertyDefinition.getValue() != null ? propertyDefinition.getValue() : propertyDefinition.getDefaultValue());
     }
 
     private void evaluatePropertyTypeForConstraintValidation(PropertyDefinition propertyDefinition) {
@@ -155,7 +155,8 @@ public class PropertyValueConstraintValidationUtil {
                 }
             }
         } else if (!isValueAToscaFunction(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType())
-                && !propertyDefinition.isToscaFunction() && !toscaType.isValidValue(propertyDefinition.getValue())) {
+                && !propertyDefinition.isToscaFunction() && !toscaType.isValidValue(
+                    propertyDefinition.getValue() != null ? propertyDefinition.getValue() : propertyDefinition.getDefaultValue())) {
             errorMessages.add(String.format("Unsupported value provided for %s property supported value type is %s.",
                 getCompletePropertyName(propertyDefinition), toscaType.getType()));
         }
@@ -245,7 +246,8 @@ public class PropertyValueConstraintValidationUtil {
                 return StringUtils.isNotEmpty(propertyDefinition.getValue()) &&
                     !"null".equals(propertyDefinition.getValue());
             } else if (ToscaType.LIST == ToscaType.isValidType(propertyDefinition.getType())) {
-                Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
+                Collection<?> list = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
+                    propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<List<?>>() {
                 });
                 return CollectionUtils.isNotEmpty(list);
             } else {
@@ -323,7 +325,8 @@ public class PropertyValueConstraintValidationUtil {
             if (propertyDefinition.getSchemaType() == null) {
                 propertyDefinition.setSchema(createStringSchema());
             }
-            Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
+            Collection<?> list = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
+                propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<List<?>>() {});
             final Map<String, Object> map = new HashMap<>();
             int index = 0;
             for (Object obj : list) {
index c58c43d..c291522 100644 (file)
@@ -268,6 +268,16 @@ public class ComponentPropertyServlet extends BeGenericServlet {
                 log.info("Property content is invalid - {}", data);
                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
             }
+
+            //Validate value and Constraint of property and Fetch all data types from cache
+            Either<Boolean, ResponseFormat> constraintValidatorResponse = new PropertyValueConstraintValidationUtil()
+                .validatePropertyConstraints(properties.values(), applicationDataTypeCache,
+                    propertyBusinessLogic.getComponentModelByComponentId(componentId));
+            if (constraintValidatorResponse.isRight()) {
+                log.error("Failed validation value and constraint of property: {}", constraintValidatorResponse.right().value());
+                return buildErrorResponse(constraintValidatorResponse.right().value());
+            }
+
             Map.Entry<String, PropertyDefinition> entry = properties.entrySet().iterator().next();
             PropertyDefinition newPropertyDefinition = entry.getValue();
             newPropertyDefinition.setParentUniqueId(componentId);
index 05e5eed..00688aa 100644 (file)
@@ -25,6 +25,8 @@ import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.when;
 
 import fj.data.Either;
+import java.util.HashMap;
+import java.util.Map;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpSession;
 import javax.ws.rs.core.Response;
@@ -36,13 +38,18 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.Spy;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.openecomp.sdc.be.components.impl.DataTypesService;
 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
 import org.openecomp.sdc.be.dao.api.ActionStatus;
+import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
 import org.openecomp.sdc.be.impl.ComponentsUtils;
 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
+import org.openecomp.sdc.be.model.DataTypeDefinition;
 import org.openecomp.sdc.be.model.PropertyDefinition;
+import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
 import org.openecomp.sdc.be.resources.data.EntryData;
 import org.openecomp.sdc.common.api.Constants;
 import org.openecomp.sdc.exception.ResponseFormat;
@@ -73,10 +80,17 @@ class ComponentPropertyServletTest extends JerseySpringBaseTest {
     private static final String INVALID_PROPERTY_NAME = "invalid_name_$.&";
     private static final String STRING_TYPE = "string";
 
+    ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
+    Map<String, DataTypeDefinition> mapreturn = new HashMap<>();
+    Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes;
+
     @BeforeEach
     public void before() throws Exception {
         super.setUp();
         when(request.getSession()).thenReturn(session);
+        mapreturn.put("Demo",new DataTypeDefinition());
+        allDataTypes = Either.left(mapreturn);
+        when(applicationDataTypeCache.getAll(null)).thenReturn(allDataTypes);
     }
 
     @AfterEach
index 8e09648..59510c4 100644 (file)
@@ -36,7 +36,7 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint {
 
     @Override
     public void validate(PropertyDefinition property) throws ConstraintViolationException {
-        validate(ToscaType.isValidType(property.getType()), property.getValue());
+        validate(ToscaType.isValidType(property.getType()), property.getValue() != null ? property.getValue() : property.getDefaultValue());
     }
     
     protected void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException {
index 83b3ab8..c356970 100644 (file)
@@ -81,7 +81,7 @@ public final class ConstraintUtil {
      * @throws ConstraintValueDoNotMatchPropertyTypeException if the property type cannot be compared
      */
     public static void checkComparableType(final ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
-        if (!isComparableType(propertyType)) {
+        if (!isComparableType(propertyType) && !ToscaType.BOOLEAN.equals(propertyType)) {
             throw new ConstraintValueDoNotMatchPropertyTypeException("Constraint is invalid for property type <" + propertyType.getType() + ">");
         }
     }
index a565580..5e9e598 100644 (file)
@@ -54,7 +54,7 @@ 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 (ConstraintUtil.isComparableType(propertyType)) {
+            if (ConstraintUtil.isComparableType(propertyType) || ToscaType.BOOLEAN.equals(propertyType)) {
                 initialize(String.valueOf(equal), propertyType);
             }
         } else {