aboutsummaryrefslogtreecommitdiffstats
path: root/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java
blob: 5128c63668a59d651def2834003d2b3f6976f86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * -
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix Foundation.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.datatypes.elements;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.openecomp.sdc.be.config.Configuration;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.datatypes.enums.PropertySource;
import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
import org.yaml.snakeyaml.Yaml;

class ToscaFunctionJsonDeserializerTest {

    private static final Path TEST_RESOURCES_PATH = Path.of("src/test/resources/toscaFunctionJsonDeserializer");

    @Test
    void testGetInputToscaFunction() throws IOException {
        //given
        final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getInput.json"));
        //when
        final ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
        //then
        assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
        final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
        assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
        assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
        assertEquals("e57525d7-2115-4934-9ba4-9cebfa22bad2.nf_naming", toscaGetFunction.getPropertyUniqueId());
        assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
        assertEquals("instance_name", toscaGetFunction.getPropertyName());
        assertEquals("ciResVFc26a0b30ec20", toscaGetFunction.getSourceName());
        assertEquals("aee643c9-6c8e-4a24-af7a-a9aff5c072c0", toscaGetFunction.getSourceUniqueId());
        assertEquals(List.of("nf_naming", "instance_name"), toscaGetFunction.getPropertyPathFromSource());
    }

    @Test
    void testGetInputToscaFunctionLegacyConversion() throws IOException {
        //given
        final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getInputLegacy.json"));
        //when
        final ToscaFunction toscaFunction = parseToscaFunction(toscaGetInputFunction);
        //then
        assertTrue(toscaFunction instanceof ToscaGetFunctionDataDefinition);
        final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
        assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
        assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
    }

