RoleBasedUserSearchUtil extends BaseLoader { public void getUsersBasedOnRole(List<String> userRoleList) { try { if (!CollectionUtils.isEmpty(userRoleList) && userRoleList != null) { Map<String, String> roleSysAdminMap = new HashMap<String, String>(); Map<String, String> roleAdminAuditorMap = new HashMap<String, String>(); Map<String, String> roleKeyAdminMap = new HashMap<String, String>(); Map<String, String> roleKeyAdminAuditorMap = new HashMap<String, String>(); Map<String, String> roleUserMap = new HashMap<String, String>(); for (String userRole : userRoleList) { List<XXPortalUser> listXXPortalUser = daoMgr.getXXPortalUser().findByRole(userRole); if (listXXPortalUser != null && !CollectionUtils.isEmpty(listXXPortalUser)) { if (userRole.equalsIgnoreCase(RangerConstants.ROLE_SYS_ADMIN)) { for (XXPortalUser xXPortalUser : listXXPortalUser) { roleSysAdminMap.put(xXPortalUser.getLoginId(),userRole); } } else if (userRole.equalsIgnoreCase(RangerConstants.ROLE_ADMIN_AUDITOR)) { for (XXPortalUser xXPortalUser : listXXPortalUser) { roleAdminAuditorMap.put(xXPortalUser.getLoginId(),userRole); } } else if (userRole.equalsIgnoreCase(RangerConstants.ROLE_KEY_ADMIN)) { for (XXPortalUser xXPortalUser : listXXPortalUser) { roleKeyAdminMap.put(xXPortalUser.getLoginId(),userRole); } } else if (userRole.equalsIgnoreCase(RangerConstants.ROLE_KEY_ADMIN_AUDITOR)) { for (XXPortalUser xXPortalUser : listXXPortalUser) { roleKeyAdminAuditorMap.put(xXPortalUser.getLoginId(),userRole); } } else if (userRole.equalsIgnoreCase(RangerConstants.ROLE_USER)) { for (XXPortalUser xXPortalUser : listXXPortalUser) { roleUserMap.put(xXPortalUser.getLoginId(),userRole); } } } } if (MapUtils.isEmpty(roleSysAdminMap) && MapUtils.isEmpty(roleKeyAdminMap) && MapUtils.isEmpty(roleUserMap) && MapUtils.isEmpty(roleAdminAuditorMap) && MapUtils.isEmpty(roleKeyAdminAuditorMap)) { System.out.println("users with given user role are not there"); logger.error("users with given user role are not there"); System.exit(1); } else { if (!MapUtils.isEmpty(roleSysAdminMap)) { for(Entry<String, String> entry : roleSysAdminMap.entrySet()){ System.out.println(entry.getValue() + " : " + entry.getKey()); } } if (!MapUtils.isEmpty(roleKeyAdminMap)) { for(Entry<String, String> entry : roleKeyAdminMap.entrySet()){ System.out.println(entry.getValue() + " : " + entry.getKey()); } } if (!MapUtils.isEmpty(roleUserMap)) { for(Entry<String, String> entry : roleUserMap.entrySet()){ System.out.println(entry.getValue() + " : " + entry.getKey()); } } if (!MapUtils.isEmpty(roleAdminAuditorMap)) { for(Entry<String, String> entry : roleAdminAuditorMap.entrySet()){ System.out.println(entry.getValue() + " : " + entry.getKey()); } } if (!MapUtils.isEmpty(roleKeyAdminAuditorMap)) { for(Entry<String, String> entry : roleKeyAdminAuditorMap.entrySet()){ System.out.println(entry.getValue() + " : " + entry.getKey()); } } if (userRoleList.contains(RangerConstants.ROLE_SYS_ADMIN)) { System.out.println("ROLE_SYS_ADMIN Total Count : " + roleSysAdminMap.size()); } if (userRoleList.contains(RangerConstants.ROLE_KEY_ADMIN)) { System.out.println("ROLE_KEY_ADMIN Total Count : " + roleKeyAdminMap.size()); } if (userRoleList.contains(RangerConstants.ROLE_USER)) { System.out.println("ROLE_USER Total Count : " + roleUserMap.size()); } if (userRoleList.contains(RangerConstants.ROLE_ADMIN_AUDITOR)) { System.out.println("ROLE_ADMIN_AUDITOR Total Count : " + roleAdminAuditorMap.size()); } if (userRoleList.contains(RangerConstants.ROLE_KEY_ADMIN_AUDITOR)) { System.out.println("ROLE_KEY_ADMIN_AUDITOR Total Count : " + roleKeyAdminAuditorMap.size()); } int total = roleSysAdminMap.size() + roleKeyAdminMap.size() + roleUserMap.size() + roleAdminAuditorMap.size() + roleKeyAdminAuditorMap.size(); System.out.println("Total Count : " + total); } } } catch (Exception e) { logger.error("Error getting User's List with the mentioned role: "+ e.getMessage()); } }  static void main(String[] args); @Override void init(); @Override void printStats(); @Override void execLoad(); void getUsersBasedOnRole(List<String> userRoleList); void validateUserAndFetchUserList(); static Boolean checkRole; static String userLoginId; static String currentPassword; static String userRole; }
@Test public void TestGetUsersBasedOnRole() { try { XXPortalUser xXPortalUser = new XXPortalUser(); xXPortalUser.setLoginId("testUser"); xXPortalUser.setId(1L); xXPortalUser.setFirstName("testUser"); xXPortalUser.setPublicScreenName("testUser"); xXPortalUser.setPassword("testUserPassword"); List<XXPortalUser> listXXPortalUser = new ArrayList<XXPortalUser>(); listXXPortalUser.add(xXPortalUser); List<String> userRoleList = new ArrayList<String>(); userRoleList.add("ROLE_SYS_ADMIN"); Mockito.when(daoMgr.getXXPortalUser()).thenReturn(xXPortalUserDao); Mockito.when(xXPortalUserDao.findByRole(RangerConstants.ROLE_SYS_ADMIN)).thenReturn(listXXPortalUser); roleBasedUserSearchUtil.getUsersBasedOnRole(userRoleList); Mockito.verify(xXPortalUserDao).findByRole(RangerConstants.ROLE_SYS_ADMIN); } catch(Exception e) { fail("test failed due to: " + e.getMessage()); } }