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