Fix bug 'Pattern constraint validation failure' 19/132619/4
authorvasraz <vasyl.razinkov@est.tech>
Mon, 12 Dec 2022 15:18:18 +0000 (15:18 +0000)
committerMichael Morris <michael.morris@est.tech>
Wed, 14 Dec 2022 15:49:17 +0000 (15:49 +0000)
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Change-Id: If7c59aa37db974c57195775f963c8400cf474a51
Issue-ID: SDC-4294

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

index 2ad6a94..ae97b89 100644 (file)
@@ -92,9 +92,9 @@ public class PropertyConvertor {
             if (props != null) {
                 Map<String, ToscaProperty> properties = new HashMap<>();
                 // take only the properties of this resource
-                props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
-                    properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
-                });
+                props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property ->
+                    properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY))
+                );
                 if (!properties.isEmpty()) {
                     toscaNodeType.setProperties(properties);
                 }
@@ -129,18 +129,16 @@ public class PropertyConvertor {
             prop.setStatus(property.getStatus());
         }
         prop.setMetadata(property.getMetadata());
-        
-        List<ToscaPropertyConstraint> constraints = new ArrayList<>();
+
         if (CollectionUtils.isNotEmpty(property.getConstraints())) {
-            constraints = convertConstraints(property.getConstraints());
-            prop.setConstraints(constraints);
+            prop.setConstraints(convertConstraints(property.getConstraints()));
         }
         return prop;
     }
-    
+
     private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints) {
         List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
-        for (PropertyConstraint constraint: constraints){
+        for (PropertyConstraint constraint : constraints) {
             if (constraint instanceof EqualConstraint) {
                 convertedConstraints.add(new ToscaPropertyConstraintEqual(((EqualConstraint) constraint).getEqual()));
             }
@@ -260,10 +258,10 @@ public class PropertyConvertor {
                     .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
             }
             return convertedValue;
-        
+
         } catch (JsonParseException e) {
             log.trace("{} not parsable as JSON. Convert as YAML instead", value);
-            return  new Yaml().load(value);
+            return new Yaml().load(value);
         } catch (Exception e) {
             log.debug("convertToToscaValue failed to parse json value :", e);
             return null;
index 0803cc8..97f9874 100644 (file)
@@ -2324,11 +2324,13 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
         }
 
         private Object getJsonPrimitive(JsonPrimitive je) {
-            if (je.isBoolean())
+            if (je.isBoolean()) {
                 return je.getAsBoolean();
-            if (je.isString())
+            }
+            if (je.isString()) {
                 return je.getAsString();
-            if (je.isNumber()){
+            }
+            if (je.isNumber()) {
                 double number = je.getAsNumber().floatValue();
                 if ((number % 1) == 0) {
                     return je.getAsNumber().intValue();
@@ -2391,7 +2393,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
                                 propertyConstraint = deserializeConstraintWithIntegerOperand(value, MaxLengthConstraint.class);
                                 break;
                             case PATTERN:
-                                propertyConstraint = deserializeConstraintWithStringOperand(value, PatternConstraint.class);
+                                propertyConstraint = deserializeConstraintWithStringPatternOperand(value, PatternConstraint.class);
                                 break;
                             default:
                                 log.warn("Key {} is not supported. Ignored.", field.getKey());
@@ -2415,6 +2417,19 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
             }
         }
 
+        private PropertyConstraint deserializeConstraintWithStringPatternOperand(JsonNode value,
+                                                                                 Class<? extends PropertyConstraint> constraintClass) {
+            String asString = value.asText();
+            log.debug("Before adding value to {} object. value = {}", constraintClass, asString);
+            try {
+                return constraintClass.getConstructor(String.class).newInstance(asString);
+            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
+                     | SecurityException exception) {
+                log.error("Error deserializing constraint", exception);
+                return null;
+            }
+        }
+
         private PropertyConstraint deserializeConstraintWithIntegerOperand(JsonNode value, Class<? extends PropertyConstraint> constraintClass) {
             Integer asInt = value.asInt();
             log.debug("Before adding value to {} object. value = {}", constraintClass, asInt);
index 9e86573..f5a9f84 100644 (file)
@@ -70,7 +70,7 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria
 
     @Override
     public ConstraintType getConstraintType() {
-        return null;
+        return ConstraintType.EQUAL;
     }
 
     @Override
index d2c8b8c..3ed6e7f 100644 (file)
@@ -68,7 +68,7 @@ public class LengthConstraint extends AbstractPropertyConstraint {
 
     @Override
     public ConstraintType getConstraintType() {
-        return null;
+        return ConstraintType.LENGTH;
     }
 
     @Override
index 004e431..388eb05 100644 (file)
@@ -62,7 +62,7 @@ public class MaxLengthConstraint extends AbstractPropertyConstraint {
 
     @Override
     public ConstraintType getConstraintType() {
-        return null;
+        return ConstraintType.MAX_LENGTH;
     }
 
     @Override
index 1ee9631..6ff9524 100644 (file)
@@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.tosca.constraints;
 
 import java.util.regex.Pattern;
 import javax.validation.constraints.NotNull;
+import lombok.Getter;
 import lombok.NoArgsConstructor;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.model.PropertyConstraint;
@@ -28,7 +29,6 @@ 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.ConstraintViolationException;
 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
-import lombok.Getter;
 
 @NoArgsConstructor
 public class PatternConstraint extends AbstractStringPropertyConstraint {
@@ -56,11 +56,12 @@ public class PatternConstraint extends AbstractStringPropertyConstraint {
 
     @Override
     public ConstraintType getConstraintType() {
-        return null;
+        return ConstraintType.PATTERN;
     }
 
     @Override
     public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
+        // no need for implementation
     }
 
     @Override