From be7ba43b95f13bb390cdd77a15c35072781e5546 Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Mon, 21 Nov 2022 09:55:26 +0000 Subject: Fix numeric constraint values generated as strings When importing a vfc the property constraints are now checked to see if they are the same type as the property If not it will attempt to convert to that type Issue-ID: SDC-4274 Signed-off-by: JvD_Ericsson Change-Id: I32c1d930166d10142ad9ca6914550c7946e9128c --- .../nsd/generator/NsDescriptorGeneratorImpl.java | 4 +- .../sdc/be/components/impl/ImportUtils.java | 37 ++++ .../openecomp/sdc/be/tosca/PropertyConvertor.java | 7 +- .../tosca/model/ToscaPropertyConstraintEqual.java | 2 +- .../ToscaPropertyConstraintGreaterOrEqual.java | 2 +- .../model/ToscaPropertyConstraintGreaterThan.java | 2 +- .../model/ToscaPropertyConstraintInRange.java | 3 +- .../tosca/model/ToscaPropertyConstraintLength.java | 2 +- .../model/ToscaPropertyConstraintLessOrEqual.java | 2 +- .../model/ToscaPropertyConstraintLessThan.java | 2 +- .../model/ToscaPropertyConstraintValidValues.java | 2 +- .../sdc/be/components/impl/ImportUtilsTest.java | 2 +- .../model/operations/impl/PropertyOperation.java | 116 +++++++------ .../openecomp/sdc/be/model/tosca/ToscaType.java | 27 +++ .../AbstractComparablePropertyConstraint.java | 6 + .../model/tosca/constraints/EqualConstraint.java | 39 ++++- .../constraints/GreaterOrEqualConstraint.java | 36 +++- .../tosca/constraints/GreaterThanConstraint.java | 50 ++++-- .../model/tosca/constraints/InRangeConstraint.java | 59 +++++-- .../tosca/constraints/LessOrEqualConstraint.java | 50 ++++-- .../tosca/constraints/LessThanConstraint.java | 38 +++- .../tosca/constraints/ValidValuesConstraint.java | 62 +++++-- .../impl/CapabilityTypeOperationTest.java | 2 +- .../operations/impl/PropertyOperationTest.java | 8 +- .../impl/RelationshipTypeOperationTest.java | 4 +- .../tosca/constraints/EqualConstraintTest.java | 98 +++++++++-- .../constraints/GreaterOrEqualConstraintTest.java | 109 ++++++++++++ .../constraints/GreaterThanConstraintTest.java | 104 ++++++++--- .../tosca/constraints/InRangeConstraintTest.java | 191 +++++++++++++++------ .../constraints/LessOrEqualConstraintTest.java | 104 ++++++++--- .../tosca/constraints/LessThanConstraintTest.java | 109 ++++++++++++ .../constraints/ValidValuesConstraintTest.java | 127 +++++++++++--- 32 files changed, 1114 insertions(+), 292 deletions(-) create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImpl.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImpl.java index 9572c44a6b..eb601d836d 100644 --- a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImpl.java +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImpl.java @@ -151,12 +151,12 @@ public class NsDescriptorGeneratorImpl implements NsDescriptorGenerator { return returnValueOnError; } final ToscaPropertyConstraintValidValues validValuesConstraint = (ToscaPropertyConstraintValidValues) toscaPropertyConstraint; - final List validValues = validValuesConstraint.getValidValues(); + final List validValues = validValuesConstraint.getValidValues(); if (CollectionUtils.isEmpty(validValues)) { LOGGER.error(errorMsg); return returnValueOnError; } - return validValues.get(0); + return String.valueOf(validValues.get(0)); } private ToscaTemplate createNetworkServiceDescriptor(final Component component, final List vnfDescriptorList) throws NsdException { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index 181c860b9f..9c2c070c74 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -63,6 +63,9 @@ import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.model.tosca.constraints.AbstractComparablePropertyConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.utils.TypeUtils; @@ -315,12 +318,46 @@ public final class ImportUtils { if (propertyConstraint instanceof ValidValuesConstraint) { try { ((ValidValuesConstraint) propertyConstraint).validateType(propertyType); + boolean valid = ((ValidValuesConstraint) propertyConstraint).validateValueType(propertyType); + if (!valid) { + ((ValidValuesConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType); + } } catch (ConstraintValueDoNotMatchPropertyTypeException e) { BeEcompErrorManager.getInstance() .logInternalFlowError("GetInitializedPropertyConstraint", e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR); throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.VALID_VALUES.name(), ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType); } + } else if (propertyConstraint instanceof AbstractComparablePropertyConstraint) { + try { + boolean valid = ((AbstractComparablePropertyConstraint) propertyConstraint).validateValueType(propertyType); + if (!valid) { + ((AbstractComparablePropertyConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType); + } + } catch (ConstraintValueDoNotMatchPropertyTypeException e) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, propertyConstraint.getConstraintType().name(), + ((AbstractComparablePropertyConstraint) propertyConstraint).getConstraintValueAsString(), propertyType); + } + } else if (propertyConstraint instanceof EqualConstraint) { + try { + boolean valid = ((EqualConstraint) propertyConstraint).validateValueType(propertyType); + if (!valid) { + ((EqualConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType); + } + } catch (ConstraintValueDoNotMatchPropertyTypeException e) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.EQUAL.name(), + String.valueOf(((EqualConstraint) propertyConstraint).getEqual()), propertyType); + } + } else if (propertyConstraint instanceof InRangeConstraint) { + try { + boolean valid = ((InRangeConstraint) propertyConstraint).validateValueType(propertyType); + if (!valid) { + ((InRangeConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType); + } + } catch (ConstraintValueDoNotMatchPropertyTypeException e) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.IN_RANGE.name(), + String.valueOf(((InRangeConstraint) propertyConstraint).getInRange()), propertyType); + } } return propertyConstraint; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index f1c8a17108..2ad6a942d0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -158,16 +158,17 @@ public class PropertyConvertor { } if (constraint instanceof InRangeConstraint) { InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint; - List range = new ArrayList<>(); + List range = new ArrayList<>(); range.add(inRangeConstraint.getRangeMinValue()); range.add(inRangeConstraint.getRangeMaxValue()); convertedConstraints.add(new ToscaPropertyConstraintInRange(range)); } if (constraint instanceof ValidValuesConstraint) { - convertedConstraints.add(new ToscaPropertyConstraintValidValues(((ValidValuesConstraint) constraint).getValidValues())); + List validValues = ((ValidValuesConstraint) constraint).getValidValues(); + convertedConstraints.add(new ToscaPropertyConstraintValidValues(validValues)); } if (constraint instanceof LengthConstraint) { - convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength().toString())); + convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength())); } if (constraint instanceof MinLengthConstraint) { convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength())); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java index 532cb8c91b..e52f8dfa87 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java @@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintEqual implements ToscaPropertyConstraint { - private String equal; + private Object equal; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.EQUAL; @Override diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java index fcc9dcc884..89c563bdbc 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java @@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintGreaterOrEqual implements ToscaPropertyConstraint { - private String greaterOrEqual; + private Object greaterOrEqual; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.GREATER_OR_EQUAL; @Override diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java index 6f2268bd9e..2168cf4b67 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java @@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintGreaterThan implements ToscaPropertyConstraint { - private String greaterThan; + private Object greaterThan; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.GREATER_THAN; @Override diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java index 6c8cfba620..91e1f553f5 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java @@ -32,7 +32,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintInRange implements ToscaPropertyConstraint { - private List inRange; + private List inRange; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.IN_RANGE; @Override @@ -40,7 +40,6 @@ public class ToscaPropertyConstraintInRange implements ToscaPropertyConstraint { if ("inRange".equals(attributeName)) { return CONSTRAINT_TYPE.getType(); } - return attributeName; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java index 67aaca0188..998a7283bb 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java @@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintLength implements ToscaPropertyConstraint { - private String length; + private Integer length; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LENGTH; @Override diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java index 3d51ec9419..75e661d15c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java @@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintLessOrEqual implements ToscaPropertyConstraint { - private String lessOrEqual; + private Object lessOrEqual; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LESS_OR_EQUAL; @Override diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java index ad51f66fde..a64382d8a8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java @@ -31,7 +31,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintLessThan implements ToscaPropertyConstraint { - private String lessThan; + private Object lessThan; private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LESS_THAN; @Override diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java index c23be5725c..4f9a65cfc1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java @@ -32,7 +32,7 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; @AllArgsConstructor public class ToscaPropertyConstraintValidValues implements ToscaPropertyConstraint { - private List validValues; + private List validValues; @Override public String getEntryToscaName(final String attributeName) { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java index b7559e3836..8fcc06ba81 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java @@ -329,7 +329,7 @@ public class ImportUtilsTest { assertTrue(property.getConstraints() != null && property.getConstraints().size() == 1); assertTrue(property.getConstraints().get(0) instanceof ValidValuesConstraint); assertNotNull(((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues()); - List validValues = ((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues(); + List validValues = ((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues(); assertTrue(validValues.containsAll(Lists.newArrayList("firewall", "analyzer", "source-nat", "loadbalancer"))); assertTrue(properties.containsKey("service_interface_type_list")); 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 dc97e25af1..9e06b42852 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 @@ -2139,20 +2139,20 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe JsonArray jsonArray = new JsonArray(); if (src instanceof InRangeConstraint) { InRangeConstraint rangeConstraint = (InRangeConstraint) src; - jsonArray.add(JsonParser.parseString(rangeConstraint.getRangeMinValue())); - jsonArray.add(JsonParser.parseString(rangeConstraint.getRangeMaxValue())); + jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getRangeMinValue()))); + jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getRangeMaxValue()))); result.add("inRange", jsonArray); } else if (src instanceof GreaterThanConstraint) { GreaterThanConstraint greaterThanConstraint = (GreaterThanConstraint) src; - jsonArray.add(JsonParser.parseString(greaterThanConstraint.getGreaterThan())); + jsonArray.add(JsonParser.parseString(String.valueOf(greaterThanConstraint.getGreaterThan()))); result.add("greaterThan", jsonArray); } else if (src instanceof LessThanConstraint) { LessThanConstraint lessThanConstraint = (LessThanConstraint) src; - jsonArray.add(JsonParser.parseString(lessThanConstraint.getLessThan())); + jsonArray.add(JsonParser.parseString(String.valueOf(lessThanConstraint.getLessThan()))); result.add("lessThan", jsonArray); } else if (src instanceof LessOrEqualConstraint) { LessOrEqualConstraint lessOrEqualConstraint = (LessOrEqualConstraint) src; - jsonArray.add(JsonParser.parseString(lessOrEqualConstraint.getLessOrEqual())); + jsonArray.add(JsonParser.parseString(String.valueOf(lessOrEqualConstraint.getLessOrEqual()))); result.add("lessOrEqual", jsonArray); } else { log.warn("PropertyConstraint {} is not supported. Ignored.", src.getClass().getName()); @@ -2173,38 +2173,32 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe Entry element = set.iterator().next(); String key = element.getKey(); JsonElement value = element.getValue(); + Object typedValue = getTypedValue(element.getValue()); ConstraintType constraintType = ConstraintType.findByType(key).orElse(null); if (constraintType == null) { log.warn("ConstraintType was not found for constraint name:{}", key); } else { switch (constraintType) { case EQUAL: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to EqualConstraint object. value = {}", asString); - propertyConstraint = new EqualConstraint(asString); + if ( typedValue != null) { + log.debug("Before adding value to EqualConstraint object. value = {}", typedValue); + propertyConstraint = new EqualConstraint(typedValue); break; } else { log.warn("The value of equal constraint is null"); } break; case IN_RANGE: - if (value != null) { - if (value instanceof JsonArray) { - JsonArray rangeArray = (JsonArray) value; - if (rangeArray.size() != 2 || rangeArray.contains(new JsonPrimitive(""))) { - log.error("The range constraint content is invalid. value = {}", value); + if (typedValue != null) { + if (typedValue instanceof ArrayList) { + ArrayList rangeArray = (ArrayList) typedValue; + if (rangeArray.size() != 2 || rangeArray.contains("")) { + log.error("The range constraint content is invalid. value = {}", typedValue); throw new JsonSyntaxException("The range constraint content is invalid"); } else { InRangeConstraint rangeConstraint = new InRangeConstraint(); - String minValue = rangeArray.get(0).getAsString(); - String maxValue; - JsonElement maxElement = rangeArray.get(1); - if (maxElement.isJsonNull()) { - maxValue = String.valueOf(maxElement.getAsJsonNull()); - } else { - maxValue = maxElement.getAsString(); - } + Object minValue = rangeArray.get(0); + Object maxValue = rangeArray.get(1); rangeConstraint.setRangeMinValue(minValue); rangeConstraint.setRangeMaxValue(maxValue); propertyConstraint = rangeConstraint; @@ -2215,58 +2209,49 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } break; case GREATER_THAN: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString); - propertyConstraint = new GreaterThanConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to GreaterThanConstraint object. value = {}", typedValue); + propertyConstraint = new GreaterThanConstraint(typedValue); break; } else { log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL); } break; case LESS_THAN: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to LessThanConstraint object. value = {}", asString); - propertyConstraint = new LessThanConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to LessThanConstraint object. value = {}", typedValue); + propertyConstraint = new LessThanConstraint(typedValue); break; } else { log.warn("The value of LessThanConstraint is null"); } break; case GREATER_OR_EQUAL: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString); - propertyConstraint = new GreaterOrEqualConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to GreaterThanConstraint object. value = {}", typedValue); + propertyConstraint = new GreaterOrEqualConstraint(typedValue); break; } else { log.warn("The value of GreaterOrEqualConstraint is null"); } break; case LESS_OR_EQUAL: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to LessOrEqualConstraint object. value = {}", asString); - propertyConstraint = new LessOrEqualConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to LessOrEqualConstraint object. value = {}", typedValue); + propertyConstraint = new LessOrEqualConstraint(typedValue); } else { log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL); } break; case VALID_VALUES: - if (value != null) { - JsonArray rangeArray = (JsonArray) value; - if (rangeArray.size() == 0 || rangeArray.contains(new JsonPrimitive(""))) { - log.error("The valid values constraint content is invalid. value = {}", value); + if (typedValue != null) { + ArrayList validValuesArray = (ArrayList)typedValue; + if (validValuesArray.size() == 0 || validValuesArray.contains("")) { + log.error("The valid values constraint content is invalid. value = {}", typedValue); throw new JsonSyntaxException("The valid values constraint content is invalid"); } else { ValidValuesConstraint vvConstraint = new ValidValuesConstraint(); - List validValues = new ArrayList<>(); - for (JsonElement jsonElement : rangeArray) { - String item = jsonElement.getAsString(); - validValues.add(item); - } - vvConstraint.setValidValues(validValues); + vvConstraint.setValidValues(validValuesArray); propertyConstraint = vvConstraint; } } @@ -2318,6 +2303,37 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return propertyConstraint; } + + private Object getTypedValue(JsonElement je) { + if (je.isJsonNull()) + return null; + if (je.isJsonPrimitive()) { + return getJsonPrimitive(je.getAsJsonPrimitive()); + } + if (je.isJsonArray()) { + ArrayList array = new ArrayList<>(); + for (JsonElement e : je.getAsJsonArray()) { + array.add(getJsonPrimitive(e.getAsJsonPrimitive())); + } + return array; + } + return je; + } + + private Object getJsonPrimitive(JsonPrimitive je) { + if (je.isBoolean()) + return je.getAsBoolean(); + if (je.isString()) + return je.getAsString(); + if (je.isNumber()){ + double number = je.getAsNumber().floatValue(); + if ((number % 1) == 0) { + return je.getAsNumber().intValue(); + } + return number; + } + return null; + } } public static class PropertyConstraintJacksonDeserializer extends com.fasterxml.jackson.databind.JsonDeserializer { @@ -2388,7 +2404,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe String asString = value.asText(); log.debug("Before adding value to {} object. value = {}", constraintClass, asString); try { - return constraintClass.getConstructor(String.class).newInstance(asString); + return constraintClass.getConstructor(Object.class).newInstance(asString); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException exception) { log.error("Error deserializing constraint", exception); @@ -2437,7 +2453,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.error("The valid values constraint content is invalid. value = {}", value); } else { ValidValuesConstraint vvConstraint = new ValidValuesConstraint(); - List validValues = new ArrayList<>(); + List validValues = new ArrayList<>(); for (JsonNode jsonElement : rangeArray) { String item = jsonElement.asText(); validValues.add(item); 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 85758707b8..f59ebe6922 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 @@ -49,6 +49,7 @@ public enum ToscaType { VERSION("version"), LIST("list"), MAP("map"), + RANGE("range"), SCALAR_UNIT("scalar-unit"), SCALAR_UNIT_SIZE("scalar-unit.size"), SCALAR_UNIT_TIME("scalar-unit.time"), @@ -93,6 +94,31 @@ public enum ToscaType { return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type); } + public Boolean isValueTypeValid(Object value) { + switch (this) { + case BOOLEAN: + return value.equals(true) || value.equals(false); + case FLOAT: + return value instanceof Float; + case INTEGER: + case RANGE: + return value instanceof Integer; + case STRING: + case SCALAR_UNIT: + case SCALAR_UNIT_SIZE: + case SCALAR_UNIT_TIME: + case SCALAR_UNIT_FREQUENCY: + case TIMESTAMP: + case VERSION: + return value instanceof String; + case LIST: + case MAP: + return true; + default: + return false; + } + } + public boolean isValidValue(String value) { switch (this) { case BOOLEAN: @@ -172,6 +198,7 @@ public enum ToscaType { return Boolean.valueOf(value); case FLOAT: return Float.valueOf(value); + case RANGE: case INTEGER: return Long.valueOf(value); case TIMESTAMP: diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java index 42fb1eb00c..e7730bd612 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java @@ -50,6 +50,12 @@ public abstract class AbstractComparablePropertyConstraint extends AbstractPrope protected abstract void doValidate(Object propertyValue) throws ConstraintViolationException; + public abstract boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException; + + public abstract String getConstraintValueAsString(); + + public abstract void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException; + @Override public void validate(Object propertyValue) throws ConstraintViolationException { if (propertyValue == null) { diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java index 0fdc570262..9e865733c9 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java @@ -21,6 +21,8 @@ package org.openecomp.sdc.be.model.tosca.constraints; import java.io.Serializable; import javax.validation.constraints.NotNull; +import lombok.Setter; +import lombok.Getter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -28,25 +30,25 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunction import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; -import lombok.Getter; @SuppressWarnings("serial") public class EqualConstraint extends AbstractPropertyConstraint implements Serializable { - @NotNull @Getter - private String equal; + @Setter + @NotNull + private Object equal; private Object typed; - public EqualConstraint(String equal) { + public EqualConstraint(Object equal) { super(); this.equal = equal; } @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - if (propertyType.isValidValue(equal)) { - typed = propertyType.convert(equal); + if (propertyType.isValidValue(String.valueOf(equal))) { + typed = propertyType.convert(String.valueOf(equal)); } else { throw new ConstraintValueDoNotMatchPropertyTypeException( "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">"); @@ -82,6 +84,29 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", equal); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", String.valueOf(equal)); + } + + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">"); + } + if (equal == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "equal constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(equal); + } + + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + equal = toscaType.convert(String.valueOf(equal)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">"); + } } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java index 00a84610c5..b45cf9791e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java @@ -37,11 +37,11 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraint public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstraint { @NotNull - private String greaterOrEqual; + private Object greaterOrEqual; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(greaterOrEqual, propertyType); + initialize(String.valueOf(greaterOrEqual), propertyType); } @Override @@ -62,6 +62,36 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", greaterOrEqual); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", String.valueOf(greaterOrEqual)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">"); + } + if (greaterOrEqual == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterOrEqual constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(greaterOrEqual); + } + + @Override + public String getConstraintValueAsString() { + return String.valueOf(greaterOrEqual); + } + + @Override + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + greaterOrEqual = toscaType.convert(String.valueOf(greaterOrEqual)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">"); + } } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java index a819a8e358..9d638cfb3f 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java @@ -20,6 +20,9 @@ package org.openecomp.sdc.be.model.tosca.constraints; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -28,18 +31,17 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; -public class GreaterThanConstraint extends AbstractComparablePropertyConstraint { +@Getter +@Setter +@AllArgsConstructor +public class GreaterThanConstraint extends AbstractComparablePropertyConstraint { @NotNull - private String greaterThan; - - public GreaterThanConstraint(String greaterThan) { - this.greaterThan = greaterThan; - } + private T greaterThan; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(greaterThan, propertyType); + initialize(String.valueOf(greaterThan), propertyType); } @Override @@ -58,16 +60,38 @@ public class GreaterThanConstraint extends AbstractComparablePropertyConstraint } } - public String getGreaterThan() { - return greaterThan; + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", String.valueOf(greaterThan)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterThan constraint has invalid values <" + greaterThan.toString() + "> property type is <" + propertyType + ">"); + } + if (greaterThan == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterThan constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(greaterThan); } - public void setGreaterThan(String greaterThan) { - this.greaterThan = greaterThan; + @Override + public String getConstraintValueAsString() { + return String.valueOf(greaterThan); } @Override - public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", greaterThan); + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + greaterThan = (T) toscaType.convert(String.valueOf(greaterThan)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterThan constraint has invalid values <" + greaterThan.toString() + "> property type is <" + propertyType + ">"); + } } } 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 ad871e5087..2256f7d311 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 @@ -22,6 +22,10 @@ package org.openecomp.sdc.be.model.tosca.constraints; import com.google.common.collect.Lists; import java.util.List; import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -30,19 +34,20 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +@NoArgsConstructor +@Getter +@Setter public class InRangeConstraint extends AbstractPropertyConstraint { - private List inRange; + @NonNull + private List inRange; private Comparable min; private Comparable max; - public InRangeConstraint(List inRange) { + public InRangeConstraint(List inRange) { this.inRange = inRange; } - public InRangeConstraint() { - } - @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { // Perform verification that the property type is supported for @@ -52,8 +57,8 @@ public class InRangeConstraint extends AbstractPropertyConstraint { if (inRange == null || inRange.size() != 2) { throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements."); } - String minRawText = inRange.get(0); - String maxRawText = inRange.get(1); + String minRawText = String.valueOf(inRange.get(0)); + String maxRawText = String.valueOf(inRange.get(1)); if (!propertyType.isValidValue(minRawText)) { throw new ConstraintValueDoNotMatchPropertyTypeException( "Invalid min value for in range constraint [" + minRawText + "] as it does not follow the property type [" + propertyType + "]"); @@ -90,7 +95,7 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } @NotNull - public String getRangeMinValue() { + public Object getRangeMinValue() { if (inRange != null) { return inRange.get(0); } else { @@ -98,16 +103,16 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } } - public void setRangeMinValue(String minValue) { + public void setRangeMinValue(Object minValue) { if (inRange == null) { - inRange = Lists.newArrayList(minValue, ""); + inRange = Lists.newArrayList(minValue, null); } else { inRange.set(0, minValue); } } @NotNull - public String getRangeMaxValue() { + public Object getRangeMaxValue() { if (inRange != null) { return inRange.get(1); } else { @@ -115,9 +120,9 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } } - public void setRangeMaxValue(String maxValue) { + public void setRangeMaxValue(Object maxValue) { if (inRange == null) { - inRange = Lists.newArrayList("", maxValue); + inRange = Lists.newArrayList(null, maxValue); } else { inRange.set(1, maxValue); } @@ -128,4 +133,32 @@ public class InRangeConstraint extends AbstractPropertyConstraint { return getErrorMessage(toscaType, e, propertyName, "%s property value must be between >= [%s] and <= [%s]", String.valueOf(min), String.valueOf(max)); } + + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">"); + } + if (inRange == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "inRange constraint has invalid value <> property type is <" + propertyType + ">"); + } + for (Object value : inRange) { + if (!toscaType.isValueTypeValid(value)) { + return false; + } + } + return true; + } + + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + inRange.replaceAll(obj -> toscaType.convert(String.valueOf(obj))); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">"); + } + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java index 86c6383677..0f64a4f9f5 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java @@ -20,6 +20,9 @@ package org.openecomp.sdc.be.model.tosca.constraints; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -28,18 +31,17 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; -public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint { +@Getter +@Setter +@AllArgsConstructor +public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint { @NotNull - private String lessOrEqual; - - public LessOrEqualConstraint(String lessOrEqual) { - this.lessOrEqual = lessOrEqual; - } + private T lessOrEqual; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(lessOrEqual, propertyType); + initialize(String.valueOf(lessOrEqual), propertyType); } @Override @@ -58,16 +60,38 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint } } - public String getLessOrEqual() { - return lessOrEqual; + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", String.valueOf(lessOrEqual)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessOrEqual constraint has invalid values <" + lessOrEqual.toString() + "> property type is <" + propertyType + ">"); + } + if (lessOrEqual == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessOrEqual constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(lessOrEqual); } - public void setLessOrEqual(String lessOrEqual) { - this.lessOrEqual = lessOrEqual; + @Override + public String getConstraintValueAsString() { + return String.valueOf(lessOrEqual); } @Override - public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", lessOrEqual); + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + lessOrEqual = (T) toscaType.convert(String.valueOf(lessOrEqual)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessOrEqual constraint has invalid values <" + lessOrEqual.toString() + "> property type is <" + propertyType + ">"); + } } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java index 740ee8b544..92ab9278b2 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java @@ -22,6 +22,7 @@ package org.openecomp.sdc.be.model.tosca.constraints; import javax.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -31,15 +32,16 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolatio import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; @Getter +@Setter @AllArgsConstructor public class LessThanConstraint extends AbstractComparablePropertyConstraint { @NotNull - private String lessThan; + private Object lessThan; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(lessThan, propertyType); + initialize(String.valueOf(lessThan), propertyType); } @Override @@ -60,6 +62,36 @@ public class LessThanConstraint extends AbstractComparablePropertyConstraint { @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", lessThan); + return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", String.valueOf(lessThan)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">"); + } + if (lessThan == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessThan constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(lessThan); + } + + @Override + public String getConstraintValueAsString() { + return String.valueOf(lessThan); + } + + @Override + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + lessThan = toscaType.convert(String.valueOf(lessThan)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">"); + } } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java index 1cafc213bb..f6bc589521 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java @@ -25,6 +25,10 @@ import com.google.common.collect.Sets; import java.util.List; import java.util.Set; import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; @@ -34,20 +38,20 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +@NoArgsConstructor public class ValidValuesConstraint extends AbstractPropertyConstraint { private static final String PROPERTY_TYPE_IS = "> property type is <"; + @Getter + @Setter @NotNull - private List validValues; + private List validValues; private Set validValuesTyped; - public ValidValuesConstraint(List validValues) { + public ValidValuesConstraint(List validValues) { this.validValues = validValues; } - public ValidValuesConstraint() { - } - @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { validValuesTyped = Sets.newHashSet(); @@ -55,12 +59,12 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">"); } - for (String value : validValues) { - if (!propertyType.isValidValue(value)) { + for (Object value : validValues) { + if (!propertyType.isValidValue(String.valueOf(value))) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">"); } else { - validValuesTyped.add(propertyType.convert(value)); + validValuesTyped.add(propertyType.convert(String.valueOf(value))); } } } @@ -75,8 +79,8 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <> property type is <" + propertyType + ">"); } - for (String value : validValues) { - if (!toscaType.isValidValue(value)) { + for (Object value : validValues) { + if (!toscaType.isValidValue(String.valueOf(value))) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType + ">"); } @@ -105,14 +109,6 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { } } - public List getValidValues() { - return validValues; - } - - public void setValidValues(List validValues) { - this.validValues = validValues; - } - @Override public ConstraintType getConstraintType() { return ConstraintType.VALID_VALUES; @@ -120,6 +116,34 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", validValues)); + return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", String.valueOf(validValues))); + } + + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "validValues constraint has invalid values <" + validValues + "> property type is <" + propertyType + ">"); + } + if (validValues == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "validValues constraint has invalid value <> property type is <" + propertyType + ">"); + } + for (Object value : validValues) { + if (!toscaType.isValueTypeValid(value)) { + return false; + } + } + return true; + } + + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + validValues.replaceAll(obj -> toscaType.convert(String.valueOf(obj))); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "validValues constraint has invalid values <" + validValues + "> property type is <" + propertyType + ">"); + } } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperationTest.java index 8b8e813501..39f9118337 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperationTest.java @@ -523,7 +523,7 @@ public class CapabilityTypeOperationTest extends ModelTestBase { property2.setDescription("Number of (actual or virtual) CPUs associated with the Compute node."); property2.setType(ToscaType.INTEGER.name().toLowerCase()); List constraints3 = new ArrayList<>(); - List range = new ArrayList<>(); + List range = new ArrayList<>(); range.add("1"); range.add("4"); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java index 33322ecfb6..3f78ff0b03 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java @@ -166,17 +166,17 @@ public class PropertyOperationTest extends ModelTestBase { constraints.add(propertyConstraint10); return constraints; } - + private InRangeConstraint buildInRangeConstraint() { - List range = new ArrayList<>(); + List range = new ArrayList<>(); range.add("23"); range.add("67"); InRangeConstraint inRangeConstraint = new InRangeConstraint(range); return inRangeConstraint; } - + private ValidValuesConstraint buildValidValuesConstraint() { - List validValues = new ArrayList<>(); + List validValues = new ArrayList<>(); validValues.add("abc"); validValues.add("def"); validValues.add("fhi"); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java index 73a60079c3..5f8bde8090 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java @@ -302,7 +302,7 @@ public class RelationshipTypeOperationTest extends ModelTestBase { property2.setDescription("Number of (actual or virtual) CPUs associated with the Compute node."); property2.setType(ToscaType.INTEGER.name().toLowerCase()); List constraints3 = new ArrayList<>(); - List range = new ArrayList<>(); + List range = new ArrayList<>(); range.add("4"); range.add("1"); InRangeConstraint propertyConstraint3 = new InRangeConstraint(range); @@ -593,7 +593,7 @@ public class RelationshipTypeOperationTest extends ModelTestBase { propertyDefinition.setDescription(PROP + "_" + value); propertyDefinition.setType(ToscaType.INTEGER.name().toLowerCase()); List constraints = new ArrayList<>(); - List range = new ArrayList<>(); + List range = new ArrayList<>(); range.add("1"); range.add("4"); InRangeConstraint propertyConstraint = new InRangeConstraint(range); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraintTest.java index 6d9a4c4e3a..7f35aff071 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraintTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraintTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,29 +20,91 @@ package org.openecomp.sdc.be.model.tosca.constraints; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; public class EqualConstraintTest { - private EqualConstraint createTestSubject() { - return new EqualConstraint(""); - } + private EqualConstraint createStringTestSubject() { + return new EqualConstraint("test"); + } - + private EqualConstraint createIntegerTestSubject() { + return new EqualConstraint(418); + } - - @Test - public void testValidate() throws Exception { - EqualConstraint testSubject; - Object propertyValue = null; + @Test + public void testGetEqual() { + EqualConstraint testSubject = createStringTestSubject(); + Object result = testSubject.getEqual(); - // test 1 - testSubject = createTestSubject(); - propertyValue = null; - testSubject.validate(propertyValue); - } + assertEquals("test", result); + } - + @Test + public void testSetEqual() { + EqualConstraint testSubject = createStringTestSubject(); + testSubject.setEqual("test2"); + Object result = testSubject.getEqual(); + assertEquals("test2", result); + } + + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + EqualConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + EqualConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + EqualConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + EqualConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertFalse(validTypes); + } + + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + EqualConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); + + String expectedMessage = + "equal constraint has invalid values <" + testSubject.getEqual() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + EqualConstraint testSubject = createIntegerTestSubject(); + + testSubject.changeConstraintValueTypeTo("string"); + Object result = testSubject.getEqual(); + + assertTrue(result instanceof String); + } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java new file mode 100644 index 0000000000..d5d8fa7b35 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraintTest.java @@ -0,0 +1,109 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.model.tosca.constraints; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; + +public class GreaterOrEqualConstraintTest { + + private GreaterOrEqualConstraint createStringTestSubject() { + return new GreaterOrEqualConstraint("test"); + } + + private GreaterOrEqualConstraint createIntegerTestSubject() { + return new GreaterOrEqualConstraint(418); + } + + @Test + public void testGetGreaterOrEqualThan() { + GreaterOrEqualConstraint testSubject = createStringTestSubject(); + Object result = testSubject.getGreaterOrEqual(); + + assertEquals("test", result); + } + + @Test + public void testSetGreaterOrEqual() { + GreaterOrEqualConstraint testSubject = createStringTestSubject(); + testSubject.setGreaterOrEqual("test2"); + Object result = testSubject.getGreaterOrEqual(); + + assertEquals("test2", result); + } + + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterOrEqualConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterOrEqualConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterOrEqualConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterOrEqualConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertFalse(validTypes); + } + + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + GreaterOrEqualConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); + + String expectedMessage = + "greaterOrEqual constraint has invalid values <" + testSubject.getGreaterOrEqual() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterOrEqualConstraint testSubject = createIntegerTestSubject(); + + testSubject.changeConstraintValueTypeTo("string"); + Object result = testSubject.getGreaterOrEqual(); + + assertTrue(result instanceof String); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraintTest.java index dd79d80a79..41bc94f661 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraintTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraintTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,37 +20,91 @@ package org.openecomp.sdc.be.model.tosca.constraints; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; public class GreaterThanConstraintTest { - private GreaterThanConstraint createTestSubject() { - return new GreaterThanConstraint(""); - } + private GreaterThanConstraint createStringTestSubject() { + return new GreaterThanConstraint("test"); + } + + private GreaterThanConstraint createIntegerTestSubject() { + return new GreaterThanConstraint(418); + } + + @Test + public void testGetGreaterThan() { + GreaterThanConstraint testSubject = createStringTestSubject(); + Object result = testSubject.getGreaterThan(); + + assertEquals("test", result); + } + + @Test + public void testSetGreaterThan() { + GreaterThanConstraint testSubject = createStringTestSubject(); + testSubject.setGreaterThan("test2"); + Object result = testSubject.getGreaterThan(); + + assertEquals("test2", result); + } + + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterThanConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterThanConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterThanConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertTrue(validTypes); + } + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterThanConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertFalse(validTypes); + } + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + GreaterThanConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); - + String expectedMessage = + "greaterThan constraint has invalid values <" + testSubject.getGreaterThan() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); - - @Test - public void testGetGreaterThan() throws Exception { - GreaterThanConstraint testSubject; - String result; + assertTrue(actualMessage.contains(expectedMessage)); + } - // default test - testSubject = createTestSubject(); - result = testSubject.getGreaterThan(); - } + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + GreaterThanConstraint testSubject = createIntegerTestSubject(); - - @Test - public void testSetGreaterThan() throws Exception { - GreaterThanConstraint testSubject; - String greaterThan = ""; + testSubject.changeConstraintValueTypeTo("string"); + Object result = testSubject.getGreaterThan(); - // default test - testSubject = createTestSubject(); - testSubject.setGreaterThan(greaterThan); - } + assertTrue(result instanceof String); + } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraintTest.java index 9d14bfc72b..8eff6f7cdc 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraintTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraintTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,59 +20,144 @@ package org.openecomp.sdc.be.model.tosca.constraints; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +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.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; public class InRangeConstraintTest { - private InRangeConstraint createTestSubject() { - return new InRangeConstraint(null); - } - - - - - - @Test - public void testGetRangeMinValue() throws Exception { - InRangeConstraint testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRangeMinValue(); - } - - - @Test - public void testSetRangeMinValue() throws Exception { - InRangeConstraint testSubject; - String minValue = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setRangeMinValue(minValue); - } - - - @Test - public void testGetRangeMaxValue() throws Exception { - InRangeConstraint testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRangeMaxValue(); - } - - - @Test - public void testSetRangeMaxValue() throws Exception { - InRangeConstraint testSubject; - String maxValue = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setRangeMaxValue(maxValue); - } + private InRangeConstraint createStringTestSubject() { + List validValues = new ArrayList<>(); + validValues.add("test1"); + validValues.add("test10"); + return new InRangeConstraint(validValues); + } + + private InRangeConstraint createIntegerTestSubject() { + List validValues = new ArrayList<>(); + validValues.add(1); + validValues.add(10); + return new InRangeConstraint(validValues); + } + + @Test + public void testGetInRange() { + InRangeConstraint testSubject = createStringTestSubject(); + List result = testSubject.getInRange(); + + assertFalse(result.isEmpty()); + assertEquals("test1", result.get(0)); + assertEquals("test10", result.get(1)); + } + + @Test + public void testSetInRange() { + InRangeConstraint testSubject = createStringTestSubject(); + List validValues = new ArrayList<>(); + validValues.add("test21"); + validValues.add("test30"); + testSubject.setInRange(validValues); + List result = testSubject.getInRange(); + + assertEquals(2, result.size()); + assertEquals("test21", result.get(0)); + assertEquals("test30", result.get(1)); + } + + @Test + public void testGetRangeMinValue() throws Exception { + InRangeConstraint testSubject = createIntegerTestSubject(); + Object result = testSubject.getRangeMinValue(); + + assertEquals(1, result); + } + + @Test + public void testSetRangeMinValue() throws Exception { + InRangeConstraint testSubject = createIntegerTestSubject(); + testSubject.setRangeMinValue(21); + + Object result = testSubject.getRangeMinValue(); + + assertEquals(21, result); + } + + @Test + public void testGetRangeMaxValue() throws Exception { + InRangeConstraint testSubject = createIntegerTestSubject(); + Object result = testSubject.getRangeMaxValue(); + + assertEquals(10, result); + } + + @Test + public void testSetRangeMaxValue() throws Exception { + InRangeConstraint testSubject = createIntegerTestSubject(); + testSubject.setRangeMaxValue(30); + + Object result = testSubject.getRangeMaxValue(); + + assertEquals(30, result); + } + + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + InRangeConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + InRangeConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + InRangeConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + InRangeConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + + assertFalse(validTypes); + } + + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + InRangeConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); + String expectedMessage = + "inRange constraint has invalid values <" + testSubject.getInRange() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + InRangeConstraint testSubject = createIntegerTestSubject(); + testSubject.changeConstraintValueTypeTo("string"); + List result = testSubject.getInRange(); + + result.forEach(value -> assertTrue(value instanceof String)); + } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraintTest.java index ef567d9672..aae21f0335 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraintTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraintTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,39 +20,91 @@ package org.openecomp.sdc.be.model.tosca.constraints; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; public class LessOrEqualConstraintTest { - private LessOrEqualConstraint createTestSubject() { - return new LessOrEqualConstraint(""); - } + private LessOrEqualConstraint createStringTestSubject() { + return new LessOrEqualConstraint("test"); + } - + private LessOrEqualConstraint createIntegerTestSubject() { + return new LessOrEqualConstraint(418); + } - + @Test + public void testGetLessOrEqualThan() { + LessOrEqualConstraint testSubject = createStringTestSubject(); + Object result = testSubject.getLessOrEqual(); + assertEquals("test", result); + } - - @Test - public void testGetLessOrEqual() throws Exception { - LessOrEqualConstraint testSubject; - String result; + @Test + public void testSetLessOrEqual() { + LessOrEqualConstraint testSubject = createStringTestSubject(); + testSubject.setLessOrEqual("test2"); + Object result = testSubject.getLessOrEqual(); - // default test - testSubject = createTestSubject(); - result = testSubject.getLessOrEqual(); - } + assertEquals("test2", result); + } - - @Test - public void testSetLessOrEqual() throws Exception { - LessOrEqualConstraint testSubject; - String lessOrEqual = ""; + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + LessOrEqualConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertTrue(validTypes); + } - // default test - testSubject = createTestSubject(); - testSubject.setLessOrEqual(lessOrEqual); - } + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + LessOrEqualConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + LessOrEqualConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + LessOrEqualConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertFalse(validTypes); + } + + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + LessOrEqualConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); + + String expectedMessage = + "lessOrEqual constraint has invalid values <" + testSubject.getLessOrEqual() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + LessOrEqualConstraint testSubject = createIntegerTestSubject(); + + testSubject.changeConstraintValueTypeTo("string"); + Object result = testSubject.getLessOrEqual(); + + assertTrue(result instanceof String); + } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java new file mode 100644 index 0000000000..6736452d4d --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraintTest.java @@ -0,0 +1,109 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.model.tosca.constraints; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; + +public class LessThanConstraintTest { + + private LessThanConstraint createStringTestSubject() { + return new LessThanConstraint("test"); + } + + private LessThanConstraint createIntegerTestSubject() { + return new LessThanConstraint(418); + } + + @Test + public void testGetLessThan() { + LessThanConstraint testSubject = createStringTestSubject(); + Object result = testSubject.getLessThan(); + + assertEquals("test", result); + } + + @Test + public void testSetLessThan() { + LessThanConstraint testSubject = createStringTestSubject(); + testSubject.setLessThan("test2"); + Object result = testSubject.getLessThan(); + + assertEquals("test2", result); + } + + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + LessThanConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + LessThanConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + LessThanConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + LessThanConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertFalse(validTypes); + } + + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + LessThanConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); + + String expectedMessage = + "lessThan constraint has invalid values <" + testSubject.getLessThan() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + LessThanConstraint testSubject = createIntegerTestSubject(); + + testSubject.changeConstraintValueTypeTo("string"); + Object result = testSubject.getLessThan(); + + assertTrue(result instanceof String); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java index 69f67b3669..01450e2632 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,41 +20,114 @@ package org.openecomp.sdc.be.model.tosca.constraints; -import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; public class ValidValuesConstraintTest { - private ValidValuesConstraint createTestSubject() { - return new ValidValuesConstraint(null); - } + private ValidValuesConstraint createStringTestSubject() { + List validValues = new ArrayList<>(); + validValues.add("test1"); + validValues.add("test2"); + validValues.add("test3"); + validValues.add("test4"); + return new ValidValuesConstraint(validValues); + } + + private ValidValuesConstraint createIntegerTestSubject() { + List validValues = new ArrayList<>(); + validValues.add(1); + validValues.add(2); + validValues.add(3); + validValues.add(4); + return new ValidValuesConstraint(validValues); + } + + @Test + public void testGetValidValues() { + ValidValuesConstraint testSubject = createStringTestSubject(); + List result = testSubject.getValidValues(); + + assertFalse(result.isEmpty()); + assertEquals("test1", result.get(0)); + assertEquals("test4", result.get(3)); + } + + @Test + public void testSetValidValues() { + ValidValuesConstraint testSubject = createStringTestSubject(); + List validValues = new ArrayList<>(); + validValues.add("test5"); + validValues.add("test6"); + validValues.add("test7"); + validValues.add("test8"); + testSubject.setValidValues(validValues); + + List result = testSubject.getValidValues(); + + assertEquals(4, result.size()); + assertEquals("test5", result.get(0)); + assertEquals("test8", result.get(3)); + } + + @Test + public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + ValidValuesConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertTrue(validTypes); + } + + @Test + public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + ValidValuesConstraint testSubject = createStringTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertFalse(validTypes); + } + + @Test + public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException { + ValidValuesConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("integer"); + assertTrue(validTypes); + } - + @Test + public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException { + ValidValuesConstraint testSubject = createIntegerTestSubject(); + Boolean validTypes = testSubject.validateValueType("string"); + assertFalse(validTypes); + } - + @Test + public void testChangeStringConstraintValueTypeToIntegerThrow() { + String propertyType = "integer"; + ValidValuesConstraint testSubject = createStringTestSubject(); + Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> { + testSubject.changeConstraintValueTypeTo(propertyType); + }); + String expectedMessage = + "validValues constraint has invalid values <" + testSubject.getValidValues() + "> property type is <" + propertyType + ">"; + String actualMessage = exception.getMessage(); - - @Test - public void testGetValidValues() throws Exception { - ValidValuesConstraint testSubject; - List result; + assertTrue(actualMessage.contains(expectedMessage)); + } - // default test - testSubject = createTestSubject(); - result = testSubject.getValidValues(); - } + @Test + public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException { + ValidValuesConstraint testSubject = createIntegerTestSubject(); - - @Test - public void testSetValidValues() throws Exception { - ValidValuesConstraint testSubject; - List validValues = null; + testSubject.changeConstraintValueTypeTo("string"); + List result = testSubject.getValidValues(); - // default test - testSubject = createTestSubject(); - testSubject.setValidValues(validValues); - } + result.forEach(value -> assertTrue(value instanceof String)); + } } -- cgit 1.2.3-korg