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.exception.ResponseFormat;
61 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
63 @RunWith(MockitoJUnitRunner.class)
64 public class InterfaceOperationBusinessLogicTest {
66 private static final String resourceId = "resourceId";
67 private static final String interfaceId = "interfaceId";
68 private static final String operationId = "operationId";
69 private static final String inputId = "inputId";
70 private static final String RESOURCE_NAME = "Resource1";
71 private static final String operationId1 = "operationId1";
72 private static final String interfaceId1 = "interfaceId1";
74 private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
76 private UserValidations userValidations;
78 private ToscaOperationFacade toscaOperationFacade;
80 private ComponentsUtils componentsUtils;
82 private IGraphLockOperation graphLockOperation;
84 private TitanDao titanDao;
86 private InterfaceLifecycleOperation interfaceLifecycleOperation;
88 private InterfaceOperationValidation interfaceOperationValidation;
90 private InterfaceOperation interfaceOperation;
92 private ArtifactCassandraDao artifactCassandraDao;
94 private Resource resource;
98 resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId(resourceId)
99 .setName(RESOURCE_NAME).build();
100 resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceId, operationId));
101 resource.setInputs(createInputsForResource());
104 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(true))).thenReturn(user);
105 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
106 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
107 .thenReturn(StorageOperationStatus.OK);
108 when(interfaceOperationValidation
109 .validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean()))
110 .thenReturn(Either.left(true));
111 when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
114 private List<InputDefinition> createInputsForResource() {
115 InputDefinition inputDefinition = new InputDefinition();
116 inputDefinition.setName(inputId);
117 inputDefinition.setInputId(inputId);
118 inputDefinition.setUniqueId(inputId);
119 inputDefinition.setValue(inputId);
120 inputDefinition.setDefaultValue(inputId);
121 return Arrays.asList(inputDefinition);
125 public void createInterfaceOperationTestOnExistingInterface() {
126 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
127 .thenReturn(Either.left(Collections.emptyMap()));
128 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
129 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
130 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
131 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
132 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
135 Assert.assertTrue(interfaceOperationEither.isLeft());
139 public void createInterfaceOperationWithoutInterfaceTest() {
140 resource.getInterfaces().clear();
141 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
142 .thenReturn(Either.left(Collections.emptyMap()));
143 when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.left(
144 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
145 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
146 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
147 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
148 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
149 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
152 Assert.assertTrue(interfaceOperationEither.isLeft());
156 public void createInterfaceOperationWithoutInterfaceTestFail() {
157 resource.getInterfaces().clear();
158 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
159 .thenReturn(Either.left(Collections.emptyMap()));
160 when(interfaceOperation.addInterfaces(any(), any()))
161 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
162 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
163 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
164 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
167 Assert.assertTrue(interfaceOperationEither.isRight());
171 public void shouldFailWhenCreateInterfaceOperationFailedTest() {
172 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
173 .thenReturn(Either.left(Collections.emptyMap()));
174 when(interfaceOperation.updateInterfaces(any(), any()))
175 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
176 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
177 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)),
178 user, true).isRight());
182 public void updateInterfaceOperationTestWithArtifactSuccess() {
183 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
184 .thenReturn(Either.left(Collections.emptyMap()));
185 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
186 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
187 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
188 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
189 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
190 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
191 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
194 Assert.assertTrue(interfaceOperation.isLeft());
198 public void updateInterfaceOperationTestWithArtifactFailure() {
199 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
200 .thenReturn(Either.left(Collections.emptyMap()));
201 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
202 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
203 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
204 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
205 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
208 Assert.assertTrue(interfaceOperation.isRight());
212 public void updateInterfaceOperationTestWithoutArtifact() {
213 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
214 .thenReturn(Either.left(Collections.emptyMap()));
215 when(artifactCassandraDao.getCountOfArtifactById(any(String.class)))
216 .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
217 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
218 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
219 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
220 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
221 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
224 Assert.assertTrue(interfaceOperation.isLeft());
228 public void updateInterfaceOperationTestDoesntExist() {
229 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
230 .thenReturn(Either.left(Collections.emptyMap()));
231 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
232 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
233 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
236 Assert.assertTrue(interfaceOperation.isRight());
240 public void createInterfaceOperationTestFailOnException() {
241 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
242 .thenReturn(Either.left(Collections.emptyMap()));
243 when(interfaceOperation.updateInterfaces(any(), any())).thenThrow(new RuntimeException());
244 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
245 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
246 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
249 Assert.assertTrue(interfaceOperationEither.isRight());
253 public void createInterfaceOperationTestFailOnFetchinGlobalTypes() {
254 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
255 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
256 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
257 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
258 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
261 Assert.assertTrue(interfaceOperationEither.isRight());
265 public void createInterfaceOperationTestFailOnValidation() {
266 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
267 .thenReturn(Either.left(Collections.emptyMap()));
268 when(interfaceOperationValidation
269 .validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean()))
270 .thenReturn(Either.right(new ResponseFormat()));
271 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
272 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
273 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
276 Assert.assertTrue(interfaceOperationEither.isRight());
280 public void deleteInterfaceOperationTestInterfaceDoesntExist() {
281 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
282 Collections.singletonList(operationId), user, true).isRight());
286 public void deleteInterfaceOperationTestOperationDoesntExist() {
287 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
288 Collections.singletonList(operationId1), user, true).isRight());
292 public void deleteInterfaceOperationTestSuccess() {
293 resource.getInterfaces().get(interfaceId).getOperations()
294 .putAll(InterfaceOperationTestUtils.createMockOperationMap(operationId1));
295 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
296 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
297 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
298 Collections.singletonList(operationId), user, true).isLeft());
302 public void shouldFailWhenDeleteInterfaceOperationFailedTest() {
303 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
304 when(interfaceOperation.updateInterfaces(any(), any()))
305 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
306 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
307 Collections.singletonList(operationId), user, true).isRight());
311 public void deleteInterfaceOperationTestFailOnArtifactDeletion() {
312 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
313 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
314 Collections.singletonList(operationId), user, true).isRight());
318 public void deleteInterfaceOperationTestFailOnException() {
319 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenThrow(new RuntimeException());
320 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
321 Collections.singletonList(operationId), user, true).isRight());
325 public void deleteInterfaceTestSuccess() {
326 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
327 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
328 when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.left(interfaceId));
329 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
330 Collections.singletonList(operationId), user, true).isLeft());
334 public void deleteInterfaceTestFailure() {
335 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
336 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
337 when(interfaceOperation.deleteInterface(any(), any()))
338 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
339 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
340 Collections.singletonList(operationId), user, true).isRight());
344 public void getInterfaceOperationTestInterfaceDoesntExist() {
345 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
346 Collections.singletonList(operationId), user, true).isRight());
350 public void getInterfaceOperationTestOperationDoesntExist() {
351 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
352 Collections.singletonList(operationId1), user, true).isRight());
356 public void getInterfaceOperationTest() {
357 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
358 Collections.singletonList(operationId), user, true).isLeft());
362 public void getInterfaceOperationTestFailOnException() {
363 when(titanDao.commit()).thenThrow(new RuntimeException());
364 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
365 Collections.singletonList(operationId), user, true).isRight());
369 public void shouldFailWhenLockComponentFailedTest() {
370 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
371 .thenReturn(StorageOperationStatus.NOT_FOUND);
372 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
373 Collections.singletonList(operationId), user, true).isRight());
374 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
375 Collections.singletonList(operationId), user, true).isRight());
376 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
377 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)),
378 user, true).isRight());
382 public void shouldFailWhenGetComponentFailedTest() {
383 when(toscaOperationFacade.getToscaElement(resourceId))
384 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
385 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
386 Collections.singletonList(operationId), user, true).isRight());
387 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
388 Collections.singletonList(operationId), user, true).isRight());
389 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
390 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)),
391 user, true).isRight());
395 public void testGetAllInterfaceLifecycleTypes_TypesNotFound() {
396 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
397 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
398 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
399 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
400 Assert.assertTrue(response.isRight());
404 public void testGetAllInterfaceLifecycleTypes_Success() {
405 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
406 interfaceDefinition.setUniqueId(interfaceId);
407 interfaceDefinition.setType(interfaceId);
408 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
409 interfaceDefinitionMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition);
410 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
411 .thenReturn(Either.left(interfaceDefinitionMap));
412 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
413 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
414 Assert.assertEquals(1, response.left().value().size());