2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.tosca.utils;
19 import com.fasterxml.jackson.databind.DeserializationFeature;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.google.gson.Gson;
22 import org.apache.commons.collections4.MapUtils;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.sdc.tosca.services.YamlUtil;
30 import org.openecomp.sdc.be.DummyConfigurationManager;
31 import org.openecomp.sdc.be.config.Configuration;
32 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
34 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.InterfaceDefinition;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.Service;
43 import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
44 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
45 import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter;
46 import org.openecomp.sdc.be.tosca.PropertyConvertor;
47 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
48 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
49 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
50 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
51 import org.openecomp.sdc.common.util.YamlToObjectConverter;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.HashMap;
56 import java.util.List;
58 import java.util.Objects;
60 import static org.mockito.Mockito.mock;
61 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF;
62 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement;
63 @RunWith(MockitoJUnitRunner.class)
64 public class InterfacesOperationsConverterTest {
66 private static final String MAPPED_PROPERTY_NAME = "mapped_property";
67 private static final String INPUT_NAME_PREFIX = "input_";
68 private static final String OUTPUT_NAME_PREFIX = "output_";
69 private static final String NODE_TYPE_NAME = "test";
70 private String[] inputTypes = {"string", "integer", "float", "boolean"};
71 private static ObjectMapper mapper;
72 private Configuration.EnvironmentContext environmentContext = mock(Configuration.EnvironmentContext.class);
73 DummyConfigurationManager dummyConfigurationManager = new DummyConfigurationManager();
74 private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
76 private InterfacesOperationsConverter interfacesOperationsConverter;
79 public static void setUp() {
80 mapper = new ObjectMapper();
81 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
85 public void setUpBeforeTest() {
86 interfacesOperationsConverter =
87 new InterfacesOperationsConverter(new PropertyConvertor());
91 public void addInterfaceTypeElementToResource() {
92 Component component = new Resource();
93 component.setNormalizedName("normalizedComponentName");
94 component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
95 component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
96 component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
97 InterfaceDefinition addedInterface = new InterfaceDefinition();
98 addedInterface.setType("Local");
99 addOperationsToInterface(component, addedInterface, 5, 3, true, false);
100 final String interfaceType = "normalizedComponentName-interface";
101 component.setInterfaces(new HashMap<>());
102 component.getInterfaces().put(interfaceType, addedInterface);
103 final Map<String, Object> interfaceTypeElement =
104 addInterfaceTypeElement(component, new ArrayList<>());
106 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
107 interfacesOperationsConverter);
108 ToscaTemplate template = new ToscaTemplate("test");
109 template.setInterface_types(interfaceTypeElement);
110 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
112 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
113 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName"));
117 public void addInterfaceTypeElementToService() {
118 Component component = new Service();
119 component.setNormalizedName("normalizedServiceComponentName");
120 component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
121 component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
122 component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
123 InterfaceDefinition addedInterface = new InterfaceDefinition();
124 addedInterface.setType("Local");
125 addOperationsToInterface(component, addedInterface, 5, 3, true, false);
126 final String interfaceType = "normalizedServiceComponentName-interface";
127 component.setInterfaces(new HashMap<>());
128 component.getInterfaces().put(interfaceType, addedInterface);
129 final Map<String, Object> interfaceTypeElement =
130 addInterfaceTypeElement(component, new ArrayList<>());
132 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
133 interfacesOperationsConverter);
134 ToscaTemplate template = new ToscaTemplate("testService");
135 template.setInterface_types(interfaceTypeElement);
136 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
138 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
139 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName"));
143 public void addInterfaceDefinitionElementToResource() {
144 Component component = new Resource();
145 component.setNormalizedName("normalizedComponentName");
146 InterfaceDefinition addedInterface = new InterfaceDefinition();
147 addedInterface.setType("com.some.resource.or.other.resourceName");
149 addOperationsToInterface(component, addedInterface, 3, 2, true, false);
150 final String interfaceType = "normalizedComponentName-interface";
151 component.setInterfaces(new HashMap<>());
152 component.getInterfaces().put(interfaceType, addedInterface);
153 ToscaNodeType nodeType = new ToscaNodeType();
154 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
156 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
157 interfacesOperationsConverter);
158 ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
159 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
160 nodeTypes.put(NODE_TYPE_NAME, nodeType);
161 template.setNode_types(nodeTypes);
162 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
164 String mainYaml = toscaRepresentation.getMainYaml();
165 Assert.assertFalse(mainYaml.contains("operations"));
166 Assert.assertTrue(mainYaml.contains("resourceName:"));
167 Assert.assertTrue(mainYaml.contains("inputs:"));
168 validateOperationInputs(mainYaml, 2, null);
169 Assert.assertFalse(mainYaml.contains("defaultp"));
170 Assert.assertTrue(mainYaml.contains("has description"));
171 Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
172 Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
176 public void addInterfaceDefinitionElementToService() {
177 Component component = new Service();
178 component.setNormalizedName("normalizedServiceComponentName");
179 InterfaceDefinition addedInterface = new InterfaceDefinition();
180 addedInterface.setType("com.some.service.or.other.serviceName");
181 addOperationsToInterface(component, addedInterface, 3, 2, true, false);
182 final String interfaceType = "normalizedServiceComponentName-interface";
183 component.setInterfaces(new HashMap<>());
184 component.getInterfaces().put(interfaceType, addedInterface);
185 ToscaNodeType nodeType = new ToscaNodeType();
186 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
188 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
189 interfacesOperationsConverter);
190 ToscaTemplate template = new ToscaTemplate("testService");
191 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
192 nodeTypes.put(NODE_TYPE_NAME, nodeType);
193 template.setNode_types(nodeTypes);
194 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
195 String mainYaml = toscaRepresentation.getMainYaml();
196 Assert.assertFalse(mainYaml.contains("operations"));
197 Assert.assertTrue(mainYaml.contains("serviceName:"));
198 Assert.assertTrue(mainYaml.contains("inputs:"));
199 validateOperationInputs(mainYaml, 2, null);
200 Assert.assertFalse(mainYaml.contains("defaultp"));
201 Assert.assertTrue(mainYaml.contains("has description"));
202 Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
203 Assert.assertTrue(mainYaml.contains("com.some.service.or.other.serviceName"));
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);
229 String mainYaml = toscaRepresentation.getMainYaml();
230 Assert.assertFalse(mainYaml.contains("operations"));
231 Assert.assertTrue(mainYaml.contains("resourceName:"));
232 Assert.assertTrue(mainYaml.contains("inputs:"));
233 validateServiceProxyOperationInputs(mainYaml);
234 Assert.assertFalse(mainYaml.contains("defaultp"));
235 Assert.assertTrue(mainYaml.contains("has description"));
236 Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
237 Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
241 public void addInterfaceDefinitionElement_noInputs() {
242 Component component = new Resource();
243 component.setNormalizedName("normalizedComponentName");
244 InterfaceDefinition addedInterface = new InterfaceDefinition();
245 addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs");
246 addOperationsToInterface(component, addedInterface, 3, 3, false, false);
247 final String interfaceType = "normalizedComponentName-interface";
248 component.setInterfaces(new HashMap<>());
249 component.getInterfaces().put(interfaceType, addedInterface);
250 ToscaNodeType nodeType = new ToscaNodeType();
251 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false);
253 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
254 interfacesOperationsConverter);
255 ToscaTemplate template = new ToscaTemplate("test");
256 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
257 nodeTypes.put("test", nodeType);
258 template.setNode_types(nodeTypes);
259 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
261 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
262 Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX));
263 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
264 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
265 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
266 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
270 public void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() {
271 String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
272 Component component = new Resource();
273 component.setNormalizedName("normalizedComponentName");
274 InterfaceDefinition addedInterface = new InterfaceDefinition();
275 addedInterface.setType(addedInterfaceType);
276 addOperationsToInterface(component, addedInterface, 2, 2, true, true);
277 addedInterface.getOperationsMap().values().stream()
278 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
280 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
281 .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer"))
282 .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType +
283 ".name_for_op_1.output_integer_1")));
284 component.setInterfaces(new HashMap<>());
285 component.getInterfaces().put(addedInterfaceType, addedInterface);
286 ToscaNodeType nodeType = new ToscaNodeType();
287 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
289 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
290 interfacesOperationsConverter);
291 ToscaTemplate template = new ToscaTemplate("test");
292 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
293 nodeTypes.put("test", nodeType);
294 template.setNode_types(nodeTypes);
295 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
296 String mainYaml = toscaRepresentation.getMainYaml();
297 Assert.assertFalse(mainYaml.contains("operations"));
298 Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:"));
299 Assert.assertTrue(mainYaml.contains("inputs:"));
300 validateOperationInputs(mainYaml, 2, "name_for_op_1");
304 public void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() {
305 String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
306 Component component = new Resource();
307 component.setNormalizedName("normalizedComponentName");
308 InterfaceDefinition addedInterface = new InterfaceDefinition();
309 addedInterface.setType(addedInterfaceType);
310 addOperationsToInterface(component, addedInterface, 2, 2, true, true);
311 addedInterface.getOperationsMap().values().stream()
312 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
314 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
315 .filter(opInputDef -> opInputDef.getName().contains("integer"))
316 .forEach(opInputDef -> opInputDef.setInputId(
317 addedInterfaceType +".name_for_op_1.output_integer_1")));
318 //Mapping to operation from another interface
319 String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second";
320 InterfaceDefinition secondInterface = new InterfaceDefinition();
321 secondInterface.setType(secondInterfaceType);
322 addOperationsToInterface(component, secondInterface, 2, 2, true, true);
323 secondInterface.getOperationsMap().values().stream()
324 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
326 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
327 .filter(opInputDef -> opInputDef.getName().contains("integer"))
328 .forEach(opInputDef -> opInputDef.setInputId(
329 addedInterfaceType +".name_for_op_1.output_integer_1")));
330 component.setInterfaces(new HashMap<>());
331 component.getInterfaces().put(addedInterfaceType, addedInterface);
332 component.getInterfaces().put(secondInterfaceType, secondInterface);
334 ToscaNodeType nodeType = new ToscaNodeType();
335 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
337 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null,
338 interfacesOperationsConverter);
339 ToscaTemplate template = new ToscaTemplate("test");
340 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
341 nodeTypes.put("test", nodeType);
342 template.setNode_types(nodeTypes);
343 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
345 String mainYaml = toscaRepresentation.getMainYaml();
346 Assert.assertFalse(mainYaml.contains("operations"));
347 Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:"));
348 Assert.assertTrue(mainYaml.contains("inputs:"));
349 validateOperationInputs(mainYaml, 2, "name_for_op_1");
352 private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps,
353 int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) {
355 addedInterface.setOperations(new HashMap<>());
356 for (int i = 0; i < numOfOps; i++) {
357 final OperationDataDefinition operation = new OperationDataDefinition();
358 operation.setName("name_for_op_" + i);
359 operation.setDescription("op " + i + " has description");
360 final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
361 implementation.setArtifactName(i + "_createBPMN.bpmn");
362 operation.setImplementation(implementation);
364 operation.setInputs(createInputs(component, numOfInputsPerOp));
367 operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(),
368 operation.getName(), numOfInputsPerOp));
370 addedInterface.getOperations().put(operation.getName(), operation);
374 private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs) {
375 ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
376 for (int i = 0; i < numOfInputs; i++) {
377 String mappedPropertyName = java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME + i;
378 operationInputDefinitionList.add(createMockOperationInputDefinition(
379 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i));
380 addMappedPropertyAsComponentInput(component, mappedPropertyName);
383 return operationInputDefinitionList;
386 private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) {
387 InputDefinition componentInput = new InputDefinition();
388 componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]);
389 componentInput.setName(mappedPropertyName.split("\\.")[1]);
390 if (Objects.isNull(component.getInputs())) {
391 component.setInputs(new ArrayList<>());
393 component.getInputs().add(componentInput);
396 private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName,
397 String operationName,
399 ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
400 for (int i = 0; i < numOfOutputs; i++) {
401 operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName,
402 OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i));
404 return operationOutputDefinitionList;
407 private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
408 OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
409 operationInputDefinition.setName(name);
410 operationInputDefinition.setInputId(id);
411 operationInputDefinition.setType(inputTypes[index]);
412 operationInputDefinition.setRequired(index % 2 == 0);
413 Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
414 List<String> toscaDefaultValues = new ArrayList<>();
415 toscaDefaultValues.add(SELF);
416 toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1));
417 toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues);
418 operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap));
419 operationInputDefinition.setSource("ServiceInput");
420 return operationInputDefinition;
423 private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName,
424 String outputName, int index) {
425 OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition();
426 operationInputDefinition.setName(outputName);
427 operationInputDefinition.setType(inputTypes[index]);
428 operationInputDefinition.setRequired(index % 2 == 0);
429 Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
430 List<String> toscaDefaultValues = new ArrayList<>();
431 toscaDefaultValues.add(SELF);
432 toscaDefaultValues.add(interfaceName);
433 toscaDefaultValues.add(operationName);
434 toscaDefaultValues.add(outputName);
435 toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues);
436 return operationInputDefinition;
439 private void validateOperationInputs(String mainYaml, int numOfInputsPerOp, String mappedOperationName) {
440 String nodeTypeKey = NODE_TYPE_NAME + ":";
441 String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
442 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()
443 + String.valueOf(numOfInputsPerOp).length());
444 YamlToObjectConverter objectConverter = new YamlToObjectConverter();
445 ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
446 Map<String, Object> interfaces = toscaNodeType.getInterfaces();
447 for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
448 Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class);
449 for (Map.Entry<String, Object> operationEntry : interfaceDefinition.entrySet()) {
450 Object operationVal = operationEntry.getValue();
451 if (operationVal instanceof Map) {
452 //Since the inputs are mapped to output operations from only first interface so using that name
453 validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName,
460 private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) {
461 Map<String, Object> operation = mapper.convertValue(operationVal, Map.class);
462 Map<String, Object> inputs = (Map<String, Object>) operation.get("inputs");
463 for (Map.Entry<String, Object> inputEntry : inputs.entrySet()) {
464 String[] inputNameSplit = inputEntry.getKey().split("_");
465 Map<String, Object> inputValueObject = (Map<String, Object>) inputEntry.getValue();
466 Assert.assertEquals(inputNameSplit[1], inputValueObject.get("type"));
467 Boolean expectedIsRequired = Integer.parseInt(inputNameSplit[2]) % 2 == 0;
468 Assert.assertEquals(expectedIsRequired, inputValueObject.get("required"));
469 validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1],
470 Integer.parseInt(inputNameSplit[2]), inputValueObject);
475 private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName,
476 String inputType, int index,
477 Map<String, Object> inputValueObject) {
478 Map<String, Object> mappedInputValue = (Map<String, Object>) inputValueObject.get("default");
479 if(mappedInputValue.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) {
480 String mappedPropertyValue = MAPPED_PROPERTY_NAME + index;
481 List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(ToscaFunctions.GET_PROPERTY.getFunctionName());
482 Assert.assertEquals(2, mappedPropertyDefaultValue.size());
483 Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF));
484 Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
485 } else if(mappedInputValue.containsKey(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) {
486 List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
487 Assert.assertEquals(4, mappedPropertyDefaultValue.size());
488 String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index;
489 Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF));
490 Assert.assertTrue(mappedPropertyDefaultValue.contains(interfaceType));
491 Assert.assertTrue(mappedPropertyDefaultValue.contains(operationName));
492 Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
494 Assert.fail("Invalid Tosca function in default value. Allowed values: "+ ToscaFunctions.GET_PROPERTY.getFunctionName() +
495 "/"+ ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
499 private void validateServiceProxyOperationInputs(String mainYaml) {
500 String nodeTypeKey = NODE_TYPE_NAME + ":";
501 String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
502 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
503 YamlUtil yamlUtil = new YamlUtil();
504 ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class);
505 for (Object interfaceVal : toscaNodeType.getInterfaces().values()) {
506 Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
507 for (Object operationVal : interfaceDefinition.values()) {
508 if (operationVal instanceof Map) {
509 Map<String, Object> operation = (Map<String, Object>) mapper.convertValue(operationVal, Map.class);
510 Map<String, Object> operationInputs = (Map<String, Object>) operation.get("inputs");
511 for (Object inputValue : operationInputs.values()) {
512 Map<String, Object> inputValueAsMap = (Map<String, Object>) inputValue;
513 Assert.assertFalse(inputValueAsMap.keySet().contains("type"));
514 Assert.assertFalse(inputValueAsMap.keySet().contains("required"));
515 Assert.assertFalse(inputValueAsMap.keySet().contains("default"));
523 public void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() {
524 Service service = new Service();
525 service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
526 service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
527 service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface");
528 service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>())));
530 Map<String, Object> resultMap = InterfacesOperationsConverter.addInterfaceTypeElement(service,
531 Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard"));
533 Assert.assertTrue(MapUtils.isNotEmpty(resultMap)
534 && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface"));
538 public void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() {
539 Service service = new Service();
540 service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
541 service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
542 service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null,
545 Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null,
546 service.getInterfaces(), null, false, false);
548 Assert.assertTrue(MapUtils.isNotEmpty(resultMap)
549 && resultMap.containsKey("NotLocal"));