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