@Test
 public void getPrimaryGroupUserIDsWhenNoMembersReturnsEmptyList()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   when(sql.sqlQuery(anyString())).thenReturn(result);
   when(result.next()).thenReturn(false);
   assertEquals(0, webGroupDao.getUserIDsFromPrimaryGroup(group1).size());
 }
 @Test
 public void getPrimaryGroupKeylessWithUnknownIDReturnsBlank()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   configuration.webappPrimaryGroupUsesKey = false;
   when(sql.sqlQuery(anyString())).thenReturn(result);
   assertEquals("", webGroupDao.getPrimaryGroupID(user_id1));
 }
 @Test
 public void convertDelimitedStringWithTwoItemsReturnsTwoItems() {
   List<String> idList = webGroupDao.convertDelimitedIDString(group1 + "," + group2);
   assertEquals(2, idList.size());
   assertTrue(idList.contains(group1));
   assertTrue(idList.contains(group2));
 }
  @Test
  public void addCleanIDDoesNotAddEmptyString() {
    List<String> list = new ArrayList<String>();

    webGroupDao.addCleanID("", list);
    assertEquals(0, list.size());
  }
 @Test
 public void getPrimaryGroupKeyedWithValidIDReturnsGroup()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   when(sql.sqlQuery(anyString())).thenReturn(result);
   when(result.next()).thenReturn(Boolean.TRUE);
   when(result.getString(configuration.webappPrimaryGroupGroupIDColumn)).thenReturn(group1);
   assertEquals(group1, webGroupDao.getPrimaryGroupID(user_id1));
 }
  @Test
  public void addCleanIDDoesCleansWhitespace() {
    List<String> list = new ArrayList<String>();

    webGroupDao.addCleanID(" 1  ", list);
    assertEquals(1, list.size());
    assertTrue(list.contains("1"));
  }
 @Test
 public void getSecondaryGroupsWithNullReturnsEmptyList()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   groups = "          ";
   when(result.getString(configuration.webappSecondaryGroupGroupIDColumn)).thenReturn(null);
   List<String> secondaryGroups = webGroupDao.getSecondaryGroupIDs(USER_ID);
   assertEquals(0, secondaryGroups.size());
 }
 @Test
 public void getSecondaryGroupUserIDsWhenWhitespaceResultsReturnsEmptyList()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   groups = "              ";
   when(result.getString(configuration.webappSecondaryGroupGroupIDColumn)).thenReturn(groups);
   List<String> secondaryGroups = webGroupDao.getSecondaryGroupUserIDs(GROUP_ID);
   assertEquals(0, secondaryGroups.size());
 }
 @Test
 public void addGroupWithTwoCountUsesCorrectQuery()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   String query = getAddGroupUpdateQuery(",");
   doNothing().when(sql).updateQuery(query);
   webGroupDao.addUserToGroup(USER_ID, GROUP_ID, 2);
   verify(sql).updateQuery(query);
 }
  @Test
  public void addCleanIDAddsID() {
    List<String> list = new ArrayList<String>();

    webGroupDao.addCleanID(group1, list);
    assertEquals(1, list.size());
    assertTrue(list.contains(group1));
  }
 @Test
 public void getSecondaryGroupsReturnsOneGroupID()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   groups = RandomStringUtils.randomNumeric(2);
   when(result.getString(configuration.webappSecondaryGroupGroupIDColumn)).thenReturn(groups);
   List<String> secondaryGroups = webGroupDao.getSecondaryGroupIDs(USER_ID);
   assertEquals(1, secondaryGroups.size());
   assertEquals(groups, secondaryGroups.get(0));
 }
 @Test
 public void getSecondaryGroupsReturnsOnlyGroupIDs()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   groups = " , " + group2;
   when(result.getString(configuration.webappSecondaryGroupGroupIDColumn)).thenReturn(groups);
   List<String> secondaryGroups = webGroupDao.getSecondaryGroupIDs(USER_ID);
   assertEquals(1, secondaryGroups.size());
   assertTrue(secondaryGroups.contains(group2));
 }
 @Test
 public void removeGroupUsesCorrectReadQuery()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   String query = getRemoveGroupReadQuery();
   when(sql.sqlQuery(query)).thenReturn(result);
   when(result.next()).thenReturn(false);
   webGroupDao.removeUserFromGroup(USER_ID, GROUP_ID);
   verify(sql).sqlQuery(query);
 }
 @Test
 public void getPrimaryGroupUserIDsReturnsUserID()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   when(sql.sqlQuery(anyString())).thenReturn(result);
   when(result.next()).thenReturn(true, false);
   when(result.getString(configuration.webappPrimaryGroupUserIDColumn)).thenReturn(user_id1);
   List<String> groupMembers = webGroupDao.getUserIDsFromPrimaryGroup(group1);
   assertEquals(1, groupMembers.size());
   assertEquals(user_id1, groupMembers.get(0));
 }
 private void testRemoveGroupUpdateQuery(String before, String after)
     throws SQLException, IllegalAccessException, MalformedURLException, InstantiationException {
   when(sql.sqlQuery(getRemoveGroupReadQuery())).thenReturn(result);
   when(result.next()).thenReturn(true);
   when(result.getString(configuration.webappSecondaryGroupGroupIDColumn)).thenReturn(before);
   String query = getRemoveGroupUpdateQuery(after);
   doNothing().when(sql).updateQuery(query);
   webGroupDao.removeUserFromGroup(USER_ID, group1);
   verify(sql).updateQuery(query);
 }
  @Test
  public void getPrimaryGroupUserIDsWithMultipleUserIDsReturnUserIDs()
      throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
    String userID2 = RandomStringUtils.randomNumeric(2);
    when(sql.sqlQuery(anyString())).thenReturn(result);
    when(result.next()).thenReturn(true, true, false);
    when(result.getString(configuration.webappPrimaryGroupUserIDColumn))
        .thenReturn(user_id1, userID2);

    List<String> groupMembers = webGroupDao.getUserIDsFromPrimaryGroup(group1);
    assertEquals(2, groupMembers.size());
    assertEquals(user_id1, groupMembers.get(0));
    assertEquals(userID2, groupMembers.get(1));
  }
 @Test
 public void getSecondaryGroupsReturnsTwoUserIDs()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   String userID2 = RandomStringUtils.randomNumeric(2);
   groups = group1 + "," + group2;
   when(result.next()).thenReturn(true, true, false);
   when(result.getString(configuration.webappSecondaryGroupGroupIDColumn)).thenReturn(groups);
   when(result.getString(configuration.webappSecondaryGroupUserIDColumn))
       .thenReturn(USER_ID, userID2);
   List<String> secondaryGroups = webGroupDao.getSecondaryGroupUserIDs(group1);
   assertEquals(2, secondaryGroups.size());
   assertTrue(secondaryGroups.contains(USER_ID));
   assertTrue(secondaryGroups.contains(userID2));
 }
  @Test
  public void getGroupUserIDs()
      throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
    String primaryID1 = RandomStringUtils.randomNumeric(2);
    String primaryID2 = RandomStringUtils.randomNumeric(2);
    when(sql.sqlQuery(anyString())).thenReturn(result);
    when(result.next()).thenReturn(true, true, false);
    when(result.getString(configuration.webappPrimaryGroupUserIDColumn))
        .thenReturn(primaryID1, primaryID2);

    List<String> groups = webGroupDao.getGroupUserIDs(group1);
    assertEquals(4, groups.size());
    assertTrue(groups.contains(primaryID1));
    assertTrue(groups.contains(primaryID2));
    assertTrue(groups.contains(user_id1));
    assertTrue(groups.contains(user_id2));
  }
 @Test
 public void getPrimaryGroupUserIDsWhenPrimaryDisabledReturnsEmptyList()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   configuration.webappPrimaryGroupEnabled = false;
   assertEquals(0, webGroupDao.getUserIDsFromPrimaryGroup(group1).size());
 }
 private void testGroupUserIDsGroupsException(Exception exception)
     throws SQLException, InstantiationException, IllegalAccessException, MalformedURLException {
   when(sql.sqlQuery(anyString())).thenThrow(exception);
   assertEquals(0, webGroupDao.getGroupUserIDs(group1).size());
   verify(log).severe(SingleWebGroupDao.EXCEPTION_MESSAGE_GET_USERIDS + exception.getMessage());
 }
 @Test
 public void getSecondaryGroupUserIDsWhenNoQueryResultsReturnsEmptyList()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   when(result.next()).thenReturn(false);
   assertEquals(0, webGroupDao.getSecondaryGroupUserIDs(GROUP_ID).size());
 }
 @Test
 public void getSecondaryGroupUserIDsWhenSecondaryDisabledReturnsEmptyList()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   configuration.webappSecondaryGroupEnabled = false;
   assertEquals(0, webGroupDao.getSecondaryGroupUserIDs(GROUP_ID).size());
 }
 @Test
 public void getSecondaryGroupUserIDsNeverReturnNull()
     throws MalformedURLException, InstantiationException, IllegalAccessException, SQLException {
   assertNotNull(webGroupDao.getSecondaryGroupUserIDs(GROUP_ID));
 }
 @Test
 public void getPrimaryGroupWithPrimaryDisabledReturnsBlank()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   configuration.webappPrimaryGroupEnabled = false;
   assertEquals("", webGroupDao.getPrimaryGroupID(user_id1));
 }
 @Test
 public void getPrimaryGroupNeverReturnsNull()
     throws IllegalAccessException, SQLException, MalformedURLException, InstantiationException {
   configuration.webappPrimaryGroupEnabled = false;
   assertNotNull(webGroupDao.getPrimaryGroupID(""));
 }
 @Test
 public void getSecondaryGroupsShouldHandleNoResult()
     throws IllegalAccessException, InstantiationException, MalformedURLException, SQLException {
   when(result.next()).thenReturn(false);
   assertNotNull(webGroupDao.getSecondaryGroupIDs(USER_ID));
 }
 @Test
 public void convertDelimitedStringWithOneItemReturnsOneItem() {
   List<String> idList = webGroupDao.convertDelimitedIDString(group1);
   assertEquals(1, idList.size());
   assertTrue(idList.contains(group1));
 }
 @Test
 public void convertDelimitedStringWithWhitespaceStringReturnsEmpty() {
   assertEquals(0, webGroupDao.convertDelimitedIDString("  ").size());
 }
 @Test
 public void convertDelimitedStringWithNullReturnsEmpty() {
   assertEquals(0, webGroupDao.convertDelimitedIDString(null).size());
 }
 @Test
 public void convertDelimitedStringNeverReturnsNull() {
   assertNotNull(webGroupDao.convertDelimitedIDString(""));
 }