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 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;
56 import java.util.ArrayList;
57 import java.util.HashSet;
58 import java.util.List;
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;
70 @RunWith(MockitoJUnitRunner.class)
71 public class ElementBusinessLogicTest extends BaseBusinessLogicMock {
76 private ComponentsUtils componentsUtils;
79 private UserBusinessLogic userAdminManager;
82 private JanusGraphDao janusGraphDao;
85 private UserValidations userValidations;
88 private ToscaOperationFacade toscaOperationFacade;
91 ElementBusinessLogic elementBusinessLogic;
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);
103 user.setUserId("admin");
107 public void testGetFollowed_givenUserWithDesignerRole_thenReturnsSuccessful() {
108 user.setUserId("designer1");
109 user.setRole(Role.DESIGNER.name());
111 Set<Component> resources = new HashSet<>();
112 Set<Component> services = new HashSet<>();
114 Resource resource = new Resource();
115 Service service = new Service();
117 resources.add(resource);
118 services.add(service);
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));
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);
132 public void testGetFollowed_givenUserWithProductStrategistRole_thenReturnsEmptyList(){
133 user.setUserId("pstra1");
134 user.setRole(Role.PRODUCT_STRATEGIST.name());
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());
142 public void testGetFollowed_givenUserWithProductManagerRole_thenReturnsProducts(){
143 user.setUserId("pmanager1");
144 user.setRole(Role.PRODUCT_MANAGER.name());
146 Set<Component> products = new HashSet<>();
147 products.add(new Product());
149 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.PRODUCT)))
150 .thenReturn(Either.left(products));
152 Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
153 Assert.assertEquals("1 product should exist", 1, result.get("products").size());
158 public void testGetFollowed_givenUserWithRoleAdminErrorOccursGettingResources_thenReturnsError() {
159 user.setUserId("admin1");
160 user.setRole(Role.ADMIN.name());
162 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
163 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
165 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
169 public void testGetAllCategories_givenUserIsNull_thenReturnsError() {
170 Assert.assertTrue(elementBusinessLogic.getAllCategories(null, null).isRight());
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());
180 public void testGetAllCategories_givenInvalidComponentType_thenReturnsError() {
181 when(userValidations.validateUserExists(eq(user.getUserId()))).thenReturn(user);
183 Assert.assertTrue(elementBusinessLogic.getAllCategories("NONE", user.getUserId()).isRight());
188 public void testGetAllCategories_givenValidUserAndComponentType_thenReturnsSuccessful() {
190 List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
191 categoryDefinitionList.add(new CategoryDefinition());
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())
201 public void testGetAllCategories_givenValidUserId_thenReturnsSuccessful() {
203 List<CategoryDefinition> dummyCategoryDefinitionList = new ArrayList<>();
204 dummyCategoryDefinitionList.add(new CategoryDefinition());
206 when(userValidations.validateUserExists(eq(user.getUserId())))
208 when(elementDao.getAllCategories(any(NodeTypeEnum.class), anyBoolean()))
209 .thenReturn(Either.left(dummyCategoryDefinitionList));
211 Assert.assertTrue(elementBusinessLogic.getAllCategories(user.getUserId()).isLeft());
215 public void testDeleteCategory_givenValidComponentTypeAndCategoryId_thenReturnsSuccessful() {
217 when(elementDao.deleteCategory(any(NodeTypeEnum.class), anyString()))
218 .thenReturn(Either.left(new CategoryDefinition()));
220 Assert.assertTrue(elementBusinessLogic.deleteCategory("cat1", "resources", user.getUserId()).isLeft());
224 public void testCreateSubCategory_givenValidSubCategory_thenReturnsSuccessful() {
225 user.setRole(Role.ADMIN.name());
226 SubCategoryDefinition subCatDef = new SubCategoryDefinition();
227 subCatDef.setName("subCat1");
229 when(userValidations.validateUserExists(eq(user.getUserId())))
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));
240 Assert.assertTrue(elementBusinessLogic.createSubCategory(subCatDef, "resources",
241 "cat1", user.getUserId()).isLeft());
245 public void testCreateSubCategory_givenNullSubCategory_thenReturnsError() {
246 Assert.assertTrue(elementBusinessLogic.createSubCategory(null, "resources",
247 "cat1", user.getUserId()).isRight());
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());
257 @Test(expected=ComponentException.class)
258 public void testcreateCategory_VALIDATION_OF_USER_FAILED() {
259 CategoryDefinition catdefinition = new CategoryDefinition();
261 ResponseFormat responseFormat = new ResponseFormat(7);
262 when(userValidations.validateUserExists("")).thenThrow(new ByResponseFormatComponentException(responseFormat));
263 elementBusinessLogic.createCategory(catdefinition,"Service", userid);
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());
280 public void testcreateCategory_Invalid_componentType() throws Exception {
282 CategoryDefinition catdefinition = new CategoryDefinition();
283 catdefinition.setName("CAT01");
284 ResponseFormat responseFormat = new ResponseFormat(9);
285 User user = new User();
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());
295 public void testcreateCategory_Invalid() throws Exception {
297 CategoryDefinition catdefinition = new CategoryDefinition();
298 catdefinition.setName("CAT01");
299 ResponseFormat responseFormat = new ResponseFormat(9);
300 User user = new User();
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());
310 public void testGetBaseTypes_givenValidUserAndComponentType_thenReturnsSuccessful() {
312 List<BaseType> baseTypes = new ArrayList<>();
313 baseTypes.add(new BaseType("org.openecomp.type"));
314 String categoryName = "CAT01";
315 String modelName = "MODEL01";
317 when(userValidations.validateUserExistsActionStatus(eq(user.getUserId()))).thenReturn(ActionStatus.OK);
318 when(elementDao.getBaseTypes(categoryName, modelName)).thenReturn(baseTypes);
319 Assert.assertTrue(elementBusinessLogic.getBaseTypes(categoryName, user.getUserId(), modelName)
324 public void testGetBaseTypes_givenUserValidationFails_thenReturnsException() {
325 when(userValidations.validateUserExistsActionStatus(eq(user.getUserId()))).thenReturn(ActionStatus.RESTRICTED_OPERATION);
326 Assert.assertTrue(elementBusinessLogic.getBaseTypes("CAT01", user.getUserId(), null).isRight());