Fix VFs and Service not visible in SDC Dashboard
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / utils / InterfacesOperationsConverterTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.openecomp.sdc.be.tosca.utils;
18
19 import com.fasterxml.jackson.databind.DeserializationFeature;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.google.gson.Gson;
22 import org.apache.commons.collections4.MapUtils;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.sdc.tosca.services.YamlUtil;
30 import org.openecomp.sdc.be.DummyConfigurationManager;
31 import org.openecomp.sdc.be.config.Configuration;
32 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
34 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.InterfaceDefinition;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.Service;
43 import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
44 import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
45 import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter;
46 import org.openecomp.sdc.be.tosca.PropertyConvertor;
47 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
48 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
49 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
50 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
51 import org.openecomp.sdc.common.util.YamlToObjectConverter;
52
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.HashMap;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Objects;
59
60 import static org.mockito.Mockito.mock;
61 import static org.mockito.Mockito.when;
62 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF;
63 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement;
64 @RunWith(MockitoJUnitRunner.class)
65 public class InterfacesOperationsConverterTest {
66
67     private static final String MAPPED_PROPERTY_NAME = "mapped_property";
68     private static final String INPUT_NAME_PREFIX = "input_";
69     private static final String OUTPUT_NAME_PREFIX = "output_";
70     private static final String NODE_TYPE_NAME = "test";
71     private String[] inputTypes = {"string", "integer", "float", "boolean"};
72     private static ObjectMapper mapper;
73     private Configuration.EnvironmentContext environmentContext = mock(Configuration.EnvironmentContext.class);
74     DummyConfigurationManager dummyConfigurationManager = new DummyConfigurationManager();
75     private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
76
77     private InterfacesOperationsConverter interfacesOperationsConverter;
78
79     @BeforeClass
80     public static void setUp() {
81         mapper = new ObjectMapper();
82         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
83     }
84
85     @Before
86     public void setUpBeforeTest() {
87         interfacesOperationsConverter =
88                 new InterfacesOperationsConverter(new PropertyConvertor());
89     }
90
91     @Test
92     public void addInterfaceTypeElementToResource() {
93         Component component = new Resource();
94         component.setNormalizedName("normalizedComponentName");
95         component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
96         component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
97         component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
98         InterfaceDefinition addedInterface = new InterfaceDefinition();
99         addedInterface.setType("Local");
100         addOperationsToInterface(component, addedInterface, 5, 3, true, false);
101         final String interfaceType = "normalizedComponentName-interface";
102         component.setInterfaces(new HashMap<>());
103         component.getInterfaces().put(interfaceType, addedInterface);
104         final Map<String, Object> interfaceTypeElement =
105                 addInterfaceTypeElement(component, new ArrayList<>());
106
107         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
108                 interfacesOperationsConverter);
109         ToscaTemplate template = new ToscaTemplate("test");
110         template.setInterface_types(interfaceTypeElement);
111         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
112
113         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
114         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName"));
115     }
116
117     @Test
118     public void addInterfaceTypeElementToService() {
119         Component component = new Service();
120         component.setNormalizedName("normalizedServiceComponentName");
121         component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
122         component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
123         component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
124         InterfaceDefinition addedInterface = new InterfaceDefinition();
125         addedInterface.setType("Local");
126         addOperationsToInterface(component, addedInterface, 5, 3, true, false);
127         final String interfaceType = "normalizedServiceComponentName-interface";
128         component.setInterfaces(new HashMap<>());
129         component.getInterfaces().put(interfaceType, addedInterface);
130         final Map<String, Object> interfaceTypeElement =
131                 addInterfaceTypeElement(component, new ArrayList<>());
132
133         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
134                 interfacesOperationsConverter);
135         ToscaTemplate template = new ToscaTemplate("testService");
136         template.setInterface_types(interfaceTypeElement);
137         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
138
139         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
140         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName"));
141     }
142
143     @Test
144     public void addInterfaceDefinitionElementToResource() {
145         Component component = new Resource();
146         component.setNormalizedName("normalizedComponentName");
147         InterfaceDefinition addedInterface = new InterfaceDefinition();
148         addedInterface.setType("com.some.resource.or.other.resourceName");
149
150         addOperationsToInterface(component, addedInterface, 3, 2, true, false);
151         final String interfaceType = "normalizedComponentName-interface";
152         component.setInterfaces(new HashMap<>());
153         component.getInterfaces().put(interfaceType, addedInterface);
154         ToscaNodeType nodeType = new ToscaNodeType();
155         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
156
157         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
158                 interfacesOperationsConverter);
159         ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
160         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
161         nodeTypes.put(NODE_TYPE_NAME, nodeType);
162         template.setNode_types(nodeTypes);
163         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
164
165         String mainYaml = toscaRepresentation.getMainYaml();
166         Assert.assertFalse(mainYaml.contains("operations"));
167         Assert.assertTrue(mainYaml.contains("resourceName:"));
168         Assert.assertTrue(mainYaml.contains("inputs:"));
169         validateOperationInputs(mainYaml, 2, null);
170         Assert.assertFalse(mainYaml.contains("defaultp"));
171         Assert.assertTrue(mainYaml.contains("has description"));
172         Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
173         Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
174     }
175
176     @Test
177     public 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
189         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
190                 interfacesOperationsConverter);
191         ToscaTemplate template = new ToscaTemplate("testService");
192         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
193         nodeTypes.put(NODE_TYPE_NAME, nodeType);
194         template.setNode_types(nodeTypes);
195         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
196         String mainYaml = toscaRepresentation.getMainYaml();
197         Assert.assertFalse(mainYaml.contains("operations"));
198         Assert.assertTrue(mainYaml.contains("serviceName:"));
199         Assert.assertTrue(mainYaml.contains("inputs:"));
200         validateOperationInputs(mainYaml, 2, null);
201         Assert.assertFalse(mainYaml.contains("defaultp"));
202         Assert.assertTrue(mainYaml.contains("has description"));
203         Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
204         Assert.assertTrue(mainYaml.contains("com.some.service.or.other.serviceName"));
205     }
206
207
208     @Test
209     public void testGetInterfaceAsMapServiceProxy() {
210         Component component = new Resource();
211         component.setNormalizedName("normalizedComponentName");
212         InterfaceDefinition addedInterface = new InterfaceDefinition();
213         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
214         addedInterface.setType("com.some.resource.or.other.resourceName");
215         addOperationsToInterface(component, addedInterface, 3, 2, true, false);
216         final String interfaceType = "normalizedComponentName-interface";
217         component.setInterfaces(new HashMap<>());
218         component.getInterfaces().put(interfaceType, addedInterface);
219         Map<String, Object> interfacesMap = interfacesOperationsConverter
220                 .getInterfacesMap(component, null, component.getInterfaces(), null, false, true);
221         ToscaNodeType nodeType = new ToscaNodeType();
222         nodeType.setInterfaces(interfacesMap);
223         ToscaExportHandler handler = new ToscaExportHandler();
224         ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
225         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
226         nodeTypes.put(NODE_TYPE_NAME, nodeType);
227         template.setNode_types(nodeTypes);
228         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
229
230         String mainYaml = toscaRepresentation.getMainYaml();
231         Assert.assertFalse(mainYaml.contains("operations"));
232         Assert.assertTrue(mainYaml.contains("resourceName:"));
233         Assert.assertTrue(mainYaml.contains("inputs:"));
234         validateServiceProxyOperationInputs(mainYaml);
235         Assert.assertFalse(mainYaml.contains("defaultp"));
236         Assert.assertTrue(mainYaml.contains("has description"));
237         Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
238         Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
239     }
240
241     @Test
242     public void addInterfaceDefinitionElement_noInputs() {
243         Component component = new Resource();
244         component.setNormalizedName("normalizedComponentName");
245         InterfaceDefinition addedInterface = new InterfaceDefinition();
246         addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs");
247         addOperationsToInterface(component, addedInterface, 3, 3, false, false);
248         final String interfaceType = "normalizedComponentName-interface";
249         component.setInterfaces(new HashMap<>());
250         component.getInterfaces().put(interfaceType, addedInterface);
251         ToscaNodeType nodeType = new ToscaNodeType();
252         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false);
253
254         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
255                 interfacesOperationsConverter);
256         ToscaTemplate template = new ToscaTemplate("test");
257         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
258         nodeTypes.put("test", nodeType);
259         template.setNode_types(nodeTypes);
260         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
261
262         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
263         Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX));
264         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
265         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
266         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
267         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
268     }
269
270     @Test
271     public void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() {
272         String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
273         Component component = new Resource();
274         component.setNormalizedName("normalizedComponentName");
275         InterfaceDefinition addedInterface = new InterfaceDefinition();
276         addedInterface.setType(addedInterfaceType);
277         addOperationsToInterface(component, addedInterface, 2, 2, true, true);
278         addedInterface.getOperationsMap().values().stream()
279                 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
280                         "name_for_op_0"))
281                 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
282                         .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer"))
283                         .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType +
284                                 ".name_for_op_1.output_integer_1")));
285         component.setInterfaces(new HashMap<>());
286         component.getInterfaces().put(addedInterfaceType, addedInterface);
287         ToscaNodeType nodeType = new ToscaNodeType();
288         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
289
290         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
291                 interfacesOperationsConverter);
292         ToscaTemplate template = new ToscaTemplate("test");
293         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
294         nodeTypes.put("test", nodeType);
295         template.setNode_types(nodeTypes);
296         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
297         String mainYaml = toscaRepresentation.getMainYaml();
298         Assert.assertFalse(mainYaml.contains("operations"));
299         Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:"));
300         Assert.assertTrue(mainYaml.contains("inputs:"));
301         validateOperationInputs(mainYaml, 2, "name_for_op_1");
302     }
303
304     @Test
305     public void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() {
306         String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
307         Component component = new Resource();
308         component.setNormalizedName("normalizedComponentName");
309         InterfaceDefinition addedInterface = new InterfaceDefinition();
310         addedInterface.setType(addedInterfaceType);
311         addOperationsToInterface(component, addedInterface, 2, 2, true, true);
312         addedInterface.getOperationsMap().values().stream()
313                 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
314                         "name_for_op_0"))
315                 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
316                         .filter(opInputDef -> opInputDef.getName().contains("integer"))
317                         .forEach(opInputDef -> opInputDef.setInputId(
318                                 addedInterfaceType +".name_for_op_1.output_integer_1")));
319         //Mapping to operation from another interface
320         String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second";
321         InterfaceDefinition secondInterface = new InterfaceDefinition();
322         secondInterface.setType(secondInterfaceType);
323         addOperationsToInterface(component, secondInterface, 2, 2, true, true);
324         secondInterface.getOperationsMap().values().stream()
325                 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
326                         "name_for_op_0"))
327                 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
328                         .filter(opInputDef -> opInputDef.getName().contains("integer"))
329                         .forEach(opInputDef -> opInputDef.setInputId(
330                                 addedInterfaceType +".name_for_op_1.output_integer_1")));
331         component.setInterfaces(new HashMap<>());
332         component.getInterfaces().put(addedInterfaceType, addedInterface);
333         component.getInterfaces().put(secondInterfaceType, secondInterface);
334
335         ToscaNodeType nodeType = new ToscaNodeType();
336         interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
337
338         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
339                 interfacesOperationsConverter);
340         ToscaTemplate template = new ToscaTemplate("test");
341         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
342         nodeTypes.put("test", nodeType);
343         template.setNode_types(nodeTypes);
344         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
345
346         String mainYaml = toscaRepresentation.getMainYaml();
347         Assert.assertFalse(mainYaml.contains("operations"));
348         Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:"));
349         Assert.assertTrue(mainYaml.contains("inputs:"));
350         validateOperationInputs(mainYaml, 2, "name_for_op_1");
351     }
352
353     private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps,
354                                           int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) {
355
356         addedInterface.setOperations(new HashMap<>());
357         for (int i = 0; i < numOfOps; i++) {
358             final OperationDataDefinition operation = new OperationDataDefinition();
359             operation.setName("name_for_op_" + i);
360             operation.setDescription("op " + i + " has description");
361             final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
362             implementation.setArtifactName(i + "_createBPMN.bpmn");
363             operation.setImplementation(implementation);
364             if (hasInputs) {
365                 operation.setInputs(createInputs(component, numOfInputsPerOp));
366             }
367             if (hasOutputs) {
368                 operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(),
369                         operation.getName(), numOfInputsPerOp));
370             }
371             addedInterface.getOperations().put(operation.getName(), operation);
372         }
373     }
374
375     private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs) {
376         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
377         for (int i = 0; i < numOfInputs; i++) {
378             String mappedPropertyName = java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME + i;
379             operationInputDefinitionList.add(createMockOperationInputDefinition(
380                     INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i));
381             addMappedPropertyAsComponentInput(component, mappedPropertyName);
382
383         }
384         return operationInputDefinitionList;
385     }
386
387     private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) {
388         InputDefinition componentInput = new InputDefinition();
389         componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]);
390         componentInput.setName(mappedPropertyName.split("\\.")[1]);
391         if (Objects.isNull(component.getInputs())) {
392             component.setInputs(new ArrayList<>());
393         }
394         component.getInputs().add(componentInput);
395     }
396
397     private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName,
398                                                                         String operationName,
399                                                                         int numOfOutputs) {
400         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
401         for (int i = 0; i < numOfOutputs; i++) {
402             operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName,
403                     OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i));
404         }
405         return operationOutputDefinitionList;
406     }
407
408     private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
409         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
410         operationInputDefinition.setName(name);
411         operationInputDefinition.setInputId(id);
412         operationInputDefinition.setType(inputTypes[index]);
413         operationInputDefinition.setRequired(index % 2 == 0);
414         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
415         List<String> toscaDefaultValues = new ArrayList<>();
416         toscaDefaultValues.add(SELF);
417         toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1));
418         toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues);
419         operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap));
420         operationInputDefinition.setSource("ServiceInput");
421         return operationInputDefinition;
422     }
423
424     private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName,
425                                                                           String outputName, int index) {
426         OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition();
427         operationInputDefinition.setName(outputName);
428         operationInputDefinition.setType(inputTypes[index]);
429         operationInputDefinition.setRequired(index % 2 == 0);
430         Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
431         List<String> toscaDefaultValues = new ArrayList<>();
432         toscaDefaultValues.add(SELF);
433         toscaDefaultValues.add(interfaceName);
434         toscaDefaultValues.add(operationName);
435         toscaDefaultValues.add(outputName);
436         toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues);
437         return operationInputDefinition;
438     }
439
440     private void validateOperationInputs(String mainYaml, int numOfInputsPerOp, String mappedOperationName) {
441         String nodeTypeKey = NODE_TYPE_NAME + ":";
442         String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
443                 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()
444                         + String.valueOf(numOfInputsPerOp).length());
445         YamlToObjectConverter objectConverter = new YamlToObjectConverter();
446         ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
447         Map<String, Object> interfaces = toscaNodeType.getInterfaces();
448         for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
449             Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class);
450             for (Map.Entry<String, Object> operationEntry : interfaceDefinition.entrySet()) {
451                 Object operationVal = operationEntry.getValue();
452                 if (operationVal instanceof Map) {
453                     //Since the inputs are mapped to output operations from only first interface so using that name
454                     validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName,
455                             operationVal);
456                 }
457             }
458         }
459     }
460
461     private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) {
462         Map<String, Object> operation = mapper.convertValue(operationVal, Map.class);
463         Map<String, Object> inputs = (Map<String, Object>) operation.get("inputs");
464         for (Map.Entry<String, Object> inputEntry : inputs.entrySet()) {
465             String[] inputNameSplit = inputEntry.getKey().split("_");
466             Map<String, Object> inputValueObject = (Map<String, Object>) inputEntry.getValue();
467             Assert.assertEquals(inputNameSplit[1], inputValueObject.get("type"));
468             Boolean expectedIsRequired = Integer.parseInt(inputNameSplit[2]) % 2 == 0;
469             Assert.assertEquals(expectedIsRequired, inputValueObject.get("required"));
470             validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1],
471                     Integer.parseInt(inputNameSplit[2]), inputValueObject);
472         }
473     }
474
475
476     private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName,
477                                                               String inputType, int index,
478                                                               Map<String, Object> inputValueObject) {
479         Map<String, Object> mappedInputValue = (Map<String, Object>) inputValueObject.get("default");
480         if(mappedInputValue.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) {
481             String mappedPropertyValue = MAPPED_PROPERTY_NAME + index;
482             List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(ToscaFunctions.GET_PROPERTY.getFunctionName());
483             Assert.assertEquals(2, mappedPropertyDefaultValue.size());
484             Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF));
485             Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
486         } else if(mappedInputValue.containsKey(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) {
487             List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
488             Assert.assertEquals(4, mappedPropertyDefaultValue.size());
489             String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index;
490             Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF));
491             Assert.assertTrue(mappedPropertyDefaultValue.contains(interfaceType));
492             Assert.assertTrue(mappedPropertyDefaultValue.contains(operationName));
493             Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
494         } else {
495             Assert.fail("Invalid Tosca function in default value. Allowed values: "+ ToscaFunctions.GET_PROPERTY.getFunctionName() +
496                     "/"+ ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
497         }
498     }
499
500     private void validateServiceProxyOperationInputs(String mainYaml) {
501         String nodeTypeKey = NODE_TYPE_NAME + ":";
502         String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
503                 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
504         YamlUtil yamlUtil = new YamlUtil();
505         ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class);
506         for (Object interfaceVal : toscaNodeType.getInterfaces().values()) {
507             Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
508             for (Object operationVal : interfaceDefinition.values()) {
509                 if (operationVal instanceof Map) {
510                     Map<String, Object> operation = (Map<String, Object>) mapper.convertValue(operationVal, Map.class);
511                     Map<String, Object> operationInputs = (Map<String, Object>) operation.get("inputs");
512                     for (Object inputValue : operationInputs.values()) {
513                         Map<String, Object> inputValueAsMap = (Map<String, Object>) inputValue;
514                         Assert.assertFalse(inputValueAsMap.keySet().contains("type"));
515                         Assert.assertFalse(inputValueAsMap.keySet().contains("required"));
516                         Assert.assertFalse(inputValueAsMap.keySet().contains("default"));
517                     }
518                 }
519             }
520         }
521     }
522
523     @Test
524     public void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() {
525         Service service = new Service();
526         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
527         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
528         service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface");
529         service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>())));
530
531         Map<String, Object> resultMap = InterfacesOperationsConverter.addInterfaceTypeElement(service,
532                 Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard"));
533
534         Assert.assertTrue(MapUtils.isNotEmpty(resultMap)
535                 && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface"));
536     }
537
538     @Test
539     public void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() {
540         Service service = new Service();
541         service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
542         service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
543         service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null,
544                 new HashMap<>())));
545
546         Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null,
547                 service.getInterfaces(), null, false, false);
548
549         Assert.assertTrue(MapUtils.isNotEmpty(resultMap)
550                 && resultMap.containsKey("NotLocal"));
551     }
552 }