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