2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.portal.controller;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNull;
45 import java.io.BufferedReader;
46 import java.io.ByteArrayInputStream;
47 import java.io.IOException;
48 import java.io.PrintWriter;
49 import java.io.StringReader;
50 import java.io.StringWriter;
51 import java.nio.charset.StandardCharsets;
52 import java.util.ArrayList;
53 import java.util.HashMap;
54 import java.util.List;
56 import java.util.SortedSet;
57 import java.util.TreeSet;
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
62 import org.json.simple.JSONObject;
63 import org.junit.Before;
64 import org.junit.Test;
65 import org.junit.runner.RunWith;
66 import org.mockito.InjectMocks;
67 import org.mockito.Matchers;
68 import org.mockito.Mock;
69 import org.mockito.Mockito;
70 import org.mockito.MockitoAnnotations;
71 import org.onap.portalapp.controller.core.RoleController;
72 import org.onap.portalapp.controller.core.RoleFunctionListController;
73 import org.onap.portalapp.controller.core.RoleListController;
74 import org.onap.portalapp.portal.core.MockEPUser;
75 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
76 import org.onap.portalapp.portal.domain.CentralizedApp;
77 import org.onap.portalapp.portal.domain.EPApp;
78 import org.onap.portalapp.portal.domain.EPUser;
79 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
80 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
81 import org.onap.portalapp.portal.ecomp.model.UploadRoleFunctionExtSystem;
82 import org.onap.portalapp.portal.framework.MockitoTestSuite;
83 import org.onap.portalapp.portal.service.AdminRolesService;
84 import org.onap.portalapp.portal.service.EPAppService;
85 import org.onap.portalapp.portal.service.ExternalAccessRolesService;
86 import org.onap.portalapp.portal.transport.CentralV2Role;
87 import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
88 import org.onap.portalapp.portal.utils.EcompPortalUtils;
89 import org.onap.portalapp.util.EPUserUtils;
90 import org.onap.portalsdk.core.domain.Role;
91 import org.onap.portalsdk.core.service.AuditService;
92 import org.powermock.api.mockito.PowerMockito;
93 import org.powermock.core.classloader.annotations.PrepareForTest;
94 import org.powermock.modules.junit4.PowerMockRunner;
95 import org.springframework.http.HttpStatus;
96 import org.springframework.http.ResponseEntity;
97 import org.springframework.mock.web.DelegatingServletInputStream;
98 import org.springframework.web.servlet.ModelAndView;
100 import com.fasterxml.jackson.databind.JsonNode;
101 import com.fasterxml.jackson.databind.ObjectMapper;
103 @RunWith(PowerMockRunner.class)
104 @PrepareForTest({ EPUserUtils.class, EcompPortalUtils.class })
105 public class RoleManageControllerTest {
108 RoleController roleController;
111 RoleListController roleListController;
114 RoleFunctionListController roleFunctionListController;
117 ExternalAccessRolesService externalAccessRolesService;
119 EPUserUtils ePUserUtils = new EPUserUtils();
122 ExternalAccessRolesService externalAccessRolesService1 = null;
125 RoleManageController roleManageController = new RoleManageController();
128 EPAppService appService;
131 AuditService auditService;
134 AdminRolesService adminRolesService;
137 public void setup() {
138 MockitoAnnotations.initMocks(this);
141 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
143 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
144 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
145 NullPointerException nullPointerException = new NullPointerException();
146 MockEPUser mockUser = new MockEPUser();
148 private DelegatingServletInputStream dsi;
151 public void removeRoleRoleFunctionTest() throws Exception {
153 ModelAndView modelandView = new ModelAndView("login.htm");
154 Mockito.when(roleController.removeRoleFunction(mockedRequest, mockedResponse)).thenReturn(modelandView);
155 ModelAndView expectedModelandView = roleManageController.removeRoleRoleFunction(mockedRequest, mockedResponse);
156 assertEquals(expectedModelandView, modelandView);
160 public void addRoleRoRoleFunctionTest() throws Exception {
161 ModelAndView modelandView = new ModelAndView("login.htm");
162 Mockito.when(roleController.addRoleFunction(mockedRequest, mockedResponse)).thenReturn(modelandView);
163 ModelAndView expectedModelandView = roleManageController.addRoleRoRoleFunction(mockedRequest, mockedResponse);
164 assertEquals(expectedModelandView, modelandView);
168 public void removeChildRoleTest() throws Exception {
169 ModelAndView modelandView = new ModelAndView("login.htm");
170 Mockito.when(roleController.removeChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView);
171 ModelAndView expectedModelandView = roleManageController.removeChildRole(mockedRequest, mockedResponse);
172 assertEquals(expectedModelandView, modelandView);
176 public void getRoleIfRoleIdNullTest() throws Exception {
177 PowerMockito.mockStatic(EPUserUtils.class);
178 PowerMockito.mockStatic(EcompPortalUtils.class);
179 EPUser user = mockUser.mockEPUser();
180 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
181 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
182 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
183 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
184 List<EPApp> apps = new ArrayList<>();
185 apps.add(CentralApp());
186 Mockito.when(externalAccessRolesService.getApp(CentralApp().getUebKey())).thenReturn(apps);
187 ResponseEntity<String> result = new ResponseEntity<>(HttpStatus.OK);
188 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(apps.get(0))).thenReturn(result);
189 CentralV2Role answer = new CentralV2Role();
190 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer);
191 List<CentralV2RoleFunction> finalRoleFunctionList = new ArrayList<>();
192 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList);
193 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(null);
194 StringWriter sw = new StringWriter();
195 PrintWriter writer = new PrintWriter(sw);
196 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
197 List<EPApp> appList = new ArrayList<>();
198 appList.add(CentralApp());
199 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
200 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
201 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
202 roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, null);
205 @Test(expected = Exception.class)
206 public void getRoleExceptionTest() throws Exception {
207 Mockito.when(appService.getApp((long) 1)).thenReturn(mockApp());
208 CentralV2Role answer = new CentralV2Role();
209 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer);
210 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenThrow(nullPointerException);
211 roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, null);
216 public void getRoleIfRoleIdNotNullTest() throws Exception {
217 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
218 CentralV2Role answer = new CentralV2Role();
219 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer);
220 List<CentralV2RoleFunction> finalRoleFunctionList = new ArrayList<>();
221 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList);
222 StringWriter sw = new StringWriter();
223 PrintWriter writer = new PrintWriter(sw);
224 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
225 CentralV2Role currentRole = new CentralV2Role();
226 SortedSet<CentralV2Role> parentRoles = new TreeSet<>();
227 CentralV2Role centralV2Role = new CentralV2Role();
228 centralV2Role.setName("test");
229 parentRoles.add(centralV2Role);
230 currentRole.setParentRoles(parentRoles);
231 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(currentRole);
232 List<CentralV2Role> availableChildRoles = new ArrayList<>();
233 availableChildRoles.add(currentRole);
234 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(availableChildRoles);
235 List<EPApp> appList = new ArrayList<>();
236 appList.add(CentralApp());
237 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
238 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
239 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
240 roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, (long) 1);
243 public EPApp mockApp() {
244 EPApp app = new EPApp();
247 app.setImageUrl("test");
248 app.setDescription("test");
249 app.setNotes("test");
252 app.setAppRestEndpoint("test");
253 app.setAlternateUrl("test");
255 app.setMlAppName("test");
256 app.setMlAppAdminId("test");
257 app.setUsername("test");
258 app.setAppPassword("test");
260 app.setEnabled(false);
261 app.setUebKey("test");
262 app.setUebSecret("test");
263 app.setUebTopicName("test");
270 public void getRolesTest() throws Exception {
271 EPApp app = mockApp();
272 app.setCentralAuth(true);
273 PowerMockito.mockStatic(EPUserUtils.class);
274 PowerMockito.mockStatic(EcompPortalUtils.class);
275 EPUser user = mockUser.mockEPUser();
276 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
277 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
278 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
279 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, app)).thenReturn(true);
280 List<CentralV2Role> answer = new ArrayList<>();
281 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
282 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(answer);
283 StringWriter sw = new StringWriter();
284 PrintWriter writer = new PrintWriter(sw);
285 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
286 List<EPApp> appList = new ArrayList<>();
287 appList.add(CentralApp());
288 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
289 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
290 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
291 roleManageController.getRoles(mockedRequest, mockedResponse, (long) 1);
295 public void getRolesExceptionTest() throws Exception {
296 EPApp app = CentralApp();
297 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
298 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenThrow(new java.lang.Exception());
299 roleManageController.getRoles(mockedRequest, mockedResponse, (long) 1);
304 public void getRoleFunctionListTest() throws Exception {
305 PowerMockito.mockStatic(EPUserUtils.class);
306 PowerMockito.mockStatic(EcompPortalUtils.class);
307 EPUser user = mockUser.mockEPUser();
308 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
309 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
310 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
311 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
312 List<CentralV2RoleFunction> answer = new ArrayList<>();
313 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(answer);
314 StringWriter sw = new StringWriter();
315 PrintWriter writer = new PrintWriter(sw);
316 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
317 List<EPApp> appList = new ArrayList<>();
318 appList.add(CentralApp());
319 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
320 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
321 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
322 roleManageController.getRoleFunctionList(mockedRequest, mockedResponse, (long) 1);
325 @Test(expected = Exception.class)
326 public void getRoleFunctionListExceptionTest() throws Exception {
327 Mockito.when(appService.getApp((long) 1)).thenReturn(NonCentralApp());
328 roleManageController.getRoleFunctionList(mockedRequest, mockedResponse, (long) 1);
332 public void saveRoleFunctionTest() throws Exception {
333 PowerMockito.mockStatic(EPUserUtils.class);
334 PowerMockito.mockStatic(EcompPortalUtils.class);
335 EPUser user = mockUser.mockEPUser();
336 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
337 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
338 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
339 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
340 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
341 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
342 addNewFunc.setCode("Test");
343 addNewFunc.setType("Test");
344 addNewFunc.setAction("Test");
345 addNewFunc.setName("Test");
346 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
347 roleFunction.setCode("Test|Test|Test");
348 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
349 Mockito.when(externalAccessRolesService.saveCentralRoleFunction(Matchers.anyObject(), Matchers.anyObject()))
351 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunction.getCode())).thenReturn("Test");
352 Mockito.when(EcompPortalUtils.getFunctionType(roleFunction.getCode())).thenReturn("Test");
353 Mockito.when(EcompPortalUtils.getFunctionAction(roleFunction.getCode())).thenReturn("Test");
354 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
355 List<EPUser> userList = new ArrayList<>();
357 List<EPApp> appList = new ArrayList<>();
358 appList.add(CentralApp());
359 Mockito.when(externalAccessRolesService.getUser("guestT")).thenReturn(userList);
360 StringWriter sw = new StringWriter();
361 PrintWriter writer = new PrintWriter(sw);
362 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
363 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
364 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
365 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
366 PortalRestResponse<String> actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse,
367 addNewFunc, (long) 1);
368 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK,
369 "Saved Successfully!", "Success");
370 assertEquals(expected, actual);
374 public void saveRoleFunctionExceptionTest() throws Exception {
375 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
376 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
377 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
378 addNewFunc.setCode("Test");
379 addNewFunc.setName("Test");
380 Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(null);
381 roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1);
385 public void saveRoleFunctionIfAppNotCentralizedTest() throws Exception {
386 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
387 addNewFunc.setCode("Test");
388 addNewFunc.setName("Test");
389 Mockito.when(appService.getApp((long) 1)).thenReturn(NonCentralApp());
390 roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1);
394 public void removeRoleFunctionTest() throws Exception {
395 PowerMockito.mockStatic(EPUserUtils.class);
396 PowerMockito.mockStatic(EcompPortalUtils.class);
397 EPUser user = mockUser.mockEPUser();
398 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
399 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
400 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
401 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
402 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
403 String roleFun = "{\"name\":\"Test\",\"type\":\"Test\",\"action\":\"Test\", \"code\":\"Test\"}";
404 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
405 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
406 StringWriter sw = new StringWriter();
407 PrintWriter writer = new PrintWriter(sw);
408 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
409 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
411 List<EPApp> appList = new ArrayList<>();
412 appList.add(CentralApp());
413 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
414 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
415 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
416 PortalRestResponse<String> actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse,
418 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK,
419 "Deleted Successfully!", "Success");
420 assertEquals(expected, actual);
424 public void removeRoleFunctionExceptionTest() throws Exception {
425 EPUser user = mockUser.mockEPUser();
426 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
427 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
428 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
429 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
430 Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(roleFunction);
431 StringWriter sw = new StringWriter();
432 PrintWriter writer = new PrintWriter(sw);
433 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
434 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
436 List<EPApp> appList = new ArrayList<>();
437 appList.add(CentralApp());
438 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
439 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
440 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
441 roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1);
445 public void removeRoleFunctionIfAppNotCentralizedTest() throws Exception {
446 EPApp app = mockApp();
447 app.setCentralAuth(false);
448 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
449 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
450 roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1);
454 public void syncRolesTest() throws Exception {
455 PowerMockito.mockStatic(EPUserUtils.class);
456 PowerMockito.mockStatic(EcompPortalUtils.class);
457 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
458 EPUser user = mockUser.mockEPUser();
459 EPApp app = mockApp();
461 Mockito.when(appService.getApp(1l)).thenReturn(app);
462 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
463 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
464 Mockito.doNothing().when(externalAccessRolesService).syncApplicationRolesWithEcompDB(app);
465 List<EPApp> appList = new ArrayList<>();
466 appList.add(CentralApp());
467 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
468 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
469 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
470 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
471 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
472 portalRestResponse.setMessage("Sync roles completed successfully!");
473 portalRestResponse.setResponse("Success");
474 portalRestResponse.setStatus(PortalRestStatusEnum.OK);
475 assertEquals(portalRestResponse, actual);
479 public void syncRolesException() throws Exception {
480 EPUser user = mockUser.mockEPUser();
481 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
482 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true);
483 Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
484 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
485 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
486 portalRestResponse.setMessage(null);
487 portalRestResponse.setResponse("Failed");
488 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
489 assertEquals(portalRestResponse, actual);
493 public void syncRolesUserNullException() throws Exception {
494 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
495 Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
496 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
497 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
498 portalRestResponse.setMessage("Unauthorized User");
499 portalRestResponse.setResponse("Failure");
500 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
501 assertEquals(portalRestResponse, actual);
505 public void syncRolesFunctionsTest() throws Exception {
506 PowerMockito.mockStatic(EPUserUtils.class);
507 PowerMockito.mockStatic(EcompPortalUtils.class);
508 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
509 EPApp app = mockApp();
511 EPUser user = mockUser.mockEPUser();
512 Mockito.when(appService.getApp(1l)).thenReturn(app);
513 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
514 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
515 Mockito.doNothing().when(externalAccessRolesService).syncRoleFunctionFromExternalAccessSystem(app);
516 List<EPApp> appList = new ArrayList<>();
517 appList.add(CentralApp());
518 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
519 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
520 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
521 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
522 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
523 portalRestResponse.setMessage("Sync Functions completed successfully!");
524 portalRestResponse.setResponse("Success");
525 portalRestResponse.setStatus(PortalRestStatusEnum.OK);
526 assertEquals(portalRestResponse, actual);
530 public void syncRolesFunctionsException() throws Exception {
531 EPUser user = mockUser.mockEPUser();
532 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
533 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true);
534 Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
535 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
536 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
537 portalRestResponse.setMessage(null);
538 portalRestResponse.setResponse("Failed");
539 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
540 assertEquals(portalRestResponse, actual);
544 public void syncRolesFunctionsUserNullException() throws Exception {
545 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
546 Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
547 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
548 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
549 portalRestResponse.setMessage("Unauthorized User");
550 portalRestResponse.setResponse("Failure");
551 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
552 assertEquals(portalRestResponse, actual);
556 public void addChildRoleTest() throws Exception {
557 ModelAndView modelandView = new ModelAndView("login.htm");
558 Mockito.when(roleController.addChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView);
559 ModelAndView expectedModelandView = roleManageController.addChildRole(mockedRequest, mockedResponse);
560 assertEquals(expectedModelandView, modelandView);
564 public void removeRoleTest() throws Exception {
565 PowerMockito.mockStatic(EPUserUtils.class);
566 PowerMockito.mockStatic(EcompPortalUtils.class);
567 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
568 List<EPUser> epuserList = new ArrayList<>();
569 List<EPApp> appList = new ArrayList<>();
570 appList.add(CentralApp());
571 EPUser user = mockUser.mockEPUser();
572 epuserList.add(user);
573 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
574 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
575 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
576 StringWriter sw = new StringWriter();
577 PrintWriter writer = new PrintWriter(sw);
578 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
579 ExternalRequestFieldsValidator res = new ExternalRequestFieldsValidator(true, "success");
580 Mockito.when(externalAccessRolesService.deleteDependencyRoleRecord(Matchers.anyLong(), Matchers.anyString(),
581 Matchers.anyString())).thenReturn(res);
582 Mockito.when(externalAccessRolesService.getUser(Matchers.anyString())).thenReturn(epuserList);
583 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
584 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
585 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
586 Map<String, Object> expedtedResponse = new HashMap<String, Object>();
587 expedtedResponse.put("restCallStatus", " Unauthorized user");
588 expedtedResponse.put("availableRoles", new ArrayList<>());
589 Map<String, Object> actualResponse = roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1,
591 assertEquals(actualResponse.size(), expedtedResponse.size());
595 @Test(expected = Exception.class)
596 public void removeRoleExceptionTest() throws Exception {
597 EPUser user = mockUser.mockEPUser();
598 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
599 Mockito.when(appService.getApp((long) 1)).thenThrow(nullPointerException);
600 StringWriter sw = new StringWriter();
601 PrintWriter writer = new PrintWriter(sw);
602 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
603 roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, (long) 1);
606 @SuppressWarnings("unchecked")
608 public void saveRoleNewTest() throws Exception {
609 PowerMockito.mockStatic(EPUserUtils.class);
610 PowerMockito.mockStatic(EcompPortalUtils.class);
611 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
612 List<EPUser> epuserList = new ArrayList<>();
613 List<EPApp> appList = new ArrayList<>();
614 appList.add(CentralApp());
615 EPUser user = mockUser.mockEPUser();
616 epuserList.add(user);
617 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
618 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
619 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
620 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
621 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
622 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
623 JSONObject roleJson = new JSONObject();
624 roleJson.put("name", "test");
625 JSONObject roleJson2 = new JSONObject();
626 List<JSONObject> childRolesJson = new ArrayList<>();
627 List<JSONObject> roleFunctions = new ArrayList<>();
628 roleJson2.put("role", roleJson);
629 roleJson2.put("childRoles", childRolesJson);
630 roleJson2.put("roleFunctions", roleFunctions);
631 ObjectMapper mapper = new ObjectMapper();
632 JsonNode actualObj = mapper.readTree(roleJson2.toString());
633 dsi = new DelegatingServletInputStream(
634 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
635 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
636 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
637 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
638 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
639 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
640 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
641 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
642 .thenReturn(externalRequestFieldsValidator);
643 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
644 final Map<String, Object> expected = new HashMap<>();
645 expected.put("role", new CentralV2Role(null, "test"));
646 expected.put("status", "Success");
647 assertEquals(expected, actual);
650 @SuppressWarnings("unchecked")
652 public void saveRoleUpdateTest() throws Exception {
653 PowerMockito.mockStatic(EPUserUtils.class);
654 PowerMockito.mockStatic(EcompPortalUtils.class);
655 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
656 List<EPUser> epuserList = new ArrayList<>();
657 List<EPApp> appList = new ArrayList<>();
658 appList.add(CentralApp());
659 EPUser user = mockUser.mockEPUser();
660 epuserList.add(user);
661 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
662 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
663 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
664 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
665 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
666 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
667 JSONObject roleJson = new JSONObject();
668 roleJson.put("id", 1);
669 roleJson.put("name", "test");
670 roleJson.put("active", true);
671 JSONObject roleJson2 = new JSONObject();
672 List<JSONObject> childRolesJson = new ArrayList<>();
673 List<JSONObject> roleFunctions = new ArrayList<>();
674 JSONObject roleFunction = new JSONObject();
675 roleFunction.put("code", "test");
676 roleFunction.put("name", "test");
677 roleFunction.put("type", "test");
678 roleFunction.put("action", "test");
679 roleFunctions.add(roleFunction);
680 roleJson.put("roleFunctions", roleFunctions);
681 roleJson2.put("role", roleJson);
682 roleJson2.put("childRoles", childRolesJson);
683 roleJson2.put("roleFunctions", roleFunctions);
684 ObjectMapper mapper = new ObjectMapper();
685 JsonNode actualObj = mapper.readTree(roleJson2.toString());
686 dsi = new DelegatingServletInputStream(
687 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
688 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
689 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
690 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
691 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
692 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
693 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
694 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
695 .thenReturn(externalRequestFieldsValidator);
696 CentralV2Role cenV2Role = new CentralV2Role(1l, "test1");
697 cenV2Role.setActive(true);
698 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(cenV2Role);
699 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
700 final Map<String, Object> expected = new HashMap<>();
701 expected.put("status", "Success");
702 assertEquals(expected.get("status"), actual.get("status"));
705 @SuppressWarnings("unchecked")
707 public void saveRoleInvalidRoleExceptionTest() throws Exception {
708 PowerMockito.mockStatic(EPUserUtils.class);
709 PowerMockito.mockStatic(EcompPortalUtils.class);
710 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
711 List<EPUser> epuserList = new ArrayList<>();
712 List<EPApp> appList = new ArrayList<>();
713 appList.add(CentralApp());
714 EPUser user = mockUser.mockEPUser();
715 epuserList.add(user);
716 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
717 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
718 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
719 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
720 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
721 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
722 JSONObject roleJson = new JSONObject();
723 roleJson.put("id", 1);
724 roleJson.put("name", "test%%");
725 roleJson.put("active", true);
726 JSONObject roleJson2 = new JSONObject();
727 List<JSONObject> childRolesJson = new ArrayList<>();
728 List<JSONObject> roleFunctions = new ArrayList<>();
729 JSONObject roleFunction = new JSONObject();
730 roleFunction.put("code", "test");
731 roleFunction.put("name", "test");
732 roleFunction.put("type", "test");
733 roleFunction.put("action", "test");
734 roleFunctions.add(roleFunction);
735 roleJson.put("roleFunctions", roleFunctions);
736 roleJson2.put("role", roleJson);
737 roleJson2.put("childRoles", childRolesJson);
738 roleJson2.put("roleFunctions", roleFunctions);
739 ObjectMapper mapper = new ObjectMapper();
740 JsonNode actualObj = mapper.readTree(roleJson2.toString());
741 dsi = new DelegatingServletInputStream(
742 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
743 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
744 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
745 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
746 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
747 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
748 final Map<String, Object> expected = new HashMap<>();
749 expected.put("error", "Invalid role name found for 'test%%'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed");
750 assertEquals(expected.get("error"), actual.get("error"));
754 public void saveRoleUnauthorizedUserExceptionTest() throws Exception {
755 PowerMockito.mockStatic(EPUserUtils.class);
756 PowerMockito.mockStatic(EcompPortalUtils.class);
757 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
758 List<EPUser> epuserList = new ArrayList<>();
759 List<EPApp> appList = new ArrayList<>();
760 appList.add(CentralApp());
761 EPUser user = mockUser.mockEPUser();
762 epuserList.add(user);
763 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
764 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
765 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
766 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
767 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
768 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
769 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
770 final Map<String, Object> expected = new HashMap<>();
771 expected.put("error", " Unauthorized user");
772 assertEquals(expected.get("error"), actual.get("error"));
776 public void toggleRoleTest() throws Exception {
777 PowerMockito.mockStatic(EPUserUtils.class);
778 PowerMockito.mockStatic(EcompPortalUtils.class);
779 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
780 List<EPUser> epuserList = new ArrayList<>();
781 List<EPApp> appList = new ArrayList<>();
782 appList.add(CentralApp());
783 EPUser user = mockUser.mockEPUser();
784 epuserList.add(user);
785 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
786 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
787 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
788 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
789 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
790 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
791 CentralV2Role role = new CentralV2Role(1l, "test");
792 role.setActive(true);
793 Role currentRole = new Role();
794 currentRole.setName("test");
795 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
796 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
797 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
798 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
799 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
800 final Map<String, Object> expected = new HashMap<>();
801 expected.put("restcallStatus", "Success");
802 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
806 public void toggleRoleUnauthorizedUserExceptionTest() throws Exception {
807 PowerMockito.mockStatic(EPUserUtils.class);
808 PowerMockito.mockStatic(EcompPortalUtils.class);
809 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
810 List<EPUser> epuserList = new ArrayList<>();
811 List<EPApp> appList = new ArrayList<>();
812 appList.add(CentralApp());
813 EPUser user = mockUser.mockEPUser();
814 epuserList.add(user);
815 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
816 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
817 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
818 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
819 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
820 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
821 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
822 final Map<String, Object> expected = new HashMap<>();
823 expected.put("restcallStatus", " Unauthorized user");
824 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
827 @Test(expected = NullPointerException.class)
828 public void toggleRoleExceptionTest() throws Exception {
829 PowerMockito.mockStatic(EPUserUtils.class);
830 PowerMockito.mockStatic(EcompPortalUtils.class);
831 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
832 List<EPUser> epuserList = new ArrayList<>();
833 List<EPApp> appList = new ArrayList<>();
834 appList.add(CentralApp());
835 EPUser user = mockUser.mockEPUser();
836 epuserList.add(user);
837 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
838 Mockito.doThrow(new NullPointerException()).when(appService).getApp((long) 1);
839 roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
843 public void toggleRoleFailedTest() throws Exception {
844 PowerMockito.mockStatic(EPUserUtils.class);
845 PowerMockito.mockStatic(EcompPortalUtils.class);
846 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
847 List<EPUser> epuserList = new ArrayList<>();
848 List<EPApp> appList = new ArrayList<>();
849 appList.add(CentralApp());
850 EPUser user = mockUser.mockEPUser();
851 epuserList.add(user);
852 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
853 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
854 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
855 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
856 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
857 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
858 CentralV2Role role = new CentralV2Role(1l, "test");
859 role.setActive(true);
860 Role currentRole = new Role();
861 currentRole.setName("test");
862 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
863 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
864 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(false, "");
865 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
866 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
867 final Map<String, Object> expected = new HashMap<>();
868 expected.put("restcallStatus", "Toggle Role Failed");
869 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
873 public void getAvailableChildRolesTest() throws Exception {
874 List<CentralV2Role> centralV2Roles = new ArrayList<>();
875 CentralV2Role centralV2Role = new CentralV2Role();
876 centralV2Role.setName("test");
877 centralV2Role.setId(1l);
878 CentralV2Role centralV2Role2 = new CentralV2Role();
879 centralV2Role2.setName("test");
880 centralV2Role2.setId(1l);
881 centralV2Roles.add(centralV2Role);
882 centralV2Roles.add(centralV2Role2);
883 SortedSet<CentralV2RoleFunction> roleFuns = new TreeSet<>();
884 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test", "test");
885 roleFuns.add(centralV2RoleFunction);
886 CentralV2Role currentRole = new CentralV2Role();
887 currentRole.setName("test");
888 currentRole.setId(1l);
889 currentRole.setRoleFunctions(roleFuns);
890 Mockito.when(externalAccessRolesService.getRolesForApp(CentralApp().getUebKey())).thenReturn(centralV2Roles);
891 Mockito.when(externalAccessRolesService.getRoleInfo(1l, CentralApp().getUebKey())).thenReturn(currentRole);
892 List<CentralV2Role> actual = roleManageController.getAvailableChildRoles(CentralApp().getUebKey(), 1l);
893 assertEquals(new ArrayList<>().size(), actual.size());
897 public void getCentralizedAppRolesTest() throws IOException {
898 PowerMockito.mockStatic(EPUserUtils.class);
899 EPUser user = mockUser.mockEPUser();
900 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
901 List<CentralizedApp> cenApps = new ArrayList<>();
902 CentralizedApp centralizedApp = new CentralizedApp();
903 centralizedApp.setAppId(1);
904 centralizedApp.setAppName("Test");
905 cenApps.add(centralizedApp);
906 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
907 Mockito.when(externalAccessRolesService.getCentralizedAppsOfUser(Matchers.anyString())).thenReturn(cenApps);
908 List<CentralizedApp> actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
909 assertEquals(cenApps.size(), actual.size());
913 public void getCentralizedAppRolesExceptionTest() throws IOException {
914 PowerMockito.mockStatic(EPUserUtils.class);
915 EPUser user = mockUser.mockEPUser();
916 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
917 List<CentralizedApp> cenApps = new ArrayList<>();
918 CentralizedApp centralizedApp = new CentralizedApp();
919 centralizedApp.setAppId(1);
920 centralizedApp.setAppName("Test");
921 cenApps.add(centralizedApp);
922 Mockito.when(adminRolesService.isAccountAdmin(Matchers.anyObject())).thenReturn(false);
923 List<CentralizedApp> actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
928 public void bulkUploadRoleFuncUserNullTest() {
929 UploadRoleFunctionExtSystem data = Mockito.mock(UploadRoleFunctionExtSystem.class);
930 Mockito.when(appService.getApp(127L)).thenReturn(null);
931 PortalRestResponse<String> response = roleManageController.bulkUploadRoleFunc(mockedRequest, mockedResponse, data, 127L);
932 assertEquals(PortalRestStatusEnum.ERROR, response.getStatus());
933 assertEquals("Unauthorized User", response.getMessage());
934 assertEquals("Failure", response.getResponse());
937 public CentralV2RoleFunction mockCentralRoleFunction() {
938 CentralV2RoleFunction roleFunction = new CentralV2RoleFunction();
939 roleFunction.setCode("Test");
940 roleFunction.setName("Test");
941 roleFunction.setAppId((long) 1);
945 public EPApp CentralApp() {
946 EPApp app = mockApp();
947 app.setCentralAuth(true);
948 app.setNameSpace("com.test");
952 public EPApp NonCentralApp() {
953 EPApp app = mockApp();
954 app.setCentralAuth(false);