From 35b5687d60558bac72c1e53ddd9044584964a3d8 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Tue, 1 Oct 2019 16:47:26 +0100 Subject: Fix for scalar-unit type value. Issue-ID: SDC-323 Change-Id: I7f19a7356e1cd34deca0a168b3cb707ef657b9cb Signed-off-by: aribeiro --- .../heat/datatypes/DefinedHeatParameterTypes.java | 41 +++++---- .../heat/datatypes/ToscaScalarUnitFrequency.java | 29 +++++++ .../sdc/heat/datatypes/ToscaScalarUnitSize.java | 29 +++++++ .../sdc/heat/datatypes/ToscaScalarUnitTime.java | 30 +++++++ .../sdc/heat/datatypes/ToscaScalarUnitTypes.java | 37 ++++++++ .../datatypes/model/ToscaScalarUnitTypesTest.java | 98 ++++++++++++++++++++++ 6 files changed, 243 insertions(+), 21 deletions(-) create mode 100644 openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitFrequency.java create mode 100644 openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitSize.java create mode 100644 openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTime.java create mode 100644 openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTypes.java create mode 100644 openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/ToscaScalarUnitTypesTest.java (limited to 'openecomp-be/lib/openecomp-heat-lib') diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/DefinedHeatParameterTypes.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/DefinedHeatParameterTypes.java index 600917201b..4205f0bee3 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/DefinedHeatParameterTypes.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/DefinedHeatParameterTypes.java @@ -24,10 +24,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; - +import lombok.AllArgsConstructor; +import lombok.Getter; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang3.ClassUtils; +import org.onap.sdc.tosca.datatypes.model.ScalarUnitValidator; +@AllArgsConstructor +@Getter public enum DefinedHeatParameterTypes { NUMBER("number"), STRING("string"), @@ -35,22 +39,19 @@ public enum DefinedHeatParameterTypes { JSON("json"), BOOLEAN("boolean"); + private static ScalarUnitValidator scalarUnitValidator = ScalarUnitValidator.getInstance(); private static Map stringToDefinedType; static { stringToDefinedType = new HashMap<>(); - for (DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) { + for (final DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) { stringToDefinedType.put(definedHeatParameterType.type, definedHeatParameterType); } } private String type; - DefinedHeatParameterTypes(String type) { - this.type = type; - } - - public static DefinedHeatParameterTypes findByHeatResource(String type) { + public static DefinedHeatParameterTypes findByHeatResource(final String type) { return stringToDefinedType.get(type); } @@ -61,12 +62,17 @@ public enum DefinedHeatParameterTypes { * @param parameterType the parameter type * @return the boolean */ - public static boolean isValueIsFromGivenType(Object value, String parameterType) { - DefinedHeatParameterTypes definedType = findByHeatResource(parameterType); + public static boolean isValueIsFromGivenType(final Object value, final String parameterType) { + final DefinedHeatParameterTypes definedType = findByHeatResource(parameterType); if (Objects.nonNull(definedType)) { switch (definedType) { case NUMBER: + if (scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitSize.class) || + scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitTime.class) || + scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitFrequency.class)) { + return isValueString(value); + } return NumberUtils.isNumber(String.valueOf(value)); case BOOLEAN: @@ -87,35 +93,28 @@ public enum DefinedHeatParameterTypes { return false; } - public static boolean isNovaServerEnvValueIsFromRightType(Object value) { + public static boolean isNovaServerEnvValueIsFromRightType(final Object value) { return isValueIsFromGivenType(value, COMMA_DELIMITED_LIST.getType()) || isValueIsFromGivenType(value, STRING.getType()); } - private static boolean isValueCommaDelimitedList(Object value) { + private static boolean isValueCommaDelimitedList(final Object value) { return value instanceof List || String.valueOf(value).contains(",") || isValueIsFromGivenType(value, DefinedHeatParameterTypes.STRING.type); } - private static boolean isValueString(Object value) { + private static boolean isValueString(final Object value) { return value instanceof String || ClassUtils.isPrimitiveOrWrapper(value.getClass()); } - private static boolean isValueJson(Object value) { + private static boolean isValueJson(final Object value) { return (value instanceof Map) || (value instanceof List); } - public static boolean isEmptyValueInEnv(Object value) { + public static boolean isEmptyValueInEnv(final Object value) { return Objects.isNull(value); } - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } } diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitFrequency.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitFrequency.java new file mode 100644 index 0000000000..8ec246555a --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitFrequency.java @@ -0,0 +1,29 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.heat.datatypes; + +/** + * This enum is responsible for defining properties that have scalar values measured in units per second. + */ +public enum ToscaScalarUnitFrequency { + + HZ, KHZ, MHZ, GHZ + +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitSize.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitSize.java new file mode 100644 index 0000000000..12f6a176a3 --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitSize.java @@ -0,0 +1,29 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.heat.datatypes; + +/** + * This enum is responsible for defining properties that have scalar values measured in size units. + */ +public enum ToscaScalarUnitSize { + + B, KB, KIB, MB, MIB, GB, GIB, TB, TIB + +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTime.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTime.java new file mode 100644 index 0000000000..0a41a63ed1 --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTime.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.heat.datatypes; + + +/** + * This enum is responsible for defining properties that have scalar values measured in size units. + */ +public enum ToscaScalarUnitTime { + + D, H, M, S, MS, US, NS + +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTypes.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTypes.java new file mode 100644 index 0000000000..05cc97c8dc --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/datatypes/ToscaScalarUnitTypes.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.heat.datatypes; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * This enum is responsible for defining all Tosca Scalar Unit types Supported. + */ +@Getter +@AllArgsConstructor +public enum ToscaScalarUnitTypes { + + SCALAR_UNIT_SIZE("scalar-unit.size"), + SCALAR_UNIT_TIME("scalar-unit.time"), + SCALAR_UNIT_FREQUENCY("scalar-unit.frequency"); + + private String type; +} diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/ToscaScalarUnitTypesTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/ToscaScalarUnitTypesTest.java new file mode 100644 index 0000000000..0b83e9b32b --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/ToscaScalarUnitTypesTest.java @@ -0,0 +1,98 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.heat.datatypes.model; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.sdc.tosca.datatypes.model.ScalarUnitValidator; +import org.openecomp.sdc.heat.datatypes.DefinedHeatParameterTypes; +import org.openecomp.sdc.heat.datatypes.ToscaScalarUnitFrequency; +import org.openecomp.sdc.heat.datatypes.ToscaScalarUnitSize; +import org.openecomp.sdc.heat.datatypes.ToscaScalarUnitTime; + +public class ToscaScalarUnitTypesTest { + + private String inputValue; + private static ScalarUnitValidator scalarUnitValidator = ScalarUnitValidator.getInstance(); + + @Test + public void shouldReturnTrueForScalarUnitSize() { + inputValue = "100 " + ToscaScalarUnitSize.GB.name(); + Assert.assertTrue(DefinedHeatParameterTypes.isValueIsFromGivenType(inputValue, + DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertTrue(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitSize.class)); + } + + @Test + public void shouldReturnTrueForScalarUnitSizeWithSpace() { + inputValue = "10 " + ToscaScalarUnitSize.GB.name(); + Assert.assertTrue(DefinedHeatParameterTypes.isValueIsFromGivenType(inputValue, + DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertTrue(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitSize.class)); + } + + @Test + public void shouldReturnTrueForScalarUnitSizeWithoutSpace() { + inputValue = "200" + ToscaScalarUnitSize.GB.name(); + Assert.assertTrue(DefinedHeatParameterTypes.isValueIsFromGivenType(inputValue, + DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertTrue(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitSize.class)); + } + + @Test + public void shouldReturnTrueForScalarUnitTime() { + inputValue = "5000 " + ToscaScalarUnitTime.S.name(); + Assert.assertTrue(DefinedHeatParameterTypes.isValueIsFromGivenType( + inputValue, DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertTrue(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitTime.class)); + } + + @Test + public void shouldReturnTrueForScalarUnitFrequency() { + inputValue = "60 " + ToscaScalarUnitFrequency.GHZ.name(); + Assert.assertTrue(DefinedHeatParameterTypes.isValueIsFromGivenType( + inputValue, DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertTrue(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitFrequency.class)); + } + + @Test + public void shouldReturnFalse() { + inputValue = "one hundred " + ToscaScalarUnitSize.GB.name(); + Assert.assertFalse(DefinedHeatParameterTypes.isValueIsFromGivenType( + inputValue, DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertFalse(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitSize.class)); + } + + @Test + public void shouldReturnFalseForMatcher() { + inputValue = "100 abc"; + Assert.assertFalse(DefinedHeatParameterTypes.isValueIsFromGivenType( + inputValue, DefinedHeatParameterTypes.NUMBER.getType())); + + Assert.assertFalse(scalarUnitValidator.isValueScalarUnit(inputValue, ToscaScalarUnitSize.class)); + } + +} -- cgit 1.2.3-korg