3add149e23b9f41ae48e4c69aa69c0dc65b478f0
[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.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertThrows;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30 import com.google.gson.Gson;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
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;
41
42 class ToscaGetFunctionDataDefinitionTest {
43
44     @Test
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());
52     }
53
54     @Test
55     void generateGetInputSinglePropertyValueTest() {
56         //given
57         final String propertyName = "property";
58         final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
59         //when
60         final String actualValue = toscaGetFunction.generatePropertyValue();
61         //then
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 List);
66         assertEquals(((List<String>)value).get(0), propertyName);
67     }
68
69     @Test
70     void generateGetInputMultiplePropertyValueTest() {
71         //given
72         final var toscaGetFunction = createGetFunction(
73             ToscaGetFunctionType.GET_INPUT,
74             null,
75             List.of("property", "subProperty", "subSubProperty"),
76             null
77         );
78         //when
79         final String actualValue = toscaGetFunction.generatePropertyValue();
80         //then
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());
86     }
87
88     @ParameterizedTest
89     @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
90     void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
91         //given
92         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
93         //when
94         String actualValue = toscaGetFunction.generatePropertyValue();
95         //then
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);
104
105         //given a sub property path
106         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
107         //when
108         actualValue = toscaGetFunction.generatePropertyValue();
109         //then
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);
118     }
119
120     @ParameterizedTest
121     @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
122     void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
123         //given
124         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
125         //when
126         String actualValue = toscaGetFunction.generatePropertyValue();
127         //then
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);
136
137         //given a sub property path
138         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
139         //when
140         actualValue = toscaGetFunction.generatePropertyValue();
141         //then
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);
150     }
151
152     @Test
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());
158     }
159
160     @Test
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());
165     }
166
167     @Test
168     void generateValuePropertySourceIsRequiredForGetPropertyTest() {
169         final var toscaGetFunction = createGetFunction(
170             ToscaGetFunctionType.GET_PROPERTY,
171             null,
172             List.of("property"),
173             null);
174         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
175         assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
176     }
177
178     @Test
179     void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
180         final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
181             ToscaGetFunctionType.GET_PROPERTY,
182             PropertySource.INSTANCE,
183             List.of("property"),
184             null);
185         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
186
187         assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
188     }
189
190     @Test
191     void getTypeTest() {
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());
200     }
201
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;
212     }
213
214     private Map<?, ?> convertJsonStringToMap(final String actualValue) {
215         final Gson gson = new Gson();
216         return gson.fromJson(actualValue, Map.class);
217     }
218 }