5ba29cbbefcf623df59851049f82b63bbbd0689c
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / InterfacesOperationsConverterTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.tosca;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.createMappedOutputDefaultValue;
28 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF;
29
30 import com.fasterxml.jackson.databind.DeserializationFeature;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.google.gson.Gson;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Objects;
40 import org.apache.commons.collections4.MapUtils;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44 import org.openecomp.sdc.be.DummyConfigurationManager;
45 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
54 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
55 import org.openecomp.sdc.be.model.Component;
56 import org.openecomp.sdc.be.model.DataTypeDefinition;
57 import org.openecomp.sdc.be.model.InputDefinition;
58 import org.openecomp.sdc.be.model.InterfaceDefinition;
59 import org.openecomp.sdc.be.model.PropertyDefinition;
60 import org.openecomp.sdc.be.model.Resource;
61 import org.openecomp.sdc.be.model.Service;
62 import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
63 import org.openecomp.sdc.be.model.tosca.ToscaType;
64 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
65 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
66 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
67
68 class InterfacesOperationsConverterTest {
69
70     private static final String MAPPED_PROPERTY_NAME = "mapped_property";
71     private static final String INPUT_NAME_PREFIX = "input_";
72     private static final String OUTPUT_NAME_PREFIX = "output_";
73     private static final String NODE_TYPE_NAME = "test";
74     private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
75     private static ObjectMapper mapper;
76     private final String[] inputTypes = {"string", "integer", "float", "boolean"};
77     private InterfacesOperationsConverter interfacesOperationsConverter;
78
79     @BeforeAll
80     public static void setUp() {
81         //initialize the static configuration manager
82         new DummyConfigurationManager();
83         mapper = new ObjectMapper();
84         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
85     }
86
87     @BeforeEach
88     public void setUpBeforeTest() {
89         interfacesOperationsConverter = new InterfacesOperationsConverter(new PropertyConvertor());
90     }
91
92     @Test
93     void addInterfaceTypeElementToResource() {
94         Component component = new Resource();
95         component.setNormalizedName("normalizedComponentName");
96         component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
97         component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
98         component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
99         InterfaceDefinition addedInterface = new InterfaceDefinition();
100         addedInterface.setType("Local");
101         addOperationsToInterface(component, addedInterface, 5, 3, true, false, false);
102         final String interfaceType = "normalizedComponentName-interface";
103         component.setInterfaces(new HashMap<>());
104         component.getInterfaces().put(interfaceType, addedInterface);
105         final Map<String, Object> interfaceTypeElement = interfacesOperationsConverter.addInterfaceTypeElement(component, new ArrayList<>());
106         assertNotNull(interfaceTypeElement);
107         assertTrue(interfaceTypeElement.containsKey("org.openecomp.interfaces.node.lifecycle.NodeTypeName"));
108         Object o = interfaceTypeElement.get("org.openecomp.interfaces.node.lifecycle.NodeTypeName");
109         assertNotNull(o);
110         assertTrue(o instanceof Map);
111         assertEquals(7, ((Map) o).size());
112
113     }
114
115     @Test
116     void addInterfaceTypeElementToService() {
117         Component component = new Service();
118         component.setNormalizedName("normalizedServiceComponentName");
119         component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
120         component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
121         component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
122         InterfaceDefinition addedInterface = new InterfaceDefinition();
123         addedInterface.setType("Local");
124         addOperationsToInterface(component, addedInterface, 5, 3, true, false, false);
125         final String interfaceType = "normalizedServiceComponentName-interface";
126         component.setInterfaces(new HashMap<>());
127         component.getInterfaces().put(interfaceType, addedInterface);
128         final Map<String, Object> interfaceTypeElement = interfacesOperationsConverter.addInterfaceTypeElement(component, new ArrayList<>());
129         assertNotNull(interfaceTypeElement);
130         assertTrue(interfaceTypeElement.containsKey("org.openecomp.interfaces.node.lifecycle.NodeTypeName"));
131         Object o = interfaceTypeElement.get("org.openecomp.interfaces.node.lifecycle.NodeTypeName");
132         assertNotNull(o);
133         assertTrue(o instanceof Map);
134         assertEquals(7, ((Map) o).size());
135
136     }
137
138     @Test
139     void addInterfaceDefinitionElementToResource() {
140         Component component = new Resource();
141         component.setNormalizedName("normalizedComponentName");
142         InterfaceDefinition addedInterface = new InterfaceDefinition();
143         addedInterface.setType("com.some.resource.or.other.resourceName");
144
145         addOperationsToInterface(component, addedInterface, 3, 2, true, false, false);
146         final String interfaceType = "normalizedComponentName-interface";
147         component.setInterfaces(new HashMap<>());
148         component.getInterfaces().put(interfaceType, addedInterface);
149         ToscaNodeType nodeType = new ToscaNodeType();
150         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
151         Map<String, Object> interfaces = nodeType.getInterfaces();
152         assertNotNull(interfaces);
153         assertEquals(1, interfaces.size());
154         assertTrue(interfaces.containsKey("resourceName"));
155         Object resourceName = interfaces.get("resourceName");
156         assertNotNull(resourceName);
157         assertTrue(resourceName instanceof Map);
158         assertEquals(4, ((Map) resourceName).size());
159
160     }
161
162     @Test
163     void addInterfaceDefinitionElementToService() {
164         Component component = new Service();
165         component.setNormalizedName("normalizedServiceComponentName");
166         InterfaceDefinition addedInterface = new InterfaceDefinition();
167         addedInterface.setType("com.some.service.or.other.serviceName");
168         addOperationsToInterface(component, addedInterface, 3, 2, true, false, false);
169         final String interfaceType = "normalizedServiceComponentName-interface";
170         component.setInterfaces(new HashMap<>());
171         component.getInterfaces().put(interfaceType, addedInterface);
172         ToscaNodeType nodeType = new ToscaNodeType();
173         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
174         Map<String, Object> interfaces = nodeType.getInterfaces();
175         assertNotNull(interfaces);
176         assertEquals(1, interfaces.size());
177         assertTrue(interfaces.containsKey("serviceName"));
178         Object resourceName = interfaces.get("serviceName");
179         assertNotNull(resourceName);
180         assertTrue(resourceName instanceof Map);
181         assertEquals(4, ((Map) resourceName).size());
182
183     }
184
185     @Test
186     void testGetInterfaceAsMapServiceProxy() {
187         Component component = new Resource();
188         component.setNormalizedName("normalizedComponentName");
189         InterfaceDefinition addedInterface = new InterfaceDefinition();
190         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
191         addedInterface.setType("com.some.resource.or.other.resourceName");
192         addOperationsToInterface(component, addedInterface, 3, 2, true, false, false);
193         final String interfaceType = "normalizedComponentName-interface";
194         component.setInterfaces(new HashMap<>());
195         component.getInterfaces().put(interfaceType, addedInterface);
196         final var interfacesMap = interfacesOperationsConverter.getInterfacesMap(component, null, component.getInterfaces(), null, false);
197         assertNotNull(interfacesMap);
198         assertEquals(1, interfacesMap.size());
199         assertTrue(interfacesMap.containsKey("resourceName"));
200         Object resourceName = interfacesMap.get("resourceName");
201         assertNotNull(resourceName);
202         assertTrue(resourceName instanceof Map);
203         assertEquals(4, ((Map) resourceName).size());
204
205     }
206
207     @Test
208     void addInterfaceDefinitionElement_noInputs() {
209         Component component = new Resource();
210         component.setNormalizedName("normalizedComponentName");
211         InterfaceDefinition addedInterface = new InterfaceDefinition();
212         addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs");
213         addOperationsToInterface(component, addedInterface, 3, 3, false, false, false);
214         final String interfaceType = "normalizedComponentName-interface";
215         component.setInterfaces(new HashMap<>());
216         component.getInterfaces().put(interfaceType, addedInterface);
217         ToscaNodeType nodeType = new ToscaNodeType();
218         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false);
219         Map<String, Object> interfaces = nodeType.getInterfaces();
220         assertNotNull(interfaces);
221         assertEquals(1, interfaces.size());
222         assertTrue(interfaces.containsKey("resourceNameNoInputs"));
223         Object resourceName = interfaces.get("resourceNameNoInputs");
224         assertNotNull(resourceName);
225         assertTrue(resourceName instanceof Map);
226         assertEquals(4, ((Map) resourceName).size());
227
228     }
229
230     @Test
231     void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() {
232         String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
233         Component component = new Resource();
234         component.setNormalizedName("normalizedComponentName");
235         InterfaceDefinition addedInterface = new InterfaceDefinition();
236         addedInterface.setType(addedInterfaceType);
237         addOperationsToInterface(component, addedInterface, 2, 2, true, true, false);
238         addedInterface.getOperationsMap().values().stream()
239             .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
240                 "name_for_op_0"))
241             .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
242                 .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer"))
243                 .forEach(operationInputDefinition -> {
244                     operationInputDefinition.setInputId(addedInterfaceType + ".name_for_op_1.output_integer_1");
245                     operationInputDefinition.setToscaDefaultValue(
246                         new Gson().toJson(createMappedOutputDefaultValue(SELF, operationInputDefinition.getInputId())));
247                 }));
248         component.setInterfaces(new HashMap<>());
249         component.getInterfaces().put(addedInterfaceType, addedInterface);
250         ToscaNodeType nodeType = new ToscaNodeType();
251         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
252         Map<String, Object> interfaces = nodeType.getInterfaces();
253         assertNotNull(interfaces);
254         assertEquals(1, interfaces.size());
255         assertTrue(interfaces.containsKey("resourceNameInputMappedToOutput"));
256         Object resourceName = interfaces.get("resourceNameInputMappedToOutput");
257         assertNotNull(resourceName);
258         assertTrue(resourceName instanceof Map);
259         assertEquals(3, ((Map) resourceName).size());
260
261     }
262
263     @Test
264     void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() {
265         String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
266         Component component = new Resource();
267         component.setNormalizedName("normalizedComponentName");
268         InterfaceDefinition addedInterface = new InterfaceDefinition();
269         addedInterface.setType(addedInterfaceType);
270         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
271         addOperationsToInterface(component, addedInterface, 2, 2, true, true, false);
272         addedInterface.getOperationsMap().values().stream()
273             .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
274                 "name_for_op_0"))
275             .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
276                 .filter(opInputDef -> opInputDef.getName().contains("integer"))
277                 .forEach(opInputDef -> opInputDef.setInputId(
278                     addedInterfaceType + ".name_for_op_1.output_integer_1")));
279         //Mapping to operation from another interface
280         String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second";
281         InterfaceDefinition secondInterface = new InterfaceDefinition();
282         secondInterface.setType(secondInterfaceType);
283         secondInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
284         addOperationsToInterface(component, secondInterface, 2, 2, true, true, false);
285         secondInterface.getOperationsMap().values().stream()
286             .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
287                 "name_for_op_0"))
288             .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
289                 .filter(opInputDef -> opInputDef.getName().contains("integer"))
290                 .forEach(opInputDef -> opInputDef.setInputId(
291                     addedInterfaceType + ".name_for_op_1.output_integer_1")));
292         component.setInterfaces(new HashMap<>());
293         component.getInterfaces().put(addedInterfaceType, addedInterface);
294         component.getInterfaces().put(secondInterfaceType, secondInterface);
295
296         ToscaNodeType nodeType = new ToscaNodeType();
297         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
298         Map<String, Object> interfaces = nodeType.getInterfaces();
299         assertNotNull(interfaces);
300         assertEquals(2, interfaces.size());
301         assertTrue(interfaces.containsKey("resourceNameInputMappedToOutput"));
302         Object resourceName = interfaces.get("resourceNameInputMappedToOutput");
303         assertNotNull(resourceName);
304         assertTrue(resourceName instanceof Map);
305         assertEquals(3, ((Map) resourceName).size());
306
307         assertTrue(interfaces.containsKey("second"));
308         resourceName = interfaces.get("second");
309         assertNotNull(resourceName);
310         assertTrue(resourceName instanceof Map);
311         assertEquals(3, ((Map) resourceName).size());
312
313     }
314
315     @Test
316     void interfaceWithInputsToscaExportTest() {
317         final Component component = new Service();
318         final InterfaceDefinition anInterfaceWithInput = new InterfaceDefinition();
319         final String interfaceName = "myInterfaceName";
320         final String interfaceType = "my.type." + interfaceName;
321         anInterfaceWithInput.setType(interfaceType);
322         final String input1Name = "input1";
323         final InputDataDefinition input1 = createInput("string", "input1 description", false, "input1 value");
324         final String input2Name = "input2";
325         final InputDataDefinition input2 = createInput("string", "input2 description", true, "input2 value");
326         final Map<String, InputDataDefinition> inputMap = new HashMap<>();
327         inputMap.put(input1Name, input1);
328         inputMap.put(input2Name, input2);
329         anInterfaceWithInput.setInputs(inputMap);
330         component.setInterfaces(new HashMap<>());
331         component.getInterfaces().put(interfaceName, anInterfaceWithInput);
332         final ToscaNodeType nodeType = new ToscaNodeType();
333         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
334         Map<String, Object> interfaces = nodeType.getInterfaces();
335         assertNotNull(interfaces);
336         assertEquals(1, interfaces.size());
337         assertTrue(interfaces.containsKey("myInterfaceName"));
338         Object resourceName = interfaces.get("myInterfaceName");
339         assertNotNull(resourceName);
340         assertTrue(resourceName instanceof Map);
341         assertEquals(2, ((Map) resourceName).size());
342
343     }
344
345     @Test
346     void interfaceWithOperationImplementationArtifactPropertiesTest() {
347         //given
348         final Component component = new Service();
349         final InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
350         final String interfaceName = "myInterfaceName";
351         interfaceDefinition.setType("my.type." + interfaceName);
352         final var operation1DataDefinition = new OperationDataDefinition();
353         operation1DataDefinition.setName("anOperation");
354
355         final PropertyDataDefinition listOfStringProperty = new PropertyDataDefinition();
356         listOfStringProperty.setName("listProperty");
357         listOfStringProperty.setType(ToscaType.LIST.getType());
358         final PropertyDataDefinition listOfStringSchemaProperty = new PropertyDataDefinition();
359         listOfStringSchemaProperty.setType(ToscaType.STRING.getType());
360         final SchemaDefinition listPropertySchema = new SchemaDefinition();
361         listPropertySchema.setProperty(listOfStringProperty);
362         listOfStringProperty.setSchema(listPropertySchema);
363         listOfStringProperty.setValue("[ \"value1\", \"value2\", \"value3\" ]");
364         final ArrayList<Object> propertyList = new ArrayList<>();
365         propertyList.add(listOfStringProperty);
366         final HashMap<String, Object> artifactDefinitionMapInitializer = new HashMap<>();
367         artifactDefinitionMapInitializer.put(JsonPresentationFields.PROPERTIES.getPresentation(), propertyList);
368         final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(artifactDefinitionMapInitializer);
369         artifactDataDefinition.setArtifactName("artifact1");
370         artifactDataDefinition.setArtifactType("my.artifact.Type");
371         operation1DataDefinition.setImplementation(artifactDataDefinition);
372         interfaceDefinition.setOperations(Map.of(operation1DataDefinition.getName(), operation1DataDefinition));
373         component.setInterfaces(new HashMap<>());
374         component.getInterfaces().put(interfaceName, interfaceDefinition);
375         //when
376         Map<String, Object> interfacesMap = interfacesOperationsConverter.getInterfacesMap(component, null, component.getInterfaces(), null, false);
377         //then
378         assertTrue(interfacesMap.containsKey(interfaceName));
379         final Map<String, Object> actualInterfaceMap = (Map<String, Object>) interfacesMap.get(interfaceName);
380         assertTrue(actualInterfaceMap.containsKey(operation1DataDefinition.getName()));
381         final Map<String, Object> actualOperationMap = (Map<String, Object>) actualInterfaceMap.get(operation1DataDefinition.getName());
382         assertTrue(actualOperationMap.containsKey("implementation"));
383         final Map<String, Object> actualImplementationMap = (Map<String, Object>) actualOperationMap.get("implementation");
384         assertTrue(actualImplementationMap.containsKey("primary"));
385         final Map<String, Object> actualArtifactImplementationMap = (Map<String, Object>) actualImplementationMap.get("primary");
386         assertTrue(actualArtifactImplementationMap.containsKey("properties"));
387         final Map<String, Object> actualArtifactPropertiesMap = (Map<String, Object>) actualArtifactImplementationMap.get("properties");
388         assertEquals(1, actualArtifactPropertiesMap.keySet().size());
389         assertTrue(actualArtifactPropertiesMap.containsKey(listOfStringProperty.getName()));
390         final Object expectedListObject = actualArtifactPropertiesMap.get(listOfStringProperty.getName());
391         assertTrue(expectedListObject instanceof List);
392         final List<String> expectedListOfStringPropValue = (List<String>) expectedListObject;
393         assertEquals(3, expectedListOfStringPropValue.size());
394         assertTrue(expectedListOfStringPropValue.contains("value1"));
395         assertTrue(expectedListOfStringPropValue.contains("value2"));
396         assertTrue(expectedListOfStringPropValue.contains("value3"));
397     }
398
399     private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps,
400                                           int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs, boolean addAComplexType) {
401
402         addedInterface.setOperations(new HashMap<>());
403         for (int i = 0; i < numOfOps; i++) {
404             final OperationDataDefinition operation = new OperationDataDefinition();
405             operation.setName("name_for_op_" + i);
406             operation.setDescription("op " + i + " has description");
407             final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
408             implementation.setArtifactName(i + "_createBPMN.bpmn");
409             operation.setImplementation(implementation);
410             if (hasInputs) {
411                 operation.setInputs(createInputs(component, numOfInputsPerOp, addAComplexType));
412             }
413             if (hasOutputs) {
414                 operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(),
415                     operation.getName(), numOfInputsPerOp));
416             }
417             addedInterface.getOperations().put(operation.getName(), operation);
418         }
419     }
420
421     private InputDataDefinition createInput(final String type, final String description, final Boolean isRequired,
422                                             final String defaultValue) {
423         final PropertyDataDefinition propertyDataDefinition = new PropertyDataDefinition();
424         if (type != null) {
425             propertyDataDefinition.setType(type);
426         }
427         if (description != null) {
428             propertyDataDefinition.setDescription(description);
429         }
430         if (defaultValue != null) {
431             propertyDataDefinition.setDefaultValue(defaultValue);
432         }
433         if (isRequired != null) {
434             propertyDataDefinition.setRequired(isRequired);
435         }
436         return new InputDataDefinition(propertyDataDefinition);
437     }
438
439     private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs, boolean addAComplexType) {
440         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
441         if (addAComplexType) {
442             String mappedPropertyName = java.util.UUID.randomUUID() + "." + MAPPED_PROPERTY_NAME + numOfInputs;
443             operationInputDefinitionList.add(
444                 createMockComplexOperationInputDefinition(INPUT_NAME_PREFIX + "Complex" + "_" + numOfInputs, mappedPropertyName));
445             numOfInputs -= 1;
446         }
447         for (int i = 0; i < numOfInputs; i++) {
448             String mappedPropertyName = java.util.UUID.randomUUID() + "." + MAPPED_PROPERTY_NAME + i;
449             operationInputDefinitionList.add(createMockOperationInputDefinition(
450                 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i));
451             addMappedPropertyAsComponentInput(component, mappedPropertyName);
452
453         }
454         return operationInputDefinitionList;
455     }
456
457     private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) {
458         InputDefinition componentInput = new InputDefinition();
459         componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]);
460         componentInput.setName(mappedPropertyName.split("\\.")[1]);
461         if (Objects.isNull(component.getInputs())) {
462             component.setInputs(new ArrayList<>());
463         }
464         component.getInputs().add(componentInput);
465     }
466
467     private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName,
468                                                                         String operationName,
469                                                                         int numOfOutputs) {
470         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
471         for (int i = 0; i < numOfOutputs; i++) {
472             operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName,
473                 OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i));
474         }
475         return operationOutputDefinitionList;
476     }
477
478     private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
479         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
480         operationInputDefinition.setName(name);
481         operationInputDefinition.setInputId(id);
482         operationInputDefinition.setType(inputTypes[index]);
483         operationInputDefinition.setRequired(index % 2 == 0);
484         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
485         List<String> toscaDefaultValues = new ArrayList<>();
486         toscaDefaultValues.add(SELF);
487         toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1));
488         toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues);
489         operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap));
490         operationInputDefinition.setSource("ServiceInput");
491         return operationInputDefinition;
492     }
493
494     private OperationInputDefinition createMockComplexOperationInputDefinition(String name, String id) {
495         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
496         operationInputDefinition.setName(name);
497         operationInputDefinition.setInputId(id);
498         operationInputDefinition.setType("complexDataType");
499         operationInputDefinition.setRequired(false);
500         operationInputDefinition.setValue(
501             "{\"intProp\":1,\"stringProp\":{\"type\":\"GET_ATTRIBUTE\",\"propertyUniqueId\":\"ac4bc339-56d1-4ea2-9802-2da219a1247a.designer\",\"propertyName\":\"designer\",\"propertySource\":\"SELF\",\"sourceUniqueId\":\"ac4bc339-56d1-4ea2-9802-2da219a1247a\",\"sourceName\":\"service\",\"functionType\":\"GET_ATTRIBUTE\",\"propertyPathFromSource\":[\"designer\"]}}");
502         return operationInputDefinition;
503     }
504
505     private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName,
506                                                                           String outputName, int index) {
507         OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition();
508         operationInputDefinition.setName(outputName);
509         operationInputDefinition.setType(inputTypes[index]);
510         operationInputDefinition.setRequired(index % 2 == 0);
511         List<String> toscaDefaultValues = new ArrayList<>();
512         toscaDefaultValues.add(SELF);
513         toscaDefaultValues.add(interfaceName);
514         toscaDefaultValues.add(operationName);
515         toscaDefaultValues.add(outputName);
516         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
517         toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues);
518         return operationInputDefinition;
519     }
520
521     @Test
522     void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() {
523         Service service = new Service();
524         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
525         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
526         service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface");
527         service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>())));
528
529         Map<String, Object> resultMap = interfacesOperationsConverter.addInterfaceTypeElement(service,
530             Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard"));
531
532         assertTrue(MapUtils.isNotEmpty(resultMap)
533             && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface"));
534     }
535
536     @Test
537     void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() {
538         Service service = new Service();
539         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
540         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
541         service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null,
542             new HashMap<>())));
543
544         Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null, service.getInterfaces(), null, false);
545
546         assertTrue(MapUtils.isNotEmpty(resultMap)
547             && resultMap.containsKey("NotLocal"));
548     }
549
550     @Test
551     void testGetInterfaceAsMapWithComplexType() {
552         addComplexTypeToDataTypes();
553         Component component = new Resource();
554         component.setNormalizedName("normalizedComponentName");
555         InterfaceDefinition addedInterface = new InterfaceDefinition();
556         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
557         addedInterface.setType("com.some.resource.or.other.resourceName");
558         addOperationsToInterface(component, addedInterface, 3, 2, true, false, true);
559         final String interfaceType = "normalizedComponentName-interface";
560         component.setInterfaces(new HashMap<>());
561         component.getInterfaces().put(interfaceType, addedInterface);
562         final var interfacesMap = interfacesOperationsConverter.getInterfacesMap(component, null, component.getInterfaces(), dataTypes, false);
563         assertNotNull(interfacesMap);
564         assertEquals(1, interfacesMap.size());
565         assertTrue(interfacesMap.containsKey("resourceName"));
566         Object resourceName = interfacesMap.get("resourceName");
567         assertNotNull(resourceName);
568         assertTrue(resourceName instanceof Map);
569         assertEquals(4, ((Map) resourceName).size());
570         assertTrue(resourceName instanceof Map);
571         Map<String, Object> resource = (Map<String, Object>) resourceName;
572         assertTrue(resource.containsKey("name_for_op_0"));
573         Map<String, Object> operation0 = (Map<String, Object>) resource.get("name_for_op_0");
574         assertTrue(operation0.containsKey("inputs"));
575         Map<String, Object> operation0Inputs = (Map<String, Object>) operation0.get("inputs");
576         assertTrue(operation0Inputs.containsKey("input_Complex_2"));
577         Map<String, Object> complexInput = (Map<String, Object>) operation0Inputs.get("input_Complex_2");
578         assertTrue(complexInput.containsKey("stringProp"));
579         Map<String, Object> complexInputStringProp = (Map<String, Object>) complexInput.get("stringProp");
580         assertTrue(complexInputStringProp.containsKey("type"));
581         assertTrue(ToscaFunctionType.findType((String) complexInputStringProp.get("type")).isPresent());
582         assertTrue(complexInputStringProp.containsKey("propertyName"));
583         assertEquals("designer", complexInputStringProp.get("propertyName"));
584         assertTrue(complexInputStringProp.containsKey("propertySource"));
585         assertEquals("SELF", complexInputStringProp.get("propertySource"));
586     }
587
588     private void addComplexTypeToDataTypes() {
589         PropertyDefinition intProp = new PropertyDefinition();
590         intProp.setType("integer");
591         intProp.setName("intProp");
592         PropertyDefinition stringProp = new PropertyDefinition();
593         stringProp.setType("string");
594         stringProp.setName("stringProp");
595         DataTypeDefinition dataType = new DataTypeDefinition();
596         dataType.setName("complexDataType");
597         dataType.setProperties(new ArrayList<>(Arrays.asList(stringProp, intProp)));
598         dataTypes.put("complexDataType", dataType);
599     }
600
601     @Test
602     void testRemoveInterfacesWithoutOperationsEmptyMap() {
603         final Map<String, Object> interfaceMap = new HashMap<>();
604         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
605         assertNotNull(interfaceMap);
606         assertTrue(interfaceMap.isEmpty());
607     }
608
609     @Test
610     void testRemoveInterfacesWithoutOperationsNullParameter() {
611         final Map<String, Object> interfaceMap = null;
612         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
613         assertNull(interfaceMap);
614     }
615
616     @Test
617     void testRemoveInterfacesWithoutOperationsSuccess() {
618         final Map<String, Object> interfaceMap = new HashMap<>();
619         final ToscaInterfaceDefinition toscaInterfaceDefinition1 = new ToscaInterfaceDefinition();
620         interfaceMap.put("toscaInterfaceDefinition1", toscaInterfaceDefinition1);
621
622         final ToscaInterfaceDefinition toscaInterfaceDefinition2 = new ToscaInterfaceDefinition();
623         final Map<String, Object> toscaInterfaceDefinition2OperationMap = new HashMap<>();
624         toscaInterfaceDefinition2OperationMap.put("operation1", new Object());
625         toscaInterfaceDefinition2.setOperations(toscaInterfaceDefinition2OperationMap);
626         interfaceMap.put("toscaInterfaceDefinition2", toscaInterfaceDefinition2);
627
628         final Map<String, Object> toscaInterfaceDefinition3 = new HashMap<>();
629         interfaceMap.put("toscaInterfaceDefinition3", toscaInterfaceDefinition3);
630
631         final Map<String, Object> toscaInterfaceDefinition4 = new HashMap<>();
632         toscaInterfaceDefinition4.put("operation1", new Object());
633         interfaceMap.put("toscaInterfaceDefinition4", toscaInterfaceDefinition4);
634
635         final Object notAToscaInterfaceDefinition = new Object();
636         interfaceMap.put("notAToscaInterfaceDefinition", notAToscaInterfaceDefinition);
637
638         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
639         assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition1"));
640         assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition2"));
641         assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition3"));
642         assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition4"));
643         assertTrue(interfaceMap.containsKey("notAToscaInterfaceDefinition"));
644     }
645 }