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