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.components.impl;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.aMapWithSize;
24 import static org.hamcrest.Matchers.containsInAnyOrder;
25 import static org.hamcrest.Matchers.equalTo;
26 import static org.hamcrest.Matchers.hasItems;
27 import static org.hamcrest.Matchers.hasSize;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.junit.jupiter.api.Assertions.fail;
30 import static org.mockito.Mockito.when;
32 import fj.data.Either;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.stream.Collectors;
42 import org.apache.commons.collections.MapUtils;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.extension.ExtendWith;
46 import org.mockito.Mock;
47 import org.mockito.junit.jupiter.MockitoExtension;
48 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
53 import org.openecomp.sdc.be.model.InterfaceDefinition;
54 import org.yaml.snakeyaml.Yaml;
56 @ExtendWith(MockitoExtension.class)
57 class InterfaceDefinitionHandlerTest {
60 private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
61 private InterfaceDefinitionHandler interfaceDefinitionHandler;
62 private InterfaceDefinition interfaceLifecyleStandard;
63 private static final Path TEST_RESOURCE_PATH = Paths.get("src/test/resources/interfaceDefinition");
64 private static final String CREATE_OPERATION = "create";
65 private static final String DELETE_OPERATION = "delete";
66 private static final String START_OPERATION = "start";
67 private static final String STOP_OPERATION = "stop";
68 private static final String INTERFACE_TYPE = "tosca.interfaces.node.lifecycle.Standard";
72 interfaceDefinitionHandler = new InterfaceDefinitionHandler(interfaceOperationBusinessLogic);
73 mockAllInterfacesLifecycle();
76 private void mockAllInterfacesLifecycle() {
77 final Map<String, InterfaceDefinition> interfaceTypes = new HashMap<>();
78 interfaceLifecyleStandard = new InterfaceDefinition();
79 interfaceLifecyleStandard.setType(INTERFACE_TYPE);
80 final Map<String, OperationDataDefinition> operations = new HashMap<>();
81 operations.put(CREATE_OPERATION, new OperationDataDefinition());
82 operations.put(START_OPERATION, new OperationDataDefinition());
83 operations.put(STOP_OPERATION, new OperationDataDefinition());
84 operations.put(DELETE_OPERATION, new OperationDataDefinition());
85 interfaceLifecyleStandard.setOperations(operations);
86 interfaceTypes.put(INTERFACE_TYPE, interfaceLifecyleStandard);
87 when(interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(interfaceTypes));
91 void testCreateWithLegacyOperationDeclarationSuccess() throws FileNotFoundException {
92 final Map<String, Object> load = loadYaml(Paths.get("interfaceDefinition-legacy.yaml"));
93 final InterfaceDefinition actualInterfaceDefinition = interfaceDefinitionHandler.create(load);
94 assertInterfaceDefinition(actualInterfaceDefinition);
98 void testCreateWithOperationSuccess() throws FileNotFoundException {
99 final Map<String, Object> load = loadYaml(Paths.get("interfaceDefinition-tosca1.3.yaml"));
100 final InterfaceDefinition actualInterfaceDefinition = interfaceDefinitionHandler.create(load);
101 assertInterfaceDefinition(actualInterfaceDefinition);
104 private void assertInterfaceDefinition(final InterfaceDefinition actualInterfaceDefinition) {
105 interfaceLifecyleStandard.getOperations().keySet().forEach(operation ->
106 assertTrue(actualInterfaceDefinition.hasOperation(operation)));
107 assertThat("Interface type should be as expected", actualInterfaceDefinition.getType(),
108 equalTo(actualInterfaceDefinition.getType()));
109 assertThat("Interface should contain 2 inputs", actualInterfaceDefinition.getInputs(), aMapWithSize(2));
110 assertThat("Interface inputs should be as expected", actualInterfaceDefinition.getInputs().keySet(),
111 containsInAnyOrder("stringInput", "actionInput"));
113 final InputDataDefinition stringInput = actualInterfaceDefinition.getInputs().get("stringInput");
114 assertInput("string", "stringInput description", true, "defaultValue", "aStatus", stringInput);
115 final InputDataDefinition actionInput = actualInterfaceDefinition.getInputs().get("actionInput");
116 assertInput("org.openecomp.resource.datatypes.Action", null, false, null, null, actionInput);
118 final OperationDataDefinition createOperation = actualInterfaceDefinition.getOperations().get(CREATE_OPERATION);
119 assertOperation("'camunda/serviceSelect'", createOperation);
121 final OperationDataDefinition deleteOperation = actualInterfaceDefinition.getOperations().get(DELETE_OPERATION);
122 assertOperation("'camunda/serviceDeselect'", deleteOperation);
124 final Map<String, String> expectedInputMap = new HashMap<>();
125 expectedInputMap.put("action", "org.openecomp.resource.datatypes.Action");
126 final OperationDataDefinition startOperation = actualInterfaceDefinition.getOperations().get(START_OPERATION);
127 assertOperation("'camunda/executeAction'", expectedInputMap, startOperation);
128 final OperationDataDefinition stopOperation = actualInterfaceDefinition.getOperations().get(STOP_OPERATION);
129 assertOperation("'camunda/executeAction'", expectedInputMap, stopOperation);
132 private void assertOperation(final String implementation, final OperationDataDefinition actualOperation) {
133 assertOperation(implementation, Collections.emptyMap(), actualOperation);
136 private void assertOperation(final String implementation, final Map<String, String> inputNameTypeMap,
137 final OperationDataDefinition actualOperation) {
138 final ArtifactDataDefinition artifactDefinition = actualOperation.getImplementation();
139 assertThat("Implementation should be as expected", artifactDefinition.getArtifactName(), equalTo(implementation));
140 final ListDataDefinition<OperationInputDefinition> inputListDataDef = actualOperation.getInputs();
141 if (inputListDataDef == null) {
142 if (MapUtils.isNotEmpty(inputNameTypeMap)) {
143 final String expectedInputNames = String.join(",", inputNameTypeMap.keySet());
144 fail(String.format("No inputs were found, but some inputs are expected: %s", expectedInputNames));
149 final String msgFormat = "Operation should have %s";
150 final List<OperationInputDefinition> inputList = inputListDataDef.getListToscaDataDefinition();
151 assertThat(String.format(msgFormat, "the expected quantity of inputs"), inputList, hasSize(inputNameTypeMap.size()));
153 final List<String> inputNames = inputList.stream()
154 .map(OperationInputDefinition::getName).collect(Collectors.toList());
156 assertThat(String.format(msgFormat, "the expected inputs"), inputNames,
157 hasItems(inputNameTypeMap.keySet().toArray(new String[0])));
160 private void assertInput(final String type, final String description, final Boolean required,
161 final String defaultValue, final String status, final InputDataDefinition actualInput) {
162 assertThat("Input type should be as expected", type, equalTo(actualInput.getType()));
163 assertThat("Input description should be as expected", description, equalTo(actualInput.getDescription()));
164 assertThat("Input required should be as expected", required, equalTo(required != null && required));
165 assertThat("Input default should be as expected", defaultValue, equalTo(actualInput.getDefaultValue()));
166 assertThat("Input status should be as expected", status, equalTo(actualInput.getStatus()));
169 private Map<String, Object> loadYaml(final Path filePathFromResource) throws FileNotFoundException {
170 final Path filePath = Paths.get(TEST_RESOURCE_PATH.toString(), filePathFromResource.toString());
171 final FileInputStream fileInputStream = new FileInputStream(filePath.toString());
172 return (Map<String, Object>) new Yaml().load(fileInputStream);