Interface is formatted using extended notation when no implementation added at VFC...
[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.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.containsInAnyOrder;
24 import static org.hamcrest.Matchers.equalTo;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.junit.jupiter.api.Assertions.fail;
31 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF;
32 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT;
33 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION;
34 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS;
35 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INTERFACES;
36 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TYPES;
37 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIRED;
38 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE;
39
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.google.gson.Gson;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Map.Entry;
49 import java.util.Objects;
50 import java.util.function.Function;
51 import java.util.stream.Collectors;
52 import org.apache.commons.collections4.MapUtils;
53 import org.junit.jupiter.api.BeforeAll;
54 import org.junit.jupiter.api.BeforeEach;
55 import org.junit.jupiter.api.Test;
56 import org.onap.sdc.tosca.services.YamlUtil;
57 import org.openecomp.sdc.be.DummyConfigurationManager;
58 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
61 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
62 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
66 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
67 import org.openecomp.sdc.be.model.Component;
68 import org.openecomp.sdc.be.model.DataTypeDefinition;
69 import org.openecomp.sdc.be.model.InputDefinition;
70 import org.openecomp.sdc.be.model.InterfaceDefinition;
71 import org.openecomp.sdc.be.model.Resource;
72 import org.openecomp.sdc.be.model.Service;
73 import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
74 import org.openecomp.sdc.be.model.tosca.ToscaType;
75 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
76 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
77 import org.openecomp.sdc.common.util.YamlToObjectConverter;
78 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
79 import org.yaml.snakeyaml.Yaml;
80
81 class InterfacesOperationsConverterTest {
82
83     private static final String MAPPED_PROPERTY_NAME = "mapped_property";
84     private static final String INPUT_NAME_PREFIX = "input_";
85     private static final String OUTPUT_NAME_PREFIX = "output_";
86     private static final String NODE_TYPE_NAME = "test";
87     private final String[] inputTypes = {"string", "integer", "float", "boolean"};
88     private static ObjectMapper mapper;
89     private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
90
91     private InterfacesOperationsConverter interfacesOperationsConverter;
92
93     @BeforeAll
94     public static void setUp() {
95         //initialize the static configuration manager
96         new DummyConfigurationManager();
97         mapper = new ObjectMapper();
98         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
99     }
100
101     @BeforeEach
102     public void setUpBeforeTest() {
103         interfacesOperationsConverter = new InterfacesOperationsConverter(new PropertyConvertor());
104     }
105
106     @Test
107     void addInterfaceTypeElementToResource() {
108         Component component = new Resource();
109         component.setNormalizedName("normalizedComponentName");
110         component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
111         component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
112         component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
113         InterfaceDefinition addedInterface = new InterfaceDefinition();
114         addedInterface.setType("Local");
115         addOperationsToInterface(component, addedInterface, 5, 3, true, false);
116         final String interfaceType = "normalizedComponentName-interface";
117         component.setInterfaces(new HashMap<>());
118         component.getInterfaces().put(interfaceType, addedInterface);
119         final Map<String, Object> interfaceTypeElement = interfacesOperationsConverter.addInterfaceTypeElement(component, new ArrayList<>());
120         assertNotNull(interfaceTypeElement);
121         assertTrue(interfaceTypeElement.containsKey("org.openecomp.interfaces.node.lifecycle.NodeTypeName"));
122         Object o = interfaceTypeElement.get("org.openecomp.interfaces.node.lifecycle.NodeTypeName");
123         assertNotNull(o);
124         assertTrue(o instanceof Map);
125         assertEquals(7, ((Map) o).size());
126
127     }
128
129     @Test
130     void addInterfaceTypeElementToService() {
131         Component component = new Service();
132         component.setNormalizedName("normalizedServiceComponentName");
133         component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
134         component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
135         component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
136         InterfaceDefinition addedInterface = new InterfaceDefinition();
137         addedInterface.setType("Local");
138         addOperationsToInterface(component, addedInterface, 5, 3, true, false);
139         final String interfaceType = "normalizedServiceComponentName-interface";
140         component.setInterfaces(new HashMap<>());
141         component.getInterfaces().put(interfaceType, addedInterface);
142         final Map<String, Object> interfaceTypeElement = interfacesOperationsConverter.addInterfaceTypeElement(component, new ArrayList<>());
143         assertNotNull(interfaceTypeElement);
144         assertTrue(interfaceTypeElement.containsKey("org.openecomp.interfaces.node.lifecycle.NodeTypeName"));
145         Object o = interfaceTypeElement.get("org.openecomp.interfaces.node.lifecycle.NodeTypeName");
146         assertNotNull(o);
147         assertTrue(o instanceof Map);
148         assertEquals(7, ((Map) o).size());
149
150     }
151
152     @Test
153     void addInterfaceDefinitionElementToResource() {
154         Component component = new Resource();
155         component.setNormalizedName("normalizedComponentName");
156         InterfaceDefinition addedInterface = new InterfaceDefinition();
157         addedInterface.setType("com.some.resource.or.other.resourceName");
158
159         addOperationsToInterface(component, addedInterface, 3, 2, true, false);
160         final String interfaceType = "normalizedComponentName-interface";
161         component.setInterfaces(new HashMap<>());
162         component.getInterfaces().put(interfaceType, addedInterface);
163         ToscaNodeType nodeType = new ToscaNodeType();
164         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
165         Map<String, Object> interfaces = nodeType.getInterfaces();
166         assertNotNull(interfaces);
167         assertEquals(1, interfaces.size());
168         assertTrue(interfaces.containsKey("resourceName"));
169         Object resourceName = interfaces.get("resourceName");
170         assertNotNull(resourceName);
171         assertTrue(resourceName instanceof Map);
172         assertEquals(4, ((Map) resourceName).size());
173
174     }
175
176     @Test
177     void addInterfaceDefinitionElementToService() {
178         Component component = new Service();
179         component.setNormalizedName("normalizedServiceComponentName");
180         InterfaceDefinition addedInterface = new InterfaceDefinition();
181         addedInterface.setType("com.some.service.or.other.serviceName");
182         addOperationsToInterface(component, addedInterface, 3, 2, true, false);
183         final String interfaceType = "normalizedServiceComponentName-interface";
184         component.setInterfaces(new HashMap<>());
185         component.getInterfaces().put(interfaceType, addedInterface);
186         ToscaNodeType nodeType = new ToscaNodeType();
187         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
188         Map<String, Object> interfaces = nodeType.getInterfaces();
189         assertNotNull(interfaces);
190         assertEquals(1, interfaces.size());
191         assertTrue(interfaces.containsKey("serviceName"));
192         Object resourceName = interfaces.get("serviceName");
193         assertNotNull(resourceName);
194         assertTrue(resourceName instanceof Map);
195         assertEquals(4, ((Map) resourceName).size());
196
197     }
198
199
200     @Test
201     void testGetInterfaceAsMapServiceProxy() {
202         Component component = new Resource();
203         component.setNormalizedName("normalizedComponentName");
204         InterfaceDefinition addedInterface = new InterfaceDefinition();
205         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
206         addedInterface.setType("com.some.resource.or.other.resourceName");
207         addOperationsToInterface(component, addedInterface, 3, 2, true, false);
208         final String interfaceType = "normalizedComponentName-interface";
209         component.setInterfaces(new HashMap<>());
210         component.getInterfaces().put(interfaceType, addedInterface);
211         final var interfacesMap = interfacesOperationsConverter.getInterfacesMap(component, null, component.getInterfaces(), null, false);
212         assertNotNull(interfacesMap);
213         assertEquals(1, interfacesMap.size());
214         assertTrue(interfacesMap.containsKey("resourceName"));
215         Object resourceName = interfacesMap.get("resourceName");
216         assertNotNull(resourceName);
217         assertTrue(resourceName instanceof Map);
218         assertEquals(4, ((Map) resourceName).size());
219
220     }
221
222     @Test
223     void addInterfaceDefinitionElement_noInputs() {
224         Component component = new Resource();
225         component.setNormalizedName("normalizedComponentName");
226         InterfaceDefinition addedInterface = new InterfaceDefinition();
227         addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs");
228         addOperationsToInterface(component, addedInterface, 3, 3, false, false);
229         final String interfaceType = "normalizedComponentName-interface";
230         component.setInterfaces(new HashMap<>());
231         component.getInterfaces().put(interfaceType, addedInterface);
232         ToscaNodeType nodeType = new ToscaNodeType();
233         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false);
234         Map<String, Object> interfaces = nodeType.getInterfaces();
235         assertNotNull(interfaces);
236         assertEquals(1, interfaces.size());
237         assertTrue(interfaces.containsKey("resourceNameNoInputs"));
238         Object resourceName = interfaces.get("resourceNameNoInputs");
239         assertNotNull(resourceName);
240         assertTrue(resourceName instanceof Map);
241         assertEquals(4, ((Map) resourceName).size());
242
243     }
244
245     @Test
246     void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() {
247         String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
248         Component component = new Resource();
249         component.setNormalizedName("normalizedComponentName");
250         InterfaceDefinition addedInterface = new InterfaceDefinition();
251         addedInterface.setType(addedInterfaceType);
252         addOperationsToInterface(component, addedInterface, 2, 2, true, true);
253         addedInterface.getOperationsMap().values().stream()
254             .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
255                 "name_for_op_0"))
256             .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
257                 .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer"))
258                 .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType +
259                     ".name_for_op_1.output_integer_1")));
260         component.setInterfaces(new HashMap<>());
261         component.getInterfaces().put(addedInterfaceType, addedInterface);
262         ToscaNodeType nodeType = new ToscaNodeType();
263         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
264         Map<String, Object> interfaces = nodeType.getInterfaces();
265         assertNotNull(interfaces);
266         assertEquals(1, interfaces.size());
267         assertTrue(interfaces.containsKey("resourceNameInputMappedToOutput"));
268         Object resourceName = interfaces.get("resourceNameInputMappedToOutput");
269         assertNotNull(resourceName);
270         assertTrue(resourceName instanceof Map);
271         assertEquals(3, ((Map) resourceName).size());
272
273     }
274
275     @Test
276     void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() {
277         String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
278         Component component = new Resource();
279         component.setNormalizedName("normalizedComponentName");
280         InterfaceDefinition addedInterface = new InterfaceDefinition();
281         addedInterface.setType(addedInterfaceType);
282         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
283         addOperationsToInterface(component, addedInterface, 2, 2, true, true);
284         addedInterface.getOperationsMap().values().stream()
285             .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
286                 "name_for_op_0"))
287             .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
288                 .filter(opInputDef -> opInputDef.getName().contains("integer"))
289                 .forEach(opInputDef -> opInputDef.setInputId(
290                     addedInterfaceType + ".name_for_op_1.output_integer_1")));
291         //Mapping to operation from another interface
292         String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second";
293         InterfaceDefinition secondInterface = new InterfaceDefinition();
294         secondInterface.setType(secondInterfaceType);
295         secondInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
296         addOperationsToInterface(component, secondInterface, 2, 2, true, true);
297         secondInterface.getOperationsMap().values().stream()
298             .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
299                 "name_for_op_0"))
300             .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
301                 .filter(opInputDef -> opInputDef.getName().contains("integer"))
302                 .forEach(opInputDef -> opInputDef.setInputId(
303                     addedInterfaceType + ".name_for_op_1.output_integer_1")));
304         component.setInterfaces(new HashMap<>());
305         component.getInterfaces().put(addedInterfaceType, addedInterface);
306         component.getInterfaces().put(secondInterfaceType, secondInterface);
307
308         ToscaNodeType nodeType = new ToscaNodeType();
309         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
310         Map<String, Object> interfaces = nodeType.getInterfaces();
311         assertNotNull(interfaces);
312         assertEquals(2, interfaces.size());
313         assertTrue(interfaces.containsKey("resourceNameInputMappedToOutput"));
314         Object resourceName = interfaces.get("resourceNameInputMappedToOutput");
315         assertNotNull(resourceName);
316         assertTrue(resourceName instanceof Map);
317         assertEquals(3, ((Map) resourceName).size());
318
319         assertTrue(interfaces.containsKey("second"));
320         resourceName = interfaces.get("second");
321         assertNotNull(resourceName);
322         assertTrue(resourceName instanceof Map);
323         assertEquals(3, ((Map) resourceName).size());
324
325     }
326
327     @Test
328     void interfaceWithInputsToscaExportTest() {
329         final Component component = new Service();
330         final InterfaceDefinition anInterfaceWithInput = new InterfaceDefinition();
331         final String interfaceName = "myInterfaceName";
332         final String interfaceType = "my.type." + interfaceName;
333         anInterfaceWithInput.setType(interfaceType);
334         final String input1Name = "input1";
335         final InputDataDefinition input1 = createInput("string", "input1 description", false, "input1 value");
336         final String input2Name = "input2";
337         final InputDataDefinition input2 = createInput("string", "input2 description", true, "input2 value");
338         final Map<String, InputDataDefinition> inputMap = new HashMap<>();
339         inputMap.put(input1Name, input1);
340         inputMap.put(input2Name, input2);
341         anInterfaceWithInput.setInputs(inputMap);
342         component.setInterfaces(new HashMap<>());
343         component.getInterfaces().put(interfaceName, anInterfaceWithInput);
344         final ToscaNodeType nodeType = new ToscaNodeType();
345         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
346         Map<String, Object> interfaces = nodeType.getInterfaces();
347         assertNotNull(interfaces);
348         assertEquals(1, interfaces.size());
349         assertTrue(interfaces.containsKey("myInterfaceName"));
350         Object resourceName = interfaces.get("myInterfaceName");
351         assertNotNull(resourceName);
352         assertTrue(resourceName instanceof Map);
353         assertEquals(2, ((Map) resourceName).size());
354
355     }
356
357     @Test
358     void interfaceWithOperationImplementationArtifactPropertiesTest() {
359         //given
360         final Component component = new Service();
361         final InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
362         final String interfaceName = "myInterfaceName";
363         interfaceDefinition.setType("my.type." + interfaceName);
364         final var operation1DataDefinition = new OperationDataDefinition();
365         operation1DataDefinition.setName("anOperation");
366
367         final PropertyDataDefinition listOfStringProperty = new PropertyDataDefinition();
368         listOfStringProperty.setName("listProperty");
369         listOfStringProperty.setType(ToscaType.LIST.getType());
370         final PropertyDataDefinition listOfStringSchemaProperty = new PropertyDataDefinition();
371         listOfStringSchemaProperty.setType(ToscaType.STRING.getType());
372         final SchemaDefinition listPropertySchema = new SchemaDefinition();
373         listPropertySchema.setProperty(listOfStringProperty);
374         listOfStringProperty.setSchema(listPropertySchema);
375         listOfStringProperty.setValue("[ \"value1\", \"value2\", \"value3\" ]");
376         final ArrayList<Object> propertyList = new ArrayList<>();
377         propertyList.add(listOfStringProperty);
378         final HashMap<String, Object> artifactDefinitionMapInitializer = new HashMap<>();
379         artifactDefinitionMapInitializer.put(JsonPresentationFields.PROPERTIES.getPresentation(), propertyList);
380         final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(artifactDefinitionMapInitializer);
381         artifactDataDefinition.setArtifactName("artifact1");
382         artifactDataDefinition.setArtifactType("my.artifact.Type");
383         operation1DataDefinition.setImplementation(artifactDataDefinition);
384         interfaceDefinition.setOperations(Map.of(operation1DataDefinition.getName(), operation1DataDefinition));
385         component.setInterfaces(new HashMap<>());
386         component.getInterfaces().put(interfaceName, interfaceDefinition);
387         //when
388         Map<String, Object> interfacesMap = interfacesOperationsConverter.getInterfacesMap(component, null, component.getInterfaces(), null, false);
389         //then
390         assertTrue(interfacesMap.containsKey(interfaceName));
391         final Map<String, Object> actualInterfaceMap = (Map<String, Object>) interfacesMap.get(interfaceName);
392         assertTrue(actualInterfaceMap.containsKey(operation1DataDefinition.getName()));
393         final Map<String, Object> actualOperationMap = (Map<String, Object>) actualInterfaceMap.get(operation1DataDefinition.getName());
394         assertTrue(actualOperationMap.containsKey("implementation"));
395         final Map<String, Object> actualImplementationMap = (Map<String, Object>) actualOperationMap.get("implementation");
396         assertTrue(actualImplementationMap.containsKey("primary"));
397         final Map<String, Object> actualArtifactImplementationMap = (Map<String, Object>) actualImplementationMap.get("primary");
398         assertTrue(actualArtifactImplementationMap.containsKey("properties"));
399         final Map<String, Object> actualArtifactPropertiesMap = (Map<String, Object>) actualArtifactImplementationMap.get("properties");
400         assertEquals(1, actualArtifactPropertiesMap.keySet().size());
401         assertTrue(actualArtifactPropertiesMap.containsKey(listOfStringProperty.getName()));
402         final Object expectedListObject = actualArtifactPropertiesMap.get(listOfStringProperty.getName());
403         assertTrue(expectedListObject instanceof List);
404         final List<String> expectedListOfStringPropValue = (List<String>) expectedListObject;
405         assertEquals(3, expectedListOfStringPropValue.size());
406         assertTrue(expectedListOfStringPropValue.contains("value1"));
407         assertTrue(expectedListOfStringPropValue.contains("value2"));
408         assertTrue(expectedListOfStringPropValue.contains("value3"));
409     }
410
411
412     private void validateInterfaceInputs(final String yaml, final String interfaceName, final Map<String, InputDataDefinition> expectedInputMap) {
413         String fixedMainYaml = yaml;
414         final String nullString = "null";
415         if (fixedMainYaml.startsWith(nullString)) {
416             fixedMainYaml = yaml.substring(nullString.length());
417         }
418         if (fixedMainYaml.endsWith(nullString)) {
419             fixedMainYaml = fixedMainYaml.substring(0, fixedMainYaml.length() - nullString.length());
420         }
421         final Map<String, Object> yamlMap = new Yaml().load(fixedMainYaml);
422         final Map<String, Object> nodeTypesMap = (Map<String, Object>) yamlMap.get(NODE_TYPES.getElementName());
423         final Map<String, Object> node = (Map<String, Object>) nodeTypesMap.get(NODE_TYPE_NAME);
424         final Map<String, Object> interfacesMap = (Map<String, Object>) node.get(INTERFACES.getElementName());
425         final Map<String, Object> interface1 = (Map<String, Object>) interfacesMap.get(interfaceName);
426         final Map<String, Object> actualInputsMap = (Map<String, Object>) interface1.get(INPUTS.getElementName());
427         assertThat(actualInputsMap.keySet(), containsInAnyOrder(expectedInputMap.keySet().toArray()));
428         expectedInputMap.forEach((inputName, inputDataDefinition) -> {
429             final Map<String, Object> actualInput = (Map<String, Object>) actualInputsMap.get(inputName);
430             compareInputYaml(inputName, actualInput, inputDataDefinition);
431         });
432     }
433
434     private void compareInputYaml(final String inputName, final Map<String, Object> actualInput,
435                                   final InputDataDefinition expectedInput) {
436         final String msgFormat = "%s should be equal in input %s";
437         String field = TYPE.getElementName();
438         assertThat(String.format(msgFormat, field, inputName),
439             actualInput.get(field), equalTo(expectedInput.getType()));
440         field = DESCRIPTION.getElementName();
441         assertThat(String.format(msgFormat, field, inputName),
442             actualInput.get(field), equalTo(expectedInput.getDescription()));
443         field = REQUIRED.getElementName();
444         assertThat(String.format(msgFormat, field, inputName),
445             actualInput.get(field), equalTo(expectedInput.getRequired()));
446         field = DEFAULT.getElementName();
447         assertThat(String.format(msgFormat, field, inputName),
448             actualInput.get(field), equalTo(expectedInput.getDefaultValue()));
449     }
450
451     @FunctionalInterface
452     interface MainYamlAssertion extends Function<String, Boolean> {
453
454     }
455
456     private static Function<String, Boolean> all(MainYamlAssertion... fs) {
457         return s -> io.vavr.collection.List.of(fs).map(f -> f.apply(s)).fold(true, (l, r) -> l && r);
458     }
459
460     private static MainYamlAssertion containsNone(String... expected) {
461         return s -> io.vavr.collection.List.of(expected).map(e -> !s.contains(e)).fold(true, (l, r) -> l && r);
462     }
463
464     private static MainYamlAssertion containsAll(String... expected) {
465         return s -> io.vavr.collection.List.of(expected).map(s::contains).fold(true, (l, r) -> l && r);
466     }
467
468     private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps,
469                                           int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) {
470
471         addedInterface.setOperations(new HashMap<>());
472         for (int i = 0; i < numOfOps; i++) {
473             final OperationDataDefinition operation = new OperationDataDefinition();
474             operation.setName("name_for_op_" + i);
475             operation.setDescription("op " + i + " has description");
476             final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
477             implementation.setArtifactName(i + "_createBPMN.bpmn");
478             operation.setImplementation(implementation);
479             if (hasInputs) {
480                 operation.setInputs(createInputs(component, numOfInputsPerOp));
481             }
482             if (hasOutputs) {
483                 operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(),
484                     operation.getName(), numOfInputsPerOp));
485             }
486             addedInterface.getOperations().put(operation.getName(), operation);
487         }
488     }
489
490     private InputDataDefinition createInput(final String type, final String description, final Boolean isRequired,
491                                             final String defaultValue) {
492         final PropertyDataDefinition propertyDataDefinition = new PropertyDataDefinition();
493         if (type != null) {
494             propertyDataDefinition.setType(type);
495         }
496         if (description != null) {
497             propertyDataDefinition.setDescription(description);
498         }
499         if (defaultValue != null) {
500             propertyDataDefinition.setDefaultValue(defaultValue);
501         }
502         if (isRequired != null) {
503             propertyDataDefinition.setRequired(isRequired);
504         }
505         return new InputDataDefinition(propertyDataDefinition);
506     }
507
508     private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs) {
509         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
510         for (int i = 0; i < numOfInputs; i++) {
511             String mappedPropertyName = java.util.UUID.randomUUID() + "." + MAPPED_PROPERTY_NAME + i;
512             operationInputDefinitionList.add(createMockOperationInputDefinition(
513                 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i));
514             addMappedPropertyAsComponentInput(component, mappedPropertyName);
515
516         }
517         return operationInputDefinitionList;
518     }
519
520     private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) {
521         InputDefinition componentInput = new InputDefinition();
522         componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]);
523         componentInput.setName(mappedPropertyName.split("\\.")[1]);
524         if (Objects.isNull(component.getInputs())) {
525             component.setInputs(new ArrayList<>());
526         }
527         component.getInputs().add(componentInput);
528     }
529
530     private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName,
531                                                                         String operationName,
532                                                                         int numOfOutputs) {
533         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
534         for (int i = 0; i < numOfOutputs; i++) {
535             operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName,
536                 OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i));
537         }
538         return operationOutputDefinitionList;
539     }
540
541     private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
542         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
543         operationInputDefinition.setName(name);
544         operationInputDefinition.setInputId(id);
545         operationInputDefinition.setType(inputTypes[index]);
546         operationInputDefinition.setRequired(index % 2 == 0);
547         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
548         List<String> toscaDefaultValues = new ArrayList<>();
549         toscaDefaultValues.add(SELF);
550         toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1));
551         toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues);
552         operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap));
553         operationInputDefinition.setSource("ServiceInput");
554         return operationInputDefinition;
555     }
556
557     private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName,
558                                                                           String outputName, int index) {
559         OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition();
560         operationInputDefinition.setName(outputName);
561         operationInputDefinition.setType(inputTypes[index]);
562         operationInputDefinition.setRequired(index % 2 == 0);
563         List<String> toscaDefaultValues = new ArrayList<>();
564         toscaDefaultValues.add(SELF);
565         toscaDefaultValues.add(interfaceName);
566         toscaDefaultValues.add(operationName);
567         toscaDefaultValues.add(outputName);
568         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
569         toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues);
570         return operationInputDefinition;
571     }
572
573     private void validateOperationInputs(final String mainYaml, int numOfInputsPerOp, String mappedOperationName) {
574         String nodeTypeKey = NODE_TYPE_NAME + ":";
575         String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
576             mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()
577                 + String.valueOf(numOfInputsPerOp).length());
578         YamlToObjectConverter objectConverter = new YamlToObjectConverter();
579         ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
580         Map<String, Object> interfaces = toscaNodeType.getInterfaces();
581         for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
582             Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class);
583             final Map<String, Object> operationsMap = interfaceDefinition.entrySet().stream()
584                 .filter(entry -> !INPUTS.getElementName().equals(entry.getKey()) &&
585                     !TYPE.getElementName().equals(entry.getKey()))
586                 .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
587             for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
588                 Object operationVal = operationEntry.getValue();
589                 if (operationVal instanceof Map) {
590                     //Since the inputs are mapped to output operations from only first interface so using that name
591                     validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName,
592                         operationVal);
593                 }
594             }
595         }
596     }
597
598     private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) {
599         Map<String, Object> operation = mapper.convertValue(operationVal, Map.class);
600         Map<String, Object> inputs = (Map<String, Object>) operation.get("inputs");
601         for (Map.Entry<String, Object> inputEntry : inputs.entrySet()) {
602             String[] inputNameSplit = inputEntry.getKey().split("_");
603             Map<String, Object> inputValueObject = (Map<String, Object>) inputEntry.getValue();
604             validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1],
605                 Integer.parseInt(inputNameSplit[2]), inputValueObject);
606         }
607     }
608
609
610     private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName,
611                                                               String inputType, int index,
612                                                               Map<String, Object> inputValueObject) {
613         if (inputValueObject.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) {
614             String mappedPropertyValue = MAPPED_PROPERTY_NAME + index;
615             List<String> mappedPropertyDefaultValue = (List<String>) inputValueObject
616                 .get(ToscaFunctions.GET_PROPERTY.getFunctionName());
617             assertEquals(2, mappedPropertyDefaultValue.size());
618             assertTrue(mappedPropertyDefaultValue.contains(SELF));
619             assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
620         } else if (inputValueObject.containsKey(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) {
621             List<String> mappedPropertyDefaultValue = (List<String>) inputValueObject
622                 .get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
623             assertEquals(4, mappedPropertyDefaultValue.size());
624             String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index;
625             assertTrue(mappedPropertyDefaultValue.contains(SELF));
626             assertTrue(mappedPropertyDefaultValue.contains(interfaceType));
627             assertTrue(mappedPropertyDefaultValue.contains(operationName));
628             assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
629         } else {
630             fail("Invalid Tosca function in default value. Allowed values: " + ToscaFunctions.GET_PROPERTY.getFunctionName() +
631                 "/" + ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
632         }
633     }
634
635     private void validateServiceProxyOperationInputs(String mainYaml) {
636         String nodeTypeKey = NODE_TYPE_NAME + ":";
637         String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
638             mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
639         YamlUtil yamlUtil = new YamlUtil();
640         ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class);
641         for (Object interfaceVal : toscaNodeType.getInterfaces().values()) {
642             Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
643             for (Object operationVal : interfaceDefinition.values()) {
644                 if (operationVal instanceof Map) {
645                     Map<String, Object> operation = (Map<String, Object>) mapper.convertValue(operationVal, Map.class);
646                     Map<String, Object> operationInputs = (Map<String, Object>) operation.get("inputs");
647                     for (Object inputValue : operationInputs.values()) {
648                         Map<String, Object> inputValueAsMap = (Map<String, Object>) inputValue;
649                         assertFalse(inputValueAsMap.keySet().contains("type"));
650                         assertFalse(inputValueAsMap.keySet().contains("required"));
651                         assertFalse(inputValueAsMap.keySet().contains("default"));
652                     }
653                 }
654             }
655         }
656     }
657
658     @Test
659     void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() {
660         Service service = new Service();
661         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
662         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
663         service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface");
664         service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>())));
665
666         Map<String, Object> resultMap = interfacesOperationsConverter.addInterfaceTypeElement(service,
667             Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard"));
668
669         assertTrue(MapUtils.isNotEmpty(resultMap)
670             && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface"));
671     }
672
673     @Test
674     void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() {
675         Service service = new Service();
676         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
677         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
678         service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null,
679             new HashMap<>())));
680
681         Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null, service.getInterfaces(), null, false);
682
683         assertTrue(MapUtils.isNotEmpty(resultMap)
684             && resultMap.containsKey("NotLocal"));
685     }
686
687     @Test
688     void testRemoveInterfacesWithoutOperationsEmptyMap() {
689         final Map<String, Object> interfaceMap = new HashMap<>();
690         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
691         assertNotNull(interfaceMap);
692         assertTrue(interfaceMap.isEmpty());
693     }
694
695     @Test
696     void testRemoveInterfacesWithoutOperationsNullParameter() {
697         final Map<String, Object> interfaceMap = null;
698         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
699         assertNull(interfaceMap);
700     }
701
702     @Test
703     void testRemoveInterfacesWithoutOperationsSuccess() {
704         final Map<String, Object> interfaceMap = new HashMap<>();
705         final ToscaInterfaceDefinition toscaInterfaceDefinition1 = new ToscaInterfaceDefinition();
706         interfaceMap.put("toscaInterfaceDefinition1", toscaInterfaceDefinition1);
707
708         final ToscaInterfaceDefinition toscaInterfaceDefinition2 = new ToscaInterfaceDefinition();
709         final Map<String, Object> toscaInterfaceDefinition2OperationMap = new HashMap<>();
710         toscaInterfaceDefinition2OperationMap.put("operation1", new Object());
711         toscaInterfaceDefinition2.setOperations(toscaInterfaceDefinition2OperationMap);
712         interfaceMap.put("toscaInterfaceDefinition2", toscaInterfaceDefinition2);
713
714         final Map<String, Object> toscaInterfaceDefinition3 = new HashMap<>();
715         interfaceMap.put("toscaInterfaceDefinition3", toscaInterfaceDefinition3);
716
717         final Map<String, Object> toscaInterfaceDefinition4 = new HashMap<>();
718         toscaInterfaceDefinition4.put("operation1", new Object());
719         interfaceMap.put("toscaInterfaceDefinition4", toscaInterfaceDefinition4);
720
721         final Object notAToscaInterfaceDefinition = new Object();
722         interfaceMap.put("notAToscaInterfaceDefinition", notAToscaInterfaceDefinition);
723
724         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
725         assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition1"));
726         assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition2"));
727         assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition3"));
728         assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition4"));
729         assertTrue(interfaceMap.containsKey("notAToscaInterfaceDefinition"));
730     }
731 }