2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.components.impl;
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;
35 import fj.data.Either;
36 import java.util.ArrayList;
37 import java.util.HashSet;
38 import java.util.List;
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;
69 class ElementBusinessLogicTest extends BaseBusinessLogicMock {
74 private ComponentsUtils componentsUtils;
77 private UserBusinessLogic userAdminManager;
80 private JanusGraphDao janusGraphDao;
83 private UserValidations userValidations;
86 private ToscaOperationFacade toscaOperationFacade;
89 private ElementBusinessLogic elementBusinessLogic;
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);
101 user.setUserId("admin");
105 void testGetFollowed_givenUserWithDesignerRole_thenReturnsSuccessful() {
106 user.setUserId("designer1");
107 user.setRole(Role.DESIGNER.name());
109 Set<Component> resources = new HashSet<>();
110 Set<Component> services = new HashSet<>();
112 Resource resource = new Resource();
113 Service service = new Service();
115 resources.add(resource);
116 services.add(service);
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));
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());
131 void testGetFollowed_givenUserWithProductStrategistRole_thenReturnsEmptyList() {
132 user.setUserId("pstra1");
133 user.setRole(Role.PRODUCT_STRATEGIST.name());
135 Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
136 assertEquals(0, result.get("products").size(), "products list should be empty");
141 void testGetFollowed_givenUserWithProductManagerRole_thenReturnsProducts() {
142 user.setUserId("pmanager1");
143 user.setRole(Role.PRODUCT_MANAGER.name());
145 Set<Component> products = new HashSet<>();
146 products.add(new Product());
148 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.PRODUCT)))
149 .thenReturn(Either.left(products));
151 Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
152 assertEquals(1, result.get("products").size(), "1 product should exist");
156 void testGetFollowed_givenUserWithRoleAdminErrorOccursGettingResources_thenReturnsError() {
157 user.setUserId("admin1");
158 user.setRole(Role.ADMIN.name());
160 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
161 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
163 assertTrue(elementBusinessLogic.getFollowed(user).isRight());
167 void testGetAllCategories_givenUserIsNull_thenReturnsError() {
168 assertTrue(elementBusinessLogic.getAllCategories(null, null).isRight());
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));
179 void testGetAllCategories_givenInvalidComponentType_thenReturnsError() {
180 when(userValidations.validateUserExists(user.getUserId())).thenReturn(user);
182 assertTrue(elementBusinessLogic.getAllCategories("NONE", user.getUserId()).isRight());
186 void testGetAllCategories_givenValidUserAndComponentType_thenReturnsSuccessful() {
187 List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
188 categoryDefinitionList.add(new CategoryDefinition());
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())
198 void testGetAllCategories_givenValidUserId_thenReturnsSuccessful() {
199 List<CategoryDefinition> dummyCategoryDefinitionList = new ArrayList<>();
200 dummyCategoryDefinitionList.add(new CategoryDefinition());
202 when(userValidations.validateUserExists(user.getUserId()))
204 when(elementDao.getAllCategories(any(NodeTypeEnum.class), anyBoolean()))
205 .thenReturn(Either.left(dummyCategoryDefinitionList));
207 assertTrue(elementBusinessLogic.getAllCategories(user.getUserId()).isLeft());
211 void testDeleteCategory_givenValidComponentTypeAndCategoryId_thenReturnsSuccessful() {
212 when(elementDao.deleteCategory(any(NodeTypeEnum.class), anyString()))
213 .thenReturn(Either.left(new CategoryDefinition()));
215 assertTrue(elementBusinessLogic.deleteCategory("cat1", "resources", user.getUserId()).isLeft());
219 void testCreateSubCategory_givenValidSubCategory_thenReturnsSuccessful() {
220 user.setRole(Role.ADMIN.name());
221 SubCategoryDefinition subCatDef = new SubCategoryDefinition();
222 subCatDef.setName("subCat1");
224 when(userValidations.validateUserExists(user.getUserId()))
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));
235 assertTrue(elementBusinessLogic.createSubCategory(subCatDef, "resources",
236 "cat1", user.getUserId()).isLeft());
240 void testCreateSubCategory_givenNullSubCategory_thenReturnsError() {
241 assertTrue(elementBusinessLogic.createSubCategory(null, "resources",
242 "cat1", user.getUserId()).isRight());
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));
255 void testcreateCategory_VALIDATION_OF_USER_FAILED() {
256 CategoryDefinition catdefinition = new CategoryDefinition();
258 ResponseFormat responseFormat = new ResponseFormat(7);
259 when(userValidations.validateUserExists("")).thenThrow(new ByResponseFormatComponentException(responseFormat));
260 assertThrows(ComponentException.class, () -> elementBusinessLogic.createCategory(catdefinition, "Service", userid));
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());
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();
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());
291 void testcreateCategory_Invalid() {
292 CategoryDefinition catdefinition = new CategoryDefinition();
293 catdefinition.setName("CAT01");
294 ResponseFormat responseFormat = new ResponseFormat(9);
295 User user = new User();
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());
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";
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());
317 void testGetBaseTypes_givenUserValidationFails_thenReturnsException() {
318 when(userValidations.validateUserExistsActionStatus(user.getUserId())).thenReturn(ActionStatus.RESTRICTED_OPERATION);
319 assertTrue(elementBusinessLogic.getBaseTypes("CAT01", user.getUserId(), null).isRight());