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.*;
26 import com.google.gson.Gson;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 import org.junit.jupiter.api.Test;
32 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
35 class ToscaGetFunctionDataDefinitionTest {
38 void isSubPropertyTest() {
39 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
40 assertFalse(toscaGetFunction.isSubProperty());
41 toscaGetFunction.setPropertyPathFromSource(List.of("property1"));
42 assertFalse(toscaGetFunction.isSubProperty());
43 toscaGetFunction.setPropertyPathFromSource(List.of("property1", "subProperty1"));
44 assertTrue(toscaGetFunction.isSubProperty());
48 void generateGetInputSinglePropertyValueTest() {
50 final String propertyName = "property";
51 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
53 final String actualValue = toscaGetFunction.generatePropertyValue();
55 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
56 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
57 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
58 assertTrue(value instanceof String);
59 assertEquals(value, propertyName);
63 void generateGetInputMultiplePropertyValueTest() {
65 final var toscaGetFunction = createGetFunction(
66 ToscaGetFunctionType.GET_INPUT,
68 List.of("property", "subProperty", "subSubProperty"),
72 final String actualValue = toscaGetFunction.generatePropertyValue();
74 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
75 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
76 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
77 assertTrue(value instanceof List);
78 assertEquals(value, toscaGetFunction.getPropertyPathFromSource());
82 void generateValueForGetPropertyFromSelfTest() {
84 final ToscaGetFunctionType toscaFunction = ToscaGetFunctionType.GET_PROPERTY;
85 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
87 String actualValue = toscaGetFunction.generatePropertyValue();
89 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
90 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
91 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
92 List<String> expectedGetPropertyValue = Stream.concat(
93 Stream.of(PropertySource.SELF.getName()),
94 toscaGetFunction.getPropertyPathFromSource().stream())
95 .collect(Collectors.toList());
96 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
98 //given a sub property path
99 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
101 actualValue = toscaGetFunction.generatePropertyValue();
103 getInputJsonAsMap = convertJsonStringToMap(actualValue);
104 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
105 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
106 expectedGetPropertyValue = Stream.concat(
107 Stream.of(PropertySource.SELF.getName()),
108 toscaGetFunction.getPropertyPathFromSource().stream())
109 .collect(Collectors.toList());
110 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
114 void generateValueForGetPropertyFromInstanceTest() {
116 final ToscaGetFunctionType toscaFunction = ToscaGetFunctionType.GET_PROPERTY;
117 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
119 String actualValue = toscaGetFunction.generatePropertyValue();
121 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
122 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
123 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
124 List<String> expectedGetPropertyValue = Stream.concat(
125 Stream.of(toscaGetFunction.getSourceName()),
126 toscaGetFunction.getPropertyPathFromSource().stream())
127 .collect(Collectors.toList());
128 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
130 //given a sub property path
131 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
133 actualValue = toscaGetFunction.generatePropertyValue();
135 getInputJsonAsMap = convertJsonStringToMap(actualValue);
136 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
137 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
138 expectedGetPropertyValue = Stream.concat(
139 Stream.of(toscaGetFunction.getSourceName()),
140 toscaGetFunction.getPropertyPathFromSource().stream())
141 .collect(Collectors.toList());
142 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
146 void generateValueFunctionTypeIsRequiredTest() {
147 final var toscaGetFunction = createGetFunction(null, null, List.of("property"), null);
148 toscaGetFunction.setPropertyPathFromSource(List.of("property"));
149 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
150 assertEquals("functionType is required in order to generate the get function value", actualException.getMessage());
154 void generateValuePropertyPathIsRequiredTest() {
155 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, null, null);
156 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
157 assertEquals("propertyPathFromSource is required in order to generate the get function value", actualException.getMessage());
161 void generateValuePropertySourceIsRequiredForGetPropertyTest() {
162 final var toscaGetFunction = createGetFunction(
163 ToscaGetFunctionType.GET_PROPERTY,
167 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
168 assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
172 void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
173 final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
174 ToscaGetFunctionType.GET_PROPERTY,
175 PropertySource.INSTANCE,
178 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
180 assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
183 private ToscaGetFunctionDataDefinition createGetFunction(final ToscaGetFunctionType toscaGetFunctionType,
184 final PropertySource propertySource,
185 final List<String> propertyPath, String sourceName) {
186 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
187 toscaGetFunction.setFunctionType(toscaGetFunctionType);
188 toscaGetFunction.setPropertySource(propertySource);
189 toscaGetFunction.setPropertyPathFromSource(propertyPath);
190 toscaGetFunction.setSourceName(sourceName);
191 return toscaGetFunction;
194 private Map<?, ?> convertJsonStringToMap(final String actualValue) {
195 final Gson gson = new Gson();
196 return gson.fromJson(actualValue, Map.class);