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