Service Consumption BE
[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 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(interfaceOperationValidation
116                 .validateDeleteOperationContainsNoMappedOutput(anyObject(), anyObject(), any()))
117                 .thenReturn(Either.left(true));
118         when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
119     }
120
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);
129     }
130
131     @Test
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)),
141                         user, true);
142         Assert.assertTrue(interfaceOperationEither.isLeft());
143     }
144
145     @Test
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)),
158                         user, true);
159         Assert.assertTrue(interfaceOperationEither.isLeft());
160     }
161
162     @Test
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)),
173                         user, true);
174         Assert.assertTrue(interfaceOperationEither.isRight());
175     }
176
177     @Test
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());
186     }
187
188     @Test
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)),
200                         user, true);
201         Assert.assertTrue(interfaceOperation.isLeft());
202     }
203
204     @Test
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)),
214                         user, true);
215         Assert.assertTrue(interfaceOperation.isRight());
216     }
217
218     @Test
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)),
230                         user, true);
231         Assert.assertTrue(interfaceOperation.isLeft());
232     }
233
234     @Test
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)),
242                         user, true);
243         Assert.assertTrue(interfaceOperation.isRight());
244     }
245
246     @Test
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)),
255                         user, true);
256         Assert.assertTrue(interfaceOperationEither.isRight());
257     }
258
259     @Test
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)),
267                         user, true);
268         Assert.assertTrue(interfaceOperationEither.isRight());
269     }
270
271     @Test
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)),
282                         user, true);
283         Assert.assertTrue(interfaceOperationEither.isRight());
284     }
285
286     @Test
287     public void deleteInterfaceOperationTestInterfaceDoesntExist() {
288         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1,
289                 Collections.singletonList(operationId), user, true).isRight());
290     }
291
292     @Test
293     public void deleteInterfaceOperationTestOperationDoesntExist() {
294         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId,
295                 Collections.singletonList(operationId1), user, true).isRight());
296     }
297
298     @Test
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());
306     }
307
308     @Test
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());
315     }
316
317     @Test
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());
322     }
323
324     @Test
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());
329     }
330
331     @Test
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());
338     }
339
340     @Test
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());
348     }
349
350     @Test
351     public void getInterfaceOperationTestInterfaceDoesntExist() {
352         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1,
353                 Collections.singletonList(operationId), user, true).isRight());
354     }
355
356     @Test
357     public void getInterfaceOperationTestOperationDoesntExist() {
358         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
359                 Collections.singletonList(operationId1), user, true).isRight());
360     }
361
362     @Test
363     public void getInterfaceOperationTest() {
364         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId,
365                 Collections.singletonList(operationId), user, true).isLeft());
366     }
367
368     @Test
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());
373     }
374
375     @Test
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());
386     }
387
388     @Test
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());
399     }
400
401     @Test
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());
408     }
409
410     @Test
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());
422     }
423 }