Interface operation feature enhancements
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / InterfaceOperationBusinessLogicTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.be.components.impl;
18
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;
26
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;
32 import java.util.Map;
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;
62
63 @RunWith(MockitoJUnitRunner.class)
64 public class InterfaceOperationBusinessLogicTest {
65
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";
73     @InjectMocks
74     private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
75     @Mock
76     private UserValidations userValidations;
77     @Mock
78     private ToscaOperationFacade toscaOperationFacade;
79     @Mock
80     private ComponentsUtils componentsUtils;
81     @Mock
82     private IGraphLockOperation graphLockOperation;
83     @Mock
84     private TitanDao titanDao;
85     @Mock
86     private InterfaceLifecycleOperation interfaceLifecycleOperation;
87     @Mock
88     private InterfaceOperationValidation interfaceOperationValidation;
89     @Mock
90     private InterfaceOperation interfaceOperation;
91     @Mock
92     private ArtifactCassandraDao artifactCassandraDao;
93     private User user;
94     private Resource resource;
95
96     @Before
97     public void setup() {
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());
102
103         user = new User();
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);
112     }
113
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);
122     }
123
124     @Test
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,
133                                 operationId)),
134                         user, true);
135         Assert.assertTrue(interfaceOperationEither.isLeft());
136     }
137
138     @Test
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,
150                                 operationId)),
151                         user, true);
152         Assert.assertTrue(interfaceOperationEither.isLeft());
153     }
154
155     @Test
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,
165                                 operationId)),
166                         user, true);
167         Assert.assertTrue(interfaceOperationEither.isRight());
168     }
169
170     @Test
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());
179     }
180
181     @Test
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,
192                                 operationId)),
193                         user, true);
194         Assert.assertTrue(interfaceOperation.isLeft());
195     }
196
197     @Test
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,
206                                 operationId)),
207                         user, true);
208         Assert.assertTrue(interfaceOperation.isRight());
209     }
210
211     @Test
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,
222                                 operationId)),
223                         user, true);
224         Assert.assertTrue(interfaceOperation.isLeft());
225     }
226
227     @Test
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,
234                                 operationId)),
235                         user, true);
236         Assert.assertTrue(interfaceOperation.isRight());
237     }
238
239     @Test
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,
247                                 operationId)),
248                         user, true);
249         Assert.assertTrue(interfaceOperationEither.isRight());
250     }
251
252     @Test
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,
259                                 operationId)),
260                         user, true);
261         Assert.assertTrue(interfaceOperationEither.isRight());
262     }
263
264     @Test
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,
274                                 operationId)),
275                         user, true);
276         Assert.assertTrue(interfaceOperationEither.isRight());
277     }
278
279     @Test
280     public void deleteInterfaceOperationTestInterfaceDoesntExist() {
281         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
282                 Collections.singletonList(operationId), user, true).isRight());
283     }
284
285     @Test
286     public void deleteInterfaceOperationTestOperationDoesntExist() {
287         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
288                 Collections.singletonList(operationId1), user, true).isRight());
289     }
290
291     @Test
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());
299     }
300
301     @Test
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());
308     }
309
310     @Test
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());
315     }
316
317     @Test
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());
322     }
323
324     @Test
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());
331     }
332
333     @Test
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());
341     }
342
343     @Test
344     public void getInterfaceOperationTestInterfaceDoesntExist() {
345         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
346                 Collections.singletonList(operationId), user, true).isRight());
347     }
348
349     @Test
350     public void getInterfaceOperationTestOperationDoesntExist() {
351         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
352                 Collections.singletonList(operationId1), user, true).isRight());
353     }
354
355     @Test
356     public void getInterfaceOperationTest() {
357         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
358                 Collections.singletonList(operationId), user, true).isLeft());
359     }
360
361     @Test
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());
366     }
367
368     @Test
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());
379     }
380
381     @Test
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());
392     }
393
394     @Test
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());
401     }
402
403     @Test
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());
415     }
416 }