aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main
diff options
context:
space:
mode:
authorvasraz <vasyl.razinkov@est.tech>2023-02-01 17:56:35 +0000
committerMichael Morris <michael.morris@est.tech>2023-02-02 12:05:26 +0000
commite61d26cb2a74813b526e10864af4d73f04df2650 (patch)
tree972a74ffc58c0b24da5af586bbbcec1f66150d97 /catalog-model/src/main
parent5da0b3c1454ac557e0b01eb678abaf882fb08243 (diff)
Fix 'in_range constraints missing from TOSCA template'-bug
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech> Change-Id: Icec2183849a8852621ec98f3ca1d94ac7c4dae31 Issue-ID: SDC-4357
Diffstat (limited to 'catalog-model/src/main')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java41
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java12
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java2
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java31
4 files changed, 55 insertions, 31 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
index 3d2e701a34..fa6df00d5a 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
@@ -27,7 +27,6 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
-import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
@@ -45,6 +44,7 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -94,6 +94,8 @@ import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
+import org.openecomp.sdc.be.model.tosca.ToscaType;
+import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
@@ -2141,8 +2143,8 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
JsonArray jsonArray = new JsonArray();
if (src instanceof InRangeConstraint) {
InRangeConstraint rangeConstraint = (InRangeConstraint) src;
- jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getMin())));
- jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getMax())));
+ jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getInRange().get(0))));
+ jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getInRange().get(1))));
result.add("inRange", jsonArray);
} else if (src instanceof GreaterThanConstraint) {
GreaterThanConstraint greaterThanConstraint = (GreaterThanConstraint) src;
@@ -2195,12 +2197,15 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
log.error("The range constraint content is invalid. value = {}", typedValue);
throw new JsonSyntaxException("The range constraint content is invalid");
} else {
- Object minValue = rangeArray.get(0);
- Object maxValue = rangeArray.get(1);
- InRangeConstraint rangeConstraint = new InRangeConstraint(Lists.newArrayList(minValue, maxValue));
- rangeConstraint.setMin(String.valueOf(minValue));
- rangeConstraint.setMax(String.valueOf(maxValue));
- propertyConstraint = rangeConstraint;
+ final Object minValue = rangeArray.get(0);
+ final Object maxValue = rangeArray.get(1);
+
+ final Comparable min = ConstraintUtil.convertToComparable(
+ ToscaType.getToscaType(minValue.getClass().getSimpleName().toLowerCase()), String.valueOf(minValue));
+ final Comparable max = ConstraintUtil.convertToComparable(
+ ToscaType.getToscaType(maxValue.getClass().getSimpleName().toLowerCase()), String.valueOf(maxValue));
+
+ propertyConstraint = new InRangeConstraint(Arrays.asList(min, max));
}
} else {
log.warn("The value of InRangeConstraint is null");
@@ -2358,7 +2363,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
propertyConstraint = deserializeConstraint(value, EqualConstraint.class);
break;
case IN_RANGE:
- propertyConstraint = deserializeInRangeConstraintConstraint(value);
+ propertyConstraint = deserializeInRangeConstraint(value);
break;
case GREATER_THAN:
propertyConstraint = deserializeConstraint(value, GreaterThanConstraint.class);
@@ -2448,18 +2453,20 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
}
}
- private PropertyConstraint deserializeInRangeConstraintConstraint(JsonNode value) {
+ private PropertyConstraint deserializeInRangeConstraint(JsonNode value) {
if (value instanceof ArrayNode) {
ArrayNode rangeArray = (ArrayNode) value;
if (rangeArray.size() != 2) {
log.error("The range constraint content is invalid. value = {}", value);
} else {
- String minValue = rangeArray.get(0).asText();
- String maxValue = rangeArray.get(1).asText();
- InRangeConstraint rangeConstraint = new InRangeConstraint(Lists.newArrayList(minValue, maxValue));
- rangeConstraint.setMin(minValue);
- rangeConstraint.setMax(maxValue);
- return rangeConstraint;
+ final String minValue = rangeArray.get(0).asText();
+ final String maxValue = rangeArray.get(1).asText();
+ final Comparable min = ConstraintUtil.convertToComparable(
+ ToscaType.getToscaType(minValue.getClass().getSimpleName().toLowerCase()), String.valueOf(minValue));
+ final Comparable max = ConstraintUtil.convertToComparable(
+ ToscaType.getToscaType(maxValue.getClass().getSimpleName().toLowerCase()), String.valueOf(maxValue));
+
+ return new InRangeConstraint(Arrays.asList(min, max));
}
}
return null;
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java
index 1f942a9352..e4e229a910 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java
@@ -47,7 +47,9 @@ public enum ToscaType {
// @formatter:off
STRING("string"),
INTEGER("integer"),
+ LONG("long"),
FLOAT("float"),
+ DOUBLE("double"),
BOOLEAN("boolean"),
TIMESTAMP("timestamp"),
VERSION("version"),
@@ -87,7 +89,7 @@ public enum ToscaType {
return null;
}
for (ToscaType type : ToscaType.values()) {
- if (type.getType().equals(typeName)) {
+ if (type.getType().equalsIgnoreCase(typeName)) {
return type;
}
}
@@ -122,7 +124,9 @@ public enum ToscaType {
case BOOLEAN:
return value.equals(true) || value.equals(false);
case FLOAT:
+ case DOUBLE:
return value instanceof Float;
+ case LONG:
case INTEGER:
case RANGE:
return value instanceof Integer;
@@ -148,8 +152,10 @@ public enum ToscaType {
case BOOLEAN:
return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
case FLOAT:
+ case DOUBLE:
return isFloat(value);
case INTEGER:
+ case LONG:
return isInteger(value);
case SCALAR_UNIT_SIZE:
return isScalarUnitSize(value);
@@ -242,10 +248,12 @@ public enum ToscaType {
case BOOLEAN:
return Boolean.valueOf(value);
case FLOAT:
+ case DOUBLE:
return Float.valueOf(value);
case RANGE:
case INTEGER:
- return Long.valueOf(value);
+ case LONG:
+ return Integer.valueOf(value);
case TIMESTAMP:
try {
return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java
index 61f069a45f..fee828c1be 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java
@@ -66,7 +66,9 @@ public final class ConstraintUtil {
final ToscaType toscaType = ToscaType.getToscaType(propertyType.getType());
switch (toscaType) {
case FLOAT:
+ case DOUBLE:
case INTEGER:
+ case LONG:
case TIMESTAMP:
case VERSION:
case STRING:
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java
index 4f362b9dfb..9330c8c106 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java
@@ -19,7 +19,6 @@
*/
package org.openecomp.sdc.be.model.tosca.constraints;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@@ -43,10 +42,6 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
@NonNull
@EqualsAndHashCode.Include
private List<Object> inRange;
- @JsonIgnore
- private Comparable min;
- @JsonIgnore
- private Comparable max;
public InRangeConstraint(List<Object> inRange) {
this.inRange = inRange;
@@ -69,8 +64,7 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
throw new ConstraintValueDoNotMatchPropertyTypeException(
"Invalid max value for in range constraint [" + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
}
- min = ConstraintUtil.convertToComparable(propertyType, minRawText);
- max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
+ inRange.replaceAll(obj -> propertyType.convert(String.valueOf(obj)));
}
@Override
@@ -78,9 +72,22 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
if (propertyValue == null) {
throw new ConstraintViolationException("Value to check is null");
}
- if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
+ if (!(inRange.get(0).getClass().isAssignableFrom(propertyValue.getClass()))) {
throw new ConstraintViolationException(
- "Value to check is not comparable to range type, value type [" + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
+ "Value to check is not comparable to range type, value type [" + propertyValue.getClass() + "], range type [" + inRange.get(0)
+ .getClass() + "]");
+ }
+ if (!(inRange.get(1).getClass().isAssignableFrom(propertyValue.getClass()))) {
+ throw new ConstraintViolationException(
+ "Value to check is not comparable to range type, value type [" + propertyValue.getClass() + "], range type [" + inRange.get(1)
+ .getClass() + "]");
+ }
+ final ToscaType propertyType = ToscaType.getToscaType(propertyValue.getClass().getSimpleName().toLowerCase());
+ final Comparable min = ConstraintUtil.convertToComparable(propertyType, String.valueOf(inRange.get(0)));
+ final Comparable max = ConstraintUtil.convertToComparable(propertyType, String.valueOf(inRange.get(1)));
+
+ if (min.compareTo(max) > 0) {
+ throw new IllegalArgumentException("The MIN [" + min + "] should be less than MAX [" + max + "]");
}
if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
@@ -98,7 +105,9 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
@Override
public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
- return getErrorMessage(toscaType, e, propertyName, "%s property value must be in a range of %s", String.valueOf(min),
+ Comparable min = ConstraintUtil.convertToComparable(toscaType, String.valueOf(inRange.get(0)));
+ Comparable max = ConstraintUtil.convertToComparable(toscaType, String.valueOf(inRange.get(1)));
+ return getErrorMessage(toscaType, e, propertyName, "'%s' value must be in a range of %s", String.valueOf(min),
String.valueOf(max));
}
@@ -124,8 +133,6 @@ public class InRangeConstraint extends AbstractPropertyConstraint {
ToscaType toscaType = ToscaType.getToscaType(propertyType);
try {
inRange.replaceAll(obj -> toscaType.convert(String.valueOf(obj)));
- min = ConstraintUtil.convertToComparable(toscaType, String.valueOf(inRange.get(0)));
- max = ConstraintUtil.convertToComparable(toscaType, String.valueOf(inRange.get(1)));
} catch (Exception e) {
throw new ConstraintValueDoNotMatchPropertyTypeException(
"inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");