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.List;
38 import org.junit.jupiter.api.Test;
39 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
40 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
41 import org.yaml.snakeyaml.Yaml;
43 class ToscaFunctionJsonDeserializerTest {
45 private static final Path TEST_RESOURCES_PATH = Path.of("src/test/resources/toscaFunctionJsonDeserializer");
48 void testGetInputToscaFunction() throws IOException {
50 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getInput.json"));
52 final ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
54 assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
55 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
56 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
57 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
58 assertEquals("e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming", toscaGetFunction.getPropertyUniqueId());
59 assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
60 assertEquals("instance_name", toscaGetFunction.getPropertyName());
61 assertEquals("ciResVFc26a0b30ec20", toscaGetFunction.getSourceName());
62 assertEquals("aee643c9-6c8e-4a24-af7a-a9aff5c072c0", toscaGetFunction.getSourceUniqueId());
63 assertEquals(List.of("nf_naming", "instance_name"), toscaGetFunction.getPropertyPathFromSource());
67 void testGetInputToscaFunctionLegacyConversion() throws IOException {
69 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getInputLegacy.json"));
71 final ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
73 assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
74 final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
75 assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
76 assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
80 void testNoFunctionTypeProvided() throws IOException {
82 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getFunctionMissingType.json"));
84 final ValueInstantiationException actualException =
85 assertThrows(ValueInstantiationException.class, () -> parseToscaFunction(toscaGetInputFunction));
86 assertTrue(actualException.getMessage().contains("Attribute type not provided"));
90 void testConcatToscaFunction() throws IOException {
92 final String toscaConcatFunction = Files.readString(TEST_RESOURCES_PATH.resolve("concatFunction.json"));
94 final ToscaFunction toscaFunction = parseToscaFunction(toscaConcatFunction);
96 assertTrue(toscaFunction instanceof ToscaConcatFunction);
97 final Object yamlObject = new Yaml().load(toscaFunction.getValue());
98 assertTrue(yamlObject instanceof Map);
99 final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
100 final Object concatFunctionObj = yamlMap.get(ToscaFunctionType.CONCAT.getName());
101 assertNotNull(concatFunctionObj);
102 assertTrue(concatFunctionObj instanceof List);
103 final List<Object> concatFunctionParameters = (List<Object>) concatFunctionObj;
104 assertEquals(3, concatFunctionParameters.size(), "Expecting three parameters");
105 assertTrue(concatFunctionParameters.get(0) instanceof Map);
106 final Map<String, Object> parameter1Map = (Map<String, Object>) concatFunctionParameters.get(0);
107 assertNotNull(parameter1Map.get(ToscaFunctionType.GET_INPUT.getName()));
108 assertTrue(parameter1Map.get(ToscaFunctionType.GET_INPUT.getName()) instanceof List);
109 List<String> getInputParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_INPUT.getName());
110 assertEquals(2, getInputParameters.size(), "Expecting two parameters in the get_input function");
111 assertEquals("nf_naming", getInputParameters.get(0));
112 assertEquals("instance_name", getInputParameters.get(1));
114 assertEquals("my string", concatFunctionParameters.get(1));
116 assertTrue(concatFunctionParameters.get(2) instanceof Map);
117 final Map<String, Object> parameter2Map = (Map<String, Object>) concatFunctionParameters.get(2);
118 assertNotNull(parameter2Map.get(ToscaFunctionType.CONCAT.getName()));
119 assertTrue(parameter2Map.get(ToscaFunctionType.CONCAT.getName()) instanceof List);
120 List<Object> concatParameters = (List<Object>) parameter2Map.get(ToscaFunctionType.CONCAT.getName());
121 assertEquals(3, concatParameters.size(), "Expecting two parameters in the sub concat function");
122 assertEquals("string1", concatParameters.get(0));
123 assertEquals("string2", concatParameters.get(1));
124 assertTrue(concatParameters.get(2) instanceof Map);
125 Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(2);
126 assertTrue(yamlFunctionValueMap.get("myList") instanceof List);
127 assertTrue(yamlFunctionValueMap.get("get_something") instanceof List);
128 assertTrue(yamlFunctionValueMap.get("string") instanceof String);
132 void testYamlFunction() throws IOException {
134 final String yamlFunction = Files.readString(TEST_RESOURCES_PATH.resolve("yamlFunction.json"));
136 final ToscaFunction toscaFunction = parseToscaFunction(yamlFunction);
138 assertTrue(toscaFunction instanceof CustomYamlFunction);
139 assertDoesNotThrow(() -> new Yaml().load(toscaFunction.getValue()));
143 void testCustomToscaFunction() throws IOException {
145 final String toscaCustomFunction = Files.readString(TEST_RESOURCES_PATH.resolve("customFunction.json"));
147 final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunction);
149 assertTrue(toscaFunction instanceof ToscaCustomFunction);
150 final Object yamlObject = new Yaml().load(toscaFunction.getValue());
151 assertTrue(yamlObject instanceof Map);
152 final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
153 final Object customFunctionObj = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName());
154 assertNotNull(customFunctionObj);
155 assertTrue(customFunctionObj instanceof List);
156 final List<Object> customFunctionParameters = (List<Object>) customFunctionObj;
157 assertEquals(3, customFunctionParameters.size(), "Expecting three parameters");
158 assertEquals("string1", customFunctionParameters.get(0));
160 assertTrue(customFunctionParameters.get(1) instanceof Map);
161 final Map<String, Object> parameter1Map = (Map<String, Object>) customFunctionParameters.get(1);
162 assertNotNull(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()));
163 assertTrue(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()) instanceof List);
164 List<String> getAttributeParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName());
165 assertEquals(2, getAttributeParameters.size(), "Expecting two parameters in the get_attribute function");
166 assertEquals("SELF", getAttributeParameters.get(0));
167 assertEquals("descriptor_id", getAttributeParameters.get(1));
169 assertTrue(customFunctionParameters.get(2) instanceof Map);
170 final Map<String, Object> parameter2Map = (Map<String, Object>) customFunctionParameters.get(2);
171 Object customFunctionObj2 = parameter2Map.get(parameter2Map.keySet().stream().iterator().next());
172 assertNotNull(customFunctionObj2);
173 assertTrue(customFunctionObj2 instanceof List);
174 List<Object> customParameters = (List<Object>) customFunctionObj2;
175 assertEquals(2, customParameters.size(), "Expecting two parameters in the sub custom function");
176 assertTrue(customParameters.get(0) instanceof Map);
177 final Map<String, Object> concatFunctionValueMap = (Map<String, Object>) customParameters.get(0);
178 assertNotNull(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()));
179 assertTrue(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()) instanceof List);
180 List<Object> concatParameters = (List<Object>) concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName());
181 assertEquals(2, concatParameters.size(), "Expecting two parameters in the sub concat function");
182 assertEquals("string2", concatParameters.get(0));
183 assertTrue(concatParameters.get(1) instanceof Map);
184 Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(1);
185 assertTrue(yamlFunctionValueMap.get("myList") instanceof List);
186 assertTrue(yamlFunctionValueMap.get("get_something") instanceof List);
187 assertTrue(yamlFunctionValueMap.get("string") instanceof String);
189 assertEquals("string3", customParameters.get(1));
192 private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException {
193 return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class);