    @Test
    void testNoFunctionTypeProvided() throws IOException {
        //given
        final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("getFunctionMissingType.json"));
        //when/then
        final ValueInstantiationException actualException =
            assertThrows(ValueInstantiationException.class, () -> parseToscaFunction(toscaGetInputFunction));
        assertTrue(actualException.getMessage().contains("Attribute type not provided"));
    }

    @Test
    void testConcatToscaFunction() throws IOException {
        //given
        final String toscaConcatFunction = Files.readString(TEST_RESOURCES_PATH.resolve("concatFunction.json"));
        //when
        final ToscaFunction toscaFunction = parseToscaFunction(toscaConcatFunction);
        //then
        assertTrue(toscaFunction instanceof ToscaConcatFunction);
        final Object yamlObject = new Yaml().load(toscaFunction.getValue());
        assertTrue(yamlObject instanceof Map);
        final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
        final Object concatFunctionObj = yamlMap.get(ToscaFunctionType.CONCAT.getName());
        assertNotNull(concatFunctionObj);
        assertTrue(concatFunctionObj instanceof List);
        final List<Object> concatFunctionParameters = (List<Object>) concatFunctionObj;
        assertEquals(3, concatFunctionParameters.size(), "Expecting three parameters");
        assertTrue(concatFunctionParameters.get(0) instanceof Map);
        final Map<String, Object> parameter1Map = (Map<String, Object>) concatFunctionParameters.get(0);
        assertNotNull(parameter1Map.get(ToscaFunctionType.GET_INPUT.getName()));
        assertTrue(parameter1Map.get(ToscaFunctionType.GET_INPUT.getName()) instanceof List);
        List<String> getInputParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_INPUT.getName());
        assertEquals(2, getInputParameters.size(), "Expecting two parameters in the get_input function");
        assertEquals("nf_naming", getInputParameters.get(0));
        assertEquals("instance_name", getInputParameters.get(1));

        assertEquals("my string", concatFunctionParameters.get(1));

        assertTrue(concatFunctionParameters.get(2) instanceof Map);
        final Map<String, Object> parameter2Map = (Map<String, Object>) concatFunctionParameters.get(2);
        assertNotNull(parameter2Map.get(ToscaFunctionType.CONCAT.getName()));
        assertTrue(parameter2Map.get(ToscaFunctionType.CONCAT.getName()) instanceof List);
        List<Object> concatParameters = (List<Object>) parameter2Map.get(ToscaFunctionType.CONCAT.getName());
        assertEquals(3, concatParameters.size(), "Expecting two parameters in the sub concat function");
        assertEquals("string1", concatParameters.get(0));
        assertEquals("string2", concatParameters.get(1));
        assertTrue(concatParameters.get(2) instanceof Map);
        Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(2);
        assertTrue(yamlFunctionValueMap.get("myList") instanceof List);
        assertTrue(yamlFunctionValueMap.get("get_something") instanceof List);
        assertTrue(yamlFunctionValueMap.get("string") instanceof String);
    }

    @Test
    void testYamlFunction() throws IOException {
        //given
        final String yamlFunction = Files.readString(TEST_RESOURCES_PATH.resolve("yamlFunction.json"));
        //when
        final ToscaFunction toscaFunction = parseToscaFunction(yamlFunction);
        //then
        assertTrue(toscaFunction instanceof CustomYamlFunction);
        assertDoesNotThrow(() -> new Yaml().load(toscaFunction.getValue()));
    }

    @Test
    void testCustomToscaFunction() throws IOException {
        //given
        setDefaultCustomToscaFunctionOnConfiguration();
        final String toscaCustomFunction = Files.readString(TEST_RESOURCES_PATH.resolve("customFunction.json"));
        //when
        final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunction);
        //then
        assertTrue(toscaFunction instanceof ToscaCustomFunction);
        final Object yamlObject = new Yaml().load(toscaFunction.getValue());
        assertTrue(yamlObject instanceof Map);
        final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
        final Object customFunctionObj = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName());
        assertNotNull(customFunctionObj);
        assertTrue(customFunctionObj instanceof List);
        final List<Object> customFunctionParameters = (List<Object>) customFunctionObj;
        assertEquals(3, customFunctionParameters.size(), "Expecting three parameters");
        assertEquals("string1", customFunctionParameters.get(0));

        assertTrue(customFunctionParameters.get(1) instanceof Map);
        final Map<String, Object> parameter1Map = (Map<String, Object>) customFunctionParameters.get(1);
        assertNotNull(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()));
        assertTrue(parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName()) instanceof List);
        List<String> getAttributeParameters = (List<String>) parameter1Map.get(ToscaFunctionType.GET_ATTRIBUTE.getName());
        assertEquals(2, getAttributeParameters.size(), "Expecting two parameters in the get_attribute function");
        assertEquals("SELF", getAttributeParameters.get(0));
        assertEquals("descriptor_id", getAttributeParameters.get(1));

        assertTrue(customFunctionParameters.get(2) instanceof Map);
        final Map<String, Object> parameter2Map = (Map<String, Object>) customFunctionParameters.get(2);
        Object customFunctionObj2 = parameter2Map.get(parameter2Map.keySet().stream().iterator().next());
        assertNotNull(customFunctionObj2);
        assertTrue(customFunctionObj2 instanceof List);
        List<Object> customParameters = (List<Object>) customFunctionObj2;
        assertEquals(2, customParameters.size(), "Expecting two parameters in the sub custom function");
        assertTrue(customParameters.get(0) instanceof Map);
        final Map<String, Object> concatFunctionValueMap = (Map<String, Object>) customParameters.get(0);
        assertNotNull(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()));
        assertTrue(concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName()) instanceof List);
        List<Object> concatParameters = (List<Object>) concatFunctionValueMap.get(ToscaFunctionType.CONCAT.getName());
        assertEquals(2, concatParameters.size(), "Expecting two parameters in the sub concat function");
        assertEquals("string2", concatParameters.get(0));
        assertTrue(concatParameters.get(1) instanceof Map);
        Map<String, Object> yamlFunctionValueMap = (Map<String, Object>) concatParameters.get(1);
        assertTrue(yamlFunctionValueMap.get("myList") instanceof List);
        assertTrue(yamlFunctionValueMap.get("get_something") instanceof List);
        assertTrue(yamlFunctionValueMap.get("string") instanceof String);

        assertEquals("string3", customParameters.get(1));
    }

    @Test
    void testCustomToscaFunctionGetInputType() throws IOException {
        //given
        setDefaultCustomToscaFunctionOnConfiguration();
        final String toscaCustomFunctionFile = Files.readString(TEST_RESOURCES_PATH.resolve("customFunctionGetInputType.json"));
        //when
        final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunctionFile);
        //then
        assertTrue(toscaFunction instanceof ToscaCustomFunction);
        ToscaCustomFunction toscaCustomFunction = (ToscaCustomFunction) toscaFunction;
        final Object yamlObject = new Yaml().load(toscaFunction.getValue());
        assertTrue(yamlObject instanceof Map);
        final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
        assertEquals(1, yamlMap.size());
        final Object customFunctionGetInputValue = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName());
        assertTrue(customFunctionGetInputValue instanceof ArrayList);
        ArrayList<Object> customFunctionGetInputValueList = (ArrayList<Object>) customFunctionGetInputValue;
        assertEquals(1, customFunctionGetInputValueList.size());
        assertEquals(1, customFunctionGetInputValueList.size());
        assertEquals("controller_actor", customFunctionGetInputValueList.get(0));

        List<ToscaFunctionParameter> parameters = toscaCustomFunction.getParameters();
        assertEquals(1, parameters.size());
        ToscaFunctionParameter paramFunction = toscaCustomFunction.getParameters().get(0);
        assertTrue(paramFunction instanceof ToscaGetFunctionDataDefinition);

        final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) paramFunction;
        assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
        assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
        assertEquals("dd0ec4d2-7e74-4d92-af2f-89c7436baa63.controller_actor", toscaGetFunction.getPropertyUniqueId());
        assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
        assertEquals("controller_actor", toscaGetFunction.getPropertyName());
        assertEquals("testService", toscaGetFunction.getSourceName());
        assertEquals("dd0ec4d2-7e74-4d92-af2f-89c7436baa63", toscaGetFunction.getSourceUniqueId());
        assertEquals(List.of("controller_actor"), toscaGetFunction.getPropertyPathFromSource());
    }

    private void setDefaultCustomToscaFunctionOnConfiguration() {
        final var configurationManager = new ConfigurationManager();
        final var configuration = new Configuration();
        List<Configuration.CustomToscaFunction> defaultCustomToscaFunctions = new ArrayList<>();
        Configuration.CustomToscaFunction defaultCustomType = new Configuration.CustomToscaFunction();
        defaultCustomType.setName("custom_function_get_input_type");
        defaultCustomType.setType("get_input");
        defaultCustomToscaFunctions.add(defaultCustomType);
        configuration.setDefaultCustomToscaFunctions(defaultCustomToscaFunctions);
        configurationManager.setConfiguration(configuration);
    }

    private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException {
        return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class);
    }
}