Support of get_property in property assignment
[sdc.git] / common-be / src / test / java / org / openecomp / sdc / be / datatypes / elements / ToscaGetFunctionDataDefinitionTest.java
1 /*
2  * -
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.datatypes.elements;
23
24 import static org.junit.jupiter.api.Assertions.*;
25
26 import com.google.gson.Gson;
27 import java.util.List;
28 import java.util.Map;
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;
34
35 class ToscaGetFunctionDataDefinitionTest {
36
37     @Test
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());
45     }
46
47     @Test
48     void generateGetInputSinglePropertyValueTest() {
49         //given
50         final String propertyName = "property";
51         final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
52         //when
53         final String actualValue = toscaGetFunction.generatePropertyValue();
54         //then
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);
60     }
61
62     @Test
63     void generateGetInputMultiplePropertyValueTest() {
64         //given
65         final var toscaGetFunction = createGetFunction(
66             ToscaGetFunctionType.GET_INPUT,
67             null,
68             List.of("property", "subProperty", "subSubProperty"),
69             null
70         );
71         //when
72         final String actualValue = toscaGetFunction.generatePropertyValue();
73         //then
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());
79     }
80
81     @Test
82     void generateValueForGetPropertyFromSelfTest() {
83         //given
84         final ToscaGetFunctionType toscaFunction = ToscaGetFunctionType.GET_PROPERTY;
85         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
86         //when
87         String actualValue = toscaGetFunction.generatePropertyValue();
88         //then
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);
97
98         //given a sub property path
99         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
100         //when
101         actualValue = toscaGetFunction.generatePropertyValue();
102         //then
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);
111     }
112
113     @Test
114     void generateValueForGetPropertyFromInstanceTest() {
115         //given
116         final ToscaGetFunctionType toscaFunction = ToscaGetFunctionType.GET_PROPERTY;
117         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
118         //when
119         String actualValue = toscaGetFunction.generatePropertyValue();
120         //then
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);
129
130         //given a sub property path
131         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
132         //when
133         actualValue = toscaGetFunction.generatePropertyValue();
134         //then
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);
143     }
144
145     @Test
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());
151     }
152
153     @Test
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());
158     }
159
160     @Test
161     void generateValuePropertySourceIsRequiredForGetPropertyTest() {
162         final var toscaGetFunction = createGetFunction(
163             ToscaGetFunctionType.GET_PROPERTY,
164             null,
165             List.of("property"),
166             null);
167         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
168         assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
169     }
170
171     @Test
172     void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
173         final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
174             ToscaGetFunctionType.GET_PROPERTY,
175             PropertySource.INSTANCE,
176             List.of("property"),
177             null);
178         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
179
180         assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
181     }
182
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;
192     }
193
194     private Map<?, ?> convertJsonStringToMap(final String actualValue) {
195         final Gson gson = new Gson();
196         return gson.fromJson(actualValue, Map.class);
197     }
198 }