36eee96848692793d26e4de9a108b2e584d8eb20
[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 fj.data.Either;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
35 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
36 import org.openecomp.sdc.be.components.validation.UserValidations;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
39 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.model.BaseType;
43 import org.openecomp.sdc.be.model.Component;
44 import org.openecomp.sdc.be.model.Product;
45 import org.openecomp.sdc.be.model.Resource;
46 import org.openecomp.sdc.be.model.Service;
47 import org.openecomp.sdc.be.model.User;
48 import org.openecomp.sdc.be.model.category.CategoryDefinition;
49 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
51 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
52 import org.openecomp.sdc.be.user.Role;
53 import org.openecomp.sdc.be.user.UserBusinessLogic;
54 import org.openecomp.sdc.exception.ResponseFormat;
55
56 import java.util.ArrayList;
57 import java.util.HashSet;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Set;
61
62 import static org.mockito.ArgumentMatchers.any;
63 import static org.mockito.ArgumentMatchers.anyBoolean;
64 import static org.mockito.ArgumentMatchers.anySet;
65 import static org.mockito.ArgumentMatchers.anyString;
66 import static org.mockito.ArgumentMatchers.eq;
67 import static org.mockito.Mockito.doThrow;
68 import static org.mockito.Mockito.when;
69
70 @RunWith(MockitoJUnitRunner.class)
71 public class ElementBusinessLogicTest extends BaseBusinessLogicMock {
72
73         private User user;
74
75         @Mock
76         private ComponentsUtils componentsUtils;
77
78         @Mock
79         private UserBusinessLogic userAdminManager;
80
81         @Mock
82         private JanusGraphDao janusGraphDao;
83
84         @Mock
85         private UserValidations userValidations;
86
87         @Mock
88         private ToscaOperationFacade toscaOperationFacade;
89
90   @InjectMocks
91         ElementBusinessLogic elementBusinessLogic;
92
93     @Before
94         public void setUp() {
95         MockitoAnnotations.initMocks(this);
96         elementBusinessLogic = new ElementBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
97                                 groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation, elementDao, userAdminManager);
98         elementBusinessLogic.setComponentsUtils(componentsUtils);
99         elementBusinessLogic.setJanusGraphDao(janusGraphDao);
100         elementBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
101         elementBusinessLogic.setUserValidations(userValidations);
102                         user = new User();
103                         user.setUserId("admin");
104         }
105
106     @Test
107         public void testGetFollowed_givenUserWithDesignerRole_thenReturnsSuccessful() {
108         user.setUserId("designer1");
109         user.setRole(Role.DESIGNER.name());
110
111         Set<Component> resources = new HashSet<>();
112         Set<Component> services = new HashSet<>();
113
114         Resource resource = new Resource();
115         Service service = new Service();
116
117         resources.add(resource);
118         services.add(service);
119
120         Mockito.when(toscaOperationFacade.getFollowed(eq(user.getUserId()), Mockito.anySet(), Mockito.anySet(), Mockito.eq(ComponentTypeEnum.RESOURCE)))
121                                 .thenReturn(Either.left(resources));
122         Mockito.when(toscaOperationFacade.getFollowed(eq(user.getUserId()), anySet(), anySet(), eq(ComponentTypeEnum.SERVICE)))
123                                 .thenReturn(Either.left(services));
124
125         Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
126         Assert.assertTrue(result.get("services").size() == 1);
127         Assert.assertTrue(result.get("resources").size() == 1);
128         }
129
130
131         @Test
132         public void testGetFollowed_givenUserWithProductStrategistRole_thenReturnsEmptyList(){
133                 user.setUserId("pstra1");
134         user.setRole(Role.PRODUCT_STRATEGIST.name());
135
136         Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
137         Assert.assertEquals("products list should be empty", 0, result.get("products").size());
138
139         }
140
141         @Test
142         public void testGetFollowed_givenUserWithProductManagerRole_thenReturnsProducts(){
143         user.setUserId("pmanager1");
144         user.setRole(Role.PRODUCT_MANAGER.name());
145
146         Set<Component> products = new HashSet<>();
147         products.add(new Product());
148
149         when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.PRODUCT)))
150                                 .thenReturn(Either.left(products));
151
152         Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
153         Assert.assertEquals("1 product should exist", 1, result.get("products").size());
154
155         }
156
157         @Test
158         public void testGetFollowed_givenUserWithRoleAdminErrorOccursGettingResources_thenReturnsError() {
159         user.setUserId("admin1");
160         user.setRole(Role.ADMIN.name());
161
162         when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
163                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
164
165         Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
166         }
167
168         @Test
169         public void testGetAllCategories_givenUserIsNull_thenReturnsError() {
170         Assert.assertTrue(elementBusinessLogic.getAllCategories(null, null).isRight());
171         }
172
173         @Test(expected = ComponentException.class)
174         public void testGetAllCategories_givenValidationOfUserFails_thenReturnsError() {
175         doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserExists(eq(user.getUserId()));
176                 elementBusinessLogic.getAllCategories(null, user.getUserId());
177         }
178
179         @Test
180         public void testGetAllCategories_givenInvalidComponentType_thenReturnsError() {
181         when(userValidations.validateUserExists(eq(user.getUserId()))).thenReturn(user);
182
183         Assert.assertTrue(elementBusinessLogic.getAllCategories("NONE", user.getUserId()).isRight());
184
185         }
186
187         @Test
188         public void testGetAllCategories_givenValidUserAndComponentType_thenReturnsSuccessful() {
189
190         List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
191         categoryDefinitionList.add(new CategoryDefinition());
192
193                 when(userValidations.validateUserExists(eq(user.getUserId()))).thenReturn(user);
194                 when(elementDao.getAllCategories(NodeTypeEnum.ResourceNewCategory, false))
195                                 .thenReturn(Either.left(categoryDefinitionList));
196                 Assert.assertTrue(elementBusinessLogic.getAllCategories(ComponentTypeEnum.RESOURCE_PARAM_NAME, user.getUserId())
197                 .isLeft());
198         }
199
200         @Test
201         public void testGetAllCategories_givenValidUserId_thenReturnsSuccessful() {
202
203         List<CategoryDefinition> dummyCategoryDefinitionList = new ArrayList<>();
204         dummyCategoryDefinitionList.add(new CategoryDefinition());
205
206         when(userValidations.validateUserExists(eq(user.getUserId())))
207                                 .thenReturn(user);
208         when(elementDao.getAllCategories(any(NodeTypeEnum.class), anyBoolean()))
209                                 .thenReturn(Either.left(dummyCategoryDefinitionList));
210
211         Assert.assertTrue(elementBusinessLogic.getAllCategories(user.getUserId()).isLeft());
212         }
213
214         @Test
215         public void testDeleteCategory_givenValidComponentTypeAndCategoryId_thenReturnsSuccessful() {
216
217         when(elementDao.deleteCategory(any(NodeTypeEnum.class), anyString()))
218                                 .thenReturn(Either.left(new CategoryDefinition()));
219
220         Assert.assertTrue(elementBusinessLogic.deleteCategory("cat1", "resources", user.getUserId()).isLeft());
221         }
222
223         @Test
224         public void testCreateSubCategory_givenValidSubCategory_thenReturnsSuccessful() {
225         user.setRole(Role.ADMIN.name());
226                 SubCategoryDefinition subCatDef = new SubCategoryDefinition();
227                 subCatDef.setName("subCat1");
228
229                 when(userValidations.validateUserExists(eq(user.getUserId())))
230                                 .thenReturn(user);
231                 when(elementDao.getCategory(any(NodeTypeEnum.class), anyString()))
232                                 .thenReturn(Either.left(new CategoryDefinition()));
233                 when(elementDao.isSubCategoryUniqueForCategory(any(NodeTypeEnum.class), anyString(), anyString()))
234                                 .thenReturn(Either.left(Boolean.TRUE));
235                 when(elementDao.getSubCategoryUniqueForType(any(NodeTypeEnum.class), anyString()))
236                                 .thenReturn(Either.left(subCatDef));
237                 when(elementDao.createSubCategory(anyString(), any(SubCategoryDefinition.class), any(NodeTypeEnum.class)))
238                                 .thenReturn(Either.left(subCatDef));
239
240                 Assert.assertTrue(elementBusinessLogic.createSubCategory(subCatDef, "resources",
241                                 "cat1", user.getUserId()).isLeft());
242         }
243
244         @Test
245         public void testCreateSubCategory_givenNullSubCategory_thenReturnsError() {
246         Assert.assertTrue(elementBusinessLogic.createSubCategory(null, "resources",
247                                 "cat1", user.getUserId()).isRight());
248         }
249
250         @Test(expected = ComponentException.class)
251         public void testCreateSubCategory_givenUserValidationFails_thenReturnsException() {
252         SubCategoryDefinition subCategoryDefinition = new SubCategoryDefinition();
253         doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserExists(eq(user.getUserId()));
254         elementBusinessLogic.createSubCategory(subCategoryDefinition, "resources", "cat1", user.getUserId());
255         }
256
257         @Test(expected=ComponentException.class)
258     public void testcreateCategory_VALIDATION_OF_USER_FAILED() {
259         CategoryDefinition catdefinition = new CategoryDefinition();
260         String userid = "";
261         ResponseFormat responseFormat = new ResponseFormat(7);
262         when(userValidations.validateUserExists("")).thenThrow(new ByResponseFormatComponentException(responseFormat));
263         elementBusinessLogic.createCategory(catdefinition,"Service", userid);
264     }
265
266     @Test
267     public void testcreateCategory_MISSING_INFORMATION() throws Exception {
268         CategoryDefinition catdefinition = new CategoryDefinition();
269         ResponseFormat responseFormat = new ResponseFormat(9);
270         User user = new User();
271         when(userValidations.validateUserExists("USR")).thenReturn(user);
272         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
273         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
274         Assert.assertTrue(response.isRight());
275         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
276     }
277
278
279     @Test
280     public void testcreateCategory_Invalid_componentType() throws Exception {
281
282         CategoryDefinition catdefinition = new CategoryDefinition();
283         catdefinition.setName("CAT01");
284         ResponseFormat responseFormat = new ResponseFormat(9);
285         User user = new User();
286
287         when(userValidations.validateUserExists("USR")).thenReturn(user);
288         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
289         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
290         Assert.assertTrue(response.isRight());
291         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
292     }
293
294     @Test
295     public void testcreateCategory_Invalid() throws Exception {
296
297         CategoryDefinition catdefinition = new CategoryDefinition();
298         catdefinition.setName("CAT01");
299         ResponseFormat responseFormat = new ResponseFormat(9);
300         User user = new User();
301
302         when(userValidations.validateUserExists("USR")).thenReturn(user);
303         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
304         Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"SERVICE_PARAM_NAME", "USR");
305         Assert.assertTrue(response.isRight());
306         Assert.assertEquals((Integer) 9, response.right().value().getStatus());
307     }
308     
309     @Test
310     public void testGetBaseTypes_givenValidUserAndComponentType_thenReturnsSuccessful() {
311
312         List<BaseType> baseTypes = new ArrayList<>();
313         baseTypes.add(new BaseType("org.openecomp.type"));
314         String categoryName = "CAT01";
315
316         when(userValidations.validateUserExistsActionStatus(eq(user.getUserId()))).thenReturn(ActionStatus.OK);
317         when(elementDao.getBaseTypes(categoryName)).thenReturn(baseTypes);
318         Assert.assertTrue(elementBusinessLogic.getBaseTypes(categoryName, user.getUserId())
319         .isLeft());
320     }
321     
322     @Test
323     public void testGetBaseTypes_givenUserValidationFails_thenReturnsException() {
324         when(userValidations.validateUserExistsActionStatus(eq(user.getUserId()))).thenReturn(ActionStatus.RESTRICTED_OPERATION);
325         Assert.assertTrue(elementBusinessLogic.getBaseTypes("CAT01", user.getUserId()).isRight());
326     }
327 }