Support TOSCA get_attribute function
[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.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;
28
29 import com.google.gson.Gson;
30 import java.util.List;
31 import java.util.Map;
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;
39
40 class ToscaGetFunctionDataDefinitionTest {
41
42     @Test
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());
50     }
51
52     @Test
53     void generateGetInputSinglePropertyValueTest() {
54         //given
55         final String propertyName = "property";
56         final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
57         //when
58         final String actualValue = toscaGetFunction.generatePropertyValue();
59         //then
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);
65     }
66
67     @Test
68     void generateGetInputMultiplePropertyValueTest() {
69         //given
70         final var toscaGetFunction = createGetFunction(
71             ToscaGetFunctionType.GET_INPUT,
72             null,
73             List.of("property", "subProperty", "subSubProperty"),
74             null
75         );
76         //when
77         final String actualValue = toscaGetFunction.generatePropertyValue();
78         //then
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());
84     }
85
86     @ParameterizedTest
87     @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
88     void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
89         //given
90         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
91         //when
92         String actualValue = toscaGetFunction.generatePropertyValue();
93         //then
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);
102
103         //given a sub property path
104         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
105         //when
106         actualValue = toscaGetFunction.generatePropertyValue();
107         //then
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);
116     }
117
118     @ParameterizedTest
119     @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
120     void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
121         //given
122         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
123         //when
124         String actualValue = toscaGetFunction.generatePropertyValue();
125         //then
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);
134
135         //given a sub property path
136         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
137         //when
138         actualValue = toscaGetFunction.generatePropertyValue();
139         //then
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);
148     }
149
150     @Test
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());
156     }
157
158     @Test
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());
163     }
164
165     @Test
166     void generateValuePropertySourceIsRequiredForGetPropertyTest() {
167         final var toscaGetFunction = createGetFunction(
168             ToscaGetFunctionType.GET_PROPERTY,
169             null,
170             List.of("property"),
171             null);
172         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
173         assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
174     }
175
176     @Test
177     void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
178         final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
179             ToscaGetFunctionType.GET_PROPERTY,
180             PropertySource.INSTANCE,
181             List.of("property"),
182             null);
183         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
184
185         assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
186     }
187
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;
197     }
198
199     private Map<?, ?> convertJsonStringToMap(final String actualValue) {
200         final Gson gson = new Gson();
201         return gson.fromJson(actualValue, Map.class);
202     }
203 }