997d32691bac33d9bd44dc3ceb8ab1591c208231
[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.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     @InjectMocks
68     private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
69     @Mock
70     private UserValidations userValidations;
71     @Mock
72     private ToscaOperationFacade toscaOperationFacade;
73     @Mock
74     private ComponentsUtils componentsUtils;
75     @Mock
76     private IGraphLockOperation graphLockOperation;
77     @Mock
78     private TitanDao titanDao;
79     @Mock
80     private InterfaceLifecycleOperation interfaceLifecycleOperation;
81     @Mock
82     private InterfaceOperationValidation interfaceOperationValidation;
83     @Mock
84     private InterfaceOperation interfaceOperation;
85     @Mock
86     private ArtifactCassandraDao artifactCassandraDao;
87
88     private static final String resourceId = "resourceId";
89     private static final String interfaceId = "interfaceId";
90     private static final String operationId = "operationId";
91     private static final String inputId = "inputId";
92     private static final String RESOURCE_NAME = "Resource1";
93     private static final String operationId1 = "operationId1";
94     private static final String interfaceId1 = "interfaceId1";
95
96     private User user;
97     private Resource resource;
98
99     @Before
100     public void setup() {
101         resource = new ResourceBuilder()
102             .setComponentType(ComponentTypeEnum.RESOURCE)
103             .setUniqueId(resourceId)
104             .setName(RESOURCE_NAME)
105             .build();
106         resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceId, operationId));
107         resource.setInputs(createInputsForResource());
108
109         user = new User();
110         when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(true))).thenReturn(user);
111         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
112         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
113         when(interfaceOperationValidation.validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean())).thenReturn(Either.left(true));
114         when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
115     }
116
117     @Test
118     public void createInterfaceOperationTestOnExistingInterface() {
119         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
120         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
121         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
122             .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
123         Assert.assertTrue(interfaceOperationEither.isLeft());
124     }
125
126     @Test
127     public void createInterfaceOperationWithoutInterfaceTest() {
128         resource.getInterfaces().clear();
129         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
130         when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
131         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
132         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
133             .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
134         Assert.assertTrue(interfaceOperationEither.isLeft());
135     }
136
137     @Test
138     public void createInterfaceOperationWithoutInterfaceTestFail() {
139         resource.getInterfaces().clear();
140         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
141         when(interfaceOperation.addInterfaces(any(), any())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
142         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
143             .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
144         Assert.assertTrue(interfaceOperationEither.isRight());
145     }
146
147     @Test
148     public void shouldFailWhenCreateInterfaceOperationFailedTest() {
149         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
150         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
151         Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId,
152             Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true).isRight());
153     }
154
155     @Test
156     public void updateInterfaceOperationTestWithArtifactSuccess() {
157         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
158         when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
159         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
160         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
161         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
162             interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
163         Assert.assertTrue(interfaceOperation.isLeft());
164     }
165
166     @Test
167     public void updateInterfaceOperationTestWithArtifactFailure() {
168         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
169         when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.left(new Long(1)));
170         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
171         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
172             interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
173         Assert.assertTrue(interfaceOperation.isRight());
174     }
175
176     @Test
177     public void updateInterfaceOperationTestWithoutArtifact() {
178         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
179         when(artifactCassandraDao.getCountOfArtifactById(any(String.class))).thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
180         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId))));
181         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
182             interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
183         Assert.assertTrue(interfaceOperation.isLeft());
184     }
185
186     @Test
187     public void updateInterfaceOperationTestDoesntExist() {
188         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
189         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperation =
190             interfaceOperationBusinessLogic.updateInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId1)), user, true);
191         Assert.assertTrue(interfaceOperation.isRight());
192     }
193
194     @Test
195     public void createInterfaceOperationTestFailOnException() {
196         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
197         when(interfaceOperation.updateInterfaces(any(), any())).thenThrow(new RuntimeException());
198         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
199             .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
200         Assert.assertTrue(interfaceOperationEither.isRight());
201     }
202
203     @Test
204     public void createInterfaceOperationTestFailOnFetchinGlobalTypes() {
205         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
206         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
207             .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
208         Assert.assertTrue(interfaceOperationEither.isRight());
209     }
210
211     @Test
212     public void createInterfaceOperationTestFailOnValidation() {
213         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(Collections.emptyMap()));
214         when(interfaceOperationValidation.validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean())).thenReturn(Either.right(new ResponseFormat()));
215         Either<List<InterfaceDefinition>, ResponseFormat> interfaceOperationEither = interfaceOperationBusinessLogic
216             .createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true);
217         Assert.assertTrue(interfaceOperationEither.isRight());
218     }
219
220     @Test
221     public void deleteInterfaceOperationTestInterfaceDoesntExist() {
222         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId1, Collections.singletonList(operationId), user, true).isRight());
223     }
224
225     @Test
226     public void deleteInterfaceOperationTestOperationDoesntExist() {
227         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId1), user, true).isRight());
228     }
229
230     @Test
231     public void deleteInterfaceOperationTestSuccess() {
232         resource.getInterfaces().get(interfaceId).getOperations().putAll(InterfaceOperationTestUtils.createMockOperationMap(operationId1));
233         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
234         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
235         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isLeft());
236     }
237
238     @Test
239     public void shouldFailWhenDeleteInterfaceOperationFailedTest() {
240         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
241         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
242         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
243     }
244
245     @Test
246     public void deleteInterfaceOperationTestFailOnArtifactDeletion() {
247         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
248         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
249     }
250
251     @Test
252     public void deleteInterfaceOperationTestFailOnException() {
253         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenThrow(new RuntimeException());
254         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
255     }
256
257     @Test
258     public void deleteInterfaceTestSuccess() {
259         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
260         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
261         when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.left(interfaceId));
262         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isLeft());
263     }
264
265     @Test
266     public void deleteInterfaceTestFailure() {
267         when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
268         when(interfaceOperation.updateInterfaces(any(), any())).thenReturn(Either.left(Collections.emptyList()));
269         when(interfaceOperation.deleteInterface(any(), any())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
270         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
271     }
272
273     @Test
274     public void getInterfaceOperationTestInterfaceDoesntExist() {
275         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId1, Collections.singletonList(operationId), user, true).isRight());
276     }
277
278     @Test
279     public void getInterfaceOperationTestOperationDoesntExist() {
280         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId1), user, true).isRight());
281     }
282
283     @Test
284     public void getInterfaceOperationTest() {
285         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isLeft());
286     }
287
288     @Test
289     public void getInterfaceOperationTestFailOnException() {
290         when(titanDao.commit()).thenThrow(new RuntimeException());
291         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
292     }
293
294     @Test
295     public void shouldFailWhenLockComponentFailedTest() {
296         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.NOT_FOUND);
297         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
298         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
299         Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true).isRight());
300     }
301
302     @Test
303     public void shouldFailWhenGetComponentFailedTest() {
304         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
305         Assert.assertTrue(interfaceOperationBusinessLogic.deleteInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
306         Assert.assertTrue(interfaceOperationBusinessLogic.getInterfaceOperation(resourceId, interfaceId, Collections.singletonList(operationId), user, true).isRight());
307         Assert.assertTrue(interfaceOperationBusinessLogic.createInterfaceOperation(resourceId, Collections.singletonList(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId)), user, true).isRight());
308     }
309
310     @Test
311     public void testGetAllInterfaceLifecycleTypes_TypesNotFound() {
312         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
313         Either<Map<String, InterfaceDefinition>, ResponseFormat> response = interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
314         Assert.assertTrue(response.isRight());
315     }
316
317     @Test
318     public void testGetAllInterfaceLifecycleTypes_Success() {
319         InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
320         interfaceDefinition.setUniqueId(interfaceId);
321         interfaceDefinition.setType(interfaceId);
322         Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
323         interfaceDefinitionMap.put(interfaceDefinition.getUniqueId(), interfaceDefinition);
324         when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()).thenReturn(Either.left(interfaceDefinitionMap));
325         Either<Map<String, InterfaceDefinition>, ResponseFormat>  response = interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes();
326         Assert.assertEquals(response.left().value().size(),1);
327     }
328
329     private List<InputDefinition> createInputsForResource(){
330         InputDefinition inputDefinition = new InputDefinition();
331         inputDefinition.setName(inputId);
332         inputDefinition.setInputId(inputId);
333         inputDefinition.setUniqueId(inputId);
334         inputDefinition.setValue(inputId);
335         inputDefinition.setDefaultValue(inputId);
336         return Arrays.asList(inputDefinition);
337     }
338 }