Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ElementBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.impl;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
39 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
40 import org.openecomp.sdc.be.components.validation.UserValidations;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.Product;
48 import org.openecomp.sdc.be.model.Resource;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.model.category.CategoryDefinition;
52 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
55 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
56 import org.openecomp.sdc.be.user.Role;
57 import org.openecomp.sdc.be.user.UserBusinessLogic;
58 import org.openecomp.sdc.exception.ResponseFormat;
59 import fj.data.Either;
60
61 import static org.mockito.ArgumentMatchers.any;
62 import static org.mockito.ArgumentMatchers.anyBoolean;
63 import static org.mockito.ArgumentMatchers.anySet;
64 import static org.mockito.ArgumentMatchers.anyString;
65 import static org.mockito.ArgumentMatchers.eq;
66 import static org.mockito.Mockito.doThrow;
67 import static org.mockito.Mockito.when;
68
69 @RunWith(MockitoJUnitRunner.class)
70 public class ElementBusinessLogicTest {
71
72         private User user;
73
74         @Mock
75         private ComponentsUtils componentsUtils;
76
77         @Mock
78         private UserBusinessLogic userAdminManager;
79
80     @Mock
81         private JanusGraphDao janusGraphDao;
82
83     @Mock
84         private UserValidations userValidations;
85
86     @Mock
87         private ToscaOperationFacade toscaOperationFacade;
88
89     @Mock
90         private IElementOperation elementOperation;
91
92     @InjectMocks
93     ElementBusinessLogic elementBusinessLogic;
94
95     @Before
96         public void setUp() {
97
98         elementBusinessLogic = new ElementBusinessLogic();
99         MockitoAnnotations.initMocks(this);
100                 user = new User();
101                 user.setUserId("admin");
102         }
103
104     @Test
105         public void testGetFollowed_givenUserWithDesignerRole_thenReturnsSuccessful() {
106         user.setUserId("designer1");
107         user.setRole(Role.DESIGNER.name());
108
109         Set<Component> resources = new HashSet<>();
110         Set<Component> services = new HashSet<>();
111
112         Resource resource = new Resource();
113         Service service = new Service();
114
115         resources.add(resource);
116         services.add(service);
117
118         Mockito.when(toscaOperationFacade.getFollowed(eq(user.getUserId()), Mockito.anySet(), Mockito.anySet(), Mockito.eq(ComponentTypeEnum.RESOURCE)))
119                                 .thenReturn(Either.left(resources));
120         Mockito.when(toscaOperationFacade.getFollowed(eq(user.getUserId()), anySet(), anySet(), eq(ComponentTypeEnum.SERVICE)))
121                                 .thenReturn(Either.left(services));
122
123         Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
124         Assert.assertTrue(result.get("services").size() == 1);
125         Assert.assertTrue(result.get("resources").size() == 1);
126         }
127
128         @Test
129         public void testGetFollowed_givenUserWithTesterRoleErrorOccursGettingService_thenReturnsError () {
130                 user.setUserId("tester1");
131                 user.setRole(Role.TESTER.name());
132
133                 Set<Component> resources = new HashSet<>();
134
135                 Resource resource = new Resource();
136                 resources.add(resource);
137
138                 Mockito.when(toscaOperationFacade.getFollowed(any(), Mockito.anySet(), any(), Mockito.eq(ComponentTypeEnum.RESOURCE)))
139                                 .thenReturn(Either.left(resources));
140                 Mockito.when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.SERVICE)))
141                                 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
142                 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
143         }
144
145         @Test
146         public void testGetFollowed_givenUserWithGovernorRole_thenReturnsSuccessful(){
147                 user.setUserId("governor1");
148         user.setRole(Role.GOVERNOR.name());
149
150         List<Service> services = new ArrayList<>();
151         services.add(new Service());
152
153         when(toscaOperationFacade.getCertifiedServicesWithDistStatus(any()))
154                                 .thenReturn(Either.left(services));
155                 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isLeft());
156         }
157
158         @Test
159         public void testGetFollowed_givenUserWithOPSRoleErrorOccursGettingServices_thenReturnsError(){
160                 user.setUserId("ops1");
161         user.setRole(Role.OPS.name());
162
163                 when(toscaOperationFacade.getCertifiedServicesWithDistStatus(any()))
164                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
165
166                 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
167
168         }
169
170         @Test
171         public void testGetFollowed_givenUserWithProductStrategistRole_thenReturnsEmptyList(){
172                 user.setUserId("pstra1");
173         user.setRole(Role.PRODUCT_STRATEGIST.name());
174
175         Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
176         Assert.assertEquals("products list should be empty", 0, result.get("products").size());
177
178         }
179
180         @Test
181         public void testGetFollowed_givenUserWithProductManagerRole_thenReturnsProducts(){
182         user.setUserId("pmanager1");
183         user.setRole(Role.PRODUCT_MANAGER.name());
184
185         Set<Component> products = new HashSet<>();
186         products.add(new Product());
187
188         when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.PRODUCT)))
189                                 .thenReturn(Either.left(products));
190
191         Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
192         Assert.assertEquals("1 product should exist", 1, result.get("products").size());
193
194         }
195
196         @Test
197         public void testGetFollowed_givenUserWithRoleAdminErrorOccursGettingResources_thenReturnsError() {
198         user.setUserId("admin1");
199         user.setRole(Role.ADMIN.name());
200
201         when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
202                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
203
204         Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
205         }
206
207         @Test
208         public void testGetAllCategories_givenUserIsNull_thenReturnsError() {
209         Assert.assertTrue(elementBusinessLogic.getAllCategories(null, null).isRight());
210         }
211
212         @Test(expected = ComponentException.class)
213         public void testGetAllCategories_givenValidationOfUserFails_thenReturnsError() {
214         doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserExists(eq(user.getUserId()),
215                                 anyString(), anyBoolean());
216                 elementBusinessLogic.getAllCategories(null, user.getUserId());
217         }
218
219         @Test
220         public void testGetAllCategories_givenInvalidComponentType_thenReturnsError() {
221         when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean())).thenReturn(user);
222
223         Assert.assertTrue(elementBusinessLogic.getAllCategories("NONE", user.getUserId()).isRight());
224
225         }
226
227         @Test
228         public void testGetAllCategories_givenValidUserAndComponentType_thenReturnsSuccessful() {
229
230         List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
231         categoryDefinitionList.add(new CategoryDefinition());
232
233                 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean())).thenReturn(user);
234                 when(elementOperation.getAllCategories(NodeTypeEnum.ResourceNewCategory, false))
235                                 .thenReturn(Either.left(categoryDefinitionList));
236                 Assert.assertTrue(elementBusinessLogic.getAllCategories(ComponentTypeEnum.RESOURCE_PARAM_NAME, user.getUserId())
237                 .isLeft());
238         }
239
240         @Test
241         public void testGetAllCategories_givenValidUserId_thenReturnsSuccessful() {
242
243         List<CategoryDefinition> dummyCategoryDefinitionList = new ArrayList<>();
244         dummyCategoryDefinitionList.add(new CategoryDefinition());
245
246         when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean()))
247                                 .thenReturn(user);
248         when(elementOperation.getAllCategories(any(NodeTypeEnum.class), anyBoolean()))
249                                 .thenReturn(Either.left(dummyCategoryDefinitionList));
250
251         Assert.assertTrue(elementBusinessLogic.getAllCategories(user.getUserId()).isLeft());
252         }
253
254         @Test
255         public void testDeleteCategory_givenValidComponentTypeAndCategoryId_thenReturnsSuccessful() {
256
257         when(elementOperation.deleteCategory(any(NodeTypeEnum.class), anyString()))
258                                 .thenReturn(Either.left(new CategoryDefinition()));
259
260         Assert.assertTrue(elementBusinessLogic.deleteCategory("cat1", "resources", user.getUserId()).isLeft());
261         }
262
263         @Test
264         public void testCreateSubCategory_givenValidSubCategory_thenReturnsSuccessful() {
265         user.setRole(Role.ADMIN.name());
266                 SubCategoryDefinition subCatDef = new SubCategoryDefinition();
267                 subCatDef.setName("subCat1");
268
269                 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean()))
270                                 .thenReturn(user);
271                 when(elementOperation.getCategory(any(NodeTypeEnum.class), anyString()))
272                                 .thenReturn(Either.left(new CategoryDefinition()));
273                 when(elementOperation.isSubCategoryUniqueForCategory(any(NodeTypeEnum.class), anyString(), anyString()))
274                                 .thenReturn(Either.left(Boolean.TRUE));
275                 when(elementOperation.getSubCategoryUniqueForType(any(NodeTypeEnum.class), anyString()))
276                                 .thenReturn(Either.left(subCatDef));
277                 when(elementOperation.createSubCategory(anyString(), any(SubCategoryDefinition.class), any(NodeTypeEnum.class)))
278                                 .thenReturn(Either.left(subCatDef));
279
280                 Assert.assertTrue(elementBusinessLogic.createSubCategory(subCatDef, "resources",
281                                 "cat1", user.getUserId()).isLeft());
282         }
283
284         @Test
285         public void testCreateSubCategory_givenNullSubCategory_thenReturnsError() {
286         Assert.assertTrue(elementBusinessLogic.createSubCategory(null, "resources",
287                                 "cat1", user.getUserId()).isRight());
288         }
289
290         @Test(expected = ComponentException.class)
291         public void testCreateSubCategory_givenUserValidationFails_thenReturnsException() {
292         SubCategoryDefinition subCategoryDefinition = new SubCategoryDefinition();
293         doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserExists(eq(user.getUserId()),
294                                 anyString(), anyBoolean());
295         elementBusinessLogic.createSubCategory(subCategoryDefinition, "resources", "cat1", user.getUserId());
296         }
297
298         @Test
299     public void testcreateCategory_VALIDATION_OF_USER_FAILED() throws Exception {
300
301
302         CategoryDefinition catdefinition = new CategoryDefinition();
303         String userid=null;
304         ResponseFormat responseFormat = new ResponseFormat(7);
305         when(componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION)).thenReturn(responseFormat);
306         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", userid);
307         Assert.assertEquals(true,response.isRight());
308         Assert.assertEquals((Integer) 7, response.right().value().getStatus());
309     }
310
311     @Test
312     public void testcreateCategory_MISSING_INFORMATION() throws Exception {
313
314         CategoryDefinition catdefinition = new CategoryDefinition();
315         ResponseFormat responseFormat = new ResponseFormat(9);
316         User user = new User();
317         when(userAdminManager.getUser("USR", false)).thenReturn(Either.left(user));
318         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
319         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
320         Assert.assertEquals(true,response.isRight());
321         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
322     }
323
324     @Test
325     public void testcreateCategory_RESTRICTED_OPERATION() throws Exception {
326
327         CategoryDefinition catdefinition = new CategoryDefinition();
328         ResponseFormat responseFormat = new ResponseFormat(9);
329         User user = new User();
330         when(userAdminManager.getUser("USR", false)).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
331         when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat);
332         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
333         Assert.assertEquals(true,response.isRight());
334         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
335     }
336
337     @Test
338     public void testcreateCategory_Invalid_componentType() throws Exception {
339
340         CategoryDefinition catdefinition = new CategoryDefinition();
341         catdefinition.setName("CAT01");
342         ResponseFormat responseFormat = new ResponseFormat(9);
343         User user = new User();
344
345         when(userAdminManager.getUser("USR", false)).thenReturn(Either.left(user));
346         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
347         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
348         Assert.assertEquals(true,response.isRight());
349         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
350     }
351
352     @Test
353     public void testcreateCategory_Invalid() throws Exception {
354
355         CategoryDefinition catdefinition = new CategoryDefinition();
356         catdefinition.setName("CAT01");
357         ResponseFormat responseFormat = new ResponseFormat(9);
358         User user = new User();
359
360         when(userAdminManager.getUser("USR", false)).thenReturn(Either.left(user));
361         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
362         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"SERVICE_PARAM_NAME", "USR");
363         Assert.assertEquals(true,response.isRight());
364         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
365     }
366 }