@Test
  public void select_groups_not_affected_to_a_user_by_query() throws Exception {
    setupData("shared");

    // 200 is member of 3 groups
    assertThat(
            dao.selectGroups(
                GroupMembershipQuery.builder()
                    .login("arthur")
                    .membership(GroupMembershipQuery.OUT)
                    .build(),
                200L))
        .isEmpty();
    // 201 is member of 1 group on 3
    assertThat(
            dao.selectGroups(
                GroupMembershipQuery.builder()
                    .login("arthur")
                    .membership(GroupMembershipQuery.OUT)
                    .build(),
                201L))
        .hasSize(2);
    // 999 is member of 0 group
    assertThat(
            dao.selectGroups(
                GroupMembershipQuery.builder()
                    .login("arthur")
                    .membership(GroupMembershipQuery.OUT)
                    .build(),
                2999L))
        .hasSize(3);
  }
  @Test
  public void search_by_group_name_with_capitalization() throws Exception {
    setupData("shared");

    List<GroupMembershipDto> result =
        dao.selectGroups(
            GroupMembershipQuery.builder().login("arthur").groupSearch("UsER").build(), 200L);
    assertThat(result).hasSize(1);

    assertThat(result.get(0).getName()).isEqualTo("sonar-users");

    result =
        dao.selectGroups(
            GroupMembershipQuery.builder().login("arthur").groupSearch("sonar").build(), 200L);
    assertThat(result).hasSize(3);
  }
  @Test
  public void select_all_groups_by_query() throws Exception {
    setupData("shared");

    GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build();
    List<GroupMembershipDto> result = dao.selectGroups(query, 200L);
    assertThat(result).hasSize(3);
  }
  @Test
  public void should_be_paginated() throws Exception {
    setupData("shared");

    List<GroupMembershipDto> result =
        dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 0, 2);
    assertThat(result).hasSize(2);
    assertThat(result.get(0).getName()).isEqualTo("sonar-administrators");
    assertThat(result.get(1).getName()).isEqualTo("sonar-reviewers");

    result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 1, 2);
    assertThat(result).hasSize(2);
    assertThat(result.get(0).getName()).isEqualTo("sonar-reviewers");
    assertThat(result.get(1).getName()).isEqualTo("sonar-users");

    result = dao.selectGroups(GroupMembershipQuery.builder().login("arthur").build(), 200L, 2, 1);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getName()).isEqualTo("sonar-users");
  }
  // > SETUP HELPER METHODS
  private Group createClass(int memberCount) throws Exception {
    Group g = new Group(new Group(null, null), "test-group");
    groupDao.saveGroup(g);

    for (int i = 0; i < memberCount; i++) {
      Contact c = new Contact("test-contact-" + i, "012345" + i, null, null, null, true);
      contactDao.saveContact(c);
      groupMembershipDao.addMember(g, c);
    }

    return g;
  }
  @Test
  public void select_user_group() throws Exception {
    setupData("select_user_group");

    GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build();
    List<GroupMembershipDto> result = dao.selectGroups(query, 201L);
    assertThat(result).hasSize(1);

    GroupMembershipDto dto = result.get(0);
    assertThat(dto.getId()).isEqualTo(101L);
    assertThat(dto.getName()).isEqualTo("sonar-users");
    assertThat(dto.getUserId()).isEqualTo(201L);
  }
  @Test
  public void find_with_paging_having_more_results() {
    GroupMembershipQuery query =
        GroupMembershipQuery.builder().login("arthur").pageIndex(1).pageSize(2).build();
    when(groupMembershipDao.selectGroups(eq(query), anyLong(), anyInt(), anyInt()))
        .thenReturn(
            newArrayList(
                new GroupMembershipDto().setId(1L).setName("group1"),
                new GroupMembershipDto().setId(2L).setName("group2"),
                new GroupMembershipDto().setId(3L).setName("group3")));
    GroupMembershipFinder.Membership result = finder.find(query);

    ArgumentCaptor<Integer> argumentOffset = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<Integer> argumentLimit = ArgumentCaptor.forClass(Integer.class);
    verify(groupMembershipDao)
        .selectGroups(eq(query), anyLong(), argumentOffset.capture(), argumentLimit.capture());

    assertThat(argumentOffset.getValue()).isEqualTo(0);
    assertThat(argumentLimit.getValue()).isEqualTo(3);
    assertThat(result.hasMoreResults()).isTrue();
  }
  @Test
  public void find() {
    GroupMembershipQuery query = GroupMembershipQuery.builder().login("arthur").build();
    when(groupMembershipDao.selectGroups(eq(query), anyLong(), anyInt(), anyInt()))
        .thenReturn(
            newArrayList(
                new GroupMembershipDto()
                    .setId(1L)
                    .setName("users")
                    .setDescription("Users group")
                    .setUserId(100L)));

    GroupMembershipFinder.Membership result = finder.find(query);
    assertThat(result.groups()).hasSize(1);
    assertThat(result.hasMoreResults()).isFalse();

    GroupMembership group = result.groups().get(0);
    assertThat(group.id()).isEqualTo(1);
    assertThat(group.name()).isEqualTo("users");
    assertThat(group.description()).isEqualTo("Users group");
    assertThat(group.isMember()).isTrue();
  }
 private Contact[] getContacts(Group g) {
   return groupMembershipDao.getActiveMembers(g).toArray(new Contact[0]);
 }