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