summaryrefslogtreecommitdiffstats
path: root/catalog-model/src
diff options
context:
space:
mode:
authorvasraz <vasyl.razinkov@est.tech>2022-12-09 12:36:05 +0000
committerMichael Morris <michael.morris@est.tech>2022-12-20 16:54:20 +0000
commitf995db01ee95606b6cded82822a73435ebc190c8 (patch)
tree1279aee5baa9005ab86ca6f77eca32c554ae149d /catalog-model/src
parent7e08a2976d34066412af14fe633eecde3ce19fc7 (diff)
Add support comparable type constraints for scalar values
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech> Change-Id: I57234399f23721506d308dfb8351067845ebe892 Issue-ID: SDC-4305
Diffstat (limited to 'catalog-model/src')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java19
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java183
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java42
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/ToscaTypeTest.java400
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtilTest.java66
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraintTest.java31
6 files changed, 526 insertions, 215 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java
index 364dfd33fb..ef08e4d107 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java
@@ -19,6 +19,7 @@
*/
package org.openecomp.sdc.be.model.tosca;
+import lombok.Getter;
import org.openecomp.sdc.be.model.tosca.converters.BooleanConverter;
import org.openecomp.sdc.be.model.tosca.converters.DefaultConverter;
import org.openecomp.sdc.be.model.tosca.converters.FloatConverter;
@@ -51,6 +52,7 @@ import org.openecomp.sdc.be.model.tosca.validators.StringValidator;
*
* @author esofer
*/
+@Getter
public enum ToscaPropertyType {
// @formatter:off
ROOT("tosca.datatypes.Root", null, null, null, true),
@@ -62,6 +64,7 @@ public enum ToscaPropertyType {
SCALAR_UNIT_SIZE("scalar-unit.size", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
SCALAR_UNIT_TIME("scalar-unit.time", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
SCALAR_UNIT_FREQUENCY("scalar-unit.frequency", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
+ SCALAR_UNIT_BITRATE("scalar-unit.bitrate", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
RANGE("range", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
TIMESTAMP("timestamp", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
MAP("map", MapValidator.getInstance(), MapConverter.getInstance(), ToscaMapValueConverter.getInstance()),
@@ -123,26 +126,10 @@ public enum ToscaPropertyType {
}
}
- public String getType() {
- return type;
- }
-
- public PropertyTypeValidator getValidator() {
- return validator;
- }
-
- public PropertyValueConverter getConverter() {
- return converter;
- }
-
public boolean isAbstract() {
return isAbstract;
}
- public ToscaValueConverter getValueConverter() {
- return valueConverter;
- }
-
@Override
public String toString() {
return name().toLowerCase();
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 f59ebe6922..a3daaa0381 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
@@ -27,6 +27,8 @@ import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
@@ -53,9 +55,23 @@ public enum ToscaType {
SCALAR_UNIT("scalar-unit"),
SCALAR_UNIT_SIZE("scalar-unit.size"),
SCALAR_UNIT_TIME("scalar-unit.time"),
+ SCALAR_UNIT_BITRATE("scalar-unit.bitrate"),
SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
// @formatter:on
+ private static final String SCALAR_UNIT_BITRATE_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([TtGgMmKk]?i?[Bb]ps)$";
+ private static final String SCALAR_UNIT_TIME_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([mun]?[dhms])$";
+ private static final String SCALAR_UNIT_SIZE_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([TtGgMmKk]?i?[Bb])$";
+ private static final String SCALAR_UNIT_FREQUENCY_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([kMG]?Hz)$";
+ private static final double B_IN_TiB = Math.pow(1024, 4);
+ private static final double B_IN_GiB = Math.pow(1024, 3);
+ private static final double B_IN_MiB = Math.pow(1024, 2);
+ private static final double B_IN_KiB = Math.pow(1024, 1);
+ private static final double B_IN_TB = Math.pow(1000, 4);
+ private static final double B_IN_GB = Math.pow(1000, 3);
+ private static final double B_IN_MB = Math.pow(1000, 2);
+ private static final double B_IN_KB = Math.pow(1000, 1);
+
@Getter
private final String type;
@@ -94,7 +110,7 @@ public enum ToscaType {
return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type);
}
- public Boolean isValueTypeValid(Object value) {
+ public boolean isValueTypeValid(Object value) {
switch (this) {
case BOOLEAN:
return value.equals(true) || value.equals(false);
@@ -104,9 +120,9 @@ public enum ToscaType {
case RANGE:
return value instanceof Integer;
case STRING:
- case SCALAR_UNIT:
case SCALAR_UNIT_SIZE:
case SCALAR_UNIT_TIME:
+ case SCALAR_UNIT_BITRATE:
case SCALAR_UNIT_FREQUENCY:
case TIMESTAMP:
case VERSION:
@@ -114,6 +130,7 @@ public enum ToscaType {
case LIST:
case MAP:
return true;
+ case SCALAR_UNIT:
default:
return false;
}
@@ -127,11 +144,15 @@ public enum ToscaType {
return isFloat(value);
case INTEGER:
return isInteger(value);
- case STRING:
- case SCALAR_UNIT:
case SCALAR_UNIT_SIZE:
+ return isScalarUnitSize(value);
case SCALAR_UNIT_TIME:
+ return isScalarUnitTime(value);
+ case SCALAR_UNIT_BITRATE:
+ return isScalarUnitBitrate(value);
case SCALAR_UNIT_FREQUENCY:
+ return isScalarUnitFrequency(value);
+ case STRING:
return true;
case TIMESTAMP:
return TimestampValidator.getInstance().isValid(value, null);
@@ -141,6 +162,7 @@ public enum ToscaType {
return isList(value);
case MAP:
return isMap(value);
+ case SCALAR_UNIT:
default:
return false;
}
@@ -189,11 +211,15 @@ public enum ToscaType {
public Object convert(String value) {
switch (this) {
case STRING:
- case SCALAR_UNIT:
- case SCALAR_UNIT_SIZE:
+ return value;
case SCALAR_UNIT_TIME:
+ return convertScalarUnitTime(value);
+ case SCALAR_UNIT_BITRATE:
+ return convertScalarUnitBitrate(value);
+ case SCALAR_UNIT_SIZE:
+ return convertScalarUnitSize(value);
case SCALAR_UNIT_FREQUENCY:
- return value;
+ return convertScalarUnitFrequency(value);
case BOOLEAN:
return Boolean.valueOf(value);
case FLOAT:
@@ -223,13 +249,150 @@ public enum ToscaType {
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
throw new IllegalArgumentException("Value must be a valid Map", e);
}
+ case SCALAR_UNIT:
default:
return null;
}
}
- @Override
- public String toString() {
- return name().toLowerCase();
+ private Long convertScalarUnitSize(final String value) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_SIZE_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "TiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TiB);
+ case "TB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TB);
+ case "GiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GiB);
+ case "GB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GB);
+ case "MiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MiB);
+ case "MB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MB);
+ case "KiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KiB);
+ case "kB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KB);
+ case "B":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.size");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.size");
+ }
+ }
+
+ private Long convertScalarUnitTime(final String value) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_TIME_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "d":
+ return (long) (Double.parseDouble(matcher.group(1)) * 24 * 60 * 60 * 1_000_000_000L); // 24hours * 60minutes * 60seconds
+ case "h":
+ return (long) (Double.parseDouble(matcher.group(1)) * 60 * 60 * 1_000_000_000L); // 60minutes * 60seconds
+ case "m":
+ return (long) (Double.parseDouble(matcher.group(1)) * 60 * 1_000_000_000L); // 60seconds
+ case "s":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000_000L);
+ case "ms":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000L);
+ case "us":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000L);
+ case "ns":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.time");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.time");
+ }
+ }
+
+ private Long convertScalarUnitFrequency(final String value) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_FREQUENCY_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "GHz":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000_000L);
+ case "MHz":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000L);
+ case "kHz":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000L);
+ case "Hz":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.frequency");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.frequency");
+ }
+ }
+
+ private Long convertScalarUnitBitrate(final String value) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_BITRATE_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "TiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_TiB);
+ case "TBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_TB);
+ case "GiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_GiB);
+ case "GBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_GB);
+ case "MiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_MiB);
+ case "MBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_MB);
+ case "KiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_KiB);
+ case "KBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_KB);
+ case "Bps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8);
+ case "Tibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TiB);
+ case "Tbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TB);
+ case "Gibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GiB);
+ case "Gbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GB);
+ case "Mibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MiB);
+ case "Mbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MB);
+ case "Kibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KiB);
+ case "Kbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KB);
+ case "bps":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.bitrate");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.bitrate");
+ }
+ }
+
+ private boolean isScalarUnitBitrate(final String value) {
+ return Pattern.compile(SCALAR_UNIT_BITRATE_PATTERN).matcher(value.trim()).find();
}
+
+ private boolean isScalarUnitSize(final String value) {
+ return Pattern.compile(SCALAR_UNIT_SIZE_PATTERN).matcher(value.trim()).find();
+ }
+
+ private boolean isScalarUnitTime(final String value) {
+ return Pattern.compile(SCALAR_UNIT_TIME_PATTERN).matcher(value.trim()).find();
+ }
+
+ private boolean isScalarUnitFrequency(final String value) {
+ return Pattern.compile(SCALAR_UNIT_FREQUENCY_PATTERN).matcher(value.trim()).find();
+ }
+
}
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java
index 1b9b94eb61..61f069a45f 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java
@@ -26,6 +26,9 @@ import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
import org.slf4j.Logger;
@@ -34,13 +37,11 @@ import org.slf4j.LoggerFactory;
/**
* Utility class to validate constraints types.
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ConstraintUtil {
private static final Logger logger = LoggerFactory.getLogger(ConstraintUtil.class);
- private ConstraintUtil() {
- }
-
/**
* Validates that the {@link ToscaType} specified is a {@link ToscaType#STRING}.
*
@@ -59,20 +60,26 @@ public final class ConstraintUtil {
* @param propertyType the tosca type to check
* @throws ConstraintValueDoNotMatchPropertyTypeException if the property type cannot be compared
*/
- public static void checkComparableType(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+ public static void checkComparableType(final ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
// The validity of the value is already assured by us with our ToscaType.convert() method
// here we just want to check that the constraint is not used on unsupported type as boolean
- switch (propertyType) {
+ final ToscaType toscaType = ToscaType.getToscaType(propertyType.getType());
+ switch (toscaType) {
case FLOAT:
case INTEGER:
case TIMESTAMP:
case VERSION:
case STRING:
+ case SCALAR_UNIT_SIZE:
+ case SCALAR_UNIT_TIME:
+ case SCALAR_UNIT_BITRATE:
+ case SCALAR_UNIT_FREQUENCY:
break;
case BOOLEAN:
- throw new ConstraintValueDoNotMatchPropertyTypeException("Constraint is invalid for property type <" + propertyType.toString() + ">");
+ case SCALAR_UNIT:
+ throw new ConstraintValueDoNotMatchPropertyTypeException("Constraint is invalid for property type <" + propertyType.getType() + ">");
default:
- throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid property type <" + propertyType.toString() + ">");
+ throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid property type <" + propertyType.getType() + ">");
}
}
@@ -86,7 +93,7 @@ public final class ConstraintUtil {
*/
@SuppressWarnings("rawtypes")
public static Comparable convertToComparable(ToscaType propertyType, String value) {
- Object comparableObj = propertyType.convert(value);
+ final Object comparableObj = propertyType.convert(value);
if (!(comparableObj instanceof Comparable)) {
throw new IllegalArgumentException("Try to convert a value of a type which is not comparable [" + propertyType + "] to Comparable");
} else {
@@ -95,9 +102,8 @@ public final class ConstraintUtil {
}
public static ConstraintInformation getConstraintInformation(Object constraint) throws IntrospectionException {
- PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(constraint.getClass()).getPropertyDescriptors();
PropertyDescriptor firstDescriptor = null;
- for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
+ for (final PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(constraint.getClass()).getPropertyDescriptors()) {
if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) {
firstDescriptor = propertyDescriptor;
break;
@@ -125,18 +131,12 @@ public final class ConstraintUtil {
return objectMap;
}
+ @AllArgsConstructor
public static class ConstraintInformation {
- private String name;
- private Object reference;
- private String value;
- private String type;
-
- public ConstraintInformation(String name, Object reference, String value, String type) {
- this.name = name;
- this.reference = reference;
- this.value = value;
- this.type = type;
- }
+ private final String name;
+ private final Object reference;
+ private final String value;
+ private final String type;
}
}
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/ToscaTypeTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/ToscaTypeTest.java
index d2bfc375a2..103b15b79d 100644
--- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/ToscaTypeTest.java
+++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/ToscaTypeTest.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.
@@ -21,8 +21,10 @@
package org.openecomp.sdc.be.model.tosca;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Date;
import java.util.List;
@@ -30,123 +32,279 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
import org.openecomp.sdc.be.model.tosca.version.Version;
+class ToscaTypeTest {
+
+ @Test
+ void testIsValidValueBoolean() throws Exception {
+ ToscaType toscaType = ToscaType.BOOLEAN;
+
+ assertFalse(toscaType.isValidValue(""));
+ assertTrue(toscaType.isValidValue("false"));
+ assertTrue(toscaType.isValidValue("FalSe"));
+ assertTrue(toscaType.isValidValue("true"));
+ assertTrue(toscaType.isValidValue("TrUe"));
+ }
+
+ @Test
+ void testIsValidValueFloat() throws Exception {
+ ToscaType toscaType = ToscaType.FLOAT;
+
+ assertFalse(toscaType.isValidValue("float"));
+ assertTrue(toscaType.isValidValue("1.2534"));
+ assertTrue(toscaType.isValidValue("1.2534f"));
+ }
+
+ @Test
+ void testIsValidValueString() throws Exception {
+ ToscaType toscaType = ToscaType.STRING;
+
+ assertTrue(toscaType.isValidValue("string"));
+ assertTrue(toscaType.isValidValue("1 string"));
+ assertTrue(toscaType.isValidValue("2 s_t_r_i_n_g"));
+ }
+
+ @Test
+ void testIsValidValueInteger() throws Exception {
+ ToscaType toscaType = ToscaType.INTEGER;
+
+ assertFalse(toscaType.isValidValue("integer"));
+ assertTrue(toscaType.isValidValue("1235"));
+ }
+
+ @Test
+ void testIsValidValueTimestamp() throws Exception {
+ ToscaType toscaType = ToscaType.TIMESTAMP;
+
+ assertFalse(toscaType.isValidValue("timestamp"));
+ assertTrue(toscaType.isValidValue("2001-12-14t21:59:43.10-05:00"));
+ assertFalse(toscaType.isValidValue("30 juin 2009 07:03:47"));
+ }
+
+ @Test
+ void testIsValidValueVersion() throws Exception {
+ ToscaType toscaType = ToscaType.VERSION;
+
+ assertFalse(toscaType.isValidValue("version"));
+ assertTrue(toscaType.isValidValue("1.2"));
+ assertTrue(toscaType.isValidValue("1.2.3"));
+ assertTrue(toscaType.isValidValue("1.2-3"));
+ }
+
+ @Test
+ void testIsValidValueList() throws Exception {
+ ToscaType toscaType = ToscaType.LIST;
+
+ assertFalse(toscaType.isValidValue("list"));
+ assertTrue(toscaType.isValidValue("[\"color\",\"type\"]"));
+ }
+
+ @Test
+ void testIsValidValueMap() throws Exception {
+ ToscaType toscaType = ToscaType.MAP;
+
+ assertFalse(toscaType.isValidValue("map"));
+ assertTrue(toscaType.isValidValue("{\"color\":\"yellow\",\"type\":\"renault\"}"));
+ }
+
+ @Test
+ void testNotValidValueScalarUnit() {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT;
+
+ assertFalse(testSubject.isValidValue("5"));
+ }
+
+ @Test
+ void testIsValidValueScalarUnitSize() throws Exception {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_SIZE;
+
+ assertTrue(testSubject.isValidValue("5 TiB"));
+ assertFalse(testSubject.isValidValue("5"));
+ }
+
+ @Test
+ void testIsValidValueScalarUnitTime() throws Exception {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_TIME;
+
+ assertTrue(testSubject.isValidValue("5 d"));
+ assertFalse(testSubject.isValidValue("a5 sz"));
+ }
+
+ @Test
+ void testIsValidValueScalarUnitBitrate() throws Exception {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_BITRATE;
+
+ assertTrue(testSubject.isValidValue("5 TiBps"));
+ assertFalse(testSubject.isValidValue("5 bps5"));
+ }
+
+ @Test
+ void testIsValidValueScalarUnitFrequency() throws Exception {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_FREQUENCY;
+
+ assertTrue(testSubject.isValidValue("5 MHz"));
+ assertFalse(testSubject.isValidValue("5"));
+ }
+
+ @Test
+ void testGetToscaType() throws Exception {
+ ToscaType toscaType = ToscaType.MAP;
+
+ assertEquals(ToscaType.getToscaType("map"), toscaType);
+ assertNull(ToscaType.getToscaType(null));
+ assertNull(ToscaType.getToscaType("InvalidType"));
+ }
+
+ @Test
+ void testIsPrimitiveType() throws Exception {
+ assertFalse(ToscaType.isPrimitiveType(null));
+ assertFalse(ToscaType.isPrimitiveType("map"));
+ assertFalse(ToscaType.isPrimitiveType("list"));
+ assertFalse(ToscaType.isPrimitiveType("String"));
+ assertTrue(ToscaType.isPrimitiveType("string"));
+ assertTrue(ToscaType.isPrimitiveType("integer"));
+ }
+
+ @Test
+ void testIsCollectionType() throws Exception {
+ assertTrue(ToscaType.isCollectionType("map"));
+ assertTrue(ToscaType.isCollectionType("list"));
+ assertFalse(ToscaType.isCollectionType("Map"));
+ assertFalse(ToscaType.isCollectionType("string"));
+ assertFalse(ToscaType.isCollectionType("integer"));
+ }
+
+ @Test
+ void testConvert() throws Exception {
+ ToscaType typeInt = ToscaType.INTEGER;
+ assertEquals(123l, typeInt.convert("123"));
+
+ ToscaType typeBool = ToscaType.BOOLEAN;
+ assertEquals(true, typeBool.convert("true"));
+
+ ToscaType typeStr = ToscaType.STRING;
+ assertEquals("str", typeStr.convert("str"));
+
+ ToscaType typeFloat = ToscaType.FLOAT;
+ assertTrue(typeFloat.convert("1.2357f") instanceof Float);
+
+ ToscaType typeTimestamp = ToscaType.TIMESTAMP;
+ assertTrue(typeTimestamp.convert("Jun 30, 2009 7:03:47 AM") instanceof Date);
+ assertThrows(IllegalArgumentException.class, () -> typeTimestamp.convert(""));
+
+ ToscaType typeVersion = ToscaType.VERSION;
+ assertTrue(typeVersion.convert("1.2.3.5.6") instanceof Version);
+
+ ToscaType typeList = ToscaType.LIST;
+ assertTrue(typeList.convert("[\"str1\",\"str2\"]") instanceof List);
+ assertThrows(IllegalArgumentException.class, () -> typeList.convert(""));
+
+ ToscaType typeMap = ToscaType.MAP;
+ assertTrue(typeMap.convert("{\"color\":\"yellow\",\"type\":\"renault\"}") instanceof Map);
+ assertThrows(IllegalArgumentException.class, () -> typeMap.convert(""));
+
+ ToscaType typeScalarUnit = ToscaType.SCALAR_UNIT;
+ assertNull(typeScalarUnit.convert(""));
+
+ }
+
+ @Test
+ void testConvertScalarUnitTime() {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_TIME;
+ assertTrue(testSubject.convert("5d") instanceof Long);
+ assertTrue(testSubject.convert("4 h") instanceof Long);
+ assertTrue(testSubject.convert(" 3 m ") instanceof Long);
+ assertTrue(testSubject.convert("9.5s") instanceof Long);
+ assertTrue(testSubject.convert("90 ms") instanceof Long);
+ assertTrue(testSubject.convert("55.44 us") instanceof Long);
+ assertTrue(testSubject.convert("111ns") instanceof Long);
+
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5z.ms"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("a5 ms"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5 msZ"));
+ }
+
+ @Test
+ void testConvertScalarUnitSize() {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_SIZE;
+ assertTrue(testSubject.convert("5 TiB") instanceof Long);
+ assertTrue(testSubject.convert("4 TB") instanceof Long);
+ assertTrue(testSubject.convert("31 GiB") instanceof Long);
+ assertTrue(testSubject.convert("19.5 GB") instanceof Long);
+ assertTrue(testSubject.convert("90 MiB") instanceof Long);
+ assertTrue(testSubject.convert("55.44 MB") instanceof Long);
+ assertTrue(testSubject.convert("111 KiB") instanceof Long);
+ assertTrue(testSubject.convert("123. kB") instanceof Long);
+ assertTrue(testSubject.convert("0.9 B") instanceof Long);
+
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5z.MB"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("a5 TB"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5 TBz"));
+ }
+
+ @Test
+ void testConvertScalarUnitFrequency() {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_FREQUENCY;
+ assertTrue(testSubject.convert("5 GHz") instanceof Long);
+ assertTrue(testSubject.convert("41 MHz") instanceof Long);
+ assertTrue(testSubject.convert("319 kHz") instanceof Long);
+ assertTrue(testSubject.convert("19.5 Hz") instanceof Long);
+
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5z.GHz"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("a5 Hz"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5 Hza"));
+ }
+
+ @Test
+ void testConvertScalarUnitBitrate() {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_BITRATE;
+ assertTrue(testSubject.convert("5 TiBps") instanceof Long);
+ assertTrue(testSubject.convert("41 TBps") instanceof Long);
+ assertTrue(testSubject.convert("319 GiBps") instanceof Long);
+ assertTrue(testSubject.convert("19.5 GBps") instanceof Long);
+ assertTrue(testSubject.convert("90 MiBps") instanceof Long);
+ assertTrue(testSubject.convert("55.44 MBps") instanceof Long);
+ assertTrue(testSubject.convert("111 KiBps") instanceof Long);
+ assertTrue(testSubject.convert("123. KBps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Bps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Tibps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Tbps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Gibps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Gbps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Mibps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Mbps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Kibps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 Kbps") instanceof Long);
+ assertTrue(testSubject.convert("0.9 bps") instanceof Long);
+
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5z.Mbps"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("a5 MBps"));
+ assertThrows(IllegalArgumentException.class, () -> testSubject.convert("5 bps5"));
+ }
+
+ @Test
+ void testIsValueTypeValid() {
+ ToscaType testSubject = ToscaType.SCALAR_UNIT_BITRATE;
+ assertTrue(testSubject.isValueTypeValid(""));
+
+ testSubject = ToscaType.LIST;
+ assertTrue(testSubject.isValueTypeValid(""));
+
+ testSubject = ToscaType.INTEGER;
+ assertTrue(testSubject.isValueTypeValid(1));
+
+ testSubject = ToscaType.FLOAT;
+ assertTrue(testSubject.isValueTypeValid(2.3f));
+
+ testSubject = ToscaType.BOOLEAN;
+ assertTrue(testSubject.isValueTypeValid(true));
+
+ testSubject = ToscaType.SCALAR_UNIT;
+ assertFalse(testSubject.isValueTypeValid(""));
+
+ }
-public class ToscaTypeTest {
-
- @Test
- public void testIsValidValueBoolean() throws Exception {
- ToscaType toscaType = ToscaType.BOOLEAN;;
-
- assertTrue(!toscaType.isValidValue(""));
- assertTrue(toscaType.isValidValue("false"));
- assertTrue(toscaType.isValidValue("FalSe"));
- assertTrue(toscaType.isValidValue("true"));
- assertTrue(toscaType.isValidValue("TrUe"));
- }
-
- @Test
- public void testIsValidValueFloat() throws Exception {
- ToscaType toscaType = ToscaType.FLOAT;;
-
- assertTrue(!toscaType.isValidValue("float"));
- assertTrue(toscaType.isValidValue("1.2534"));
- assertTrue(toscaType.isValidValue("1.2534f"));
- }
-
- @Test
- public void testIsValidValueInteger() throws Exception {
- ToscaType toscaType = ToscaType.INTEGER;;
-
- assertTrue(!toscaType.isValidValue("integer"));
- assertTrue(toscaType.isValidValue("1235"));
- }
-
- @Test
- public void testIsValidValueTimestamp() throws Exception {
- ToscaType toscaType = ToscaType.TIMESTAMP;;
-
- assertTrue(!toscaType.isValidValue("timestamp"));
- assertTrue(toscaType.isValidValue("2001-12-14t21:59:43.10-05:00"));
- assertTrue(!toscaType.isValidValue("30 juin 2009 07:03:47"));
- }
-
- @Test
- public void testIsValidValueVersion() throws Exception {
- ToscaType toscaType = ToscaType.VERSION;;
-
- assertTrue(!toscaType.isValidValue("version"));
- assertTrue(toscaType.isValidValue("1.2"));
- assertTrue(toscaType.isValidValue("1.2.3"));
- assertTrue(toscaType.isValidValue("1.2-3"));
- }
-
- @Test
- public void testIsValidValueList() throws Exception {
- ToscaType toscaType = ToscaType.LIST;;
-
- assertTrue(!toscaType.isValidValue("list"));
- assertTrue(toscaType.isValidValue("[\"color\",\"type\"]"));
- }
-
- @Test
- public void testIsValidValueMap() throws Exception {
- ToscaType toscaType = ToscaType.MAP;;
-
- assertTrue(!toscaType.isValidValue("map"));
- assertTrue(toscaType.isValidValue("{\"color\":\"yellow\",\"type\":\"renault\"}"));
- }
-
- @Test
- public void testGetToscaType() throws Exception {
- ToscaType toscaType = ToscaType.MAP;;
-
- assertEquals(ToscaType.getToscaType("map"), toscaType);
- assertNull(ToscaType.getToscaType(null));
- assertNull(ToscaType.getToscaType("InvalidType"));
- }
-
- @Test
- public void testIsPrimitiveType() throws Exception {
- assertTrue(!ToscaType.isPrimitiveType("map"));
- assertTrue(!ToscaType.isPrimitiveType("list"));
- assertTrue(!ToscaType.isPrimitiveType("String"));
- assertTrue(ToscaType.isPrimitiveType("string"));
- assertTrue(ToscaType.isPrimitiveType("integer"));
- }
-
- @Test
- public void testIsCollectionType() throws Exception {
- assertTrue(ToscaType.isCollectionType("map"));
- assertTrue(ToscaType.isCollectionType("list"));
- assertTrue(!ToscaType.isCollectionType("Map"));
- assertTrue(!ToscaType.isCollectionType("string"));
- assertTrue(!ToscaType.isCollectionType("integer"));
- }
-
- @Test
- public void testConvert() throws Exception {
- ToscaType typeStr = ToscaType.STRING;
- assertEquals(typeStr.convert("str"), "str");
-
- ToscaType typeFloat = ToscaType.FLOAT;
- assertTrue(typeFloat.convert("1.2357f") instanceof Float);
-
- ToscaType typeTimestamp = ToscaType.TIMESTAMP;
- assertTrue(typeTimestamp.convert("Jun 30, 2009 7:03:47 AM") instanceof Date);
-
- ToscaType typeVersion = ToscaType.VERSION;
- assertTrue(typeVersion.convert("1.2.3.5.6") instanceof Version);
-
- ToscaType typeList = ToscaType.LIST;
- assertTrue(typeList.convert("[\"str1\",\"str2\"]") instanceof List);
-
- ToscaType typeMap = ToscaType.MAP;
- assertTrue(typeMap.convert("{\"color\":\"yellow\",\"type\":\"renault\"}") instanceof Map);
- }
-
- @Test
- public void testToString() throws Exception {
- ToscaType testToscaType = ToscaType.SCALAR_UNIT;
- assertEquals(testToscaType.toString(), "scalar_unit");
- }
}
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtilTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtilTest.java
index 7f7e6980e6..99987800d3 100644
--- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtilTest.java
+++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtilTest.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,47 @@
package org.openecomp.sdc.be.model.tosca.constraints;
-import org.junit.Test;
-import org.openecomp.sdc.be.model.tosca.ToscaType;
-
-public class ConstraintUtilTest {
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
- @Test
- public void testCheckStringType() throws Exception {
- ToscaType propertyType = ToscaType.STRING;
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.ToscaType;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
- // default test
- ConstraintUtil.checkStringType(propertyType);
- }
+class ConstraintUtilTest {
-
- @Test
- public void testCheckComparableType() throws Exception {
- ToscaType propertyType = ToscaType.INTEGER;
+ @Test
+ void testCheckStringType() throws Exception {
+ assertDoesNotThrow(() -> ConstraintUtil.checkStringType(ToscaType.STRING));
+ assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> ConstraintUtil.checkStringType(ToscaType.SCALAR_UNIT));
+ }
- // default test
- ConstraintUtil.checkComparableType(propertyType);
- }
+ @Test
+ void testCheckComparableType() throws Exception {
+ assertDoesNotThrow(() -> ConstraintUtil.checkComparableType(ToscaType.INTEGER));
+ assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> ConstraintUtil.checkComparableType(ToscaType.SCALAR_UNIT));
+ }
-
- @Test
- public void testConvertToComparable() throws Exception {
- ToscaType propertyType = ToscaType.BOOLEAN;
- String value = "";
- Comparable result;
+ @Test
+ void testConvertToComparable() throws Exception {
+ assertTrue(ConstraintUtil.convertToComparable(ToscaType.BOOLEAN, "true") instanceof Comparable);
+ assertThrows(IllegalArgumentException.class, () -> ConstraintUtil.convertToComparable(ToscaType.SCALAR_UNIT, "value"));
+ }
- // default test
- result = ConstraintUtil.convertToComparable(propertyType, value);
- }
+ @Test
+ void testParseToCollection() throws Exception {
+ List<Object> list = ConstraintUtil.parseToCollection("[\"color\",\"type\"]", new TypeReference<List<Object>>() {
+ });
+ assertTrue(list instanceof List);
+ assertEquals(2, list.size());
-
+ assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class,
+ () -> ConstraintUtil.parseToCollection("", new TypeReference<List<Object>>() {
+ }));
+ }
}
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 01450e2632..1e03513aa4 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
@@ -30,8 +30,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
-
-public class ValidValuesConstraintTest {
+class ValidValuesConstraintTest {
private ValidValuesConstraint createStringTestSubject() {
List<Object> validValues = new ArrayList<>();
@@ -52,7 +51,7 @@ public class ValidValuesConstraintTest {
}
@Test
- public void testGetValidValues() {
+ void testGetValidValues() {
ValidValuesConstraint testSubject = createStringTestSubject();
List<Object> result = testSubject.getValidValues();
@@ -62,7 +61,7 @@ public class ValidValuesConstraintTest {
}
@Test
- public void testSetValidValues() {
+ void testSetValidValues() {
ValidValuesConstraint testSubject = createStringTestSubject();
List<Object> validValues = new ArrayList<>();
validValues.add("test5");
@@ -79,35 +78,31 @@ public class ValidValuesConstraintTest {
}
@Test
- public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
ValidValuesConstraint testSubject = createStringTestSubject();
- Boolean validTypes = testSubject.validateValueType("string");
- assertTrue(validTypes);
+ assertTrue(testSubject.validateValueType("string"));
}
@Test
- public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+ void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
ValidValuesConstraint testSubject = createStringTestSubject();
- Boolean validTypes = testSubject.validateValueType("integer");
- assertFalse(validTypes);
+ assertFalse(testSubject.validateValueType("integer"));
}
@Test
- public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
+ void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
ValidValuesConstraint testSubject = createIntegerTestSubject();
- Boolean validTypes = testSubject.validateValueType("integer");
- assertTrue(validTypes);
+ assertTrue(testSubject.validateValueType("integer"));
}
@Test
- public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
+ void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
ValidValuesConstraint testSubject = createIntegerTestSubject();
- Boolean validTypes = testSubject.validateValueType("string");
- assertFalse(validTypes);
+ assertFalse(testSubject.validateValueType("string"));
}
@Test
- public void testChangeStringConstraintValueTypeToIntegerThrow() {
+ void testChangeStringConstraintValueTypeToIntegerThrow() {
String propertyType = "integer";
ValidValuesConstraint testSubject = createStringTestSubject();
Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
@@ -122,7 +117,7 @@ public class ValidValuesConstraintTest {
}
@Test
- public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
+ void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
ValidValuesConstraint testSubject = createIntegerTestSubject();
testSubject.changeConstraintValueTypeTo("string");