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.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;
55 import java.util.ArrayList;
56 import java.util.HashSet;
57 import java.util.List;
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;
69 @RunWith(MockitoJUnitRunner.class)
70 public class ElementBusinessLogicTest extends BaseBusinessLogicMock {
75 private ComponentsUtils componentsUtils;
78 private UserBusinessLogic userAdminManager;
81 private JanusGraphDao janusGraphDao;
84 private UserValidations userValidations;
87 private ToscaOperationFacade toscaOperationFacade;
90 ElementBusinessLogic elementBusinessLogic;
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);
102 user.setUserId("admin");
106 public void testGetFollowed_givenUserWithDesignerRole_thenReturnsSuccessful() {
107 user.setUserId("designer1");
108 user.setRole(Role.DESIGNER.name());
110 Set<Component> resources = new HashSet<>();
111 Set<Component> services = new HashSet<>();
113 Resource resource = new Resource();
114 Service service = new Service();
116 resources.add(resource);
117 services.add(service);
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));
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);
131 public 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 Assert.assertEquals("products list should be empty", 0, result.get("products").size());
141 public 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 Assert.assertEquals("1 product should exist", 1, result.get("products").size());
157 public void testGetFollowed_givenUserWithRoleAdminErrorOccursGettingResources_thenReturnsError() {
158 user.setUserId("admin1");
159 user.setRole(Role.ADMIN.name());
161 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
162 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
164 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
168 public void testGetAllCategories_givenUserIsNull_thenReturnsError() {
169 Assert.assertTrue(elementBusinessLogic.getAllCategories(null, null).isRight());
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());
179 public void testGetAllCategories_givenInvalidComponentType_thenReturnsError() {
180 when(userValidations.validateUserExists(eq(user.getUserId()))).thenReturn(user);
182 Assert.assertTrue(elementBusinessLogic.getAllCategories("NONE", user.getUserId()).isRight());
187 public void testGetAllCategories_givenValidUserAndComponentType_thenReturnsSuccessful() {
189 List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
190 categoryDefinitionList.add(new CategoryDefinition());
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())
200 public void testGetAllCategories_givenValidUserId_thenReturnsSuccessful() {
202 List<CategoryDefinition> dummyCategoryDefinitionList = new ArrayList<>();
203 dummyCategoryDefinitionList.add(new CategoryDefinition());
205 when(userValidations.validateUserExists(eq(user.getUserId())))
207 when(elementDao.getAllCategories(any(NodeTypeEnum.class), anyBoolean()))
208 .thenReturn(Either.left(dummyCategoryDefinitionList));
210 Assert.assertTrue(elementBusinessLogic.getAllCategories(user.getUserId()).isLeft());
214 public void testDeleteCategory_givenValidComponentTypeAndCategoryId_thenReturnsSuccessful() {
216 when(elementDao.deleteCategory(any(NodeTypeEnum.class), anyString()))
217 .thenReturn(Either.left(new CategoryDefinition()));
219 Assert.assertTrue(elementBusinessLogic.deleteCategory("cat1", "resources", user.getUserId()).isLeft());
223 public void testCreateSubCategory_givenValidSubCategory_thenReturnsSuccessful() {
224 user.setRole(Role.ADMIN.name());
225 SubCategoryDefinition subCatDef = new SubCategoryDefinition();
226 subCatDef.setName("subCat1");
228 when(userValidations.validateUserExists(eq(user.getUserId())))
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));
239 Assert.assertTrue(elementBusinessLogic.createSubCategory(subCatDef, "resources",
240 "cat1", user.getUserId()).isLeft());
244 public void testCreateSubCategory_givenNullSubCategory_thenReturnsError() {
245 Assert.assertTrue(elementBusinessLogic.createSubCategory(null, "resources",
246 "cat1", user.getUserId()).isRight());
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());
256 @Test(expected=ComponentException.class)
257 public void testcreateCategory_VALIDATION_OF_USER_FAILED() {
258 CategoryDefinition catdefinition = new CategoryDefinition();
260 ResponseFormat responseFormat = new ResponseFormat(7);
261 when(userValidations.validateUserExists("")).thenThrow(new ByResponseFormatComponentException(responseFormat));
262 elementBusinessLogic.createCategory(catdefinition,"Service", userid);
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());
279 public void testcreateCategory_Invalid_componentType() throws Exception {
281 CategoryDefinition catdefinition = new CategoryDefinition();
282 catdefinition.setName("CAT01");
283 ResponseFormat responseFormat = new ResponseFormat(9);
284 User user = new User();
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());
294 public void testcreateCategory_Invalid() throws Exception {
296 CategoryDefinition catdefinition = new CategoryDefinition();
297 catdefinition.setName("CAT01");
298 ResponseFormat responseFormat = new ResponseFormat(9);
299 User user = new User();
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());