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