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