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