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.anyString;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.when;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
32 import fj.data.Either;
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.ArtifactDefinition;
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.ArtifactsOperations;
57 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
58 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
59 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
60 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
61 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
62 import org.openecomp.sdc.exception.ResponseFormat;
63 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
65 @RunWith(MockitoJUnitRunner.class)
66 public class InterfaceOperationBusinessLogicTest {
68 private static final String resourceId = "resourceId";
69 private static final String interfaceId = "interfaceId";
70 private static final String operationId = "operationId";
71 private static final String inputId = "inputId";
72 private static final String RESOURCE_NAME = "Resource1";
73 private static final String operationId1 = "operationId1";
74 private static final String interfaceId1 = "interfaceId1";
75 private static final String operationName = "createOperation";
78 private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
80 private UserValidations userValidations;
82 private ToscaOperationFacade toscaOperationFacade;
84 private ComponentsUtils componentsUtils;
86 private IGraphLockOperation graphLockOperation;
88 private TitanDao titanDao;
90 private InterfaceLifecycleOperation interfaceLifecycleOperation;
92 private InterfaceOperationValidation interfaceOperationValidation;
94 private InterfaceOperation interfaceOperation;
96 private ArtifactCassandraDao artifactCassandraDao;
98 protected ArtifactsOperations artifactToscaOperation;
100 private Resource resource;
103 public void setup() {
104 resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId(resourceId)
105 .setName(RESOURCE_NAME).build();
106 resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceId, operationId,
108 resource.setInputs(createInputsForResource());
111 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(true))).thenReturn(user);
112 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
113 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
114 .thenReturn(StorageOperationStatus.OK);
115 when(interfaceOperationValidation
116 .validateInterfaceOperations(any(), any(), any(), anyMap(), anyBoolean()))
117 .thenReturn(Either.left(true));
118 when(interfaceOperationValidation
119 .validateDeleteOperationContainsNoMappedOutput(any(), any(), any()))
120 .thenReturn(Either.left(true));
121 when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
124 private List<InputDefinition> createInputsForResource() {
125 InputDefinition inputDefinition = new InputDefinition();
126 inputDefinition.setName(inputId);
127 inputDefinition.setInputId(inputId);
128 inputDefinition.setUniqueId(inputId);
129 inputDefinition.setValue(inputId);
130 inputDefinition.setDefaultValue(inputId);
131 return Arrays.asList(inputDefinition);
135 public void createInterfaceOperationTestOnExistingInterface() {
136 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
137 .thenReturn(Either.left(Collections.emptyMap()));
138 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
139 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
140 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
141 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
142 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
143 operationId, operationName)),
145 Assert.assertTrue(interfaceOperationEither.isLeft());
149 public void createInterfaceOperationWithoutInterfaceTest() {
150 resource.getInterfaces().clear();
151 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
152 .thenReturn(Either.left(Collections.emptyMap()));
153 when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.left(
154 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
155 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
156 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
157 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
158 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
159 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
160 operationId, operationName)),
162 Assert.assertTrue(interfaceOperationEither.isLeft());
166 public void createInterfaceOperationWithoutInterfaceTestFail() {
167 resource.getInterfaces().clear();
168 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
169 .thenReturn(Either.left(Collections.emptyMap()));
170 when(interfaceOperation.addInterfaces(any(), any()))
171 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
172 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
173 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
174 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
175 operationId, operationName)),
177 Assert.assertTrue(interfaceOperationEither.isRight());
181 public void shouldFailWhenCreateInterfaceOperationFailedTest() {
182 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
183 .thenReturn(Either.left(Collections.emptyMap()));
184 when(interfaceOperation.updateInterfaces(any(), any()))
185 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
186 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
187 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)),
188 user, true).isRight());
192 public void updateInterfaceOperationTestWithArtifactSuccess() {
193 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
194 .thenReturn(Either.left(Collections.emptyMap()));
195 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
196 .thenReturn(Either.left(new ArtifactDefinition()));
197 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
198 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
199 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
200 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
201 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
202 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
203 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
204 operationId, operationName)),
206 Assert.assertTrue(interfaceOperation.isLeft());
210 public void updateInterfaceOperationTestWithArtifactFailure() {
211 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
212 .thenReturn(Either.left(Collections.emptyMap()));
213 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
214 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
215 .thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
216 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
217 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
218 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
219 operationId, operationName)),
221 Assert.assertTrue(interfaceOperation.isRight());
225 public void updateInterfaceOperationTestWithoutArtifact() {
226 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
227 .thenReturn(Either.left(Collections.emptyMap()));
228 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
229 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(
230 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName))));
231 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
232 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
233 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
234 operationId, operationName)),
236 Assert.assertTrue(interfaceOperation.isLeft());
240 public void updateInterfaceOperationTestDoesntExist() {
241 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
242 .thenReturn(Either.left(Collections.emptyMap()));
243 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
244 interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId,
245 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
246 operationId, operationName)),
248 Assert.assertTrue(interfaceOperation.isRight());
252 public void createInterfaceOperationTestFailOnException() {
253 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
254 .thenReturn(Either.left(Collections.emptyMap()));
255 when(interfaceOperation.updateInterfaces(any(), any())).thenThrow(new RuntimeException());
256 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
257 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
258 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
259 operationId, operationName)),
261 Assert.assertTrue(interfaceOperationEither.isRight());
265 public void createInterfaceOperationTestFailOnFetchinGlobalTypes() {
266 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
267 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
268 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
269 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
270 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
271 operationId, operationName)),
273 Assert.assertTrue(interfaceOperationEither.isRight());
277 public void createInterfaceOperationTestFailOnValidation() {
278 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
279 .thenReturn(Either.left(Collections.emptyMap()));
280 when(interfaceOperationValidation
281 .validateInterfaceOperations(any(), any(), any(), anyMap(), anyBoolean()))
282 .thenReturn(Either.right(new ResponseFormat()));
283 Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither =
284 interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
285 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId,
286 operationId, operationName)),
288 Assert.assertTrue(interfaceOperationEither.isRight());
292 public void deleteInterfaceOperationTestInterfaceDoesntExist() {
293 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
294 Collections.singletonList(operationId), user, true).isRight());
298 public void deleteInterfaceOperationTestOperationDoesntExist() {
299 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
300 Collections.singletonList(operationId1), user, true).isRight());
304 public void deleteInterfaceOperationTestSuccess() {
305 resource.getInterfaces().get(interfaceId).getOperations()
306 .putAll(InterfaceOperationTestUtils.createMockOperationMap(operationId1, operationName));
307 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
308 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
309 .thenReturn(Either.left(new ArtifactDefinition()));
310 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
311 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
312 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
313 Collections.singletonList(operationId), user, true).isLeft());
317 public void shouldFailWhenDeleteInterfaceOperationFailedTest() {
318 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
319 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
320 .thenReturn(Either.left(new ArtifactDefinition()));
321 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
322 when(interfaceOperation.updateInterfaces(any(), any()))
323 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
324 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
325 Collections.singletonList(operationId), user, true).isRight());
329 public void deleteInterfaceOperationTestFailOnArtifactDeletion() {
330 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
331 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
332 .thenReturn(Either.left(new ArtifactDefinition()));
333 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
334 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
335 Collections.singletonList(operationId), user, true).isRight());
339 public void deleteInterfaceOperationTestFailOnException() {
340 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
341 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
342 .thenReturn(Either.left(new ArtifactDefinition()));
343 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenThrow(new RuntimeException());
344 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
345 Collections.singletonList(operationId), user, true).isRight());
349 public void deleteInterfaceTestSuccess() {
350 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
351 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
352 .thenReturn(Either.left(new ArtifactDefinition()));
353 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
354 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
355 when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.left(interfaceId));
356 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
357 Collections.singletonList(operationId), user, true).isLeft());
361 public void deleteInterfaceTestFailure() {
362 when(artifactToscaOperation.getArtifactById(any(), any())).thenReturn(Either.left(new ArtifactDefinition()));
363 when(artifactToscaOperation.removeArifactFromResource(any(), any(), any(), anyBoolean()))
364 .thenReturn(Either.left(new ArtifactDefinition()));
365 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
366 when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
367 when(interfaceOperation.deleteInterface(any(), any()))
368 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
369 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
370 Collections.singletonList(operationId), user, true).isRight());
374 public void getInterfaceOperationTestInterfaceDoesntExist() {
375 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
376 Collections.singletonList(operationId), user, true).isRight());
380 public void getInterfaceOperationTestOperationDoesntExist() {
381 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
382 Collections.singletonList(operationId1), user, true).isRight());
386 public void getInterfaceOperationTest() {
387 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
388 Collections.singletonList(operationId), user, true).isLeft());
392 public void getInterfaceOperationTestFailOnException() {
393 when(titanDao.commit()).thenThrow(new RuntimeException());
394 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
395 Collections.singletonList(operationId), user, true).isRight());
399 public void shouldFailWhenLockComponentFailedTest() {
400 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource)))
401 .thenReturn(StorageOperationStatus.NOT_FOUND);
402 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
403 Collections.singletonList(operationId), user, true).isRight());
404 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
405 Collections.singletonList(operationId), user, true).isRight());
406 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
407 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)),
408 user, true).isRight());
412 public void shouldFailWhenGetComponentFailedTest() {
413 when(toscaOperationFacade.getToscaElement(resourceId))
414 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
415 Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
416 Collections.singletonList(operationId), user, true).isRight());
417 Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
418 Collections.singletonList(operationId), user, true).isRight());
419 Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
420 Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId,
421 operationName)), user, true).isRight());
425 public void testGetAllInterfaceLifecycleTypes_TypesNotFound() {
426 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
427 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
428 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
429 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
430 Assert.assertTrue(response.isRight());
434 public void testGetAllInterfaceLifecycleTypes_Success() {
435 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
436 interfaceDefinition.setUniqueId(interfaceId);
437 interfaceDefinition.setType(interfaceId);
438 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
439 interfaceDefinitionMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition);
440 when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes())
441 .thenReturn(Either.left(interfaceDefinitionMap));
442 Either<Map<String, InterfaceDefinition>, ResponseFormat> response =
443 interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
444 Assert.assertEquals(1, response.left().value().size());