Added unit tests for AbstractPropertyConstraint 89/90089/3
authorPiotr Borelowski <p.borelowski@partner.samsung.com>
Tue, 18 Jun 2019 14:08:08 +0000 (16:08 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 6 Aug 2019 08:54:15 +0000 (08:54 +0000)
Improved the unit test coverage in the package
org.openecomp.sdc.be.model.tosca.constraints

Issue-ID: SDC-2327
Signed-off-by: Piotr Borelowski <p.borelowski@partner.samsung.com>
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I094ab463cc010ecd48e062a1a806b3b907223228

catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraintTest.java [new file with mode: 0644]

index 04b6874..da1b3c4 100644 (file)
@@ -163,7 +163,7 @@ public enum ToscaType {
                        case BOOLEAN:
                                return Boolean.valueOf(value);
                        case FLOAT:
-                               return Double.valueOf(value);
+                               return Float.valueOf(value);
                        case INTEGER:
                                return Long.valueOf(value);
                        case TIMESTAMP:
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraintTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraintTest.java
new file mode 100644 (file)
index 0000000..82d0394
--- /dev/null
@@ -0,0 +1,113 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP SDC
+ * ================================================================================
+ * Copyright (C) 2019 Samsung. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ */
+
+package org.openecomp.sdc.be.model.tosca.constraints;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.openecomp.sdc.be.model.tosca.ToscaType;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
+import org.openecomp.sdc.be.model.tosca.version.Version;
+
+public class AbstractPropertyConstraintTest {
+
+    private final TestPropertyConstraint constraint = new TestPropertyConstraint();
+
+    @Test
+    public void testValidateBoolean() throws ConstraintViolationException {
+        // given
+        final String value = "faLsE";
+
+        // when
+        constraint.validate(ToscaType.BOOLEAN, value);
+
+        // then
+        assertEquals(Boolean.valueOf(value), constraint.last);
+    }
+
+    @Test
+    public void testValidateFloat() throws ConstraintViolationException {
+        // given
+        final String value = "123.456";
+
+        // when
+        constraint.validate(ToscaType.FLOAT, value);
+
+        // then
+        assertEquals(Float.valueOf(value), constraint.last);
+    }
+
+    @Test
+    public void testValidateVersion() throws ConstraintViolationException {
+        // given
+        final String value = "12.34.1002-Release7";
+
+        // when
+        constraint.validate(ToscaType.VERSION, value);
+
+        // then
+        assertEquals(new Version(value), constraint.last);
+    }
+
+    @Test
+    public void testGetErrorMessageIsInstanceOf() {
+        // given
+        final ConstraintFunctionalException exception = new ConstraintViolationException("exc");
+
+        // when
+        final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT, exception,
+                "area", "Message: %s --> %s", "prop1", "prop2");
+
+        // then
+        assertEquals("Message: area --> [prop1, prop2]", message);
+    }
+
+    @Test
+    public void testGetErrorMessageOtherType() {
+        // given
+        final ConstraintFunctionalException exception = new ConstraintFunctionalException("exc");
+
+        // when
+        final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT_FREQUENCY, exception,
+                "freq", null);
+
+        // then
+        assertEquals("Unsupported value provided for freq property supported value type is scalar-unit.frequency.",
+                message);
+    }
+}
+
+class TestPropertyConstraint extends AbstractPropertyConstraint {
+
+    Object last;
+
+    @Override
+    public void validate(Object propertyValue) {
+        last = propertyValue;
+    }
+
+    @Override
+    public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName) {
+        return null;
+    }
+}