@Test
  public void testFindGroupsWithNoMatch() throws Exception {
    final MockUser mockUser = new MockUser("mocky");

    expect(authenticationContext.getLoggedInUser()).andReturn(mockUser);
    expect(permissionManager.hasPermission(Permissions.USER_PICKER, mockUser)).andReturn(true);
    expect(groupPickerSearchService.findGroups("la")).andStubReturn(matchingGroups);
    expect(authenticationContext.getI18nHelper()).andStubReturn(i18nHelper);
    expect(applicationProperties.getDefaultBackedString(APKeys.JIRA_AJAX_AUTOCOMPLETE_LIMIT))
        .andStubReturn("20");

    GroupPickerResource resource = new GroupPickerResource(groupPickerResourceHelper);

    final List<GroupSuggestionBean> expectedGroups = Lists.newArrayList();

    final GroupSuggestionsBean expectedSuggestions =
        new GroupSuggestionsBean(
            0,
            NoopI18nHelper.makeTranslation(
                GroupPickerResourceHelperImpl.MORE_GROUP_RESULTS_I18N_KEY, "0", "0"),
            expectedGroups);

    replay(
        groupPickerSearchService, authenticationContext, applicationProperties, permissionManager);

    Response response = resource.findGroups("la", null, null);
    assertResponseCacheNever(response);
    assertResponseBody(expectedSuggestions, response);

    verify(groupPickerSearchService, authenticationContext, applicationProperties);
  }
  @Test
  public void testUserWithNoBrowseUserPermissionsDoesntReturnNonExactMatchedGroup() {
    String mockGroupName = "mockGroup";
    final MockUser mockUser = new MockUser("mocky");
    final MockGroup mockGroup = new MockGroup(mockGroupName);

    expect(authenticationContext.getLoggedInUser()).andReturn(mockUser);
    expect(permissionManager.hasPermission(Permissions.USER_PICKER, mockUser)).andReturn(false);
    expect(groupPickerSearchService.getGroupByName(mockGroupName)).andStubReturn(mockGroup);
    expect(authenticationContext.getI18nHelper()).andStubReturn(i18nHelper);
    expect(applicationProperties.getDefaultBackedString(APKeys.JIRA_AJAX_AUTOCOMPLETE_LIMIT))
        .andStubReturn("20");

    GroupPickerResource resource = new GroupPickerResource(groupPickerResourceHelper);

    final List<GroupSuggestionBean> expectedGroups = Lists.newArrayList();

    final GroupSuggestionsBean expectedSuggestions =
        new GroupSuggestionsBean(
            0,
            NoopI18nHelper.makeTranslation(
                GroupPickerResourceHelperImpl.MORE_GROUP_RESULTS_I18N_KEY, 0, 0),
            expectedGroups);

    replay(
        groupPickerSearchService, authenticationContext, applicationProperties, permissionManager);
    Response response = resource.findGroups("mockGr", null, null);
    assertResponseBody(expectedSuggestions, response);
  }
  @Test
  public void testFindGroupsWithDefaultLimit() throws Exception {
    for (int i = 0; i < GroupPickerResourceHelperImpl.DEFAULT_MAX_RESULTS + 1; ++i) {
      matchingGroups.add(new MockGroup("a" + String.valueOf(i)));
    }

    final MockUser mockUser = new MockUser("mocky");

    expect(authenticationContext.getLoggedInUser()).andReturn(mockUser);
    expect(permissionManager.hasPermission(Permissions.USER_PICKER, mockUser)).andReturn(true);
    expect(groupPickerSearchService.findGroups("a")).andStubReturn(matchingGroups);
    expect(authenticationContext.getI18nHelper()).andStubReturn(i18nHelper);
    expect(applicationProperties.getDefaultBackedString(APKeys.JIRA_AJAX_AUTOCOMPLETE_LIMIT))
        .andStubReturn(null);

    GroupPickerResource resource = new GroupPickerResource(groupPickerResourceHelper);

    final List<GroupSuggestionBean> expectedGroups = Lists.newArrayList();
    for (int i = 0; i < GroupPickerResourceHelperImpl.DEFAULT_MAX_RESULTS; ++i) {
      expectedGroups.add(
          new GroupSuggestionBean("a" + String.valueOf(i), "<b>a</b>" + String.valueOf(i)));
    }

    final GroupSuggestionsBean expectedSuggestions =
        new GroupSuggestionsBean(
            GroupPickerResourceHelperImpl.DEFAULT_MAX_RESULTS + 1,
            NoopI18nHelper.makeTranslation(
                GroupPickerResourceHelperImpl.MORE_GROUP_RESULTS_I18N_KEY,
                String.valueOf(GroupPickerResourceHelperImpl.DEFAULT_MAX_RESULTS),
                String.valueOf(GroupPickerResourceHelperImpl.DEFAULT_MAX_RESULTS + 1)),
            expectedGroups);

    replay(
        groupPickerSearchService, authenticationContext, applicationProperties, permissionManager);

    Response response = resource.findGroups("a", null, null);
    assertResponseCacheNever(response);
    assertResponseBody(expectedSuggestions, response);

    verify(groupPickerSearchService, authenticationContext, applicationProperties);
  }
  @Test
  public void testExcludedGroups() {
    for (int i = 1; i < 4; ++i) {
      matchingGroups.add(new MockGroup("a" + String.valueOf(i)));
    }

    final MockUser mockUser = new MockUser("mocky");

    expect(authenticationContext.getLoggedInUser()).andReturn(mockUser);
    expect(permissionManager.hasPermission(Permissions.USER_PICKER, mockUser)).andReturn(true);
    expect(groupPickerSearchService.findGroups("a")).andStubReturn(matchingGroups);
    expect(authenticationContext.getI18nHelper()).andStubReturn(i18nHelper);
    expect(applicationProperties.getDefaultBackedString(APKeys.JIRA_AJAX_AUTOCOMPLETE_LIMIT))
        .andStubReturn("20");

    GroupPickerResource resource = new GroupPickerResource(groupPickerResourceHelper);

    final List<GroupSuggestionBean> expectedGroups = Lists.newArrayList();
    expectedGroups.add(new GroupSuggestionBean("a3", "<b>a</b>3"));

    final GroupSuggestionsBean expectedSuggestions =
        new GroupSuggestionsBean(
            1,
            NoopI18nHelper.makeTranslation(
                GroupPickerResourceHelperImpl.MORE_GROUP_RESULTS_I18N_KEY, 1, 1),
            expectedGroups);

    List<String> excluded = new ArrayList<String>();
    excluded.add("a1");
    excluded.add("a2");

    replay(
        groupPickerSearchService, authenticationContext, applicationProperties, permissionManager);
    Response response = resource.findGroups("a", excluded, null);
    assertResponseBody(expectedSuggestions, response);
  }