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.ArrayList;
32 import java.util.List;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.EnumSource;
39 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
40 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
42 class ToscaGetFunctionDataDefinitionTest {
45 void isSubPropertyTest() {
46 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
47 assertFalse(toscaGetFunction.isSubProperty());
48 toscaGetFunction.setPropertyPathFromSource(List.of("property1"));
49 assertFalse(toscaGetFunction.isSubProperty());
50 toscaGetFunction.setPropertyPathFromSource(List.of("property1", "subProperty1"));
51 assertTrue(toscaGetFunction.isSubProperty());
55 void generateGetInputSinglePropertyValueTest() {
57 final String propertyName = "property";
58 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
60 final String actualValue = toscaGetFunction.generatePropertyValue();
62 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
63 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
64 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
65 assertTrue(value instanceof String);
66 assertEquals(value, propertyName);
70 void generateGetInputMultiplePropertyValueTest() {
72 final var toscaGetFunction = createGetFunction(
73 ToscaGetFunctionType.GET_INPUT,
75 List.of("property", "subProperty", "subSubProperty"),
79 final String actualValue = toscaGetFunction.generatePropertyValue();
81 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
82 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
83 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
84 assertTrue(value instanceof List);
85 assertEquals(value, toscaGetFunction.getPropertyPathFromSource());
89 @EnumSource(value = ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
90 void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
92 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
94 String actualValue = toscaGetFunction.generatePropertyValue();
96 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
97 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
98 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
99 List<String> expectedGetPropertyValue = Stream.concat(
100 Stream.of(PropertySource.SELF.getName()),
101 toscaGetFunction.getPropertyPathFromSource().stream())
102 .collect(Collectors.toList());
103 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
105 //given a sub property path
106 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
108 actualValue = toscaGetFunction.generatePropertyValue();
110 getInputJsonAsMap = convertJsonStringToMap(actualValue);
111 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
112 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
113 expectedGetPropertyValue = Stream.concat(
114 Stream.of(PropertySource.SELF.getName()),
115 toscaGetFunction.getPropertyPathFromSource().stream())
116 .collect(Collectors.toList());
117 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
121 @EnumSource(value = ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
122 void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
124 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
126 String actualValue = toscaGetFunction.generatePropertyValue();
128 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
129 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
130 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
131 List<String> expectedGetPropertyValue = Stream.concat(
132 Stream.of(toscaGetFunction.getSourceName()),
133 toscaGetFunction.getPropertyPathFromSource().stream())
134 .collect(Collectors.toList());
135 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
137 //given a sub property path
138 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
140 actualValue = toscaGetFunction.generatePropertyValue();
142 getInputJsonAsMap = convertJsonStringToMap(actualValue);
143 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
144 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
145 expectedGetPropertyValue = Stream.concat(
146 Stream.of(toscaGetFunction.getSourceName()),
147 toscaGetFunction.getPropertyPathFromSource().stream())
148 .collect(Collectors.toList());
149 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
153 void generateValueFunctionTypeIsRequiredTest() {
154 final var toscaGetFunction = createGetFunction(null, null, List.of("property"), null);
155 toscaGetFunction.setPropertyPathFromSource(List.of("property"));
156 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
157 assertEquals("functionType is required in order to generate the get function value", actualException.getMessage());
161 void generateValuePropertyPathIsRequiredTest() {
162 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, null, null);
163 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
164 assertEquals("propertyPathFromSource is required in order to generate the get function value", actualException.getMessage());
168 void generateValuePropertySourceIsRequiredForGetPropertyTest() {
169 final var toscaGetFunction = createGetFunction(
170 ToscaGetFunctionType.GET_PROPERTY,
174 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
175 assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
179 void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
180 final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
181 ToscaGetFunctionType.GET_PROPERTY,
182 PropertySource.INSTANCE,
185 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
187 assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
192 final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
193 assertNull(toscaGetFunctionDataDefinition.getType());
194 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_INPUT);
195 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.getType());
196 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
197 assertEquals(ToscaFunctionType.GET_ATTRIBUTE, toscaGetFunctionDataDefinition.getType());
198 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
199 assertEquals(ToscaFunctionType.GET_PROPERTY, toscaGetFunctionDataDefinition.getType());
202 private ToscaGetFunctionDataDefinition createGetFunction(final ToscaGetFunctionType toscaGetFunctionType,
203 final PropertySource propertySource,
204 final List<String> propertyPath, String sourceName) {
205 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
206 toscaGetFunction.setFunctionType(toscaGetFunctionType);
207 toscaGetFunction.setPropertySource(propertySource);
208 toscaGetFunction.setPropertyPathFromSource(propertyPath);
209 toscaGetFunction.setSourceName(sourceName);
210 toscaGetFunction.setToscaIndexList(new ArrayList<>());
211 return toscaGetFunction;
214 private Map<?, ?> convertJsonStringToMap(final String actualValue) {
215 final Gson gson = new Gson();
216 return gson.fromJson(actualValue, Map.class);