DBUserGroupCallbackImpl extends AbstractUserGroupInfo implements UserGroupCallback { public List<String> getGroupsForUser(String userId) { if (userId == null) { throw new IllegalArgumentException("UserId cannot be null"); } List<String> roles = new ArrayList<String>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = ds.getConnection(); ps = conn.prepareStatement(this.config.getProperty(USER_ROLES_QUERY)); try { ps.setString(1, userId); } catch (ArrayIndexOutOfBoundsException ignore) { } rs = ps.executeQuery(); while (rs.next()) { roles.add(rs.getString(1)); } } catch (Exception e) { logger.error("Error when checking roles in db, parameter: " + userId, e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { } } if (ps != null) { try { ps.close(); } catch (SQLException e) { } } if (conn != null) { try { conn.close(); } catch (Exception ex) { } } } return roles; } DBUserGroupCallbackImpl(boolean activate);  DBUserGroupCallbackImpl(Properties config); boolean existsUser(String userId); boolean existsGroup(String groupId); List<String> getGroupsForUser(String userId); static final String DS_JNDI_NAME; static final String PRINCIPAL_QUERY; static final String USER_ROLES_QUERY; static final String ROLES_QUERY; }
@Test public void testUserGroups() { List<String> groups = callback.getGroupsForUser("john"); assertNotNull(groups); assertEquals(1, groups.size()); assertEquals("PM", groups.get(0)); }
@Test public void testNoUserGroups() { List<String> groups = callback.getGroupsForUser("mike"); assertNotNull(groups); assertEquals(0, groups.size()); }
@Test(expected = IllegalArgumentException.class) public void testInvalidArgument() { callback.getGroupsForUser(null); fail("Should fail as it does not have valid configuration"); }