5daeaa5aadb02ace5dc2d7ede812428623475628
[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.List;
32 import java.util.Map;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.params.ParameterizedTest;
37 import org.junit.jupiter.params.provider.EnumSource;
38 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
40
41 class ToscaGetFunctionDataDefinitionTest {
42
43     @Test
44     void isSubPropertyTest() {
45         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
46         assertFalse(toscaGetFunction.isSubProperty());
47         toscaGetFunction.setPropertyPathFromSource(List.of("property1"));
48         assertFalse(toscaGetFunction.isSubProperty());
49         toscaGetFunction.setPropertyPathFromSource(List.of("property1", "subProperty1"));
50         assertTrue(toscaGetFunction.isSubProperty());
51     }
52
53     @Test
54     void generateGetInputSinglePropertyValueTest() {
55         //given
56         final String propertyName = "property";
57         final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
58         //when
59         final String actualValue = toscaGetFunction.generatePropertyValue();
60         //then
61         final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
62         assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
63         final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
64         assertTrue(value instanceof String);
65         assertEquals(value, propertyName);
66     }
67
68     @Test
69     void generateGetInputMultiplePropertyValueTest() {
70         //given
71         final var toscaGetFunction = createGetFunction(
72             ToscaGetFunctionType.GET_INPUT,
73             null,
74             List.of("property", "subProperty", "subSubProperty"),
75             null
76         );
77         //when
78         final String actualValue = toscaGetFunction.generatePropertyValue();
79         //then
80         final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
81         assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
82         final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
83         assertTrue(value instanceof List);
84         assertEquals(value, toscaGetFunction.getPropertyPathFromSource());
85     }
86
87     @ParameterizedTest
88     @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
89     void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
90         //given
91         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
92         //when
93         String actualValue = toscaGetFunction.generatePropertyValue();
94         //then
95         Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
96         assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
97         Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
98         List<String> expectedGetPropertyValue = Stream.concat(
99                 Stream.of(PropertySource.SELF.getName()),
100                 toscaGetFunction.getPropertyPathFromSource().stream())
101             .collect(Collectors.toList());
102         assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
103
104         //given a sub property path
105         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
106         //when
107         actualValue = toscaGetFunction.generatePropertyValue();
108         //then
109         getInputJsonAsMap = convertJsonStringToMap(actualValue);
110         assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
111         actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
112         expectedGetPropertyValue = Stream.concat(
113                 Stream.of(PropertySource.SELF.getName()),
114                 toscaGetFunction.getPropertyPathFromSource().stream())
115             .collect(Collectors.toList());
116         assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
117     }
118
119     @ParameterizedTest
120     @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
121     void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
122         //given
123         final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
124         //when
125         String actualValue = toscaGetFunction.generatePropertyValue();
126         //then
127         Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
128         assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
129         Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
130         List<String> expectedGetPropertyValue = Stream.concat(
131                 Stream.of(toscaGetFunction.getSourceName()),
132                 toscaGetFunction.getPropertyPathFromSource().stream())
133             .collect(Collectors.toList());
134         assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
135
136         //given a sub property path
137         toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
138         //when
139         actualValue = toscaGetFunction.generatePropertyValue();
140         //then
141         getInputJsonAsMap = convertJsonStringToMap(actualValue);
142         assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
143         actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
144         expectedGetPropertyValue = Stream.concat(
145                 Stream.of(toscaGetFunction.getSourceName()),
146                 toscaGetFunction.getPropertyPathFromSource().stream())
147             .collect(Collectors.toList());
148         assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
149     }
150
151     @Test
152     void generateValueFunctionTypeIsRequiredTest() {
153         final var toscaGetFunction = createGetFunction(null, null, List.of("property"), null);
154         toscaGetFunction.setPropertyPathFromSource(List.of("property"));
155         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
156         assertEquals("functionType is required in order to generate the get function value", actualException.getMessage());
157     }
158
159     @Test
160     void generateValuePropertyPathIsRequiredTest() {
161         final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, null, null);
162         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
163         assertEquals("propertyPathFromSource is required in order to generate the get function value", actualException.getMessage());
164     }
165
166     @Test
167     void generateValuePropertySourceIsRequiredForGetPropertyTest() {
168         final var toscaGetFunction = createGetFunction(
169             ToscaGetFunctionType.GET_PROPERTY,
170             null,
171             List.of("property"),
172             null);
173         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
174         assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
175     }
176
177     @Test
178     void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
179         final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
180             ToscaGetFunctionType.GET_PROPERTY,
181             PropertySource.INSTANCE,
182             List.of("property"),
183             null);
184         final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
185
186         assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
187     }
188
189     @Test
190     void getTypeTest() {
191         final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
192         assertNull(toscaGetFunctionDataDefinition.getType());
193         toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_INPUT);
194         assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.getType());
195         toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
196         assertEquals(ToscaFunctionType.GET_ATTRIBUTE, toscaGetFunctionDataDefinition.getType());
197         toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
198         assertEquals(ToscaFunctionType.GET_PROPERTY, toscaGetFunctionDataDefinition.getType());
199     }
200
201     private ToscaGetFunctionDataDefinition createGetFunction(final ToscaGetFunctionType toscaGetFunctionType,
202                                                              final PropertySource propertySource,
203                                                              final List<String> propertyPath, String sourceName) {
204         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
205         toscaGetFunction.setFunctionType(toscaGetFunctionType);
206         toscaGetFunction.setPropertySource(propertySource);
207         toscaGetFunction.setPropertyPathFromSource(propertyPath);
208         toscaGetFunction.setSourceName(sourceName);
209         return toscaGetFunction;
210     }
211
212     private Map<?, ?> convertJsonStringToMap(final String actualValue) {
213         final Gson gson = new Gson();
214         return gson.fromJson(actualValue, Map.class);
215     }
216 }