Service Import - Input appearing as a property
[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, true);
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
389             .getInterfacesMap(component, null, component.getInterfaces(), null, false, true);
390         //then
391         assertTrue(interfacesMap.containsKey(interfaceName));
392         final Map<String, Object> actualInterfaceMap = (Map<String, Object>) interfacesMap.get(interfaceName);
393         assertTrue(actualInterfaceMap.containsKey(operation1DataDefinition.getName()));
394         final Map<String, Object> actualOperationMap = (Map<String, Object>) actualInterfaceMap.get(operation1DataDefinition.getName());
395         assertTrue(actualOperationMap.containsKey("implementation"));
396         final Map<String, Object> actualImplementationMap = (Map<String, Object>) actualOperationMap.get("implementation");
397         assertTrue(actualImplementationMap.containsKey("primary"));
398         final Map<String, Object> actualArtifactImplementationMap = (Map<String, Object>) actualImplementationMap.get("primary");
399         assertTrue(actualArtifactImplementationMap.containsKey("properties"));
400         final Map<String, Object> actualArtifactPropertiesMap = (Map<String, Object>) actualArtifactImplementationMap.get("properties");
401         assertEquals(1, actualArtifactPropertiesMap.keySet().size());
402         assertTrue(actualArtifactPropertiesMap.containsKey(listOfStringProperty.getName()));
403         final Object expectedListObject = actualArtifactPropertiesMap.get(listOfStringProperty.getName());
404         assertTrue(expectedListObject instanceof List);
405         final List<String> expectedListOfStringPropValue = (List<String>) expectedListObject;
406         assertEquals(3, expectedListOfStringPropValue.size());
407         assertTrue(expectedListOfStringPropValue.contains("value1"));
408         assertTrue(expectedListOfStringPropValue.contains("value2"));
409         assertTrue(expectedListOfStringPropValue.contains("value3"));
410     }
411
412
413     private void validateInterfaceInputs(final String yaml, final String interfaceName, final Map<String, InputDataDefinition> expectedInputMap) {
414         String fixedMainYaml = yaml;
415         final String nullString = "null";
416         if (fixedMainYaml.startsWith(nullString)) {
417             fixedMainYaml = yaml.substring(nullString.length());
418         }
419         if (fixedMainYaml.endsWith(nullString)) {
420             fixedMainYaml = fixedMainYaml.substring(0, fixedMainYaml.length() - nullString.length());
421         }
422         final Map<String, Object> yamlMap = new Yaml().load(fixedMainYaml);
423         final Map<String, Object> nodeTypesMap = (Map<String, Object>) yamlMap.get(NODE_TYPES.getElementName());
424         final Map<String, Object> node = (Map<String, Object>) nodeTypesMap.get(NODE_TYPE_NAME);
425         final Map<String, Object> interfacesMap = (Map<String, Object>) node.get(INTERFACES.getElementName());
426         final Map<String, Object> interface1 = (Map<String, Object>) interfacesMap.get(interfaceName);
427         final Map<String, Object> actualInputsMap = (Map<String, Object>) interface1.get(INPUTS.getElementName());
428         assertThat(actualInputsMap.keySet(), containsInAnyOrder(expectedInputMap.keySet().toArray()));
429         expectedInputMap.forEach((inputName, inputDataDefinition) -> {
430             final Map<String, Object> actualInput = (Map<String, Object>) actualInputsMap.get(inputName);
431             compareInputYaml(inputName, actualInput, inputDataDefinition);
432         });
433     }
434
435     private void compareInputYaml(final String inputName, final Map<String, Object> actualInput,
436                                   final InputDataDefinition expectedInput) {
437         final String msgFormat = "%s should be equal in input %s";
438         String field = TYPE.getElementName();
439         assertThat(String.format(msgFormat, field, inputName),
440             actualInput.get(field), equalTo(expectedInput.getType()));
441         field = DESCRIPTION.getElementName();
442         assertThat(String.format(msgFormat, field, inputName),
443             actualInput.get(field), equalTo(expectedInput.getDescription()));
444         field = REQUIRED.getElementName();
445         assertThat(String.format(msgFormat, field, inputName),
446             actualInput.get(field), equalTo(expectedInput.getRequired()));
447         field = DEFAULT.getElementName();
448         assertThat(String.format(msgFormat, field, inputName),
449             actualInput.get(field), equalTo(expectedInput.getDefaultValue()));
450     }
451
452     @FunctionalInterface
453     interface MainYamlAssertion extends Function<String, Boolean> {
454
455     }
456
457     private static Function<String, Boolean> all(MainYamlAssertion... fs) {
458         return s -> io.vavr.collection.List.of(fs).map(f -> f.apply(s)).fold(true, (l, r) -> l && r);
459     }
460
461     private static MainYamlAssertion containsNone(String... expected) {
462         return s -> io.vavr.collection.List.of(expected).map(e -> !s.contains(e)).fold(true, (l, r) -> l && r);
463     }
464
465     private static MainYamlAssertion containsAll(String... expected) {
466         return s -> io.vavr.collection.List.of(expected).map(s::contains).fold(true, (l, r) -> l && r);
467     }
468
469     private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps,
470                                           int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) {
471
472         addedInterface.setOperations(new HashMap<>());
473         for (int i = 0; i < numOfOps; i++) {
474             final OperationDataDefinition operation = new OperationDataDefinition();
475             operation.setName("name_for_op_" + i);
476             operation.setDescription("op " + i + " has description");
477             final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
478             implementation.setArtifactName(i + "_createBPMN.bpmn");
479             operation.setImplementation(implementation);
480             if (hasInputs) {
481                 operation.setInputs(createInputs(component, numOfInputsPerOp));
482             }
483             if (hasOutputs) {
484                 operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(),
485                     operation.getName(), numOfInputsPerOp));
486             }
487             addedInterface.getOperations().put(operation.getName(), operation);
488         }
489     }
490
491     private InputDataDefinition createInput(final String type, final String description, final Boolean isRequired,
492                                             final String defaultValue) {
493         final PropertyDataDefinition propertyDataDefinition = new PropertyDataDefinition();
494         if (type != null) {
495             propertyDataDefinition.setType(type);
496         }
497         if (description != null) {
498             propertyDataDefinition.setDescription(description);
499         }
500         if (defaultValue != null) {
501             propertyDataDefinition.setDefaultValue(defaultValue);
502         }
503         if (isRequired != null) {
504             propertyDataDefinition.setRequired(isRequired);
505         }
506         return new InputDataDefinition(propertyDataDefinition);
507     }
508
509     private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs) {
510         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
511         for (int i = 0; i < numOfInputs; i++) {
512             String mappedPropertyName = java.util.UUID.randomUUID() + "." + MAPPED_PROPERTY_NAME + i;
513             operationInputDefinitionList.add(createMockOperationInputDefinition(
514                 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i));
515             addMappedPropertyAsComponentInput(component, mappedPropertyName);
516
517         }
518         return operationInputDefinitionList;
519     }
520
521     private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) {
522         InputDefinition componentInput = new InputDefinition();
523         componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]);
524         componentInput.setName(mappedPropertyName.split("\\.")[1]);
525         if (Objects.isNull(component.getInputs())) {
526             component.setInputs(new ArrayList<>());
527         }
528         component.getInputs().add(componentInput);
529     }
530
531     private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName,
532                                                                         String operationName,
533                                                                         int numOfOutputs) {
534         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
535         for (int i = 0; i < numOfOutputs; i++) {
536             operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName,
537                 OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i));
538         }
539         return operationOutputDefinitionList;
540     }
541
542     private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
543         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
544         operationInputDefinition.setName(name);
545         operationInputDefinition.setInputId(id);
546         operationInputDefinition.setType(inputTypes[index]);
547         operationInputDefinition.setRequired(index % 2 == 0);
548         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
549         List<String> toscaDefaultValues = new ArrayList<>();
550         toscaDefaultValues.add(SELF);
551         toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1));
552         toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues);
553         operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap));
554         operationInputDefinition.setSource("ServiceInput");
555         return operationInputDefinition;
556     }
557
558     private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName,
559                                                                           String outputName, int index) {
560         OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition();
561         operationInputDefinition.setName(outputName);
562         operationInputDefinition.setType(inputTypes[index]);
563         operationInputDefinition.setRequired(index % 2 == 0);
564         List<String> toscaDefaultValues = new ArrayList<>();
565         toscaDefaultValues.add(SELF);
566         toscaDefaultValues.add(interfaceName);
567         toscaDefaultValues.add(operationName);
568         toscaDefaultValues.add(outputName);
569         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
570         toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues);
571         return operationInputDefinition;
572     }
573
574     private void validateOperationInputs(final String mainYaml, int numOfInputsPerOp, String mappedOperationName) {
575         String nodeTypeKey = NODE_TYPE_NAME + ":";
576         String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
577             mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()
578                 + String.valueOf(numOfInputsPerOp).length());
579         YamlToObjectConverter objectConverter = new YamlToObjectConverter();
580         ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
581         Map<String, Object> interfaces = toscaNodeType.getInterfaces();
582         for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
583             Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class);
584             final Map<String, Object> operationsMap = interfaceDefinition.entrySet().stream()
585                 .filter(entry -> !INPUTS.getElementName().equals(entry.getKey()) &&
586                     !TYPE.getElementName().equals(entry.getKey()))
587                 .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
588             for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
589                 Object operationVal = operationEntry.getValue();
590                 if (operationVal instanceof Map) {
591                     //Since the inputs are mapped to output operations from only first interface so using that name
592                     validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName,
593                         operationVal);
594                 }
595             }
596         }
597     }
598
599     private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) {
600         Map<String, Object> operation = mapper.convertValue(operationVal, Map.class);
601         Map<String, Object> inputs = (Map<String, Object>) operation.get("inputs");
602         for (Map.Entry<String, Object> inputEntry : inputs.entrySet()) {
603             String[] inputNameSplit = inputEntry.getKey().split("_");
604             Map<String, Object> inputValueObject = (Map<String, Object>) inputEntry.getValue();
605             validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1],
606                 Integer.parseInt(inputNameSplit[2]), inputValueObject);
607         }
608     }
609
610
611     private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName,
612                                                               String inputType, int index,
613                                                               Map<String, Object> inputValueObject) {
614         if (inputValueObject.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) {
615             String mappedPropertyValue = MAPPED_PROPERTY_NAME + index;
616             List<String> mappedPropertyDefaultValue = (List<String>) inputValueObject
617                 .get(ToscaFunctions.GET_PROPERTY.getFunctionName());
618             assertEquals(2, mappedPropertyDefaultValue.size());
619             assertTrue(mappedPropertyDefaultValue.contains(SELF));
620             assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
621         } else if (inputValueObject.containsKey(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) {
622             List<String> mappedPropertyDefaultValue = (List<String>) inputValueObject
623                 .get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
624             assertEquals(4, mappedPropertyDefaultValue.size());
625             String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index;
626             assertTrue(mappedPropertyDefaultValue.contains(SELF));
627             assertTrue(mappedPropertyDefaultValue.contains(interfaceType));
628             assertTrue(mappedPropertyDefaultValue.contains(operationName));
629             assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
630         } else {
631             fail("Invalid Tosca function in default value. Allowed values: " + ToscaFunctions.GET_PROPERTY.getFunctionName() +
632                 "/" + ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
633         }
634     }
635
636     private void validateServiceProxyOperationInputs(String mainYaml) {
637         String nodeTypeKey = NODE_TYPE_NAME + ":";
638         String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
639             mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
640         YamlUtil yamlUtil = new YamlUtil();
641         ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class);
642         for (Object interfaceVal : toscaNodeType.getInterfaces().values()) {
643             Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
644             for (Object operationVal : interfaceDefinition.values()) {
645                 if (operationVal instanceof Map) {
646                     Map<String, Object> operation = (Map<String, Object>) mapper.convertValue(operationVal, Map.class);
647                     Map<String, Object> operationInputs = (Map<String, Object>) operation.get("inputs");
648                     for (Object inputValue : operationInputs.values()) {
649                         Map<String, Object> inputValueAsMap = (Map<String, Object>) inputValue;
650                         assertFalse(inputValueAsMap.keySet().contains("type"));
651                         assertFalse(inputValueAsMap.keySet().contains("required"));
652                         assertFalse(inputValueAsMap.keySet().contains("default"));
653                     }
654                 }
655             }
656         }
657     }
658
659     @Test
660     void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() {
661         Service service = new Service();
662         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
663         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
664         service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface");
665         service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>())));
666
667         Map<String, Object> resultMap = interfacesOperationsConverter.addInterfaceTypeElement(service,
668             Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard"));
669
670         assertTrue(MapUtils.isNotEmpty(resultMap)
671             && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface"));
672     }
673
674     @Test
675     void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() {
676         Service service = new Service();
677         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
678         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
679         service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null,
680             new HashMap<>())));
681
682         Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null,
683             service.getInterfaces(), null, false, false);
684
685         assertTrue(MapUtils.isNotEmpty(resultMap)
686             && resultMap.containsKey("NotLocal"));
687     }
688
689     @Test
690     void testRemoveInterfacesWithoutOperationsEmptyMap() {
691         final Map<String, Object> interfaceMap = new HashMap<>();
692         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
693         assertNotNull(interfaceMap);
694         assertTrue(interfaceMap.isEmpty());
695     }
696
697     @Test
698     void testRemoveInterfacesWithoutOperationsNullParameter() {
699         final Map<String, Object> interfaceMap = null;
700         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
701         assertNull(interfaceMap);
702     }
703
704     @Test
705     void testRemoveInterfacesWithoutOperationsSuccess() {
706         final Map<String, Object> interfaceMap = new HashMap<>();
707         final ToscaInterfaceDefinition toscaInterfaceDefinition1 = new ToscaInterfaceDefinition();
708         interfaceMap.put("toscaInterfaceDefinition1", toscaInterfaceDefinition1);
709
710         final ToscaInterfaceDefinition toscaInterfaceDefinition2 = new ToscaInterfaceDefinition();
711         final Map<String, Object> toscaInterfaceDefinition2OperationMap = new HashMap<>();
712         toscaInterfaceDefinition2OperationMap.put("operation1", new Object());
713         toscaInterfaceDefinition2.setOperations(toscaInterfaceDefinition2OperationMap);
714         interfaceMap.put("toscaInterfaceDefinition2", toscaInterfaceDefinition2);
715
716         final Map<String, Object> toscaInterfaceDefinition3 = new HashMap<>();
717         interfaceMap.put("toscaInterfaceDefinition3", toscaInterfaceDefinition3);
718
719         final Map<String, Object> toscaInterfaceDefinition4 = new HashMap<>();
720         toscaInterfaceDefinition4.put("operation1", new Object());
721         interfaceMap.put("toscaInterfaceDefinition4", toscaInterfaceDefinition4);
722
723         final Object notAToscaInterfaceDefinition = new Object();
724         interfaceMap.put("notAToscaInterfaceDefinition", notAToscaInterfaceDefinition);
725
726         interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
727         assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition1"));
728         assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition2"));
729         assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition3"));
730         assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition4"));
731         assertTrue(interfaceMap.containsKey("notAToscaInterfaceDefinition"));
732     }
733 }