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 java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
39 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
40 import org.openecomp.sdc.be.components.validation.UserValidations;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.Product;
48 import org.openecomp.sdc.be.model.Resource;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.model.category.CategoryDefinition;
52 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
55 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
56 import org.openecomp.sdc.be.user.Role;
57 import org.openecomp.sdc.be.user.UserBusinessLogic;
58 import org.openecomp.sdc.exception.ResponseFormat;
59 import fj.data.Either;
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 {
75 private ComponentsUtils componentsUtils;
78 private UserBusinessLogic userAdminManager;
81 private JanusGraphDao janusGraphDao;
84 private UserValidations userValidations;
87 private ToscaOperationFacade toscaOperationFacade;
90 private IElementOperation elementOperation;
93 ElementBusinessLogic elementBusinessLogic;
98 elementBusinessLogic = new ElementBusinessLogic();
99 MockitoAnnotations.initMocks(this);
101 user.setUserId("admin");
105 public 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);
118 Mockito.when(toscaOperationFacade.getFollowed(eq(user.getUserId()), Mockito.anySet(), Mockito.anySet(), Mockito.eq(ComponentTypeEnum.RESOURCE)))
119 .thenReturn(Either.left(resources));
120 Mockito.when(toscaOperationFacade.getFollowed(eq(user.getUserId()), anySet(), anySet(), eq(ComponentTypeEnum.SERVICE)))
121 .thenReturn(Either.left(services));
123 Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
124 Assert.assertTrue(result.get("services").size() == 1);
125 Assert.assertTrue(result.get("resources").size() == 1);
129 public void testGetFollowed_givenUserWithTesterRoleErrorOccursGettingService_thenReturnsError () {
130 user.setUserId("tester1");
131 user.setRole(Role.TESTER.name());
133 Set<Component> resources = new HashSet<>();
135 Resource resource = new Resource();
136 resources.add(resource);
138 Mockito.when(toscaOperationFacade.getFollowed(any(), Mockito.anySet(), any(), Mockito.eq(ComponentTypeEnum.RESOURCE)))
139 .thenReturn(Either.left(resources));
140 Mockito.when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.SERVICE)))
141 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
142 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
146 public void testGetFollowed_givenUserWithGovernorRole_thenReturnsSuccessful(){
147 user.setUserId("governor1");
148 user.setRole(Role.GOVERNOR.name());
150 List<Service> services = new ArrayList<>();
151 services.add(new Service());
153 when(toscaOperationFacade.getCertifiedServicesWithDistStatus(any()))
154 .thenReturn(Either.left(services));
155 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isLeft());
159 public void testGetFollowed_givenUserWithOPSRoleErrorOccursGettingServices_thenReturnsError(){
160 user.setUserId("ops1");
161 user.setRole(Role.OPS.name());
163 when(toscaOperationFacade.getCertifiedServicesWithDistStatus(any()))
164 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
166 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
171 public void testGetFollowed_givenUserWithProductStrategistRole_thenReturnsEmptyList(){
172 user.setUserId("pstra1");
173 user.setRole(Role.PRODUCT_STRATEGIST.name());
175 Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
176 Assert.assertEquals("products list should be empty", 0, result.get("products").size());
181 public void testGetFollowed_givenUserWithProductManagerRole_thenReturnsProducts(){
182 user.setUserId("pmanager1");
183 user.setRole(Role.PRODUCT_MANAGER.name());
185 Set<Component> products = new HashSet<>();
186 products.add(new Product());
188 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.PRODUCT)))
189 .thenReturn(Either.left(products));
191 Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
192 Assert.assertEquals("1 product should exist", 1, result.get("products").size());
197 public void testGetFollowed_givenUserWithRoleAdminErrorOccursGettingResources_thenReturnsError() {
198 user.setUserId("admin1");
199 user.setRole(Role.ADMIN.name());
201 when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
202 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
204 Assert.assertTrue(elementBusinessLogic.getFollowed(user).isRight());
208 public void testGetAllCategories_givenUserIsNull_thenReturnsError() {
209 Assert.assertTrue(elementBusinessLogic.getAllCategories(null, null).isRight());
212 @Test(expected = ComponentException.class)
213 public void testGetAllCategories_givenValidationOfUserFails_thenReturnsError() {
214 doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserExists(eq(user.getUserId()),
215 anyString(), anyBoolean());
216 elementBusinessLogic.getAllCategories(null, user.getUserId());
220 public void testGetAllCategories_givenInvalidComponentType_thenReturnsError() {
221 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean())).thenReturn(user);
223 Assert.assertTrue(elementBusinessLogic.getAllCategories("NONE", user.getUserId()).isRight());
228 public void testGetAllCategories_givenValidUserAndComponentType_thenReturnsSuccessful() {
230 List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
231 categoryDefinitionList.add(new CategoryDefinition());
233 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean())).thenReturn(user);
234 when(elementOperation.getAllCategories(NodeTypeEnum.ResourceNewCategory, false))
235 .thenReturn(Either.left(categoryDefinitionList));
236 Assert.assertTrue(elementBusinessLogic.getAllCategories(ComponentTypeEnum.RESOURCE_PARAM_NAME, user.getUserId())
241 public void testGetAllCategories_givenValidUserId_thenReturnsSuccessful() {
243 List<CategoryDefinition> dummyCategoryDefinitionList = new ArrayList<>();
244 dummyCategoryDefinitionList.add(new CategoryDefinition());
246 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean()))
248 when(elementOperation.getAllCategories(any(NodeTypeEnum.class), anyBoolean()))
249 .thenReturn(Either.left(dummyCategoryDefinitionList));
251 Assert.assertTrue(elementBusinessLogic.getAllCategories(user.getUserId()).isLeft());
255 public void testDeleteCategory_givenValidComponentTypeAndCategoryId_thenReturnsSuccessful() {
257 when(elementOperation.deleteCategory(any(NodeTypeEnum.class), anyString()))
258 .thenReturn(Either.left(new CategoryDefinition()));
260 Assert.assertTrue(elementBusinessLogic.deleteCategory("cat1", "resources", user.getUserId()).isLeft());
264 public void testCreateSubCategory_givenValidSubCategory_thenReturnsSuccessful() {
265 user.setRole(Role.ADMIN.name());
266 SubCategoryDefinition subCatDef = new SubCategoryDefinition();
267 subCatDef.setName("subCat1");
269 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean()))
271 when(elementOperation.getCategory(any(NodeTypeEnum.class), anyString()))
272 .thenReturn(Either.left(new CategoryDefinition()));
273 when(elementOperation.isSubCategoryUniqueForCategory(any(NodeTypeEnum.class), anyString(), anyString()))
274 .thenReturn(Either.left(Boolean.TRUE));
275 when(elementOperation.getSubCategoryUniqueForType(any(NodeTypeEnum.class), anyString()))
276 .thenReturn(Either.left(subCatDef));
277 when(elementOperation.createSubCategory(anyString(), any(SubCategoryDefinition.class), any(NodeTypeEnum.class)))
278 .thenReturn(Either.left(subCatDef));
280 Assert.assertTrue(elementBusinessLogic.createSubCategory(subCatDef, "resources",
281 "cat1", user.getUserId()).isLeft());
285 public void testCreateSubCategory_givenNullSubCategory_thenReturnsError() {
286 Assert.assertTrue(elementBusinessLogic.createSubCategory(null, "resources",
287 "cat1", user.getUserId()).isRight());
290 @Test(expected = ComponentException.class)
291 public void testCreateSubCategory_givenUserValidationFails_thenReturnsException() {
292 SubCategoryDefinition subCategoryDefinition = new SubCategoryDefinition();
293 doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserExists(eq(user.getUserId()),
294 anyString(), anyBoolean());
295 elementBusinessLogic.createSubCategory(subCategoryDefinition, "resources", "cat1", user.getUserId());
299 public void testcreateCategory_VALIDATION_OF_USER_FAILED() throws Exception {
302 CategoryDefinition catdefinition = new CategoryDefinition();
304 ResponseFormat responseFormat = new ResponseFormat(7);
305 when(componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION)).thenReturn(responseFormat);
306 Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", userid);
307 Assert.assertEquals(true,response.isRight());
308 Assert.assertEquals((Integer) 7, response.right().value().getStatus());
312 public void testcreateCategory_MISSING_INFORMATION() throws Exception {
314 CategoryDefinition catdefinition = new CategoryDefinition();
315 ResponseFormat responseFormat = new ResponseFormat(9);
316 User user = new User();
317 when(userAdminManager.getUser("USR", false)).thenReturn(Either.left(user));
318 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
319 Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
320 Assert.assertEquals(true,response.isRight());
321 Assert.assertEquals((Integer) 9, response.right().value().getStatus());
325 public void testcreateCategory_RESTRICTED_OPERATION() throws Exception {
327 CategoryDefinition catdefinition = new CategoryDefinition();
328 ResponseFormat responseFormat = new ResponseFormat(9);
329 User user = new User();
330 when(userAdminManager.getUser("USR", false)).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
331 when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat);
332 Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
333 Assert.assertEquals(true,response.isRight());
334 Assert.assertEquals((Integer) 9, response.right().value().getStatus());
338 public void testcreateCategory_Invalid_componentType() throws Exception {
340 CategoryDefinition catdefinition = new CategoryDefinition();
341 catdefinition.setName("CAT01");
342 ResponseFormat responseFormat = new ResponseFormat(9);
343 User user = new User();
345 when(userAdminManager.getUser("USR", false)).thenReturn(Either.left(user));
346 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
347 Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"Service", "USR");
348 Assert.assertEquals(true,response.isRight());
349 Assert.assertEquals((Integer) 9, response.right().value().getStatus());
353 public void testcreateCategory_Invalid() throws Exception {
355 CategoryDefinition catdefinition = new CategoryDefinition();
356 catdefinition.setName("CAT01");
357 ResponseFormat responseFormat = new ResponseFormat(9);
358 User user = new User();
360 when(userAdminManager.getUser("USR", false)).thenReturn(Either.left(user));
361 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
362 Either<CategoryDefinition, ResponseFormat> response = elementBusinessLogic.createCategory(catdefinition,"SERVICE_PARAM_NAME", "USR");
363 Assert.assertEquals(true,response.isRight());
364 Assert.assertEquals((Integer) 9, response.right().value().getStatus());