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 java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
33 import fj.data.Either;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
43 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
44 import org.openecomp.sdc.be.components.validation.UserValidations;
45 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
46 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
47 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
48 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
49 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
50 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
51 import org.openecomp.sdc.be.impl.ComponentsUtils;
52 import org.openecomp.sdc.be.model.InputDefinition;
53 import org.openecomp.sdc.be.model.InterfaceDefinition;
54 import org.openecomp.sdc.be.model.Resource;
55 import org.openecomp.sdc.be.model.User;
56 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
57 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
58 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
59 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
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 {
67 private static final String resourceId = "resourceId";
68 private static final String interfaceId = "interfaceId";
69 private static final String operationId = "operationId";
70 private static final String inputId = "inputId";
71 private static final String RESOURCE_NAME = "Resource1";
72 private static final String operationId1 = "operationId1";
73 private static final String interfaceId1 = "interfaceId1";
74 private static final String operationName = "createOperation";
77 private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
79 private UserValidations userValidations;
81 private ToscaOperationFacade toscaOperationFacade;
83 private ComponentsUtils componentsUtils;
85 private IGraphLockOperation graphLockOperation;
87 private TitanDao titanDao;
89 private InterfaceLifecycleOperation interfaceLifecycleOperation;
91 private InterfaceOperationValidation interfaceOperationValidation;
93 private InterfaceOperation interfaceOperation;
95 private ArtifactCassandraDao artifactCassandraDao;
97 private Resource resource;
100 public void setup() {
101 resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId(resourceId)
102 .setName(RESOURCE_NAME).build();
103 resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceId, operationId,
105 resource.setInputs(createInputsForResource());
108 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(true))).thenReturn(user);
109 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
110 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
111 .thenReturn(StorageOperationStatus.OK);
112 when(interfaceOperationValidation
113 .validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean()))
114 .thenReturn(Either.left(true));
115 when(interfaceOperationValidation
116 .validateDeleteOperationContainsNoMappedOutput(anyObject(), anyObject(), any()))
117 .thenReturn(Either.left(true));
118 when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
121 private List<InputDefinition> createInputsForResource() {
122 InputDefinition inputDefinition = new InputDefinition();
123 inputDefinition.setName(inputId);
124 inputDefinition.setInputId(inputId);
125 inputDefinition.setUniqueId(inputId);
126 inputDefinition.setValue(inputId);
127 inputDefinition.setDefaultValue(inputId);
128 return Arrays.asList(inputDefinition);
132 public void createInterfaceOperationTestOnExistingInterface() {
133 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
134 .thenReturn(Either.left(Collections.emptyMap()));
135 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
136 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
137 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
138 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
139 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
140 operationId, operationName)),
142 Assert.assertTrue(interfaceOperationEither.isLeft());
146 public void createInterfaceOperationWithoutInterfaceTest() {
147 resource.getInterfaces().clear();
148 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
149 .thenReturn(Either.left(Collections.emptyMap()));
150 when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.left(
151 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
152 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
153 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
154 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
155 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
156 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
157 operationId, operationName)),
159 Assert.assertTrue(interfaceOperationEither.isLeft());
163 public void createInterfaceOperationWithoutInterfaceTestFail() {
164 resource.getInterfaces().clear();
165 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
166 .thenReturn(Either.left(Collections.emptyMap()));
167 when(interfaceOperation.addInterfaces(any(), any()))
168 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
169 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
170 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
171 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
172 operationId, operationName)),
174 Assert.assertTrue(interfaceOperationEither.isRight());
178 public void shouldFailWhenCreateInterfaceOperationFailedTest() {
179 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
180 .thenReturn(Either.left(Collections.emptyMap()));
181 when(interfaceOperation.updateInterfaces(any(), any()))
182 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
183 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
184 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)),
185 user, true).isRight());
189 public void updateInterfaceOperationTestWithArtifactSuccess() {
190 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
191 .thenReturn(Either.left(Collections.emptyMap()));
192 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
193 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
194 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
195 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
196 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
197 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
198 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
199 operationId, operationName)),
201 Assert.assertTrue(interfaceOperation.isLeft());
205 public void updateInterfaceOperationTestWithArtifactFailure() {
206 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
207 .thenReturn(Either.left(Collections.emptyMap()));
208 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
209 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
210 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
211 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
212 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
213 operationId, operationName)),
215 Assert.assertTrue(interfaceOperation.isRight());
219 public void updateInterfaceOperationTestWithoutArtifact() {
220 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
221 .thenReturn(Either.left(Collections.emptyMap()));
222 when(artifactCassandraDao.getCountOfArtifactById(any(String.class)))
223 .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
224 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
225 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
226 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
227 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
228 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
229 operationId, operationName)),
231 Assert.assertTrue(interfaceOperation.isLeft());
235 public void updateInterfaceOperationTestDoesntExist() {
236 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
237 .thenReturn(Either.left(Collections.emptyMap()));
238 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
239 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
240 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
241 operationId, operationName)),
243 Assert.assertTrue(interfaceOperation.isRight());
247 public void createInterfaceOperationTestFailOnException() {
248 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
249 .thenReturn(Either.left(Collections.emptyMap()));
250 when(interfaceOperation.updateInterfaces(any(), any())).thenThrow(new RuntimeException());
251 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
252 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
253 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
254 operationId, operationName)),
256 Assert.assertTrue(interfaceOperationEither.isRight());
260 public void createInterfaceOperationTestFailOnFetchinGlobalTypes() {
261 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
262 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
263 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
264 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
265 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
266 operationId, operationName)),
268 Assert.assertTrue(interfaceOperationEither.isRight());
272 public void createInterfaceOperationTestFailOnValidation() {
273 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
274 .thenReturn(Either.left(Collections.emptyMap()));
275 when(interfaceOperationValidation
276 .validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean()))
277 .thenReturn(Either.right(new ResponseFormat()));
278 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
279 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
280 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
281 operationId, operationName)),
283 Assert.assertTrue(interfaceOperationEither.isRight());
287 public void deleteInterfaceOperationTestInterfaceDoesntExist() {
288 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
289 Collections.singletonList(operationId), user, true).isRight());
293 public void deleteInterfaceOperationTestOperationDoesntExist() {
294 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
295 Collections.singletonList(operationId1), user, true).isRight());
299 public void deleteInterfaceOperationTestSuccess() {
300 resource.getInterfaces().get(interfaceId).getOperations()
301 .putAll(InterfaceOperationTestUtils.createMockOperationMap(operationId1, operationName));
302 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
303 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
304 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
305 Collections.singletonList(operationId), user, true).isLeft());
309 public void shouldFailWhenDeleteInterfaceOperationFailedTest() {
310 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
311 when(interfaceOperation.updateInterfaces(any(), any()))
312 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
313 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
314 Collections.singletonList(operationId), user, true).isRight());
318 public void deleteInterfaceOperationTestFailOnArtifactDeletion() {
319 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
320 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
321 Collections.singletonList(operationId), user, true).isRight());
325 public void deleteInterfaceOperationTestFailOnException() {
326 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenThrow(new RuntimeException());
327 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
328 Collections.singletonList(operationId), user, true).isRight());
332 public void deleteInterfaceTestSuccess() {
333 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
334 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
335 when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.left(interfaceId));
336 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
337 Collections.singletonList(operationId), user, true).isLeft());
341 public void deleteInterfaceTestFailure() {
342 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
343 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
344 when(interfaceOperation.deleteInterface(any(), any()))
345 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
346 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
347 Collections.singletonList(operationId), user, true).isRight());
351 public void getInterfaceOperationTestInterfaceDoesntExist() {
352 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
353 Collections.singletonList(operationId), user, true).isRight());
357 public void getInterfaceOperationTestOperationDoesntExist() {
358 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
359 Collections.singletonList(operationId1), user, true).isRight());
363 public void getInterfaceOperationTest() {
364 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
365 Collections.singletonList(operationId), user, true).isLeft());
369 public void getInterfaceOperationTestFailOnException() {
370 when(titanDao.commit()).thenThrow(new RuntimeException());
371 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
372 Collections.singletonList(operationId), user, true).isRight());
376 public void shouldFailWhenLockComponentFailedTest() {
377 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
378 .thenReturn(StorageOperationStatus.NOT_FOUND);
379 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
380 Collections.singletonList(operationId), user, true).isRight());
381 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
382 Collections.singletonList(operationId), user, true).isRight());
383 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
384 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)),
385 user, true).isRight());
389 public void shouldFailWhenGetComponentFailedTest() {
390 when(toscaOperationFacade.getToscaElement(resourceId))
391 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
392 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
393 Collections.singletonList(operationId), user, true).isRight());
394 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
395 Collections.singletonList(operationId), user, true).isRight());
396 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
397 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId,
398 operationName)), user, true).isRight());
402 public void testGetAllInterfaceLifecycleTypes_TypesNotFound() {
403 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
404 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
405 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
406 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
407 Assert.assertTrue(response.isRight());
411 public void testGetAllInterfaceLifecycleTypes_Success() {
412 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
413 interfaceDefinition.setUniqueId(interfaceId);
414 interfaceDefinition.setType(interfaceId);
415 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
416 interfaceDefinitionMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition);
417 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
418 .thenReturn(Either.left(interfaceDefinitionMap));
419 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
420 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
421 Assert.assertEquals(1, response.left().value().size());