Support for TOSCA functions for Service Import
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / ToscaFunctionYamlParsingHandlerTest.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.components.csar;
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.assertTrue;
28
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Optional;
33 import org.junit.jupiter.api.Test;
34 import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
35 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
36 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
37 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.ToscaStringParameter;
39 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
40 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
41
42 class ToscaFunctionYamlParsingHandlerTest {
43
44     final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler = new ToscaFunctionYamlParsingHandler();
45
46     @Test
47     void buildToscaFunctionBasedOnPropertyValue_NotAToscaFunctionTest() {
48         assertEquals(Optional.empty(), toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(null));
49     }
50
51     @Test
52     void buildToscaFunctionBasedOnPropertyValue_GetInputTest() {
53         final List<String> getInputParameters = List.of("input", "subProperty");
54         final Map<String, Object> getInput = Map.of(ToscaFunctionType.GET_INPUT.getName(), getInputParameters);
55         final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(getInput);
56         assertTrue(actualToscaFunctionOpt.isPresent());
57         final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
58         assertGetInput(actualToscaFunction, getInputParameters);
59     }
60
61     @Test
62     void buildToscaFunctionBasedOnPropertyValue_GetPropertyTest() {
63         final List<String> getPropertyValue = List.of(PropertySource.SELF.getName(), "aProperty", "aSubProperty");
64         final Map<String, Object> getProperty = Map.of(ToscaFunctionType.GET_PROPERTY.getName(), getPropertyValue);
65
66         final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(getProperty);
67         assertTrue(actualToscaFunctionOpt.isPresent());
68         final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
69         assertEquals(ToscaFunctionType.GET_PROPERTY, actualToscaFunction.getType());
70         assertTrue(actualToscaFunction instanceof ToscaGetFunctionDataDefinition);
71         final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualToscaFunction;
72         assertEquals(ToscaGetFunctionType.GET_PROPERTY, toscaGetFunction.getFunctionType());
73         assertEquals("aSubProperty", toscaGetFunction.getPropertyName());
74         assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
75         assertEquals(getPropertyValue.subList(1, getPropertyValue.size()), toscaGetFunction.getPropertyPathFromSource());
76         assertNull(toscaGetFunction.getPropertyUniqueId());
77         assertNull(toscaGetFunction.getSourceName());
78     }
79
80     @Test
81     void buildToscaFunctionBasedOnPropertyValue_GetAttributeTest() {
82         final List<String> getPropertyValue = List.of(PropertySource.INSTANCE.getName(), "anAttribute", "aSubAttribute");
83         final Map<String, Object> getProperty = Map.of(ToscaFunctionType.GET_ATTRIBUTE.getName(), getPropertyValue);
84
85         final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(getProperty);
86         assertTrue(actualToscaFunctionOpt.isPresent());
87         final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
88         assertEquals(ToscaFunctionType.GET_ATTRIBUTE, actualToscaFunction.getType());
89         assertTrue(actualToscaFunction instanceof ToscaGetFunctionDataDefinition);
90         final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualToscaFunction;
91         assertEquals(ToscaGetFunctionType.GET_ATTRIBUTE, toscaGetFunction.getFunctionType());
92         assertEquals("aSubAttribute", toscaGetFunction.getPropertyName());
93         assertEquals(PropertySource.INSTANCE, toscaGetFunction.getPropertySource());
94         assertEquals(getPropertyValue.subList(1, getPropertyValue.size()), toscaGetFunction.getPropertyPathFromSource());
95         assertEquals(getPropertyValue.get(0), toscaGetFunction.getSourceName());
96         assertNull(toscaGetFunction.getPropertyUniqueId());
97     }
98
99     @Test
100     void buildToscaFunctionBasedOnPropertyValue_ConcatTest() {
101         final List<Object> concatValue = List.of("string1", "-", Map.of(ToscaFunctionType.GET_INPUT.getName(), "inputName"));
102         final Map<String, Object> concatValueMap = Map.of(ToscaFunctionType.CONCAT.getName(), concatValue);
103
104         final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(concatValueMap);
105         assertTrue(actualToscaFunctionOpt.isPresent());
106         final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get();
107         assertEquals(ToscaFunctionType.CONCAT, actualToscaFunction.getType());
108         assertTrue(actualToscaFunction instanceof ToscaConcatFunction);
109         final ToscaConcatFunction toscaConcatFunction = (ToscaConcatFunction) actualToscaFunction;
110         assertEquals(3, toscaConcatFunction.getParameters().size());
111         assertTrue(toscaConcatFunction.getParameters().get(0) instanceof ToscaStringParameter);
112         final ToscaStringParameter parameter1 = (ToscaStringParameter) toscaConcatFunction.getParameters().get(0);
113         assertEquals("string1", parameter1.getValue());
114         assertTrue(toscaConcatFunction.getParameters().get(1) instanceof ToscaStringParameter);
115         final ToscaStringParameter parameter2 = (ToscaStringParameter) toscaConcatFunction.getParameters().get(1);
116         assertEquals("-", parameter2.getValue());
117         assertTrue(toscaConcatFunction.getParameters().get(2) instanceof ToscaGetFunctionDataDefinition);
118         final ToscaGetFunctionDataDefinition getFunction = (ToscaGetFunctionDataDefinition) toscaConcatFunction.getParameters().get(2);
119         assertGetInput(getFunction, List.of("inputName"));
120     }
121
122
123     @Test
124     void isPropertyValueToscaFunctionTest() {
125         assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(ToscaFunctionType.GET_INPUT.getName()));
126         assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(new HashMap<>()));
127         assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(
128                 Map.of(ToscaFunctionType.GET_ATTRIBUTE.getName(), "", ToscaFunctionType.GET_INPUT.getName(), "")
129             )
130         );
131         assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.GET_ATTRIBUTE.getName(), "")));
132         assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.GET_INPUT.getName(), "")));
133         assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.GET_PROPERTY.getName(), "")));
134         assertTrue(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.CONCAT.getName(), "")));
135         assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.YAML.getName(), "")));
136         assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.STRING.getName(), "")));
137     }
138
139     private static void assertGetInput(final ToscaFunction actualGetInputFunction, final List<String> expectedGetInputParameters) {
140         assertEquals(ToscaFunctionType.GET_INPUT, actualGetInputFunction.getType());
141         assertTrue(actualGetInputFunction instanceof ToscaGetFunctionDataDefinition);
142         final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualGetInputFunction;
143         assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
144         assertEquals(expectedGetInputParameters.get(expectedGetInputParameters.size() - 1), toscaGetFunction.getPropertyName());
145         assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
146         assertEquals(expectedGetInputParameters, toscaGetFunction.getPropertyPathFromSource());
147         assertNull(toscaGetFunction.getPropertyUniqueId());
148         assertNull(toscaGetFunction.getSourceName());
149     }
150 }