3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.datatypes.elements;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertThrows;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import com.google.gson.Gson;
31 import java.util.List;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.params.ParameterizedTest;
37 import org.junit.jupiter.params.provider.EnumSource;
38 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
41 class ToscaGetFunctionDataDefinitionTest {
44 void isSubPropertyTest() {
45 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
46 assertFalse(toscaGetFunction.isSubProperty());
47 toscaGetFunction.setPropertyPathFromSource(List.of("property1"));
48 assertFalse(toscaGetFunction.isSubProperty());
49 toscaGetFunction.setPropertyPathFromSource(List.of("property1", "subProperty1"));
50 assertTrue(toscaGetFunction.isSubProperty());
54 void generateGetInputSinglePropertyValueTest() {
56 final String propertyName = "property";
57 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
59 final String actualValue = toscaGetFunction.generatePropertyValue();
61 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
62 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
63 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
64 assertTrue(value instanceof String);
65 assertEquals(value, propertyName);
69 void generateGetInputMultiplePropertyValueTest() {
71 final var toscaGetFunction = createGetFunction(
72 ToscaGetFunctionType.GET_INPUT,
74 List.of("property", "subProperty", "subSubProperty"),
78 final String actualValue = toscaGetFunction.generatePropertyValue();
80 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
81 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
82 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
83 assertTrue(value instanceof List);
84 assertEquals(value, toscaGetFunction.getPropertyPathFromSource());
88 @EnumSource(value = ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
89 void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
91 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
93 String actualValue = toscaGetFunction.generatePropertyValue();
95 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
96 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
97 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
98 List<String> expectedGetPropertyValue = Stream.concat(
99 Stream.of(PropertySource.SELF.getName()),
100 toscaGetFunction.getPropertyPathFromSource().stream())
101 .collect(Collectors.toList());
102 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
104 //given a sub property path
105 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
107 actualValue = toscaGetFunction.generatePropertyValue();
109 getInputJsonAsMap = convertJsonStringToMap(actualValue);
110 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
111 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
112 expectedGetPropertyValue = Stream.concat(
113 Stream.of(PropertySource.SELF.getName()),
114 toscaGetFunction.getPropertyPathFromSource().stream())
115 .collect(Collectors.toList());
116 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
120 @EnumSource(value = ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
121 void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
123 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
125 String actualValue = toscaGetFunction.generatePropertyValue();
127 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
128 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
129 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
130 List<String> expectedGetPropertyValue = Stream.concat(
131 Stream.of(toscaGetFunction.getSourceName()),
132 toscaGetFunction.getPropertyPathFromSource().stream())
133 .collect(Collectors.toList());
134 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
136 //given a sub property path
137 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
139 actualValue = toscaGetFunction.generatePropertyValue();
141 getInputJsonAsMap = convertJsonStringToMap(actualValue);
142 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
143 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
144 expectedGetPropertyValue = Stream.concat(
145 Stream.of(toscaGetFunction.getSourceName()),
146 toscaGetFunction.getPropertyPathFromSource().stream())
147 .collect(Collectors.toList());
148 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
152 void generateValueFunctionTypeIsRequiredTest() {
153 final var toscaGetFunction = createGetFunction(null, null, List.of("property"), null);
154 toscaGetFunction.setPropertyPathFromSource(List.of("property"));
155 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
156 assertEquals("functionType is required in order to generate the get function value", actualException.getMessage());
160 void generateValuePropertyPathIsRequiredTest() {
161 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, null, null);
162 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
163 assertEquals("propertyPathFromSource is required in order to generate the get function value", actualException.getMessage());
167 void generateValuePropertySourceIsRequiredForGetPropertyTest() {
168 final var toscaGetFunction = createGetFunction(
169 ToscaGetFunctionType.GET_PROPERTY,
173 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
174 assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
178 void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
179 final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
180 ToscaGetFunctionType.GET_PROPERTY,
181 PropertySource.INSTANCE,
184 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
186 assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
191 final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
192 assertNull(toscaGetFunctionDataDefinition.getType());
193 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_INPUT);
194 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.getType());
195 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
196 assertEquals(ToscaFunctionType.GET_ATTRIBUTE, toscaGetFunctionDataDefinition.getType());
197 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
198 assertEquals(ToscaFunctionType.GET_PROPERTY, toscaGetFunctionDataDefinition.getType());
201 private ToscaGetFunctionDataDefinition createGetFunction(final ToscaGetFunctionType toscaGetFunctionType,
202 final PropertySource propertySource,
203 final List<String> propertyPath, String sourceName) {
204 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
205 toscaGetFunction.setFunctionType(toscaGetFunctionType);
206 toscaGetFunction.setPropertySource(propertySource);
207 toscaGetFunction.setPropertyPathFromSource(propertyPath);
208 toscaGetFunction.setSourceName(sourceName);
209 return toscaGetFunction;
212 private Map<?, ?> convertJsonStringToMap(final String actualValue) {
213 final Gson gson = new Gson();
214 return gson.fromJson(actualValue, Map.class);