df4ef646ee1936d465a12505987d39d9f8e0c0db
[sdc.git] /
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 java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
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;
63
64 @RunWith(MockitoJUnitRunner.class)
65 public class InterfaceOperationBusinessLogicTest {
66
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";
75
76     @InjectMocks
77     private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
78     @Mock
79     private UserValidations userValidations;
80     @Mock
81     private ToscaOperationFacade toscaOperationFacade;
82     @Mock
83     private ComponentsUtils componentsUtils;
84     @Mock
85     private IGraphLockOperation graphLockOperation;
86     @Mock
87     private TitanDao titanDao;
88     @Mock
89     private InterfaceLifecycleOperation interfaceLifecycleOperation;
90     @Mock
91     private InterfaceOperationValidation interfaceOperationValidation;
92     @Mock
93     private InterfaceOperation interfaceOperation;
94     @Mock
95     private ArtifactCassandraDao artifactCassandraDao;
96     private User user;
97     private Resource resource;
98
99     @Before
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,
104                 operationName));
105         resource.setInputs(createInputsForResource());
106
107         user = new User();
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);
116     }
117
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);
126     }
127
128     @Test
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)),
138                         user, true);
139         Assert.assertTrue(interfaceOperationEither.isLeft());
140     }
141
142     @Test
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)),
155                         user, true);
156         Assert.assertTrue(interfaceOperationEither.isLeft());
157     }
158
159     @Test
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)),
170                         user, true);
171         Assert.assertTrue(interfaceOperationEither.isRight());
172     }
173
174     @Test
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());
183     }
184
185     @Test
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)),
197                         user, true);
198         Assert.assertTrue(interfaceOperation.isLeft());
199     }
200
201     @Test
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)),
211                         user, true);
212         Assert.assertTrue(interfaceOperation.isRight());
213     }
214
215     @Test
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)),
227                         user, true);
228         Assert.assertTrue(interfaceOperation.isLeft());
229     }
230
231     @Test
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)),
239                         user, true);
240         Assert.assertTrue(interfaceOperation.isRight());
241     }
242
243     @Test
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)),
252                         user, true);
253         Assert.assertTrue(interfaceOperationEither.isRight());
254     }
255
256     @Test
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)),
264                         user, true);
265         Assert.assertTrue(interfaceOperationEither.isRight());
266     }
267
268     @Test
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)),
279                         user, true);
280         Assert.assertTrue(interfaceOperationEither.isRight());
281     }
282
283     @Test
284     public void deleteInterfaceOperationTestInterfaceDoesntExist() {
285         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
286                 Collections.singletonList(operationId), user, true).isRight());
287     }
288
289     @Test
290     public void deleteInterfaceOperationTestOperationDoesntExist() {
291         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
292                 Collections.singletonList(operationId1), user, true).isRight());
293     }
294
295     @Test
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());
303     }
304
305     @Test
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());
312     }
313
314     @Test
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());
319     }
320
321     @Test
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());
326     }
327
328     @Test
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());
335     }
336
337     @Test
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());
345     }
346
347     @Test
348     public void getInterfaceOperationTestInterfaceDoesntExist() {
349         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
350                 Collections.singletonList(operationId), user, true).isRight());
351     }
352
353     @Test
354     public void getInterfaceOperationTestOperationDoesntExist() {
355         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
356                 Collections.singletonList(operationId1), user, true).isRight());
357     }
358
359     @Test
360     public void getInterfaceOperationTest() {
361         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
362                 Collections.singletonList(operationId), user, true).isLeft());
363     }
364
365     @Test
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());
370     }
371
372     @Test
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());
383     }
384
385     @Test
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());
396     }
397
398     @Test
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());
405     }
406
407     @Test
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());
419     }
420 }