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.components.impl;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyBoolean;
21 import static org.mockito.ArgumentMatchers.anyMap;
22 import static org.mockito.ArgumentMatchers.anyObject;
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
27 import fj.data.Either;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
42 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
43 import org.openecomp.sdc.be.components.validation.UserValidations;
44 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
45 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
46 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
47 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
48 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
49 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.model.InputDefinition;
52 import org.openecomp.sdc.be.model.InterfaceDefinition;
53 import org.openecomp.sdc.be.model.Resource;
54 import org.openecomp.sdc.be.model.User;
55 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
56 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
57 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
58 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
59 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
60 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
61 import org.openecomp.sdc.exception.ResponseFormat;
62 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
64 @RunWith(MockitoJUnitRunner.class)
65 public class InterfaceOperationBusinessLogicTest {
68 private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
70 private UserValidations userValidations;
72 private ToscaOperationFacade toscaOperationFacade;
74 private ComponentsUtils componentsUtils;
76 private IGraphLockOperation graphLockOperation;
78 private TitanDao titanDao;
80 private InterfaceLifecycleOperation interfaceLifecycleOperation;
82 private InterfaceOperationValidation interfaceOperationValidation;
84 private InterfaceOperation interfaceOperation;
86 private ArtifactCassandraDao artifactCassandraDao;
88 private static final String resourceId = "resourceId";
89 private static final String interfaceId = "interfaceId";
90 private static final String operationId = "operationId";
91 private static final String inputId = "inputId";
92 private static final String RESOURCE_NAME = "Resource1";
93 private static final String operationId1 = "operationId1";
94 private static final String interfaceId1 = "interfaceId1";
97 private Resource resource;
100 public void setup() {
101 resource = new ResourceBuilder()
102 .setComponentType(ComponentTypeEnum.RESOURCE)
103 .setUniqueId(resourceId)
104 .setName(RESOURCE_NAME)
106 resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceId, operationId));
107 resource.setInputs(createInputsForResource());
110 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(true))).thenReturn(user);
111 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
112 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
113 when(interfaceOperationValidation.validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean())).thenReturn(Either.left(true));
114 when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
118 public void createInterfaceOperationTestOnExistingInterface() {
119 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
120 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
121 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
122 .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
123 Assert.assertTrue(interfaceOperationEither.isLeft());
127 public void createInterfaceOperationWithoutInterfaceTest() {
128 resource.getInterfaces().clear();
129 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
130 when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
131 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
132 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
133 .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
134 Assert.assertTrue(interfaceOperationEither.isLeft());
138 public void createInterfaceOperationWithoutInterfaceTestFail() {
139 resource.getInterfaces().clear();
140 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
141 when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
142 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
143 .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
144 Assert.assertTrue(interfaceOperationEither.isRight());
148 public void shouldFailWhenCreateInterfaceOperationFailedTest() {
149 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
150 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
151 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
152 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true).isRight());
156 public void updateInterfaceOperationTestWithArtifactSuccess() {
157 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
158 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
159 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
160 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
161 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
162 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
163 Assert.assertTrue(interfaceOperation.isLeft());
167 public void updateInterfaceOperationTestWithArtifactFailure() {
168 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
169 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
170 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
171 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
172 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
173 Assert.assertTrue(interfaceOperation.isRight());
177 public void updateInterfaceOperationTestWithoutArtifact() {
178 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
179 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
180 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
181 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
182 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
183 Assert.assertTrue(interfaceOperation.isLeft());
187 public void updateInterfaceOperationTestDoesntExist() {
188 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
189 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
190 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId1)), user, true);
191 Assert.assertTrue(interfaceOperation.isRight());
195 public void createInterfaceOperationTestFailOnException() {
196 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
197 when(interfaceOperation.updateInterfaces(any(), any())).thenThrow(new RuntimeException());
198 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
199 .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
200 Assert.assertTrue(interfaceOperationEither.isRight());
204 public void createInterfaceOperationTestFailOnFetchinGlobalTypes() {
205 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
206 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
207 .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
208 Assert.assertTrue(interfaceOperationEither.isRight());
212 public void createInterfaceOperationTestFailOnValidation() {
213 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
214 when(interfaceOperationValidation.validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean())).thenReturn(Either.right(new ResponseFormat()));
215 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
216 .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
217 Assert.assertTrue(interfaceOperationEither.isRight());
221 public void deleteInterfaceOperationTestInterfaceDoesntExist() {
222 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1, Collections.singletonList(operationId), user, true).isRight());
226 public void deleteInterfaceOperationTestOperationDoesntExist() {
227 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId1), user, true).isRight());
231 public void deleteInterfaceOperationTestSuccess() {
232 resource.getInterfaces().get(interfaceId).getOperations().putAll(InterfaceOperationTestUtils.createMockOperationMap(operationId1));
233 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
234 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
235 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isLeft());
239 public void shouldFailWhenDeleteInterfaceOperationFailedTest() {
240 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
241 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
242 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
246 public void deleteInterfaceOperationTestFailOnArtifactDeletion() {
247 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
248 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
252 public void deleteInterfaceOperationTestFailOnException() {
253 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenThrow(new RuntimeException());
254 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
258 public void deleteInterfaceTestSuccess() {
259 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
260 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
261 when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.left(interfaceId));
262 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isLeft());
266 public void deleteInterfaceTestFailure() {
267 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
268 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
269 when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
270 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
274 public void getInterfaceOperationTestInterfaceDoesntExist() {
275 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1, Collections.singletonList(operationId), user, true).isRight());
279 public void getInterfaceOperationTestOperationDoesntExist() {
280 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId1), user, true).isRight());
284 public void getInterfaceOperationTest() {
285 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isLeft());
289 public void getInterfaceOperationTestFailOnException() {
290 when(titanDao.commit()).thenThrow(new RuntimeException());
291 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
295 public void shouldFailWhenLockComponentFailedTest() {
296 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.NOT_FOUND);
297 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
298 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
299 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true).isRight());
303 public void shouldFailWhenGetComponentFailedTest() {
304 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
305 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
306 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
307 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true).isRight());
311 public void testGetAllInterfaceLifecycleTypes_TypesNotFound() {
312 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
313 Either<Map<String, InterfaceDefinition>, ResponseFormat> response = interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
314 Assert.assertTrue(response.isRight());
318 public void testGetAllInterfaceLifecycleTypes_Success() {
319 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
320 interfaceDefinition.setUniqueId(interfaceId);
321 interfaceDefinition.setType(interfaceId);
322 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
323 interfaceDefinitionMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition);
324 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(interfaceDefinitionMap));
325 Either<Map<String, InterfaceDefinition>, ResponseFormat> response = interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
326 Assert.assertEquals(response.left().value().size(),1);
329 private List<InputDefinition> createInputsForResource(){
330 InputDefinition inputDefinition = new InputDefinition();
331 inputDefinition.setName(inputId);
332 inputDefinition.setInputId(inputId);
333 inputDefinition.setUniqueId(inputId);
334 inputDefinition.setValue(inputId);
335 inputDefinition.setDefaultValue(inputId);
336 return Arrays.asList(inputDefinition);