summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java
diff options
context:
space:
mode:
authoreschcam <cameron.scholes@est.tech>2023-05-29 16:45:58 +0100
committerMichael Morris <michael.morris@est.tech>2023-06-13 10:47:13 +0000
commit762545c2f1ba3480d900d39adde3b1afe3823303 (patch)
tree255449f969854cee81eaf653b917bb28b0ae72d9 /catalog-model/src/test/java
parent89fb96bf935063fdffedafc10a13923dcf02e460 (diff)
[BUG] Fix error when adding valid_values constraint
Issue-ID: SDC-4515 Signed-off-by: eschcam <cameron.scholes@est.tech> Change-Id: Id8f951053581337504b8d75c76d2409ebd71940e
Diffstat (limited to 'catalog-model/src/test/java')
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java165
1 files changed, 165 insertions, 0 deletions
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 1e03513aa4..f6c63ad186 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
@@ -20,6 +20,7 @@
package org.openecomp.sdc.be.model.tosca.constraints;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -28,7 +29,11 @@ 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.datatypes.elements.SchemaDefinition;
+import org.openecomp.sdc.be.model.PropertyDefinition;
+import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
class ValidValuesConstraintTest {
@@ -125,4 +130,164 @@ class ValidValuesConstraintTest {
result.forEach(value -> assertTrue(value instanceof String));
}
+
+ @Test
+ void testValidateConstraintWithoutDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setType(ToscaType.INTEGER.getType());
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
+
+ @Test
+ void testValidateConstraintWithDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setDefaultValue("2");
+ prop.setType(ToscaType.INTEGER.getType());
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
+ @Test
+ void testValidateConstraintWithValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setValue("2");
+ prop.setType(ToscaType.INTEGER.getType());
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
+
+ @Test
+ void testValidateConstraintWithIncorrectDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setDefaultValue("1000");
+ prop.setType(ToscaType.INTEGER.getType());
+
+ assertThrows(ConstraintViolationException.class, () -> {
+ constraint.validate(prop);
+ });
+ }
+
+ @Test
+ void testValidateConstraintWithInvalidDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setDefaultValue("A String");
+ prop.setType(ToscaType.INTEGER.getType());
+
+ assertThrows(NumberFormatException.class, () -> {
+ constraint.validate(prop);
+ });
+ }
+
+ @Test
+ void testValidateConstraintWithNullProp() {
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+
+ assertThrows(NullPointerException.class, () -> {
+ constraint.validate(null);
+ });
+ }
+
+ @Test
+ void testValidateConstraintWithListPropWithDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ PropertyDefinition intProp = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setDefaultValue("[\"2\"]");
+ prop.setType(ToscaType.LIST.getType());
+
+ intProp.setType(ToscaType.INTEGER.getType());
+ schemaDefinition.setProperty(intProp);
+ prop.setSchema(schemaDefinition);
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
+ @Test
+ void testValidateConstraintWithListPropWithValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ PropertyDefinition intProp = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setValue("[\"2\"]");
+ prop.setType(ToscaType.LIST.getType());
+
+ intProp.setType(ToscaType.INTEGER.getType());
+ schemaDefinition.setProperty(intProp);
+ prop.setSchema(schemaDefinition);
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
+
+ @Test
+ void testValidateConstraintWithMapPropWithDefaultValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ PropertyDefinition intProp = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setDefaultValue("{\"key\": \"2\"}");
+ prop.setType(ToscaType.MAP.getType());
+
+ intProp.setType(ToscaType.INTEGER.getType());
+ schemaDefinition.setProperty(intProp);
+ prop.setSchema(schemaDefinition);
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
+ @Test
+ void testValidateConstraintWithMapPropWithValue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ PropertyDefinition prop = new PropertyDefinition();
+ PropertyDefinition intProp = new PropertyDefinition();
+ ValidValuesConstraint constraint = createIntegerTestSubject();
+ SchemaDefinition schemaDefinition = new SchemaDefinition();
+ constraint.initialize(ToscaType.INTEGER, schemaDefinition);
+
+ prop.setValue("{\"key\": \"2\"}");
+ prop.setType(ToscaType.MAP.getType());
+
+ intProp.setType(ToscaType.INTEGER.getType());
+ schemaDefinition.setProperty(intProp);
+ prop.setSchema(schemaDefinition);
+
+ assertDoesNotThrow(() -> {
+ constraint.validate(prop);
+ });
+ }
}