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