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(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
118 private List<InputDefinition> createInputsForResource() {
119 InputDefinition inputDefinition = new InputDefinition();
120 inputDefinition.setName(inputId);
121 inputDefinition.setInputId(inputId);
122 inputDefinition.setUniqueId(inputId);
123 inputDefinition.setValue(inputId);
124 inputDefinition.setDefaultValue(inputId);
125 return Arrays.asList(inputDefinition);
129 public void createInterfaceOperationTestOnExistingInterface() {
130 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
131 .thenReturn(Either.left(Collections.emptyMap()));
132 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
133 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
134 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
135 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
136 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
137 operationId, operationName)),
139 Assert.assertTrue(interfaceOperationEither.isLeft());
143 public void createInterfaceOperationWithoutInterfaceTest() {
144 resource.getInterfaces().clear();
145 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
146 .thenReturn(Either.left(Collections.emptyMap()));
147 when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.left(
148 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
149 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
150 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
151 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
152 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
153 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
154 operationId, operationName)),
156 Assert.assertTrue(interfaceOperationEither.isLeft());
160 public void createInterfaceOperationWithoutInterfaceTestFail() {
161 resource.getInterfaces().clear();
162 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
163 .thenReturn(Either.left(Collections.emptyMap()));
164 when(interfaceOperation.addInterfaces(any(), any()))
165 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
166 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
167 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
168 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
169 operationId, operationName)),
171 Assert.assertTrue(interfaceOperationEither.isRight());
175 public void shouldFailWhenCreateInterfaceOperationFailedTest() {
176 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
177 .thenReturn(Either.left(Collections.emptyMap()));
178 when(interfaceOperation.updateInterfaces(any(), any()))
179 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
180 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
181 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)),
182 user, true).isRight());
186 public void updateInterfaceOperationTestWithArtifactSuccess() {
187 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
188 .thenReturn(Either.left(Collections.emptyMap()));
189 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
190 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
191 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
192 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
193 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
194 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
195 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
196 operationId, operationName)),
198 Assert.assertTrue(interfaceOperation.isLeft());
202 public void updateInterfaceOperationTestWithArtifactFailure() {
203 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
204 .thenReturn(Either.left(Collections.emptyMap()));
205 when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
206 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
207 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
208 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
209 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
210 operationId, operationName)),
212 Assert.assertTrue(interfaceOperation.isRight());
216 public void updateInterfaceOperationTestWithoutArtifact() {
217 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
218 .thenReturn(Either.left(Collections.emptyMap()));
219 when(artifactCassandraDao.getCountOfArtifactById(any(String.class)))
220 .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
221 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
222 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
223 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
224 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
225 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
226 operationId, operationName)),
228 Assert.assertTrue(interfaceOperation.isLeft());
232 public void updateInterfaceOperationTestDoesntExist() {
233 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
234 .thenReturn(Either.left(Collections.emptyMap()));
235 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
236 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
237 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
238 operationId, operationName)),
240 Assert.assertTrue(interfaceOperation.isRight());
244 public void createInterfaceOperationTestFailOnException() {
245 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
246 .thenReturn(Either.left(Collections.emptyMap()));
247 when(interfaceOperation.updateInterfaces(any(), any())).thenThrow(new RuntimeException());
248 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
249 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
250 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
251 operationId, operationName)),
253 Assert.assertTrue(interfaceOperationEither.isRight());
257 public void createInterfaceOperationTestFailOnFetchinGlobalTypes() {
258 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
259 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
260 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
261 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
262 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
263 operationId, operationName)),
265 Assert.assertTrue(interfaceOperationEither.isRight());
269 public void createInterfaceOperationTestFailOnValidation() {
270 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
271 .thenReturn(Either.left(Collections.emptyMap()));
272 when(interfaceOperationValidation
273 .validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean()))
274 .thenReturn(Either.right(new ResponseFormat()));
275 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
276 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
277 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
278 operationId, operationName)),
280 Assert.assertTrue(interfaceOperationEither.isRight());
284 public void deleteInterfaceOperationTestInterfaceDoesntExist() {
285 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
286 Collections.singletonList(operationId), user, true).isRight());
290 public void deleteInterfaceOperationTestOperationDoesntExist() {
291 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
292 Collections.singletonList(operationId1), user, true).isRight());
296 public void deleteInterfaceOperationTestSuccess() {
297 resource.getInterfaces().get(interfaceId).getOperations()
298 .putAll(InterfaceOperationTestUtils.createMockOperationMap(operationId1, operationName));
299 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
300 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
301 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
302 Collections.singletonList(operationId), user, true).isLeft());
306 public void shouldFailWhenDeleteInterfaceOperationFailedTest() {
307 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
308 when(interfaceOperation.updateInterfaces(any(), any()))
309 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
310 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
311 Collections.singletonList(operationId), user, true).isRight());
315 public void deleteInterfaceOperationTestFailOnArtifactDeletion() {
316 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
317 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
318 Collections.singletonList(operationId), user, true).isRight());
322 public void deleteInterfaceOperationTestFailOnException() {
323 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenThrow(new RuntimeException());
324 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
325 Collections.singletonList(operationId), user, true).isRight());
329 public void deleteInterfaceTestSuccess() {
330 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
331 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
332 when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.left(interfaceId));
333 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
334 Collections.singletonList(operationId), user, true).isLeft());
338 public void deleteInterfaceTestFailure() {
339 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
340 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
341 when(interfaceOperation.deleteInterface(any(), any()))
342 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
343 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
344 Collections.singletonList(operationId), user, true).isRight());
348 public void getInterfaceOperationTestInterfaceDoesntExist() {
349 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
350 Collections.singletonList(operationId), user, true).isRight());
354 public void getInterfaceOperationTestOperationDoesntExist() {
355 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
356 Collections.singletonList(operationId1), user, true).isRight());
360 public void getInterfaceOperationTest() {
361 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
362 Collections.singletonList(operationId), user, true).isLeft());
366 public void getInterfaceOperationTestFailOnException() {
367 when(titanDao.commit()).thenThrow(new RuntimeException());
368 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
369 Collections.singletonList(operationId), user, true).isRight());
373 public void shouldFailWhenLockComponentFailedTest() {
374 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
375 .thenReturn(StorageOperationStatus.NOT_FOUND);
376 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
377 Collections.singletonList(operationId), user, true).isRight());
378 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
379 Collections.singletonList(operationId), user, true).isRight());
380 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
381 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)),
382 user, true).isRight());
386 public void shouldFailWhenGetComponentFailedTest() {
387 when(toscaOperationFacade.getToscaElement(resourceId))
388 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
389 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
390 Collections.singletonList(operationId), user, true).isRight());
391 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
392 Collections.singletonList(operationId), user, true).isRight());
393 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
394 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId,
395 operationName)), user, true).isRight());
399 public void testGetAllInterfaceLifecycleTypes_TypesNotFound() {
400 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
401 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
402 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
403 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
404 Assert.assertTrue(response.isRight());
408 public void testGetAllInterfaceLifecycleTypes_Success() {
409 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
410 interfaceDefinition.setUniqueId(interfaceId);
411 interfaceDefinition.setType(interfaceId);
412 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
413 interfaceDefinitionMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition);
414 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
415 .thenReturn(Either.left(interfaceDefinitionMap));
416 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
417 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
418 Assert.assertEquals(1, response.left().value().size());