Reduce number of parameters
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / RoleManageControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
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
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
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
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
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.
35  *
36  * ============LICENSE_END============================================
37  *
38  * 
39  */
40 package org.onap.portalapp.portal.controller;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNull;
44 import java.io.BufferedReader;
45 import java.io.ByteArrayInputStream;
46 import java.io.IOException;
47 import java.io.PrintWriter;
48 import java.io.StringReader;
49 import java.io.StringWriter;
50 import java.nio.charset.StandardCharsets;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.SortedSet;
56 import java.util.TreeSet;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59 import org.json.simple.JSONObject;
60 import org.junit.Before;
61 import org.junit.Test;
62 import org.junit.runner.RunWith;
63 import org.mockito.InjectMocks;
64 import org.mockito.Matchers;
65 import org.mockito.Mock;
66 import org.mockito.Mockito;
67 import org.mockito.MockitoAnnotations;
68 import org.onap.portalapp.controller.core.RoleController;
69 import org.onap.portalapp.controller.core.RoleFunctionListController;
70 import org.onap.portalapp.controller.core.RoleListController;
71 import org.onap.portalapp.portal.core.MockEPUser;
72 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
73 import org.onap.portalapp.portal.domain.CentralizedApp;
74 import org.onap.portalapp.portal.domain.EPApp;
75 import org.onap.portalapp.portal.domain.EPUser;
76 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
77 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
78 import org.onap.portalapp.portal.ecomp.model.UploadRoleFunctionExtSystem;
79 import org.onap.portalapp.portal.framework.MockitoTestSuite;
80 import org.onap.portalapp.portal.service.AdminRolesService;
81 import org.onap.portalapp.portal.service.EPAppService;
82 import org.onap.portalapp.portal.service.ExternalAccessRolesService;
83 import org.onap.portalapp.portal.transport.CentralV2Role;
84 import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
85 import org.onap.portalapp.portal.utils.EcompPortalUtils;
86 import org.onap.portalapp.util.EPUserUtils;
87 import org.onap.portalsdk.core.domain.Role;
88 import org.onap.portalsdk.core.service.AuditService;
89 import org.powermock.api.mockito.PowerMockito;
90 import org.powermock.core.classloader.annotations.PrepareForTest;
91 import org.powermock.modules.junit4.PowerMockRunner;
92 import org.springframework.http.HttpStatus;
93 import org.springframework.http.ResponseEntity;
94 import org.springframework.mock.web.DelegatingServletInputStream;
95 import org.springframework.web.servlet.ModelAndView;
96 import com.fasterxml.jackson.databind.JsonNode;
97 import com.fasterxml.jackson.databind.ObjectMapper;
98
99 @RunWith(PowerMockRunner.class)
100 @PrepareForTest({ EPUserUtils.class, EcompPortalUtils.class })
101 public class RoleManageControllerTest {
102
103         @Mock
104         RoleController roleController;
105
106         @Mock
107         RoleListController roleListController;
108
109         @Mock
110         RoleFunctionListController roleFunctionListController;
111
112         @Mock
113         ExternalAccessRolesService externalAccessRolesService;
114         @Mock
115         EPUserUtils ePUserUtils = new EPUserUtils();
116
117         @Mock
118         ExternalAccessRolesService externalAccessRolesService1 = null;
119
120         @InjectMocks
121         RoleManageController roleManageController = new RoleManageController();
122
123         @Mock
124         EPAppService appService;
125
126         @Mock
127         AuditService auditService;
128
129         @Mock
130         AdminRolesService adminRolesService;
131
132         @Before
133         public void setup() {
134                 MockitoAnnotations.initMocks(this);
135         }
136
137         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
138
139         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
140         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
141         NullPointerException nullPointerException = new NullPointerException();
142         MockEPUser mockUser = new MockEPUser();
143
144         private DelegatingServletInputStream dsi;
145
146         @Test
147         public void removeRoleRoleFunctionTest() throws Exception {
148
149                 ModelAndView modelandView = new ModelAndView("login.htm");
150                 Mockito.when(roleController.removeRoleFunction(mockedRequest, mockedResponse)).thenReturn(modelandView);
151                 ModelAndView expectedModelandView = roleManageController.removeRoleRoleFunction(mockedRequest, mockedResponse);
152                 assertEquals(expectedModelandView, modelandView);
153         }
154
155         @Test
156         public void addRoleRoRoleFunctionTest() throws Exception {
157                 ModelAndView modelandView = new ModelAndView("login.htm");
158                 Mockito.when(roleController.addRoleFunction(mockedRequest, mockedResponse)).thenReturn(modelandView);
159                 ModelAndView expectedModelandView = roleManageController.addRoleRoRoleFunction(mockedRequest, mockedResponse);
160                 assertEquals(expectedModelandView, modelandView);
161         }
162
163         @Test
164         public void removeChildRoleTest() throws Exception {
165                 ModelAndView modelandView = new ModelAndView("login.htm");
166                 Mockito.when(roleController.removeChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView);
167                 ModelAndView expectedModelandView = roleManageController.removeChildRole(mockedRequest, mockedResponse);
168                 assertEquals(expectedModelandView, modelandView);
169         }
170
171         @Test
172         public void getRoleIfRoleIdNullTest() throws Exception {
173                 PowerMockito.mockStatic(EPUserUtils.class);
174                 PowerMockito.mockStatic(EcompPortalUtils.class);
175                 EPUser user = mockUser.mockEPUser();
176                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
177                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
178                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
179                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
180                 List<EPApp> apps = new ArrayList<>();
181                 apps.add(CentralApp());
182                 Mockito.when(externalAccessRolesService.getApp(CentralApp().getUebKey())).thenReturn(apps);
183                 ResponseEntity<String> result = new ResponseEntity<>(HttpStatus.OK);
184                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(apps.get(0))).thenReturn(result);
185                 CentralV2Role answer = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
186                 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer);
187                 List<CentralV2RoleFunction> finalRoleFunctionList = new ArrayList<>();
188                 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList);
189                 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(null);
190                 StringWriter sw = new StringWriter();
191                 PrintWriter writer = new PrintWriter(sw);
192                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
193                 List<EPApp> appList = new ArrayList<>();
194                 appList.add(CentralApp());
195                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
196                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
197                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
198                 roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, null);
199         }
200
201         @Test(expected = Exception.class)
202         public void getRoleExceptionTest() throws Exception {
203                 Mockito.when(appService.getApp((long) 1)).thenReturn(mockApp());
204                 CentralV2Role answer = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
205                 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer);
206                 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenThrow(nullPointerException);
207                 roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, null);
208
209         }
210
211         @Test
212         public void getRoleIfRoleIdNotNullTest() throws Exception {
213                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
214                 CentralV2Role answer = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
215                 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(answer);
216                 List<CentralV2RoleFunction> finalRoleFunctionList = new ArrayList<>();
217                 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(finalRoleFunctionList);
218                 StringWriter sw = new StringWriter();
219                 PrintWriter writer = new PrintWriter(sw);
220                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
221                 CentralV2Role currentRole = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
222                 SortedSet<CentralV2Role> parentRoles = new TreeSet<>();
223         CentralV2Role centralV2Role = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
224                 centralV2Role.setName("test");
225                 parentRoles.add(centralV2Role);
226                 currentRole.setParentRoles(parentRoles);
227                 Mockito.when(externalAccessRolesService.getRoleInfo((long) 1, "test")).thenReturn(currentRole);
228                 List<CentralV2Role> availableChildRoles = new ArrayList<>();
229                 availableChildRoles.add(currentRole);
230                 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(availableChildRoles);
231                 List<EPApp> appList = new ArrayList<>();
232                 appList.add(CentralApp());
233                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
234                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
235                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
236                 roleManageController.getRole(mockedRequest, mockedResponse, (long) 1, (long) 1);
237         }
238
239         public EPApp mockApp() {
240                 EPApp app = new EPApp();
241
242                 app.setName("Test");
243                 app.setImageUrl("test");
244                 app.setDescription("test");
245                 app.setNotes("test");
246                 app.setUrl("test");
247                 app.setId((long) 1);
248                 app.setAppRestEndpoint("test");
249                 app.setAlternateUrl("test");
250                 app.setName("test");
251                 app.setMlAppName("test");
252                 app.setMlAppAdminId("test");
253                 app.setUsername("test");
254                 app.setAppPassword("test");
255                 app.setOpen(false);
256                 app.setEnabled(false);
257                 app.setUebKey("test");
258                 app.setUebSecret("test");
259                 app.setUebTopicName("test");
260                 app.setAppType(1);
261                 return app;
262
263         }
264
265         @Test
266         public void getRolesTest() throws Exception {
267                 EPApp app = mockApp();
268                 app.setCentralAuth(true);
269                 PowerMockito.mockStatic(EPUserUtils.class);
270                 PowerMockito.mockStatic(EcompPortalUtils.class);
271                 EPUser user = mockUser.mockEPUser();
272                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
273                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
274                 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
275                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, app)).thenReturn(true);
276                 List<CentralV2Role> answer = new ArrayList<>();
277                 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
278                 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenReturn(answer);
279                 StringWriter sw = new StringWriter();
280                 PrintWriter writer = new PrintWriter(sw);
281                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
282                 List<EPApp> appList = new ArrayList<>();
283                 appList.add(CentralApp());
284                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
285                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
286                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
287                 roleManageController.getRoles(mockedRequest, mockedResponse, (long) 1);
288         }
289
290         @Test
291         public void getRolesExceptionTest() throws Exception {
292                 EPApp app = CentralApp();
293                 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
294                 Mockito.when(externalAccessRolesService.getRolesForApp("test")).thenThrow(new java.lang.Exception());
295                 roleManageController.getRoles(mockedRequest, mockedResponse, (long) 1);
296
297         }
298
299         @Test
300         public void getRoleFunctionListTest() throws Exception {
301                 PowerMockito.mockStatic(EPUserUtils.class);
302                 PowerMockito.mockStatic(EcompPortalUtils.class);
303                 EPUser user = mockUser.mockEPUser();
304                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
305                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
306                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
307                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
308                 List<CentralV2RoleFunction> answer = new ArrayList<>();
309                 Mockito.when(externalAccessRolesService.getRoleFuncList("test")).thenReturn(answer);
310                 StringWriter sw = new StringWriter();
311                 PrintWriter writer = new PrintWriter(sw);
312                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
313                 List<EPApp> appList = new ArrayList<>();
314                 appList.add(CentralApp());
315                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
316                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
317                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
318                 roleManageController.getRoleFunctionList(mockedRequest, mockedResponse, (long) 1);
319         }
320
321         @Test(expected = Exception.class)
322         public void getRoleFunctionListExceptionTest() throws Exception {
323                 Mockito.when(appService.getApp((long) 1)).thenReturn(NonCentralApp());
324                 roleManageController.getRoleFunctionList(mockedRequest, mockedResponse, (long) 1);
325         }
326
327         @Test
328         public void saveRoleFunctionTest() throws Exception {
329                 PowerMockito.mockStatic(EPUserUtils.class);
330                 PowerMockito.mockStatic(EcompPortalUtils.class);
331                 EPUser user = mockUser.mockEPUser();
332                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
333                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
334                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
335                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
336                 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
337                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
338                 addNewFunc.setCode("Test");
339                 addNewFunc.setType("Test");
340                 addNewFunc.setAction("Test");
341                 addNewFunc.setName("Test");
342                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
343                 roleFunction.setCode("Test|Test|Test");
344                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
345                 Mockito.when(externalAccessRolesService.saveCentralRoleFunction(Matchers.anyObject(), Matchers.anyObject()))
346                                 .thenReturn(true);
347                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunction.getCode())).thenReturn("Test");
348                 Mockito.when(EcompPortalUtils.getFunctionType(roleFunction.getCode())).thenReturn("Test");
349                 Mockito.when(EcompPortalUtils.getFunctionAction(roleFunction.getCode())).thenReturn("Test");
350                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
351                 List<EPUser> userList = new ArrayList<>();
352                 userList.add(user);
353                 List<EPApp> appList = new ArrayList<>();
354                 appList.add(CentralApp());
355                 Mockito.when(externalAccessRolesService.getUser("guestT")).thenReturn(userList);
356                 StringWriter sw = new StringWriter();
357                 PrintWriter writer = new PrintWriter(sw);
358                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
359                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
360                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
361                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
362                 PortalRestResponse<String> actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse,
363                                 addNewFunc, (long) 1);
364                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK,
365                                 "Saved Successfully!", "Success");
366                 assertEquals(expected, actual);
367         }
368
369         @Test
370         public void saveRoleFunctionXSSTest() throws Exception {
371                 PowerMockito.mockStatic(EPUserUtils.class);
372                 PowerMockito.mockStatic(EcompPortalUtils.class);
373                 EPUser user = mockUser.mockEPUser();
374                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
375                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
376                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
377                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
378                 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
379                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
380                 addNewFunc.setCode("“><script>alert(“XSS”)</script>");
381                 addNewFunc.setType("Test");
382                 addNewFunc.setAction("Test");
383                 addNewFunc.setName("Test");
384                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
385                 roleFunction.setCode("Test|Test|Test");
386                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
387                 Mockito.when(externalAccessRolesService.saveCentralRoleFunction(Matchers.anyObject(), Matchers.anyObject()))
388                         .thenReturn(true);
389                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunction.getCode())).thenReturn("Test");
390                 Mockito.when(EcompPortalUtils.getFunctionType(roleFunction.getCode())).thenReturn("Test");
391                 Mockito.when(EcompPortalUtils.getFunctionAction(roleFunction.getCode())).thenReturn("Test");
392                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
393                 List<EPUser> userList = new ArrayList<>();
394                 userList.add(user);
395                 List<EPApp> appList = new ArrayList<>();
396                 appList.add(CentralApp());
397                 Mockito.when(externalAccessRolesService.getUser("guestT")).thenReturn(userList);
398                 StringWriter sw = new StringWriter();
399                 PrintWriter writer = new PrintWriter(sw);
400                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
401                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
402                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
403                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
404                 PortalRestResponse<String> actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse,
405                         addNewFunc, (long) 1);
406                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
407                         "Data is not valid", "ERROR");
408                 assertEquals(expected, actual);
409         }
410
411         @Test
412         public void saveRoleFunctionExceptionTest() throws Exception {
413                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
414                 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
415                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
416                 addNewFunc.setCode("Test");
417                 addNewFunc.setName("Test");
418                 Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(null);
419                 roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1);
420         }
421
422         @Test
423         public void saveRoleFunctionIfAppNotCentralizedTest() throws Exception {
424                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
425                 addNewFunc.setCode("Test");
426                 addNewFunc.setName("Test");
427                 Mockito.when(appService.getApp((long) 1)).thenReturn(NonCentralApp());
428                 roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1);
429         }
430
431         @Test
432         public void removeRoleFunctionTest() throws Exception {
433                 PowerMockito.mockStatic(EPUserUtils.class);
434                 PowerMockito.mockStatic(EcompPortalUtils.class);
435                 EPUser user = mockUser.mockEPUser();
436                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
437                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
438                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
439                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
440                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
441                 String roleFun = "{\"name\":\"Test\",\"type\":\"Test\",\"action\":\"Test\", \"code\":\"Test\"}";
442                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
443                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
444                 StringWriter sw = new StringWriter();
445                 PrintWriter writer = new PrintWriter(sw);
446                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
447                 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
448                                 .thenReturn(true);
449                 List<EPApp> appList = new ArrayList<>();
450                 appList.add(CentralApp());
451                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
452                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
453                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
454                 PortalRestResponse<String> actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse,
455                                 roleFun, (long) 1);
456                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK,
457                                 "Deleted Successfully!", "Success");
458                 assertEquals(expected, actual);
459         }
460
461         @Test
462         public void removeRoleFunctionXSSTest() throws Exception {
463                 PowerMockito.mockStatic(EPUserUtils.class);
464                 PowerMockito.mockStatic(EcompPortalUtils.class);
465                 EPUser user = mockUser.mockEPUser();
466                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
467                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
468                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
469                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
470                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
471                 String roleFun = "<script>alert(/XSS”)</script>";
472                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
473                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
474                 StringWriter sw = new StringWriter();
475                 PrintWriter writer = new PrintWriter(sw);
476                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
477                 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
478                         .thenReturn(true);
479                 List<EPApp> appList = new ArrayList<>();
480                 appList.add(CentralApp());
481                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
482                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
483                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
484                 PortalRestResponse<String> actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse,
485                         roleFun, (long) 1);
486                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
487                         "Data is not valid", "ERROR");
488                 assertEquals(expected, actual);
489         }
490
491         @Test
492         public void removeRoleFunctionExceptionTest() throws Exception {
493                 EPUser user = mockUser.mockEPUser();
494                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
495                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
496                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
497                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
498                 Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(roleFunction);
499                 StringWriter sw = new StringWriter();
500                 PrintWriter writer = new PrintWriter(sw);
501                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
502                 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
503                                 .thenReturn(false);
504                 List<EPApp> appList = new ArrayList<>();
505                 appList.add(CentralApp());
506                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
507                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
508                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
509                 roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1);
510         }
511
512         @Test
513         public void removeRoleFunctionIfAppNotCentralizedTest() throws Exception {
514                 EPApp app = mockApp();
515                 app.setCentralAuth(false);
516                 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
517                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
518                 roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1);
519         }
520
521         @Test
522         public void syncRolesTest() throws Exception {
523                 PowerMockito.mockStatic(EPUserUtils.class);
524                 PowerMockito.mockStatic(EcompPortalUtils.class);
525                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
526                 EPUser user = mockUser.mockEPUser();
527                 EPApp app = mockApp();
528                 app.setId((long) 1);
529                 Mockito.when(appService.getApp(1l)).thenReturn(app);
530                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
531                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
532                 Mockito.doNothing().when(externalAccessRolesService).syncApplicationRolesWithEcompDB(app);
533                 List<EPApp> appList = new ArrayList<>();
534                 appList.add(CentralApp());
535                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
536                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
537                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
538                 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
539                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
540                 portalRestResponse.setMessage("Sync roles completed successfully!");
541                 portalRestResponse.setResponse("Success");
542                 portalRestResponse.setStatus(PortalRestStatusEnum.OK);
543                 assertEquals(portalRestResponse, actual);
544         }
545
546         @Test
547         public void syncRolesException() throws Exception {
548       EPUser user = mockUser.mockEPUser();
549       Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
550       Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true);
551                 Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
552                 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
553                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
554                 portalRestResponse.setMessage(null);
555                 portalRestResponse.setResponse("Failed");
556                 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
557                 assertEquals(portalRestResponse, actual);
558         }
559
560     @Test
561     public void syncRolesUserNullException() throws Exception {
562         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
563         Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
564         PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
565         PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
566         portalRestResponse.setMessage("Unauthorized User");
567         portalRestResponse.setResponse("Failure");
568         portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
569         assertEquals(portalRestResponse, actual);
570     }
571
572         @Test
573         public void syncRolesFunctionsTest() throws Exception {
574                 PowerMockito.mockStatic(EPUserUtils.class);
575                 PowerMockito.mockStatic(EcompPortalUtils.class);
576                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
577                 EPApp app = mockApp();
578                 app.setId((long) 1);
579                 EPUser user = mockUser.mockEPUser();
580                 Mockito.when(appService.getApp(1l)).thenReturn(app);
581                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
582                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
583                 Mockito.doNothing().when(externalAccessRolesService).syncRoleFunctionFromExternalAccessSystem(app);
584                 List<EPApp> appList = new ArrayList<>();
585                 appList.add(CentralApp());
586                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
587                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
588                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
589                 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
590                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
591                 portalRestResponse.setMessage("Sync Functions completed successfully!");
592                 portalRestResponse.setResponse("Success");
593                 portalRestResponse.setStatus(PortalRestStatusEnum.OK);
594                 assertEquals(portalRestResponse, actual);
595         }
596
597         @Test
598         public void syncRolesFunctionsException() throws Exception {
599       EPUser user = mockUser.mockEPUser();
600       Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
601       Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true);
602       Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
603                 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
604                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
605                 portalRestResponse.setMessage(null);
606                 portalRestResponse.setResponse("Failed");
607                 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
608                 assertEquals(portalRestResponse, actual);
609         }
610
611     @Test
612     public void syncRolesFunctionsUserNullException() throws Exception {
613         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
614         Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
615         PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
616         PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
617         portalRestResponse.setMessage("Unauthorized User");
618         portalRestResponse.setResponse("Failure");
619         portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
620         assertEquals(portalRestResponse, actual);
621     }
622
623         @Test
624         public void addChildRoleTest() throws Exception {
625                 ModelAndView modelandView = new ModelAndView("login.htm");
626                 Mockito.when(roleController.addChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView);
627                 ModelAndView expectedModelandView = roleManageController.addChildRole(mockedRequest, mockedResponse);
628                 assertEquals(expectedModelandView, modelandView);
629         }
630
631         @Test
632         public void removeRoleTest() throws Exception {
633                 PowerMockito.mockStatic(EPUserUtils.class);
634                 PowerMockito.mockStatic(EcompPortalUtils.class);
635                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
636                 List<EPUser> epuserList = new ArrayList<>();
637                 List<EPApp> appList = new ArrayList<>();
638                 appList.add(CentralApp());
639                 EPUser user = mockUser.mockEPUser();
640                 epuserList.add(user);
641                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
642                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
643                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
644                 StringWriter sw = new StringWriter();
645                 PrintWriter writer = new PrintWriter(sw);
646                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
647                 ExternalRequestFieldsValidator res = new ExternalRequestFieldsValidator(true, "success");
648                 Mockito.when(externalAccessRolesService.deleteDependencyRoleRecord(Matchers.anyLong(), Matchers.anyString(),
649                                 Matchers.anyString())).thenReturn(res);
650                 Mockito.when(externalAccessRolesService.getUser(Matchers.anyString())).thenReturn(epuserList);
651                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
652                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
653                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
654                 Map<String, Object> expedtedResponse = new HashMap<String, Object>();
655                 expedtedResponse.put("restCallStatus", " Unauthorized user");
656                 expedtedResponse.put("availableRoles", new ArrayList<>());
657                 Map<String, Object> actualResponse = roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1,
658                                 (long) 1);
659                 assertEquals(actualResponse.size(), expedtedResponse.size());
660
661         }
662
663         @Test(expected = Exception.class)
664         public void removeRoleExceptionTest() throws Exception {
665                 EPUser user = mockUser.mockEPUser();
666                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
667                 Mockito.when(appService.getApp((long) 1)).thenThrow(nullPointerException);
668                 StringWriter sw = new StringWriter();
669                 PrintWriter writer = new PrintWriter(sw);
670                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
671                 roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, (long) 1);
672         }
673
674         @SuppressWarnings("unchecked")
675         @Test
676         public void saveRoleNewTest() throws Exception {
677                 PowerMockito.mockStatic(EPUserUtils.class);
678                 PowerMockito.mockStatic(EcompPortalUtils.class);
679                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
680                 List<EPUser> epuserList = new ArrayList<>();
681                 List<EPApp> appList = new ArrayList<>();
682                 appList.add(CentralApp());
683                 EPUser user = mockUser.mockEPUser();
684                 epuserList.add(user);
685                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
686                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
687                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
688                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
689                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
690                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
691                 JSONObject roleJson = new JSONObject();
692                 roleJson.put("name", "test");
693                 JSONObject roleJson2 = new JSONObject();
694                 List<JSONObject> childRolesJson = new ArrayList<>();
695                 List<JSONObject> roleFunctions = new ArrayList<>();
696                 roleJson2.put("role", roleJson);
697                 roleJson2.put("childRoles", childRolesJson);
698                 roleJson2.put("roleFunctions", roleFunctions);
699                 ObjectMapper mapper = new ObjectMapper();
700                 JsonNode actualObj = mapper.readTree(roleJson2.toString());
701                 dsi = new DelegatingServletInputStream(
702                                 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
703                 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
704                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
705                 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
706                 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
707                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
708                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
709                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
710                                 .thenReturn(externalRequestFieldsValidator);
711                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
712                 final Map<String, Object> expected = new HashMap<>();
713         expected.put("role",
714                 new CentralV2Role.CentralV2RoleBuilder().setId(null).setName("test").createCentralV2Role());
715                 expected.put("status", "Success");
716                 assertEquals(expected, actual);
717         }
718
719         @SuppressWarnings("unchecked")
720         @Test
721         public void saveRoleUpdateTest() throws Exception {
722                 PowerMockito.mockStatic(EPUserUtils.class);
723                 PowerMockito.mockStatic(EcompPortalUtils.class);
724                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
725                 List<EPUser> epuserList = new ArrayList<>();
726                 List<EPApp> appList = new ArrayList<>();
727                 appList.add(CentralApp());
728                 EPUser user = mockUser.mockEPUser();
729                 epuserList.add(user);
730                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
731                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
732                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
733                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
734                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
735                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
736                 JSONObject roleJson = new JSONObject();
737                 roleJson.put("id", 1);
738                 roleJson.put("name", "test");
739                 roleJson.put("active", true);
740                 JSONObject roleJson2 = new JSONObject();
741                 List<JSONObject> childRolesJson = new ArrayList<>();
742                 List<JSONObject> roleFunctions = new ArrayList<>();
743                 JSONObject roleFunction = new JSONObject();
744                 roleFunction.put("code", "test");
745                 roleFunction.put("name", "test");
746                 roleFunction.put("type", "test");
747                 roleFunction.put("action", "test");
748                 roleFunctions.add(roleFunction);
749                 roleJson.put("roleFunctions", roleFunctions);
750                 roleJson2.put("role", roleJson);
751                 roleJson2.put("childRoles", childRolesJson);
752                 roleJson2.put("roleFunctions", roleFunctions);
753                 ObjectMapper mapper = new ObjectMapper();
754                 JsonNode actualObj = mapper.readTree(roleJson2.toString());
755                 dsi = new DelegatingServletInputStream(
756                                 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
757                 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
758                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
759                 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
760                 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
761                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
762                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
763                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
764                                 .thenReturn(externalRequestFieldsValidator);
765         CentralV2Role cenV2Role =
766                 new CentralV2Role.CentralV2RoleBuilder().setId(1l).setName("test1").createCentralV2Role();
767                 cenV2Role.setActive(true);
768                 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(cenV2Role);
769                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
770                 final Map<String, Object> expected = new HashMap<>();
771                 expected.put("status", "Success");
772                 assertEquals(expected.get("status"), actual.get("status"));
773         }
774         
775         @SuppressWarnings("unchecked")
776         @Test
777         public void saveRoleInvalidRoleExceptionTest() throws Exception {
778                 PowerMockito.mockStatic(EPUserUtils.class);
779                 PowerMockito.mockStatic(EcompPortalUtils.class);
780                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
781                 List<EPUser> epuserList = new ArrayList<>();
782                 List<EPApp> appList = new ArrayList<>();
783                 appList.add(CentralApp());
784                 EPUser user = mockUser.mockEPUser();
785                 epuserList.add(user);
786                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
787                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
788                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
789                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
790                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
791                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
792                 JSONObject roleJson = new JSONObject();
793                 roleJson.put("id", 1);
794                 roleJson.put("name", "test%%");
795                 roleJson.put("active", true);
796                 JSONObject roleJson2 = new JSONObject();
797                 List<JSONObject> childRolesJson = new ArrayList<>();
798                 List<JSONObject> roleFunctions = new ArrayList<>();
799                 JSONObject roleFunction = new JSONObject();
800                 roleFunction.put("code", "test");
801                 roleFunction.put("name", "test");
802                 roleFunction.put("type", "test");
803                 roleFunction.put("action", "test");
804                 roleFunctions.add(roleFunction);
805                 roleJson.put("roleFunctions", roleFunctions);
806                 roleJson2.put("role", roleJson);
807                 roleJson2.put("childRoles", childRolesJson);
808                 roleJson2.put("roleFunctions", roleFunctions);
809                 ObjectMapper mapper = new ObjectMapper();
810                 JsonNode actualObj = mapper.readTree(roleJson2.toString());
811                 dsi = new DelegatingServletInputStream(
812                                 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
813                 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
814                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
815                 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
816                 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
817                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
818                 final Map<String, Object> expected = new HashMap<>();
819                 expected.put("error", "Invalid role name found for 'test%%'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed");
820                 assertEquals(expected.get("error"), actual.get("error"));
821         }
822         
823         @Test
824         public void saveRoleUnauthorizedUserExceptionTest() throws Exception {
825                 PowerMockito.mockStatic(EPUserUtils.class);
826                 PowerMockito.mockStatic(EcompPortalUtils.class);
827                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
828                 List<EPUser> epuserList = new ArrayList<>();
829                 List<EPApp> appList = new ArrayList<>();
830                 appList.add(CentralApp());
831                 EPUser user = mockUser.mockEPUser();
832                 epuserList.add(user);
833                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
834                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
835                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
836                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
837                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
838                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
839                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
840                 final Map<String, Object> expected = new HashMap<>();
841                 expected.put("error", " Unauthorized user");
842                 assertEquals(expected.get("error"), actual.get("error"));
843         }
844         
845         @Test
846         public void toggleRoleTest() throws Exception {
847                 PowerMockito.mockStatic(EPUserUtils.class);
848                 PowerMockito.mockStatic(EcompPortalUtils.class);
849                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
850                 List<EPUser> epuserList = new ArrayList<>();
851                 List<EPApp> appList = new ArrayList<>();
852                 appList.add(CentralApp());
853                 EPUser user = mockUser.mockEPUser();
854                 epuserList.add(user);
855                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
856                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
857                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
858                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
859                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
860                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
861         CentralV2Role role = new CentralV2Role.CentralV2RoleBuilder().setId(1l).setName("test").createCentralV2Role();
862                 role.setActive(true);
863                 Role currentRole = new Role();
864                 currentRole.setName("test");
865                 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
866                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
867                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
868                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
869                 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
870                 final Map<String, Object> expected = new HashMap<>();
871                 expected.put("restcallStatus", "Success");
872                 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
873         }
874
875         @Test
876         public void toggleRoleUnauthorizedUserExceptionTest() throws Exception {
877                 PowerMockito.mockStatic(EPUserUtils.class);
878                 PowerMockito.mockStatic(EcompPortalUtils.class);
879                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
880                 List<EPUser> epuserList = new ArrayList<>();
881                 List<EPApp> appList = new ArrayList<>();
882                 appList.add(CentralApp());
883                 EPUser user = mockUser.mockEPUser();
884                 epuserList.add(user);
885                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
886                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
887                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
888                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
889                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
890                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
891                 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
892                 final Map<String, Object> expected = new HashMap<>();
893                 expected.put("restcallStatus", " Unauthorized user");
894                 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
895         }
896         
897         @Test(expected = NullPointerException.class)
898         public void toggleRoleExceptionTest() throws Exception {
899                 PowerMockito.mockStatic(EPUserUtils.class);
900                 PowerMockito.mockStatic(EcompPortalUtils.class);
901                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
902                 List<EPUser> epuserList = new ArrayList<>();
903                 List<EPApp> appList = new ArrayList<>();
904                 appList.add(CentralApp());
905                 EPUser user = mockUser.mockEPUser();
906                 epuserList.add(user);
907                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
908                 Mockito.doThrow(new NullPointerException()).when(appService).getApp((long) 1);
909                 roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
910         }
911         
912         @Test
913         public void toggleRoleFailedTest() throws Exception {
914                 PowerMockito.mockStatic(EPUserUtils.class);
915                 PowerMockito.mockStatic(EcompPortalUtils.class);
916                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
917                 List<EPUser> epuserList = new ArrayList<>();
918                 List<EPApp> appList = new ArrayList<>();
919                 appList.add(CentralApp());
920                 EPUser user = mockUser.mockEPUser();
921                 epuserList.add(user);
922                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
923                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
924                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
925                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
926                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
927                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
928         CentralV2Role role = new CentralV2Role.CentralV2RoleBuilder().setId(1l).setName("test").createCentralV2Role();
929                 role.setActive(true);
930                 Role currentRole = new Role();
931                 currentRole.setName("test");
932                 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
933                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
934                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(false, "");
935                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
936                 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
937                 final Map<String, Object> expected = new HashMap<>();
938                 expected.put("restcallStatus", "Toggle Role Failed");
939                 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
940         }
941         
942         @Test
943         public void getAvailableChildRolesTest() throws Exception {
944                 List<CentralV2Role> centralV2Roles = new ArrayList<>();
945                 CentralV2Role centralV2Role = new CentralV2Role();
946                 centralV2Role.setName("test");
947                 centralV2Role.setId(1l);
948                 CentralV2Role centralV2Role2 = new CentralV2Role();
949                 centralV2Role2.setName("test");
950                 centralV2Role2.setId(1l);
951                 centralV2Roles.add(centralV2Role);
952                 centralV2Roles.add(centralV2Role2);
953                 SortedSet<CentralV2RoleFunction> roleFuns = new TreeSet<>();
954                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test", "test");
955                 roleFuns.add(centralV2RoleFunction);
956                 CentralV2Role currentRole = new CentralV2Role();
957                 currentRole.setName("test");
958                 currentRole.setId(1l);
959                 currentRole.setRoleFunctions(roleFuns);
960                 Mockito.when(externalAccessRolesService.getRolesForApp(CentralApp().getUebKey())).thenReturn(centralV2Roles);
961                 Mockito.when(externalAccessRolesService.getRoleInfo(1l, CentralApp().getUebKey())).thenReturn(currentRole);
962                 List<CentralV2Role> actual = roleManageController.getAvailableChildRoles(CentralApp().getUebKey(), 1l);
963                 assertEquals(new ArrayList<>().size(), actual.size());
964         }
965         
966         @Test
967         public void getCentralizedAppRolesTest() throws IOException {
968                 PowerMockito.mockStatic(EPUserUtils.class);
969                 EPUser user = mockUser.mockEPUser();
970                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
971                 List<CentralizedApp> cenApps = new ArrayList<>();
972                 CentralizedApp centralizedApp = new CentralizedApp();
973                 centralizedApp.setAppId(1);
974                 centralizedApp.setAppName("Test");
975                 cenApps.add(centralizedApp);
976                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
977                 Mockito.when(externalAccessRolesService.getCentralizedAppsOfUser(Matchers.anyString())).thenReturn(cenApps);
978                 List<CentralizedApp> actual  = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
979                 assertEquals(cenApps.size(), actual.size());
980         }
981
982         @Test
983         public void getCentralizedAppRolesXSSTest() throws IOException {
984                 String id = ("<ScRipT>alert(\"XSS\");</ScRipT>");
985                 List<CentralizedApp> actual  = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, id);
986                 assertNull(actual);
987         }
988         
989         @Test
990         public void getCentralizedAppRolesExceptionTest() throws IOException {
991                 PowerMockito.mockStatic(EPUserUtils.class);
992                 EPUser user = mockUser.mockEPUser();
993                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
994                 List<CentralizedApp> cenApps = new ArrayList<>();
995                 CentralizedApp centralizedApp = new CentralizedApp();
996                 centralizedApp.setAppId(1);
997                 centralizedApp.setAppName("Test");
998                 cenApps.add(centralizedApp);
999                 Mockito.when(adminRolesService.isAccountAdmin(Matchers.anyObject())).thenReturn(false);
1000                 List<CentralizedApp> actual  = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
1001                 assertNull(actual);
1002         }
1003
1004         @Test
1005   public void bulkUploadRoleFuncUserNullTest() {
1006       UploadRoleFunctionExtSystem data = Mockito.mock(UploadRoleFunctionExtSystem.class);
1007       Mockito.when(appService.getApp(127L)).thenReturn(null);
1008       PortalRestResponse<String> response = roleManageController.bulkUploadRoleFunc(mockedRequest, mockedResponse, data, 127L);
1009       assertEquals(PortalRestStatusEnum.ERROR, response.getStatus());
1010       assertEquals("Unauthorized User", response.getMessage());
1011       assertEquals("Failure", response.getResponse());
1012   }
1013         
1014         public CentralV2RoleFunction mockCentralRoleFunction() {
1015                 CentralV2RoleFunction roleFunction = new CentralV2RoleFunction();
1016                 roleFunction.setCode("Test");
1017                 roleFunction.setName("Test");
1018                 roleFunction.setAppId((long) 1);
1019                 return roleFunction;
1020         }
1021
1022         public EPApp CentralApp() {
1023                 EPApp app = mockApp();
1024                 app.setCentralAuth(true);
1025                 app.setNameSpace("com.test");
1026                 return app;
1027         }
1028
1029         public EPApp NonCentralApp() {
1030                 EPApp app = mockApp();
1031                 app.setCentralAuth(false);
1032                 return app;
1033         }
1034
1035 }