+ @SuppressWarnings("unchecked")
+ @Test
+ public void saveRoleInvalidRoleExceptionTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ JSONObject roleJson = new JSONObject();
+ roleJson.put("id", 1);
+ roleJson.put("name", "test%%");
+ roleJson.put("active", true);
+ JSONObject roleJson2 = new JSONObject();
+ List<JSONObject> childRolesJson = new ArrayList<>();
+ List<JSONObject> roleFunctions = new ArrayList<>();
+ JSONObject roleFunction = new JSONObject();
+ roleFunction.put("code", "test");
+ roleFunction.put("name", "test");
+ roleFunction.put("type", "test");
+ roleFunction.put("action", "test");
+ roleFunctions.add(roleFunction);
+ roleJson.put("roleFunctions", roleFunctions);
+ roleJson2.put("role", roleJson);
+ roleJson2.put("childRoles", childRolesJson);
+ roleJson2.put("roleFunctions", roleFunctions);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(roleJson2.toString());
+ Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
+ new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))));
+ Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
+ Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
+ Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
+ Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("error", "Invalid role name found for 'test%%'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed");
+ assertEquals(expected.get("error"), actual.get("error"));
+ }
+
+ @Test
+ public void saveRoleUnauthorizedUserExceptionTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("error", " Unauthorized user");
+ assertEquals(expected.get("error"), actual.get("error"));
+ }
+
+ @Test
+ public void toggleRoleTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ CentralV2Role role = new CentralV2Role(1l, "test");
+ role.setActive(true);
+ Role currentRole = new Role();
+ currentRole.setName("test");
+ Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
+ Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
+ ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
+ Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
+ Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("restcallStatus", "Success");
+ assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
+ }
+
+ @Test
+ public void toggleRoleUnauthorizedUserExceptionTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("restcallStatus", " Unauthorized user");
+ assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void toggleRoleExceptionTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.doThrow(new NullPointerException()).when(appService).getApp((long) 1);
+ roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
+ }
+
+ @Test
+ public void toggleRoleFailedTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ CentralV2Role role = new CentralV2Role(1l, "test");
+ role.setActive(true);
+ Role currentRole = new Role();
+ currentRole.setName("test");
+ Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(role);
+ Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(currentRole);
+ ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(false, "");
+ Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(),Matchers.any())).thenReturn(externalRequestFieldsValidator);
+ Map<String, Object> actual = roleManageController.toggleRole(mockedRequest, mockedResponse, CentralApp().getId(), 1l);
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("restcallStatus", "Toggle Role Failed");
+ assertEquals(expected.get("restcallStatus"), actual.get("restcallStatus"));
+ }
+
+ @Test
+ public void getAvailableChildRolesTest() throws Exception {
+ List<CentralV2Role> centralV2Roles = new ArrayList<>();
+ CentralV2Role centralV2Role = new CentralV2Role();
+ centralV2Role.setName("test");
+ centralV2Role.setId(1l);
+ CentralV2Role centralV2Role2 = new CentralV2Role();
+ centralV2Role2.setName("test");
+ centralV2Role2.setId(1l);
+ centralV2Roles.add(centralV2Role);
+ centralV2Roles.add(centralV2Role2);
+ SortedSet<CentralV2RoleFunction> roleFuns = new TreeSet<>();
+ CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test", "test");
+ roleFuns.add(centralV2RoleFunction);
+ CentralV2Role currentRole = new CentralV2Role();
+ currentRole.setName("test");
+ currentRole.setId(1l);
+ currentRole.setRoleFunctions(roleFuns);
+ Mockito.when(externalAccessRolesService.getRolesForApp(CentralApp().getUebKey())).thenReturn(centralV2Roles);
+ Mockito.when(externalAccessRolesService.getRoleInfo(1l, CentralApp().getUebKey())).thenReturn(currentRole);
+ List<CentralV2Role> actual = roleManageController.getAvailableChildRoles(CentralApp().getUebKey(), 1l);
+ assertEquals(new ArrayList<>().size(), actual.size());
+ }
+
+ @Test
+ public void getCentralizedAppRolesTest() throws IOException {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ EPUser user = mockUser.mockEPUser();
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ List<CentralizedApp> cenApps = new ArrayList<>();
+ CentralizedApp centralizedApp = new CentralizedApp();
+ centralizedApp.setAppId(1);
+ centralizedApp.setAppName("Test");
+ cenApps.add(centralizedApp);
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getCentralizedAppsOfUser(Matchers.anyString())).thenReturn(cenApps);
+ List<CentralizedApp> actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
+ assertEquals(cenApps.size(), actual.size());
+ }
+
+ @Test
+ public void getCentralizedAppRolesExceptionTest() throws IOException {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ EPUser user = mockUser.mockEPUser();
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ List<CentralizedApp> cenApps = new ArrayList<>();
+ CentralizedApp centralizedApp = new CentralizedApp();
+ centralizedApp.setAppId(1);
+ centralizedApp.setAppName("Test");
+ cenApps.add(centralizedApp);
+ Mockito.when(adminRolesService.isAccountAdmin(Matchers.anyObject())).thenReturn(false);
+ List<CentralizedApp> actual = roleManageController.getCentralizedAppRoles(mockedRequest, mockedResponse, user.getOrgUserId());
+ assertNull(actual);
+ }
+
+ public CentralV2RoleFunction mockCentralRoleFunction() {