UsersListProvider implements ResourceListProvider { @Override public Map<String, String> getList(String listName, ResourceListQuery query) { Map<String, String> usersList = new HashMap<>(); int offset = 0; int limit = 0; if (query != null) { if (query.getLimit().isSome()) limit = query.getLimit().get(); if (query.getOffset().isSome()) offset = query.getOffset().get(); } Iterator<User> users = userDirectoryService.findUsers("%", offset, limit); while (users.hasNext()) { User u = users.next(); if (EMAIL.equals(listName) && StringUtils.isNotBlank(u.getEmail())) { usersList.put(u.getUsername(), u.getEmail()); } else if (USERNAME.equals(listName) && StringUtils.isNotBlank(u.getUsername())) { usersList.put(u.getUsername(), u.getUsername()); } else if (NAME.equals(listName) && StringUtils.isNotBlank(u.getName())) { usersList.put(u.getUsername(), u.getName()); } else if (NAME.equals(listName) || NAME_AND_EMAIL.equals(listName) || NAME_AND_USERNAME.equals(listName)) { usersList.put(u.getUsername(), createDisplayName(u, listName)); } else if (NAME_ONLY.equals(listName) && StringUtils.isNotBlank(u.getName())) { usersList.put(u.getName(), u.getName()); } else if (EMAIL_ONLY.equals(listName) && StringUtils.isNotBlank(u.getEmail())) { usersList.put(u.getEmail(), u.getEmail()); } else if (USERDIRECTORY_ONLY.equals(listName) && StringUtils.isNotBlank(u.getProvider())) { usersList.put(u.getProvider(), u.getProvider()); } else if (ROLE_ONLY.equals(listName) && u.getRoles().size() > 0) { for (Role role : u.getRoles()) { usersList.put(role.getName(), role.getName()); } } } return usersList; }  void setUserDirectoryService(UserDirectoryService userDirectoryService); @Override String[] getListNames(); @Override Map<String, String> getList(String listName, ResourceListQuery query); @Override boolean isTranslatable(String listName); @Override String getDefault(); static final String NAME; static final String NAME_AND_EMAIL; static final String NAME_AND_USERNAME; static final String USERNAME; static final String EMAIL; static final String NAME_ONLY; static final String EMAIL_ONLY; static final String ROLE_ONLY; static final String USERDIRECTORY_ONLY; }
@Test public void testListSimple() throws ListProviderException { Map<String, String> list = usersListProvider.getList(UsersListProvider.NAME_ONLY, null); Assert.assertTrue(list.containsKey(user1.getName())); Assert.assertTrue(list.containsKey(user2.getName())); Assert.assertTrue(list.containsKey(user3.getName())); Assert.assertEquals(3, list.size()); list = usersListProvider.getList(UsersListProvider.USERNAME, null); Assert.assertTrue(list.containsKey(user1.getUsername())); Assert.assertTrue(list.containsKey(user2.getUsername())); Assert.assertTrue(list.containsKey(user3.getUsername())); Assert.assertTrue(list.containsKey(user4.getUsername())); Assert.assertEquals(4, list.size()); list = usersListProvider.getList(UsersListProvider.EMAIL_ONLY, null); Assert.assertTrue(list.containsKey(user1.getEmail())); Assert.assertTrue(list.containsKey(user4.getEmail())); Assert.assertEquals(2, list.size()); list = usersListProvider.getList(UsersListProvider.USERDIRECTORY_ONLY, null); Assert.assertTrue(list.containsKey(user1.getProvider())); Assert.assertEquals(1, list.size()); }
@Test public void testListQuery() { ResourceListQueryImpl query = new ResourceListQueryImpl(); query.setLimit(0); query.setOffset(0); Assert.assertEquals(4, usersListProvider.getList(UsersListProvider.USERNAME, query).size()); query.setOffset(3); Assert.assertEquals(1, usersListProvider.getList(UsersListProvider.USERNAME, query).size()); query.setOffset(0); query.setLimit(1); Assert.assertEquals(1, usersListProvider.getList(UsersListProvider.USERNAME, query).size()); }