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.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import com.google.gson.Gson;
30 import java.util.List;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.params.ParameterizedTest;
36 import org.junit.jupiter.params.provider.EnumSource;
37 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
38 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
40 class ToscaGetFunctionDataDefinitionTest {
43 void isSubPropertyTest() {
44 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
45 assertFalse(toscaGetFunction.isSubProperty());
46 toscaGetFunction.setPropertyPathFromSource(List.of("property1"));
47 assertFalse(toscaGetFunction.isSubProperty());
48 toscaGetFunction.setPropertyPathFromSource(List.of("property1", "subProperty1"));
49 assertTrue(toscaGetFunction.isSubProperty());
53 void generateGetInputSinglePropertyValueTest() {
55 final String propertyName = "property";
56 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
58 final String actualValue = toscaGetFunction.generatePropertyValue();
60 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
61 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
62 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
63 assertTrue(value instanceof String);
64 assertEquals(value, propertyName);
68 void generateGetInputMultiplePropertyValueTest() {
70 final var toscaGetFunction = createGetFunction(
71 ToscaGetFunctionType.GET_INPUT,
73 List.of("property", "subProperty", "subSubProperty"),
77 final String actualValue = toscaGetFunction.generatePropertyValue();
79 final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
80 assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
81 final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
82 assertTrue(value instanceof List);
83 assertEquals(value, toscaGetFunction.getPropertyPathFromSource());
87 @EnumSource(value = ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
88 void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
90 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
92 String actualValue = toscaGetFunction.generatePropertyValue();
94 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
95 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
96 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
97 List<String> expectedGetPropertyValue = Stream.concat(
98 Stream.of(PropertySource.SELF.getName()),
99 toscaGetFunction.getPropertyPathFromSource().stream())
100 .collect(Collectors.toList());
101 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
103 //given a sub property path
104 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
106 actualValue = toscaGetFunction.generatePropertyValue();
108 getInputJsonAsMap = convertJsonStringToMap(actualValue);
109 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
110 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
111 expectedGetPropertyValue = Stream.concat(
112 Stream.of(PropertySource.SELF.getName()),
113 toscaGetFunction.getPropertyPathFromSource().stream())
114 .collect(Collectors.toList());
115 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
119 @EnumSource(value = ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
120 void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
122 final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
124 String actualValue = toscaGetFunction.generatePropertyValue();
126 Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
127 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
128 Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
129 List<String> expectedGetPropertyValue = Stream.concat(
130 Stream.of(toscaGetFunction.getSourceName()),
131 toscaGetFunction.getPropertyPathFromSource().stream())
132 .collect(Collectors.toList());
133 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
135 //given a sub property path
136 toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
138 actualValue = toscaGetFunction.generatePropertyValue();
140 getInputJsonAsMap = convertJsonStringToMap(actualValue);
141 assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
142 actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
143 expectedGetPropertyValue = Stream.concat(
144 Stream.of(toscaGetFunction.getSourceName()),
145 toscaGetFunction.getPropertyPathFromSource().stream())
146 .collect(Collectors.toList());
147 assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
151 void generateValueFunctionTypeIsRequiredTest() {
152 final var toscaGetFunction = createGetFunction(null, null, List.of("property"), null);
153 toscaGetFunction.setPropertyPathFromSource(List.of("property"));
154 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
155 assertEquals("functionType is required in order to generate the get function value", actualException.getMessage());
159 void generateValuePropertyPathIsRequiredTest() {
160 final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, null, null);
161 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
162 assertEquals("propertyPathFromSource is required in order to generate the get function value", actualException.getMessage());
166 void generateValuePropertySourceIsRequiredForGetPropertyTest() {
167 final var toscaGetFunction = createGetFunction(
168 ToscaGetFunctionType.GET_PROPERTY,
172 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
173 assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
177 void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
178 final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
179 ToscaGetFunctionType.GET_PROPERTY,
180 PropertySource.INSTANCE,
183 final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
185 assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
188 private ToscaGetFunctionDataDefinition createGetFunction(final ToscaGetFunctionType toscaGetFunctionType,
189 final PropertySource propertySource,
190 final List<String> propertyPath, String sourceName) {
191 final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
192 toscaGetFunction.setFunctionType(toscaGetFunctionType);
193 toscaGetFunction.setPropertySource(propertySource);
194 toscaGetFunction.setPropertyPathFromSource(propertyPath);
195 toscaGetFunction.setSourceName(sourceName);
196 return toscaGetFunction;
199 private Map<?, ?> convertJsonStringToMap(final String actualValue) {
200 final Gson gson = new Gson();
201 return gson.fromJson(actualValue, Map.class);