9673cb2c077c6bb63e1a95bac964a183f19af99d
[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
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;
55 import java.util.Map;
56 import java.util.SortedSet;
57 import java.util.TreeSet;
58
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
61
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;
99
100 import com.fasterxml.jackson.databind.JsonNode;
101 import com.fasterxml.jackson.databind.ObjectMapper;
102
103 @RunWith(PowerMockRunner.class)
104 @PrepareForTest({ EPUserUtils.class, EcompPortalUtils.class })
105 public class RoleManageControllerTest {
106
107         @Mock
108         RoleController roleController;
109
110         @Mock
111         RoleListController roleListController;
112
113         @Mock
114         RoleFunctionListController roleFunctionListController;
115
116         @Mock
117         ExternalAccessRolesService externalAccessRolesService;
118         @Mock
119         EPUserUtils ePUserUtils = new EPUserUtils();
120
121         @Mock
122         ExternalAccessRolesService externalAccessRolesService1 = null;
123
124         @InjectMocks
125         RoleManageController roleManageController = new RoleManageController();
126
127         @Mock
128         EPAppService appService;
129
130         @Mock
131         AuditService auditService;
132
133         @Mock
134         AdminRolesService adminRolesService;
135
136         @Before
137         public void setup() {
138                 MockitoAnnotations.initMocks(this);
139         }
140
141         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
142
143         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
144         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
145         NullPointerException nullPointerException = new NullPointerException();
146         MockEPUser mockUser = new MockEPUser();
147
148         private DelegatingServletInputStream dsi;
149
150         @Test
151         public void removeRoleRoleFunctionTest() throws Exception {
152
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);
157         }
158
159         @Test
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);
165         }
166
167         @Test
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);
173         }
174
175         @Test
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);
203         }
204
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);
212
213         }
214
215         @Test
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);
241         }
242
243         public EPApp mockApp() {
244                 EPApp app = new EPApp();
245
246                 app.setName("Test");
247                 app.setImageUrl("test");
248                 app.setDescription("test");
249                 app.setNotes("test");
250                 app.setUrl("test");
251                 app.setId((long) 1);
252                 app.setAppRestEndpoint("test");
253                 app.setAlternateUrl("test");
254                 app.setName("test");
255                 app.setMlAppName("test");
256                 app.setMlAppAdminId("test");
257                 app.setUsername("test");
258                 app.setAppPassword("test");
259                 app.setOpen(false);
260                 app.setEnabled(false);
261                 app.setUebKey("test");
262                 app.setUebSecret("test");
263                 app.setUebTopicName("test");
264                 app.setAppType(1);
265                 return app;
266
267         }
268
269         @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);
292         }
293
294         @Test
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);
300
301         }
302
303         @Test
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);
323         }
324
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);
329         }
330
331         @Test
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()))
350                                 .thenReturn(true);
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<>();
356                 userList.add(user);
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);
371         }
372
373         @Test
374         public void saveRoleFunctionXSSTest() throws Exception {
375                 PowerMockito.mockStatic(EPUserUtils.class);
376                 PowerMockito.mockStatic(EcompPortalUtils.class);
377                 EPUser user = mockUser.mockEPUser();
378                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
379                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
380                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
381                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
382                 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
383                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
384                 addNewFunc.setCode("“><script>alert(“XSS”)</script>");
385                 addNewFunc.setType("Test");
386                 addNewFunc.setAction("Test");
387                 addNewFunc.setName("Test");
388                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
389                 roleFunction.setCode("Test|Test|Test");
390                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
391                 Mockito.when(externalAccessRolesService.saveCentralRoleFunction(Matchers.anyObject(), Matchers.anyObject()))
392                         .thenReturn(true);
393                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunction.getCode())).thenReturn("Test");
394                 Mockito.when(EcompPortalUtils.getFunctionType(roleFunction.getCode())).thenReturn("Test");
395                 Mockito.when(EcompPortalUtils.getFunctionAction(roleFunction.getCode())).thenReturn("Test");
396                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
397                 List<EPUser> userList = new ArrayList<>();
398                 userList.add(user);
399                 List<EPApp> appList = new ArrayList<>();
400                 appList.add(CentralApp());
401                 Mockito.when(externalAccessRolesService.getUser("guestT")).thenReturn(userList);
402                 StringWriter sw = new StringWriter();
403                 PrintWriter writer = new PrintWriter(sw);
404                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
405                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
406                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
407                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
408                 PortalRestResponse<String> actual = roleManageController.saveRoleFunction(mockedRequest, mockedResponse,
409                         addNewFunc, (long) 1);
410                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
411                         "Data is not valid", "ERROR");
412                 assertEquals(expected, actual);
413         }
414
415         @Test
416         public void saveRoleFunctionExceptionTest() throws Exception {
417                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
418                 Mockito.doNothing().when(roleFunctionListController).saveRoleFunction(mockedRequest, mockedResponse, "test");
419                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
420                 addNewFunc.setCode("Test");
421                 addNewFunc.setName("Test");
422                 Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(null);
423                 roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1);
424         }
425
426         @Test
427         public void saveRoleFunctionIfAppNotCentralizedTest() throws Exception {
428                 CentralV2RoleFunction addNewFunc = new CentralV2RoleFunction();
429                 addNewFunc.setCode("Test");
430                 addNewFunc.setName("Test");
431                 Mockito.when(appService.getApp((long) 1)).thenReturn(NonCentralApp());
432                 roleManageController.saveRoleFunction(mockedRequest, mockedResponse, addNewFunc, (long) 1);
433         }
434
435         @Test
436         public void removeRoleFunctionTest() throws Exception {
437                 PowerMockito.mockStatic(EPUserUtils.class);
438                 PowerMockito.mockStatic(EcompPortalUtils.class);
439                 EPUser user = mockUser.mockEPUser();
440                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
441                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
442                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
443                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
444                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
445                 String roleFun = "{\"name\":\"Test\",\"type\":\"Test\",\"action\":\"Test\", \"code\":\"Test\"}";
446                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
447                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
448                 StringWriter sw = new StringWriter();
449                 PrintWriter writer = new PrintWriter(sw);
450                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
451                 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
452                                 .thenReturn(true);
453                 List<EPApp> appList = new ArrayList<>();
454                 appList.add(CentralApp());
455                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
456                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
457                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
458                 PortalRestResponse<String> actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse,
459                                 roleFun, (long) 1);
460                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK,
461                                 "Deleted Successfully!", "Success");
462                 assertEquals(expected, actual);
463         }
464
465         @Test
466         public void removeRoleFunctionXSSTest() throws Exception {
467                 PowerMockito.mockStatic(EPUserUtils.class);
468                 PowerMockito.mockStatic(EcompPortalUtils.class);
469                 EPUser user = mockUser.mockEPUser();
470                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
471                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
472                 Mockito.when(adminRolesService.isAccountAdminOfApplication(user, CentralApp())).thenReturn(true);
473                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
474                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
475                 String roleFun = "<script>alert(/XSS”)</script>";
476                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
477                 Mockito.when(externalAccessRolesService.getRoleFunction("Test|Test|Test", "test")).thenReturn(roleFunction);
478                 StringWriter sw = new StringWriter();
479                 PrintWriter writer = new PrintWriter(sw);
480                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
481                 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
482                         .thenReturn(true);
483                 List<EPApp> appList = new ArrayList<>();
484                 appList.add(CentralApp());
485                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
486                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
487                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
488                 PortalRestResponse<String> actual = roleManageController.removeRoleFunction(mockedRequest, mockedResponse,
489                         roleFun, (long) 1);
490                 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
491                         "Data is not valid", "ERROR");
492                 assertEquals(expected, actual);
493         }
494
495         @Test
496         public void removeRoleFunctionExceptionTest() throws Exception {
497                 EPUser user = mockUser.mockEPUser();
498                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
499                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
500                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
501                 CentralV2RoleFunction roleFunction = mockCentralRoleFunction();
502                 Mockito.when(externalAccessRolesService.getRoleFunction("Test", "test")).thenReturn(roleFunction);
503                 StringWriter sw = new StringWriter();
504                 PrintWriter writer = new PrintWriter(sw);
505                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
506                 Mockito.when(externalAccessRolesService.deleteCentralRoleFunction(Matchers.anyString(), Matchers.anyObject()))
507                                 .thenReturn(false);
508                 List<EPApp> appList = new ArrayList<>();
509                 appList.add(CentralApp());
510                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
511                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
512                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
513                 roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1);
514         }
515
516         @Test
517         public void removeRoleFunctionIfAppNotCentralizedTest() throws Exception {
518                 EPApp app = mockApp();
519                 app.setCentralAuth(false);
520                 Mockito.when(appService.getApp((long) 1)).thenReturn(app);
521                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
522                 roleManageController.removeRoleFunction(mockedRequest, mockedResponse, roleFun, (long) 1);
523         }
524
525         @Test
526         public void syncRolesTest() throws Exception {
527                 PowerMockito.mockStatic(EPUserUtils.class);
528                 PowerMockito.mockStatic(EcompPortalUtils.class);
529                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
530                 EPUser user = mockUser.mockEPUser();
531                 EPApp app = mockApp();
532                 app.setId((long) 1);
533                 Mockito.when(appService.getApp(1l)).thenReturn(app);
534                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
535                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
536                 Mockito.doNothing().when(externalAccessRolesService).syncApplicationRolesWithEcompDB(app);
537                 List<EPApp> appList = new ArrayList<>();
538                 appList.add(CentralApp());
539                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
540                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
541                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
542                 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
543                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
544                 portalRestResponse.setMessage("Sync roles completed successfully!");
545                 portalRestResponse.setResponse("Success");
546                 portalRestResponse.setStatus(PortalRestStatusEnum.OK);
547                 assertEquals(portalRestResponse, actual);
548         }
549
550         @Test
551         public void syncRolesException() throws Exception {
552       EPUser user = mockUser.mockEPUser();
553       Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
554       Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true);
555                 Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
556                 PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
557                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
558                 portalRestResponse.setMessage(null);
559                 portalRestResponse.setResponse("Failed");
560                 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
561                 assertEquals(portalRestResponse, actual);
562         }
563
564     @Test
565     public void syncRolesUserNullException() throws Exception {
566         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
567         Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
568         PortalRestResponse<String> actual = roleManageController.syncRoles(mockedRequest, mockedResponse, 1l);
569         PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
570         portalRestResponse.setMessage("Unauthorized User");
571         portalRestResponse.setResponse("Failure");
572         portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
573         assertEquals(portalRestResponse, actual);
574     }
575
576         @Test
577         public void syncRolesFunctionsTest() throws Exception {
578                 PowerMockito.mockStatic(EPUserUtils.class);
579                 PowerMockito.mockStatic(EcompPortalUtils.class);
580                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
581                 EPApp app = mockApp();
582                 app.setId((long) 1);
583                 EPUser user = mockUser.mockEPUser();
584                 Mockito.when(appService.getApp(1l)).thenReturn(app);
585                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
586                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
587                 Mockito.doNothing().when(externalAccessRolesService).syncRoleFunctionFromExternalAccessSystem(app);
588                 List<EPApp> appList = new ArrayList<>();
589                 appList.add(CentralApp());
590                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
591                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
592                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
593                 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
594                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
595                 portalRestResponse.setMessage("Sync Functions completed successfully!");
596                 portalRestResponse.setResponse("Success");
597                 portalRestResponse.setStatus(PortalRestStatusEnum.OK);
598                 assertEquals(portalRestResponse, actual);
599         }
600
601         @Test
602         public void syncRolesFunctionsException() throws Exception {
603       EPUser user = mockUser.mockEPUser();
604       Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
605       Mockito.when(adminRolesService.isAccountAdminOfApplication(user, null)).thenReturn(true);
606       Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
607                 PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
608                 PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
609                 portalRestResponse.setMessage(null);
610                 portalRestResponse.setResponse("Failed");
611                 portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
612                 assertEquals(portalRestResponse, actual);
613         }
614
615     @Test
616     public void syncRolesFunctionsUserNullException() throws Exception {
617         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
618         Mockito.when(appService.getAppDetailByAppName("test")).thenThrow(nullPointerException);
619         PortalRestResponse<String> actual = roleManageController.syncFunctions(mockedRequest, mockedResponse, 1l);
620         PortalRestResponse<String> portalRestResponse = new PortalRestResponse<>();
621         portalRestResponse.setMessage("Unauthorized User");
622         portalRestResponse.setResponse("Failure");
623         portalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
624         assertEquals(portalRestResponse, actual);
625     }
626
627         @Test
628         public void addChildRoleTest() throws Exception {
629                 ModelAndView modelandView = new ModelAndView("login.htm");
630                 Mockito.when(roleController.addChildRole(mockedRequest, mockedResponse)).thenReturn(modelandView);
631                 ModelAndView expectedModelandView = roleManageController.addChildRole(mockedRequest, mockedResponse);
632                 assertEquals(expectedModelandView, modelandView);
633         }
634
635         @Test
636         public void removeRoleTest() throws Exception {
637                 PowerMockito.mockStatic(EPUserUtils.class);
638                 PowerMockito.mockStatic(EcompPortalUtils.class);
639                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
640                 List<EPUser> epuserList = new ArrayList<>();
641                 List<EPApp> appList = new ArrayList<>();
642                 appList.add(CentralApp());
643                 EPUser user = mockUser.mockEPUser();
644                 epuserList.add(user);
645                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
646                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
647                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
648                 StringWriter sw = new StringWriter();
649                 PrintWriter writer = new PrintWriter(sw);
650                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
651                 ExternalRequestFieldsValidator res = new ExternalRequestFieldsValidator(true, "success");
652                 Mockito.when(externalAccessRolesService.deleteDependencyRoleRecord(Matchers.anyLong(), Matchers.anyString(),
653                                 Matchers.anyString())).thenReturn(res);
654                 Mockito.when(externalAccessRolesService.getUser(Matchers.anyString())).thenReturn(epuserList);
655                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
656                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
657                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
658                 Map<String, Object> expedtedResponse = new HashMap<String, Object>();
659                 expedtedResponse.put("restCallStatus", " Unauthorized user");
660                 expedtedResponse.put("availableRoles", new ArrayList<>());
661                 Map<String, Object> actualResponse = roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1,
662                                 (long) 1);
663                 assertEquals(actualResponse.size(), expedtedResponse.size());
664
665         }
666
667         @Test(expected = Exception.class)
668         public void removeRoleExceptionTest() throws Exception {
669                 EPUser user = mockUser.mockEPUser();
670                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
671                 Mockito.when(appService.getApp((long) 1)).thenThrow(nullPointerException);
672                 StringWriter sw = new StringWriter();
673                 PrintWriter writer = new PrintWriter(sw);
674                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
675                 roleManageController.removeRole(mockedRequest, mockedResponse, (long) 1, (long) 1);
676         }
677
678         @SuppressWarnings("unchecked")
679         @Test
680         public void saveRoleNewTest() throws Exception {
681                 PowerMockito.mockStatic(EPUserUtils.class);
682                 PowerMockito.mockStatic(EcompPortalUtils.class);
683                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
684                 List<EPUser> epuserList = new ArrayList<>();
685                 List<EPApp> appList = new ArrayList<>();
686                 appList.add(CentralApp());
687                 EPUser user = mockUser.mockEPUser();
688                 epuserList.add(user);
689                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
690                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
691                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
692                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
693                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
694                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
695                 JSONObject roleJson = new JSONObject();
696                 roleJson.put("name", "test");
697                 JSONObject roleJson2 = new JSONObject();
698                 List<JSONObject> childRolesJson = new ArrayList<>();
699                 List<JSONObject> roleFunctions = new ArrayList<>();
700                 roleJson2.put("role", roleJson);
701                 roleJson2.put("childRoles", childRolesJson);
702                 roleJson2.put("roleFunctions", roleFunctions);
703                 ObjectMapper mapper = new ObjectMapper();
704                 JsonNode actualObj = mapper.readTree(roleJson2.toString());
705                 dsi = new DelegatingServletInputStream(
706                                 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
707                 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
708                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
709                 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
710                 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
711                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
712                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
713                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
714                                 .thenReturn(externalRequestFieldsValidator);
715                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
716                 final Map<String, Object> expected = new HashMap<>();
717                 expected.put("role", new CentralV2Role(null, "test"));
718                 expected.put("status", "Success");
719                 assertEquals(expected, actual);
720         }
721
722         @SuppressWarnings("unchecked")
723         @Test
724         public void saveRoleUpdateTest() throws Exception {
725                 PowerMockito.mockStatic(EPUserUtils.class);
726                 PowerMockito.mockStatic(EcompPortalUtils.class);
727                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
728                 List<EPUser> epuserList = new ArrayList<>();
729                 List<EPApp> appList = new ArrayList<>();
730                 appList.add(CentralApp());
731                 EPUser user = mockUser.mockEPUser();
732                 epuserList.add(user);
733                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
734                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
735                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
736                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
737                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
738                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
739                 JSONObject roleJson = new JSONObject();
740                 roleJson.put("id", 1);
741                 roleJson.put("name", "test");
742                 roleJson.put("active", true);
743                 JSONObject roleJson2 = new JSONObject();
744                 List<JSONObject> childRolesJson = new ArrayList<>();
745                 List<JSONObject> roleFunctions = new ArrayList<>();
746                 JSONObject roleFunction = new JSONObject();
747                 roleFunction.put("code", "test");
748                 roleFunction.put("name", "test");
749                 roleFunction.put("type", "test");
750                 roleFunction.put("action", "test");
751                 roleFunctions.add(roleFunction);
752                 roleJson.put("roleFunctions", roleFunctions);
753                 roleJson2.put("role", roleJson);
754                 roleJson2.put("childRoles", childRolesJson);
755                 roleJson2.put("roleFunctions", roleFunctions);
756                 ObjectMapper mapper = new ObjectMapper();
757                 JsonNode actualObj = mapper.readTree(roleJson2.toString());
758                 dsi = new DelegatingServletInputStream(
759                                 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
760                 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
761                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
762                 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
763                 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
764                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
765                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
766                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
767                                 .thenReturn(externalRequestFieldsValidator);
768                 CentralV2Role cenV2Role = new CentralV2Role(1l, "test1");
769                 cenV2Role.setActive(true);
770                 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(cenV2Role);
771                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
772                 final Map<String, Object> expected = new HashMap<>();
773                 expected.put("status", "Success");
774                 assertEquals(expected.get("status"), actual.get("status"));
775         }
776         
777         @SuppressWarnings("unchecked")
778         @Test
779         public void saveRoleInvalidRoleExceptionTest() throws Exception {
780                 PowerMockito.mockStatic(EPUserUtils.class);
781                 PowerMockito.mockStatic(EcompPortalUtils.class);
782                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
783                 List<EPUser> epuserList = new ArrayList<>();
784                 List<EPApp> appList = new ArrayList<>();
785                 appList.add(CentralApp());
786                 EPUser user = mockUser.mockEPUser();
787                 epuserList.add(user);
788                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
789                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
790                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
791                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
792                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
793                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
794                 JSONObject roleJson = new JSONObject();
795                 roleJson.put("id", 1);
796                 roleJson.put("name", "test%%");
797                 roleJson.put("active", true);
798                 JSONObject roleJson2 = new JSONObject();
799                 List<JSONObject> childRolesJson = new ArrayList<>();
800                 List<JSONObject> roleFunctions = new ArrayList<>();
801                 JSONObject roleFunction = new JSONObject();
802                 roleFunction.put("code", "test");
803                 roleFunction.put("name", "test");
804                 roleFunction.put("type", "test");
805                 roleFunction.put("action", "test");
806                 roleFunctions.add(roleFunction);
807                 roleJson.put("roleFunctions", roleFunctions);
808                 roleJson2.put("role", roleJson);
809                 roleJson2.put("childRoles", childRolesJson);
810                 roleJson2.put("roleFunctions", roleFunctions);
811                 ObjectMapper mapper = new ObjectMapper();
812                 JsonNode actualObj = mapper.readTree(roleJson2.toString());
813                 dsi = new DelegatingServletInputStream(
814                                 new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8)));
815                 Mockito.when(mockedRequest.getInputStream()).thenReturn(dsi);
816                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
817                 Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
818                 Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
819                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
820                 final Map<String, Object> expected = new HashMap<>();
821                 expected.put("error", "Invalid role name found for 'test%%'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed");
822                 assertEquals(expected.get("error"), actual.get("error"));
823         }
824         
825         @Test
826         public void saveRoleUnauthorizedUserExceptionTest() throws Exception {
827                 PowerMockito.mockStatic(EPUserUtils.class);
828                 PowerMockito.mockStatic(EcompPortalUtils.class);
829                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
830                 List<EPUser> epuserList = new ArrayList<>();
831                 List<EPApp> appList = new ArrayList<>();
832                 appList.add(CentralApp());
833                 EPUser user = mockUser.mockEPUser();
834                 epuserList.add(user);
835                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
836                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
837                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
838                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
839                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
840                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
841                 Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
842                 final Map<String, Object> expected = new HashMap<>();
843                 expected.put("error", " Unauthorized user");
844                 assertEquals(expected.get("error"), actual.get("error"));
845         }
846         
847         @Test
848         public void toggleRoleTest() throws Exception {
849                 PowerMockito.mockStatic(EPUserUtils.class);
850                 PowerMockito.mockStatic(EcompPortalUtils.class);
851                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
852                 List<EPUser> epuserList = new ArrayList<>();
853                 List<EPApp> appList = new ArrayList<>();
854                 appList.add(CentralApp());
855                 EPUser user = mockUser.mockEPUser();
856                 epuserList.add(user);
857                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
858                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
859                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
860                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
861                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
862                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
863                 CentralV2Role role = new CentralV2Role(1l, "test");
864                 role.setActive(true);
865                 Role currentRole = new Role();
866                 currentRole.setName("test");
867                 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
868                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
869                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
870                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
871                 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
872                 final Map<String, Object> expected = new HashMap<>();
873                 expected.put("restcallStatus", "Success");
874                 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
875         }
876
877         @Test
878         public void toggleRoleUnauthorizedUserExceptionTest() throws Exception {
879                 PowerMockito.mockStatic(EPUserUtils.class);
880                 PowerMockito.mockStatic(EcompPortalUtils.class);
881                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
882                 List<EPUser> epuserList = new ArrayList<>();
883                 List<EPApp> appList = new ArrayList<>();
884                 appList.add(CentralApp());
885                 EPUser user = mockUser.mockEPUser();
886                 epuserList.add(user);
887                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
888                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
889                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
890                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
891                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
892                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
893                 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
894                 final Map<String, Object> expected = new HashMap<>();
895                 expected.put("restcallStatus", " Unauthorized user");
896                 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
897         }
898         
899         @Test(expected = NullPointerException.class)
900         public void toggleRoleExceptionTest() throws Exception {
901                 PowerMockito.mockStatic(EPUserUtils.class);
902                 PowerMockito.mockStatic(EcompPortalUtils.class);
903                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
904                 List<EPUser> epuserList = new ArrayList<>();
905                 List<EPApp> appList = new ArrayList<>();
906                 appList.add(CentralApp());
907                 EPUser user = mockUser.mockEPUser();
908                 epuserList.add(user);
909                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
910                 Mockito.doThrow(new NullPointerException()).when(appService).getApp((long) 1);
911                 roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
912         }
913         
914         @Test
915         public void toggleRoleFailedTest() throws Exception {
916                 PowerMockito.mockStatic(EPUserUtils.class);
917                 PowerMockito.mockStatic(EcompPortalUtils.class);
918                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
919                 List<EPUser> epuserList = new ArrayList<>();
920                 List<EPApp> appList = new ArrayList<>();
921                 appList.add(CentralApp());
922                 EPUser user = mockUser.mockEPUser();
923                 epuserList.add(user);
924                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
925                 Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
926                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
927                 Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
928                 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
929                 Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
930                 CentralV2Role role = new CentralV2Role(1l, "test");
931                 role.setActive(true);
932                 Role currentRole = new Role();
933                 currentRole.setName("test");
934                 Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
935                 Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
936                 ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(false, "");
937                 Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
938                 Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
939                 final Map<String, Object> expected = new HashMap<>();
940                 expected.put("restcallStatus", "Toggle Role Failed");
941                 assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
942         }
943         
944         @Test
945         public void getAvailableChildRolesTest() throws Exception {
946                 List<CentralV2Role> centralV2Roles = new ArrayList<>();
947                 CentralV2Role centralV2Role = new CentralV2Role();
948                 centralV2Role.setName("test");
949                 centralV2Role.setId(1l);
950                 CentralV2Role centralV2Role2 = new CentralV2Role();
951                 centralV2Role2.setName("test");
952                 centralV2Role2.setId(1l);
953                 centralV2Roles.add(centralV2Role);
954                 centralV2Roles.add(centralV2Role2);
955                 SortedSet<CentralV2RoleFunction> roleFuns = new TreeSet<>();
956                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test", "test");
957                 roleFuns.add(centralV2RoleFunction);
958                 CentralV2Role currentRole = new CentralV2Role();
959                 currentRole.setName("test");
960                 currentRole.setId(1l);
961                 currentRole.setRoleFunctions(roleFuns);
962                 Mockito.when(externalAccessRolesService.getRolesForApp(CentralApp().getUebKey())).thenReturn(centralV2Roles);
963                 Mockito.when(externalAccessRolesService.getRoleInfo(1l, CentralApp().getUebKey())).thenReturn(currentRole);
964                 List<CentralV2Role> actual = roleManageController.getAvailableChildRoles(CentralApp().getUebKey(), 1l);
965                 assertEquals(new ArrayList<>().size(), actual.size());
966         }
967         
968         @Test
969         public void getCentralizedAppRolesTest() throws IOException {
970                 PowerMockito.mockStatic(EPUserUtils.class);
971                 EPUser user = mockUser.mockEPUser();
972                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
973                 List<CentralizedApp> cenApps = new ArrayList<>();
974                 CentralizedApp centralizedApp = new CentralizedApp();
975                 centralizedApp.setAppId(1);
976                 centralizedApp.setAppName("Test");
977                 cenApps.add(centralizedApp);
978                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
979                 Mockito.when(externalAccessRolesService.getCentralizedAppsOfUser(Matchers.anyString())).thenReturn(cenApps);
980                 List<CentralizedApp> actual  = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
981                 assertEquals(cenApps.size(), actual.size());
982         }
983
984         @Test
985         public void getCentralizedAppRolesXSSTest() throws IOException {
986                 String id = ("<ScRipT>alert(\"XSS\");</ScRipT>");
987                 List<CentralizedApp> actual  = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, id);
988                 assertNull(actual);
989         }
990         
991         @Test
992         public void getCentralizedAppRolesExceptionTest() throws IOException {
993                 PowerMockito.mockStatic(EPUserUtils.class);
994                 EPUser user = mockUser.mockEPUser();
995                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
996                 List<CentralizedApp> cenApps = new ArrayList<>();
997                 CentralizedApp centralizedApp = new CentralizedApp();
998                 centralizedApp.setAppId(1);
999                 centralizedApp.setAppName("Test");
1000                 cenApps.add(centralizedApp);
1001                 Mockito.when(adminRolesService.isAccountAdmin(Matchers.anyObject())).thenReturn(false);
1002                 List<CentralizedApp> actual  = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
1003                 assertNull(actual);
1004         }
1005
1006         @Test
1007   public void bulkUploadRoleFuncUserNullTest() {
1008       UploadRoleFunctionExtSystem data = Mockito.mock(UploadRoleFunctionExtSystem.class);
1009       Mockito.when(appService.getApp(127L)).thenReturn(null);
1010       PortalRestResponse<String> response = roleManageController.bulkUploadRoleFunc(mockedRequest, mockedResponse, data, 127L);
1011       assertEquals(PortalRestStatusEnum.ERROR, response.getStatus());
1012       assertEquals("Unauthorized User", response.getMessage());
1013       assertEquals("Failure", response.getResponse());
1014   }
1015         
1016         public CentralV2RoleFunction mockCentralRoleFunction() {
1017                 CentralV2RoleFunction roleFunction = new CentralV2RoleFunction();
1018                 roleFunction.setCode("Test");
1019                 roleFunction.setName("Test");
1020                 roleFunction.setAppId((long) 1);
1021                 return roleFunction;
1022         }
1023
1024         public EPApp CentralApp() {
1025                 EPApp app = mockApp();
1026                 app.setCentralAuth(true);
1027                 app.setNameSpace("com.test");
1028                 return app;
1029         }
1030
1031         public EPApp NonCentralApp() {
1032                 EPApp app = mockApp();
1033                 app.setCentralAuth(false);
1034                 return app;
1035         }
1036
1037 }