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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.datatypes.elements;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertThrows;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.util.ArrayList;
37 import java.util.List;
39 import org.junit.jupiter.api.Test;
40 import org.openecomp.sdc.be.config.Configuration;
41 import org.openecomp.sdc.be.config.ConfigurationManager;
42 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
43 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
44 import org.yaml.snakeyaml.Yaml;
46 class ToscaFunctionJsonDeserializerTest {
48 private static final Path TEST_RESOURCES_PATH = Path.of("src/test/resources/toscaFunctionJsonDeserializer");
51 void testGetInputToscaFunction() throws IOException {
53 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getInput.json"));
55 final ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
57 assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
58 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
59 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
60 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
61 assertEquals("e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming", toscaGetFunction.getPropertyUniqueId());
62 assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
63 assertEquals("instance_name", toscaGetFunction.getPropertyName());
64 assertEquals("ciResVFc26a0b30ec20", toscaGetFunction.getSourceName());
65 assertEquals("aee643c9-6c8e-4a24-af7a-a9aff5c072c0", toscaGetFunction.getSourceUniqueId());
66 assertEquals(List.of("nf_naming", "instance_name"), toscaGetFunction.getPropertyPathFromSource());
70 void testGetInputToscaFunctionLegacyConversion() throws IOException {
72 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getInputLegacy.json"));
74 final ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
76 assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
77 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
78 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
79 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
83 void testNoFunctionTypeProvided() throws IOException {
85 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getFunctionMissingType.json"));
87 final ValueInstantiationException actualException =
88 assertThrows(ValueInstantiationException.class, () -> parseToscaFunction(toscaGetInputFunction));
89 assertTrue(actualException.getMessage().contains("Attribute type not provided"));
93 void testConcatToscaFunction() throws IOException {
95 final String toscaConcatFunction = Files.readString(TEST_RESOURCES_PATH.resolve("concatFunction.json"));
97 final ToscaFunction toscaFunction = parseToscaFunction(toscaConcatFunction);
99 assertTrue(toscaFunction instanceof ToscaConcatFunction);
100 final Object yamlObject = new Yaml().load(toscaFunction.getValue());
101 assertTrue(yamlObject instanceof Map);
102 final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
103 final Object concatFunctionObj = yamlMap.get(ToscaFunctionType.CONCAT.getName());
104 assertNotNull(concatFunctionObj);
105 assertTrue(concatFunctionObj instanceof List);
106 final List<Object> concatFunctionParameters = (List<Object>) concatFunctionObj;
107 assertEquals(3, concatFunctionParameters.size(), "Expecting three parameters");
108 assertTrue(concatFunctionParameters.get(0) instanceof Map);
109 final Map<String, Object> parameter1Map = (Map<String, Object>) concatFunctionParameters.get(0);
110 assertNotNull(parameter1Map.get(ToscaFunctionType.GET_INPUT.getName()));
111 assertTrue(parameter1Map.get(ToscaFunctionType.GET_INPUT.getName()) instanceof List);
112 List<String> getInputParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_INPUT.getName());
113 assertEquals(2, getInputParameters.size(), "Expecting two parameters in the get_input function");
114 assertEquals("nf_naming", getInputParameters.get(0));
115 assertEquals("instance_name", getInputParameters.get(1));
117 assertEquals("my string", concatFunctionParameters.get(1));
119 assertTrue(concatFunctionParameters.get(2) instanceof Map);
120 final Map<String, Object> parameter2Map = (Map<String, Object>) concatFunctionParameters.get(2);
121 assertNotNull(parameter2Map.get(ToscaFunctionType.CONCAT.getName()));
122 assertTrue(parameter2Map.get(ToscaFunctionType.CONCAT.getName()) instanceof List);
123 List<Object> concatParameters = (List<Object>) parameter2Map.get(ToscaFunctionType.CONCAT.getName());
124 assertEquals(3, concatParameters.size(), "Expecting two parameters in the sub concat function");
125 assertEquals("string1", concatParameters.get(0));
126 assertEquals("string2", concatParameters.get(1));
127 assertTrue(concatParameters.get(2) instanceof Map);
128 Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(2);
129 assertTrue(yamlFunctionValueMap.get("myList") instanceof List);
130 assertTrue(yamlFunctionValueMap.get("get_something") instanceof List);
131 assertTrue(yamlFunctionValueMap.get("string") instanceof String);
135 void testYamlFunction() throws IOException {
137 final String yamlFunction = Files.readString(TEST_RESOURCES_PATH.resolve("yamlFunction.json"));
139 final ToscaFunction toscaFunction = parseToscaFunction(yamlFunction);
141 assertTrue(toscaFunction instanceof CustomYamlFunction);
142 assertDoesNotThrow(() -> new Yaml().load(toscaFunction.getValue()));
146 void testCustomToscaFunction() throws IOException {
148 setDefaultCustomToscaFunctionOnConfiguration();
149 final String toscaCustomFunction = Files.readString(TEST_RESOURCES_PATH.resolve("customFunction.json"));
151 final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunction);
153 assertTrue(toscaFunction instanceof ToscaCustomFunction);
154 final Object yamlObject = new Yaml().load(toscaFunction.getValue());
155 assertTrue(yamlObject instanceof Map);
156 final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
157 final Object customFunctionObj = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName());
158 assertNotNull(customFunctionObj);
159 assertTrue(customFunctionObj instanceof List);
160 final List<Object> customFunctionParameters = (List<Object>) customFunctionObj;
161 assertEquals(3, customFunctionParameters.size(), "Expecting three parameters");
162 assertEquals("string1", customFunctionParameters.get(0));
164 assertTrue(customFunctionParameters.get(1) instanceof Map);
165 final Map<String, Object> parameter1Map = (Map<String, Object>) customFunctionParameters.get(1);
166 assertNotNull(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()));
167 assertTrue(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()) instanceof List);
168 List<String> getAttributeParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName());
169 assertEquals(2, getAttributeParameters.size(), "Expecting two parameters in the get_attribute function");
170 assertEquals("SELF", getAttributeParameters.get(0));
171 assertEquals("descriptor_id", getAttributeParameters.get(1));
173 assertTrue(customFunctionParameters.get(2) instanceof Map);
174 final Map<String, Object> parameter2Map = (Map<String, Object>) customFunctionParameters.get(2);
175 Object customFunctionObj2 = parameter2Map.get(parameter2Map.keySet().stream().iterator().next());
176 assertNotNull(customFunctionObj2);
177 assertTrue(customFunctionObj2 instanceof List);
178 List<Object> customParameters = (List<Object>) customFunctionObj2;
179 assertEquals(2, customParameters.size(), "Expecting two parameters in the sub custom function");
180 assertTrue(customParameters.get(0) instanceof Map);
181 final Map<String, Object> concatFunctionValueMap = (Map<String, Object>) customParameters.get(0);
182 assertNotNull(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()));
183 assertTrue(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()) instanceof List);
184 List<Object> concatParameters = (List<Object>) concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName());
185 assertEquals(2, concatParameters.size(), "Expecting two parameters in the sub concat function");
186 assertEquals("string2", concatParameters.get(0));
187 assertTrue(concatParameters.get(1) instanceof Map);
188 Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(1);
189 assertTrue(yamlFunctionValueMap.get("myList") instanceof List);
190 assertTrue(yamlFunctionValueMap.get("get_something") instanceof List);
191 assertTrue(yamlFunctionValueMap.get("string") instanceof String);
193 assertEquals("string3", customParameters.get(1));
197 void testCustomToscaFunctionGetInputType() throws IOException {
199 setDefaultCustomToscaFunctionOnConfiguration();
200 final String toscaCustomFunctionFile = Files.readString(TEST_RESOURCES_PATH.resolve("customFunctionGetInputType.json"));
202 final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunctionFile);
204 assertTrue(toscaFunction instanceof ToscaCustomFunction);
205 ToscaCustomFunction toscaCustomFunction = (ToscaCustomFunction) toscaFunction;
206 final Object yamlObject = new Yaml().load(toscaFunction.getValue());
207 assertTrue(yamlObject instanceof Map);
208 final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
209 assertEquals(1, yamlMap.size());
210 final Object customFunctionGetInputValue = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName());
211 assertTrue(customFunctionGetInputValue instanceof ArrayList);
212 ArrayList<Object> customFunctionGetInputValueList = (ArrayList<Object>) customFunctionGetInputValue;
213 assertEquals(1, customFunctionGetInputValueList.size());
214 assertEquals(1, customFunctionGetInputValueList.size());
215 assertEquals("controller_actor", customFunctionGetInputValueList.get(0));
217 List<ToscaFunctionParameter> parameters = toscaCustomFunction.getParameters();
218 assertEquals(1, parameters.size());
219 ToscaFunctionParameter paramFunction = toscaCustomFunction.getParameters().get(0);
220 assertTrue(paramFunction instanceof ToscaGetFunctionDataDefinition);
222 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) paramFunction;
223 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
224 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
225 assertEquals("dd0ec4d2-7e74-4d92-af2f-89c7436baa63.controller_actor", toscaGetFunction.getPropertyUniqueId());
226 assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
227 assertEquals("controller_actor", toscaGetFunction.getPropertyName());
228 assertEquals("testService", toscaGetFunction.getSourceName());
229 assertEquals("dd0ec4d2-7e74-4d92-af2f-89c7436baa63", toscaGetFunction.getSourceUniqueId());
230 assertEquals(List.of("controller_actor"), toscaGetFunction.getPropertyPathFromSource());
233 private void setDefaultCustomToscaFunctionOnConfiguration() {
234 final var configurationManager = new ConfigurationManager();
235 final var configuration = new Configuration();
236 List<Configuration.CustomToscaFunction> defaultCustomToscaFunctions = new ArrayList<>();
237 Configuration.CustomToscaFunction defaultCustomType = new Configuration.CustomToscaFunction();
238 defaultCustomType.setName("custom_function_get_input_type");
239 defaultCustomType.setType("get_input");
240 defaultCustomToscaFunctions.add(defaultCustomType);
241 configuration.setDefaultCustomToscaFunctions(defaultCustomToscaFunctions);
242 configurationManager.setConfiguration(configuration);
245 private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException {
246 return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class);