2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * 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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.tosca;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.allOf;
24 import static org.hamcrest.Matchers.anEmptyMap;
25 import static org.hamcrest.Matchers.containsInAnyOrder;
26 import static org.hamcrest.Matchers.containsString;
27 import static org.hamcrest.Matchers.equalTo;
28 import static org.hamcrest.Matchers.is;
29 import static org.hamcrest.Matchers.nullValue;
30 import static org.junit.jupiter.api.Assertions.assertEquals;
31 import static org.junit.jupiter.api.Assertions.assertFalse;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import static org.junit.jupiter.api.Assertions.fail;
34 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF;
35 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement;
36 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT;
37 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION;
38 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS;
39 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INTERFACES;
40 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TYPES;
41 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIRED;
42 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.google.gson.Gson;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.List;
52 import java.util.Map.Entry;
53 import java.util.Objects;
54 import java.util.function.Function;
55 import java.util.stream.Collectors;
56 import org.apache.commons.collections4.MapUtils;
57 import org.junit.jupiter.api.BeforeAll;
58 import org.junit.jupiter.api.BeforeEach;
59 import org.junit.jupiter.api.Test;
60 import org.onap.sdc.tosca.services.YamlUtil;
61 import org.openecomp.sdc.be.DummyConfigurationManager;
62 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
69 import org.openecomp.sdc.be.model.Component;
70 import org.openecomp.sdc.be.model.DataTypeDefinition;
71 import org.openecomp.sdc.be.model.InputDefinition;
72 import org.openecomp.sdc.be.model.InterfaceDefinition;
73 import org.openecomp.sdc.be.model.Resource;
74 import org.openecomp.sdc.be.model.Service;
75 import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
76 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
77 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
78 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
79 import org.openecomp.sdc.common.util.YamlToObjectConverter;
80 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
81 import org.yaml.snakeyaml.Yaml;
83 class InterfacesOperationsConverterTest {
85 private static final String MAPPED_PROPERTY_NAME = "mapped_property";
86 private static final String INPUT_NAME_PREFIX = "input_";
87 private static final String OUTPUT_NAME_PREFIX = "output_";
88 private static final String NODE_TYPE_NAME = "test";
89 private final String[] inputTypes = {"string", "integer", "float", "boolean"};
90 private static ObjectMapper mapper;
91 private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
93 private InterfacesOperationsConverter interfacesOperationsConverter;
96 public static void setUp() {
97 //initialize the static configuration manager
98 new DummyConfigurationManager();
99 mapper = new ObjectMapper();
100 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
104 public void setUpBeforeTest() {
105 interfacesOperationsConverter =
106 new InterfacesOperationsConverter(new PropertyConvertor());
110 void addInterfaceTypeElementToResource() {
111 Component component = new Resource();
112 component.setNormalizedName("normalizedComponentName");
113 component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
114 component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
115 component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
116 InterfaceDefinition addedInterface = new InterfaceDefinition();
117 addedInterface.setType("Local");
118 addOperationsToInterface(component, addedInterface, 5, 3, true, false);
119 final String interfaceType = "normalizedComponentName-interface";
120 component.setInterfaces(new HashMap<>());
121 component.getInterfaces().put(interfaceType, addedInterface);
122 final Map<String, Object> interfaceTypeElement =
123 addInterfaceTypeElement(component, new ArrayList<>());
125 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
126 interfacesOperationsConverter);
127 ToscaTemplate template = new ToscaTemplate("test");
128 template.setInterface_types(interfaceTypeElement);
129 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
132 containsAll("NodeTypeName"),
133 containsNone("operations")
134 ).apply(new String(toscaRepresentation.getMainYaml())));
138 void addInterfaceTypeElementToService() {
139 Component component = new Service();
140 component.setNormalizedName("normalizedServiceComponentName");
141 component.setComponentMetadataDefinition(new ServiceMetadataDefinition());
142 component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName");
143 component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName");
144 InterfaceDefinition addedInterface = new InterfaceDefinition();
145 addedInterface.setType("Local");
146 addOperationsToInterface(component, addedInterface, 5, 3, true, false);
147 final String interfaceType = "normalizedServiceComponentName-interface";
148 component.setInterfaces(new HashMap<>());
149 component.getInterfaces().put(interfaceType, addedInterface);
150 final Map<String, Object> interfaceTypeElement =
151 addInterfaceTypeElement(component, new ArrayList<>());
153 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
154 interfacesOperationsConverter);
155 ToscaTemplate template = new ToscaTemplate("testService");
156 template.setInterface_types(interfaceTypeElement);
157 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
160 containsAll("NodeTypeName"),
161 containsNone("operations")
162 ).apply(new String(toscaRepresentation.getMainYaml())));
166 void addInterfaceDefinitionElementToResource() {
167 Component component = new Resource();
168 component.setNormalizedName("normalizedComponentName");
169 InterfaceDefinition addedInterface = new InterfaceDefinition();
170 addedInterface.setType("com.some.resource.or.other.resourceName");
172 addOperationsToInterface(component, addedInterface, 3, 2, true, false);
173 final String interfaceType = "normalizedComponentName-interface";
174 component.setInterfaces(new HashMap<>());
175 component.getInterfaces().put(interfaceType, addedInterface);
176 ToscaNodeType nodeType = new ToscaNodeType();
177 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
179 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
180 interfacesOperationsConverter);
181 ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
182 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
183 nodeTypes.put(NODE_TYPE_NAME, nodeType);
184 template.setNode_types(nodeTypes);
185 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
187 String mainYaml = new String(toscaRepresentation.getMainYaml());
189 containsAll("resourceName:", "inputs:", "has description", MAPPED_PROPERTY_NAME, "com.some.resource.or.other.resourceName"),
190 containsNone("operations", "defaultp")
193 validateOperationInputs(mainYaml, 2, null);
197 void addInterfaceDefinitionElementToService() {
198 Component component = new Service();
199 component.setNormalizedName("normalizedServiceComponentName");
200 InterfaceDefinition addedInterface = new InterfaceDefinition();
201 addedInterface.setType("com.some.service.or.other.serviceName");
202 addOperationsToInterface(component, addedInterface, 3, 2, true, false);
203 final String interfaceType = "normalizedServiceComponentName-interface";
204 component.setInterfaces(new HashMap<>());
205 component.getInterfaces().put(interfaceType, addedInterface);
206 ToscaNodeType nodeType = new ToscaNodeType();
207 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
209 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
210 interfacesOperationsConverter);
211 ToscaTemplate template = new ToscaTemplate("testService");
212 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
213 nodeTypes.put(NODE_TYPE_NAME, nodeType);
214 template.setNode_types(nodeTypes);
215 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
216 String mainYaml = new String(toscaRepresentation.getMainYaml());
218 containsAll("serviceName", "inputs:", "has description", MAPPED_PROPERTY_NAME, "com.some.service.or.other.serviceName"),
219 containsNone("operations", "defaultp")
221 validateOperationInputs(mainYaml, 2, null);
226 void testGetInterfaceAsMapServiceProxy() {
227 Component component = new Resource();
228 component.setNormalizedName("normalizedComponentName");
229 InterfaceDefinition addedInterface = new InterfaceDefinition();
230 addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
231 addedInterface.setType("com.some.resource.or.other.resourceName");
232 addOperationsToInterface(component, addedInterface, 3, 2, true, false);
233 final String interfaceType = "normalizedComponentName-interface";
234 component.setInterfaces(new HashMap<>());
235 component.getInterfaces().put(interfaceType, addedInterface);
236 Map<String, Object> interfacesMap = interfacesOperationsConverter
237 .getInterfacesMap(component, null, component.getInterfaces(), null, false, true);
238 ToscaNodeType nodeType = new ToscaNodeType();
239 nodeType.setInterfaces(interfacesMap);
240 ToscaExportHandler handler = new ToscaExportHandler();
241 ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
242 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
243 nodeTypes.put(NODE_TYPE_NAME, nodeType);
244 template.setNode_types(nodeTypes);
245 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
247 String mainYaml = new String(toscaRepresentation.getMainYaml());
249 containsAll("resourceName:", "inputs:", "has description", MAPPED_PROPERTY_NAME, "com.some.resource.or.other.resourceName"),
250 containsNone("operations", "defaultp")
252 validateServiceProxyOperationInputs(mainYaml);
256 void addInterfaceDefinitionElement_noInputs() {
257 Component component = new Resource();
258 component.setNormalizedName("normalizedComponentName");
259 InterfaceDefinition addedInterface = new InterfaceDefinition();
260 addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs");
261 addOperationsToInterface(component, addedInterface, 3, 3, false, false);
262 final String interfaceType = "normalizedComponentName-interface";
263 component.setInterfaces(new HashMap<>());
264 component.getInterfaces().put(interfaceType, addedInterface);
265 ToscaNodeType nodeType = new ToscaNodeType();
266 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false);
268 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
269 interfacesOperationsConverter);
270 ToscaTemplate template = new ToscaTemplate("test");
271 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
272 nodeTypes.put("test", nodeType);
273 template.setNode_types(nodeTypes);
274 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
277 containsAll("resourceNameNoInputs", "has description", "com.some.resource.or.other.resourceName"),
278 containsNone("operations", INPUT_NAME_PREFIX, "defaultp")
279 ).apply(new String(toscaRepresentation.getMainYaml())));
283 void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() {
284 String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
285 Component component = new Resource();
286 component.setNormalizedName("normalizedComponentName");
287 InterfaceDefinition addedInterface = new InterfaceDefinition();
288 addedInterface.setType(addedInterfaceType);
289 addOperationsToInterface(component, addedInterface, 2, 2, true, true);
290 addedInterface.getOperationsMap().values().stream()
291 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
293 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
294 .filter(operationInputDefinition -> operationInputDefinition.getName().contains("integer"))
295 .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType +
296 ".name_for_op_1.output_integer_1")));
297 component.setInterfaces(new HashMap<>());
298 component.getInterfaces().put(addedInterfaceType, addedInterface);
299 ToscaNodeType nodeType = new ToscaNodeType();
300 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
302 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
303 interfacesOperationsConverter);
304 ToscaTemplate template = new ToscaTemplate("test");
305 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
306 nodeTypes.put("test", nodeType);
307 template.setNode_types(nodeTypes);
308 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
309 String mainYaml = new String(toscaRepresentation.getMainYaml());
311 containsAll("resourceNameInputMappedToOutput:", "inputs:"),
312 containsNone("operations")
314 validateOperationInputs(mainYaml, 2, "name_for_op_1");
318 void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() {
319 String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput";
320 Component component = new Resource();
321 component.setNormalizedName("normalizedComponentName");
322 InterfaceDefinition addedInterface = new InterfaceDefinition();
323 addedInterface.setType(addedInterfaceType);
324 addOperationsToInterface(component, addedInterface, 2, 2, true, true);
325 addedInterface.getOperationsMap().values().stream()
326 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
328 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
329 .filter(opInputDef -> opInputDef.getName().contains("integer"))
330 .forEach(opInputDef -> opInputDef.setInputId(
331 addedInterfaceType + ".name_for_op_1.output_integer_1")));
332 //Mapping to operation from another interface
333 String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second";
334 InterfaceDefinition secondInterface = new InterfaceDefinition();
335 secondInterface.setType(secondInterfaceType);
336 addOperationsToInterface(component, secondInterface, 2, 2, true, true);
337 secondInterface.getOperationsMap().values().stream()
338 .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase(
340 .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream()
341 .filter(opInputDef -> opInputDef.getName().contains("integer"))
342 .forEach(opInputDef -> opInputDef.setInputId(
343 addedInterfaceType + ".name_for_op_1.output_integer_1")));
344 component.setInterfaces(new HashMap<>());
345 component.getInterfaces().put(addedInterfaceType, addedInterface);
346 component.getInterfaces().put(secondInterfaceType, secondInterface);
348 ToscaNodeType nodeType = new ToscaNodeType();
349 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
351 ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
352 interfacesOperationsConverter);
353 ToscaTemplate template = new ToscaTemplate("test");
354 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
355 nodeTypes.put("test", nodeType);
356 template.setNode_types(nodeTypes);
357 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
359 String mainYaml = new String(toscaRepresentation.getMainYaml());
361 containsAll("resourceNameInputMappedToOutput:", "inputs:"),
362 containsNone("operations")
364 validateOperationInputs(mainYaml, 2, "name_for_op_1");
368 void interfaceWithInputsToscaExportTest() {
369 final Component component = new Service();
370 final InterfaceDefinition aInterfaceWithInput = new InterfaceDefinition();
371 final String interfaceName = "myInterfaceName";
372 final String interfaceType = "my.type." + interfaceName;
373 aInterfaceWithInput.setType(interfaceType);
374 final String input1Name = "input1";
375 final InputDataDefinition input1 = createInput("string", "input1 description", false, "input1 value");
376 final String input2Name = "input2";
377 final InputDataDefinition input2 = createInput("string", "input2 description", true, "input2 value");
378 final Map<String, InputDataDefinition> inputMap = new HashMap<>();
379 inputMap.put(input1Name, input1);
380 inputMap.put(input2Name, input2);
381 aInterfaceWithInput.setInputs(inputMap);
382 component.setInterfaces(new HashMap<>());
383 component.getInterfaces().put(interfaceName, aInterfaceWithInput);
384 final ToscaNodeType nodeType = new ToscaNodeType();
385 interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false);
386 final ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null,
387 interfacesOperationsConverter);
388 final ToscaTemplate template = new ToscaTemplate("testService");
389 final Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
390 nodeTypes.put(NODE_TYPE_NAME, nodeType);
391 template.setNode_types(nodeTypes);
392 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
393 final String toscaTemplateYaml = new String(toscaRepresentation.getMainYaml());
394 assertThat(toscaTemplateYaml,
395 allOf(containsString(INPUTS.getElementName() + ":"), containsString(input1Name), containsString(interfaceName)));
396 validateInterfaceInputs(toscaTemplateYaml, interfaceName, inputMap);
399 private void validateInterfaceInputs(final String yaml, final String interfaceName, final Map<String, InputDataDefinition> expectedInputMap) {
400 String fixedMainYaml = yaml;
401 final String nullString = "null";
402 if (fixedMainYaml.startsWith(nullString)) {
403 fixedMainYaml = yaml.substring(nullString.length());
405 if (fixedMainYaml.endsWith(nullString)) {
406 fixedMainYaml = fixedMainYaml.substring(0, fixedMainYaml.length() - nullString.length());
408 final Map<String, Object> yamlMap = (Map<String, Object>) new Yaml().load(fixedMainYaml);
409 final Map<String, Object> nodeTypesMap = (Map<String, Object>) yamlMap.get(NODE_TYPES.getElementName());
410 final Map<String, Object> node = (Map<String, Object>) nodeTypesMap.get(NODE_TYPE_NAME);
411 final Map<String, Object> interfacesMap = (Map<String, Object>) node.get(INTERFACES.getElementName());
412 final Map<String, Object> interface1 = (Map<String, Object>) interfacesMap.get(interfaceName);
413 final Map<String, Object> actualInputsMap = (Map<String, Object>) interface1.get(INPUTS.getElementName());
414 assertThat(actualInputsMap.keySet(), containsInAnyOrder(expectedInputMap.keySet().toArray()));
415 expectedInputMap.forEach((inputName, inputDataDefinition) -> {
416 final Map<String, Object> actualInput = (Map<String, Object>) actualInputsMap.get(inputName);
417 compareInputYaml(inputName, actualInput, inputDataDefinition);
421 private void compareInputYaml(final String inputName, final Map<String, Object> actualInput,
422 final InputDataDefinition expectedInput) {
423 final String msgFormat = "%s should be equal in input %s";
424 String field = TYPE.getElementName();
425 assertThat(String.format(msgFormat, field, inputName),
426 actualInput.get(field), equalTo(expectedInput.getType()));
427 field = DESCRIPTION.getElementName();
428 assertThat(String.format(msgFormat, field, inputName),
429 actualInput.get(field), equalTo(expectedInput.getDescription()));
430 field = REQUIRED.getElementName();
431 assertThat(String.format(msgFormat, field, inputName),
432 actualInput.get(field), equalTo(expectedInput.getRequired()));
433 field = DEFAULT.getElementName();
434 assertThat(String.format(msgFormat, field, inputName),
435 actualInput.get(field), equalTo(expectedInput.getDefaultValue()));
439 interface MainYamlAssertion extends Function<String, Boolean> {}
441 private static Function<String, Boolean> all(MainYamlAssertion... fs) {
442 return s -> io.vavr.collection.List.of(fs).map(f -> f.apply(s)).fold(true, (l, r) -> l && r);
445 private static MainYamlAssertion containsNone(String... expected) {
446 return s -> io.vavr.collection.List.of(expected).map(e -> !s.contains(e)).fold(true, (l, r) -> l && r);
449 private static MainYamlAssertion containsAll(String... expected) {
450 return s -> io.vavr.collection.List.of(expected).map(s::contains).fold(true, (l, r) -> l && r);
453 private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps,
454 int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) {
456 addedInterface.setOperations(new HashMap<>());
457 for (int i = 0; i < numOfOps; i++) {
458 final OperationDataDefinition operation = new OperationDataDefinition();
459 operation.setName("name_for_op_" + i);
460 operation.setDescription("op " + i + " has description");
461 final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
462 implementation.setArtifactName(i + "_createBPMN.bpmn");
463 operation.setImplementation(implementation);
465 operation.setInputs(createInputs(component, numOfInputsPerOp));
468 operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(),
469 operation.getName(), numOfInputsPerOp));
471 addedInterface.getOperations().put(operation.getName(), operation);
475 private InputDataDefinition createInput(final String type, final String description, final Boolean isRequired,
476 final String defaultValue) {
477 final PropertyDataDefinition propertyDataDefinition = new PropertyDataDefinition();
479 propertyDataDefinition.setType(type);
481 if (description != null) {
482 propertyDataDefinition.setDescription(description);
484 if (defaultValue != null) {
485 propertyDataDefinition.setDefaultValue(defaultValue);
487 if (isRequired != null) {
488 propertyDataDefinition.setRequired(isRequired);
490 return new InputDataDefinition(propertyDataDefinition);
493 private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs) {
494 ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
495 for (int i = 0; i < numOfInputs; i++) {
496 String mappedPropertyName = java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME + i;
497 operationInputDefinitionList.add(createMockOperationInputDefinition(
498 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i));
499 addMappedPropertyAsComponentInput(component, mappedPropertyName);
502 return operationInputDefinitionList;
505 private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) {
506 InputDefinition componentInput = new InputDefinition();
507 componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]);
508 componentInput.setName(mappedPropertyName.split("\\.")[1]);
509 if (Objects.isNull(component.getInputs())) {
510 component.setInputs(new ArrayList<>());
512 component.getInputs().add(componentInput);
515 private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName,
516 String operationName,
518 ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
519 for (int i = 0; i < numOfOutputs; i++) {
520 operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName,
521 OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i));
523 return operationOutputDefinitionList;
526 private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
527 OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
528 operationInputDefinition.setName(name);
529 operationInputDefinition.setInputId(id);
530 operationInputDefinition.setType(inputTypes[index]);
531 operationInputDefinition.setRequired(index % 2 == 0);
532 Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
533 List<String> toscaDefaultValues = new ArrayList<>();
534 toscaDefaultValues.add(SELF);
535 toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1));
536 toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues);
537 operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap));
538 operationInputDefinition.setSource("ServiceInput");
539 return operationInputDefinition;
542 private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName,
543 String outputName, int index) {
544 OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition();
545 operationInputDefinition.setName(outputName);
546 operationInputDefinition.setType(inputTypes[index]);
547 operationInputDefinition.setRequired(index % 2 == 0);
548 List<String> toscaDefaultValues = new ArrayList<>();
549 toscaDefaultValues.add(SELF);
550 toscaDefaultValues.add(interfaceName);
551 toscaDefaultValues.add(operationName);
552 toscaDefaultValues.add(outputName);
553 Map<String, List<String>> toscaDefaultValueMap = new HashMap<>();
554 toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues);
555 return operationInputDefinition;
558 private void validateOperationInputs(final String mainYaml, int numOfInputsPerOp, String mappedOperationName) {
559 String nodeTypeKey = NODE_TYPE_NAME + ":";
560 String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
561 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()
562 + String.valueOf(numOfInputsPerOp).length());
563 YamlToObjectConverter objectConverter = new YamlToObjectConverter();
564 ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
565 Map<String, Object> interfaces = toscaNodeType.getInterfaces();
566 for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
567 Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class);
568 final Map<String, Object> operationsMap = interfaceDefinition.entrySet().stream()
569 .filter(entry -> !INPUTS.getElementName().equals(entry.getKey()) &&
570 !TYPE.getElementName().equals(entry.getKey()))
571 .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
572 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
573 Object operationVal = operationEntry.getValue();
574 if (operationVal instanceof Map) {
575 //Since the inputs are mapped to output operations from only first interface so using that name
576 validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName,
583 private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) {
584 Map<String, Object> operation = mapper.convertValue(operationVal, Map.class);
585 Map<String, Object> inputs = (Map<String, Object>) operation.get("inputs");
586 for (Map.Entry<String, Object> inputEntry : inputs.entrySet()) {
587 String[] inputNameSplit = inputEntry.getKey().split("_");
588 Map<String, Object> inputValueObject = (Map<String, Object>) inputEntry.getValue();
589 validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1],
590 Integer.parseInt(inputNameSplit[2]), inputValueObject);
595 private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName,
596 String inputType, int index,
597 Map<String, Object> inputValueObject) {
598 if (inputValueObject.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) {
599 String mappedPropertyValue = MAPPED_PROPERTY_NAME + index;
600 List<String> mappedPropertyDefaultValue = (List<String>) inputValueObject
601 .get(ToscaFunctions.GET_PROPERTY.getFunctionName());
602 assertEquals(2, mappedPropertyDefaultValue.size());
603 assertTrue(mappedPropertyDefaultValue.contains(SELF));
604 assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
605 } else if (inputValueObject.containsKey(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) {
606 List<String> mappedPropertyDefaultValue = (List<String>) inputValueObject
607 .get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
608 assertEquals(4, mappedPropertyDefaultValue.size());
609 String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index;
610 assertTrue(mappedPropertyDefaultValue.contains(SELF));
611 assertTrue(mappedPropertyDefaultValue.contains(interfaceType));
612 assertTrue(mappedPropertyDefaultValue.contains(operationName));
613 assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue));
615 fail("Invalid Tosca function in default value. Allowed values: " + ToscaFunctions.GET_PROPERTY.getFunctionName() +
616 "/" + ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName());
620 private void validateServiceProxyOperationInputs(String mainYaml) {
621 String nodeTypeKey = NODE_TYPE_NAME + ":";
622 String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
623 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
624 YamlUtil yamlUtil = new YamlUtil();
625 ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class);
626 for (Object interfaceVal : toscaNodeType.getInterfaces().values()) {
627 Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
628 for (Object operationVal : interfaceDefinition.values()) {
629 if (operationVal instanceof Map) {
630 Map<String, Object> operation = (Map<String, Object>) mapper.convertValue(operationVal, Map.class);
631 Map<String, Object> operationInputs = (Map<String, Object>) operation.get("inputs");
632 for (Object inputValue : operationInputs.values()) {
633 Map<String, Object> inputValueAsMap = (Map<String, Object>) inputValue;
634 assertFalse(inputValueAsMap.keySet().contains("type"));
635 assertFalse(inputValueAsMap.keySet().contains("required"));
636 assertFalse(inputValueAsMap.keySet().contains("default"));
644 void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() {
645 Service service = new Service();
646 service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
647 service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
648 service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface");
649 service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>())));
651 Map<String, Object> resultMap = InterfacesOperationsConverter.addInterfaceTypeElement(service,
652 Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard"));
654 assertTrue(MapUtils.isNotEmpty(resultMap)
655 && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface"));
659 void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() {
660 Service service = new Service();
661 service.setComponentMetadataDefinition(new ServiceMetadataDefinition());
662 service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface");
663 service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null,
666 Map<String, Object> resultMap = interfacesOperationsConverter.getInterfacesMap(service, null,
667 service.getInterfaces(), null, false, false);
669 assertTrue(MapUtils.isNotEmpty(resultMap)
670 && resultMap.containsKey("NotLocal"));
674 void testRemoveInterfacesWithoutOperationsEmptyMap() {
675 final Map<String, Object> interfaceMap = new HashMap<>();
676 interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
677 assertThat(interfaceMap, is(anEmptyMap()));
681 void testRemoveInterfacesWithoutOperationsNullParameter() {
682 final Map<String, Object> interfaceMap = null;
683 interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
684 assertThat(interfaceMap, is(nullValue()));
688 void testRemoveInterfacesWithoutOperationsSuccess() {
689 final Map<String, Object> interfaceMap = new HashMap<>();
690 final ToscaInterfaceDefinition toscaInterfaceDefinition1 = new ToscaInterfaceDefinition();
691 interfaceMap.put("toscaInterfaceDefinition1", toscaInterfaceDefinition1);
693 final ToscaInterfaceDefinition toscaInterfaceDefinition2 = new ToscaInterfaceDefinition();
694 final Map<String, Object> toscaInterfaceDefinition2OperationMap = new HashMap<>();
695 toscaInterfaceDefinition2OperationMap.put("operation1", new Object());
696 toscaInterfaceDefinition2.setOperations(toscaInterfaceDefinition2OperationMap);
697 interfaceMap.put("toscaInterfaceDefinition2", toscaInterfaceDefinition2);
699 final Map<String, Object> toscaInterfaceDefinition3 = new HashMap<>();
700 interfaceMap.put("toscaInterfaceDefinition3", toscaInterfaceDefinition3);
702 final Map<String, Object> toscaInterfaceDefinition4 = new HashMap<>();
703 toscaInterfaceDefinition4.put("operation1", new Object());
704 interfaceMap.put("toscaInterfaceDefinition4", toscaInterfaceDefinition4);
706 final Object notAToscaInterfaceDefinition = new Object();
707 interfaceMap.put("notAToscaInterfaceDefinition", notAToscaInterfaceDefinition);
709 interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap);
710 assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition1"));
711 assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition2"));
712 assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition3"));
713 assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition4"));
714 assertTrue(interfaceMap.containsKey("notAToscaInterfaceDefinition"));