summaryrefslogtreecommitdiffstats
path: root/common/onap-tosca-datatype
diff options
context:
space:
mode:
Diffstat (limited to 'common/onap-tosca-datatype')
-rw-r--r--common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java6
-rw-r--r--common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidatorTest.java60
2 files changed, 64 insertions, 2 deletions
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java
index d7e538e0a7..5e2e2e5ee7 100644
--- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidator.java
@@ -28,10 +28,12 @@ import org.apache.commons.lang3.StringUtils;
*/
public class ScalarUnitValidator {
- private static ScalarUnitValidator scalarUnitValidator = new ScalarUnitValidator();
+ private static final ScalarUnitValidator scalarUnitValidator = new ScalarUnitValidator();
/**
* Tosca Scalar Unit Types structure.
+ *
+ * At least positive integers, no single white space, 1 to 3 chars
*/
private final Pattern pattern = Pattern.compile("\\d+\\s*[a-zA-Z]{1,3}");
@@ -71,4 +73,4 @@ public class ScalarUnitValidator {
.anyMatch(scalarUnit ->
scalarUnit.name().equalsIgnoreCase(strValue) || strValue.endsWith(scalarUnit.name())));
}
-}
+} \ No newline at end of file
diff --git a/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidatorTest.java b/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidatorTest.java
new file mode 100644
index 0000000000..bee5f7ae2f
--- /dev/null
+++ b/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/ScalarUnitValidatorTest.java
@@ -0,0 +1,60 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2020 AT&T. 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.onap.sdc.tosca.datatypes.model;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+public class ScalarUnitValidatorTest {
+
+ @Test
+ public void shouldGetAnInstance() {
+ assertNotNull(ScalarUnitValidator.getInstance());
+ }
+
+ @Test
+ public void testScalarUnit() {
+ ScalarUnitValidator validator = ScalarUnitValidator.getInstance();
+ assertTrue(validator.isScalarUnit(null));
+ assertTrue(validator.isScalarUnit("1y"));
+ assertTrue(validator.isScalarUnit("2ye"));
+ assertTrue(validator.isScalarUnit("3yes"));
+
+ assertFalse(validator.isScalarUnit("no"));
+ assertFalse(validator.isScalarUnit("1nooo"));
+ }
+
+ @Test
+ public void testValueScalarUnit() {
+ ScalarUnitValidator validator = ScalarUnitValidator.getInstance();
+ assertTrue(validator.isValueScalarUnit("2B", TestEnum.class));
+
+ assertFalse(validator.isValueScalarUnit("1val", TestEnum.class));
+ assertFalse(validator.isValueScalarUnit("null", null));
+ assertFalse(validator.isValueScalarUnit(null, null));
+ }
+}
+
+enum TestEnum {
+ B, KB
+} \ No newline at end of file