@Test
  public void test_getUsersForGroupOrUser_oneUserDoesNotExist_success() {
    when(mockUserAccessor.getGroup(SOME_USER1.getName())).thenReturn(null);
    when(mockUserAccessor.getUser(SOME_USER1.getName())).thenReturn(null);

    final List<User> result = classUnderTest.getActiveUsersForGroupOrUser(SOME_USER1.getName());

    assertThat(result, hasSize(0));
  }
  @Test
  public void test_getUsersForGroupOrUser_oneUserExistsAndIsNotDeactivated_success() {
    when(mockUserAccessor.getGroup(SOME_USER1.getName())).thenReturn(null);
    when(mockUserAccessor.getUser(SOME_USER1.getName())).thenReturn(SOME_USER1);
    when(mockUserAccessor.isDeactivated(SOME_USER1)).thenReturn(false);

    final List<User> result = classUnderTest.getActiveUsersForGroupOrUser(SOME_USER1.getName());

    assertThat(result, containsInAnyOrder(SOME_USER1));
  }
  @Test
  public void test_getUsersForGroupOrUser_oneGroupReturnsTwoUsers_success() {
    final DefaultGroup group1 = new DefaultGroup("group1");

    when(mockUserAccessor.getGroup(group1.getName())).thenReturn(group1);
    when(mockUserAccessor.getMemberNamesAsList(group1))
        .thenReturn(newArrayList(SOME_USER1.getName(), SOME_USER2.getName()));
    when(mockUserAccessor.getUser(SOME_USER1.getName())).thenReturn(SOME_USER1);
    when(mockUserAccessor.getUser(SOME_USER2.getName())).thenReturn(SOME_USER2);

    final List<User> result = classUnderTest.getActiveUsersForGroupOrUser(group1.getName());

    assertThat(result, containsInAnyOrder(SOME_USER1, SOME_USER2));
  }
  @Test
  public void test_getUserByName_nullNotFound_anonymous() {
    when(mockUserAccessor.getUser(null)).thenReturn(null);

    User result = classUnderTest.getUserByName(null);

    assertThat(result.getFullName(), is(new AnonymousUser().getFullName()));
  }
  @Test
  public void test_getRemoteUser_success() {
    when(mockUserManager.getRemoteUsername()).thenReturn("someUser");
    when(mockUserAccessor.getUser("someUser")).thenReturn(new DefaultUser("someUser"));

    final User remoteUser = classUnderTest.getRemoteUser();

    assertThat(remoteUser.getName(), is("someUser"));
  }
 @Test
 public void test_isPermissionListEmptyOrContainsGivenUser_isMemberInGroup_success() {
   List<String> permissionsList = new ArrayList<String>();
   permissionsList.add("IsAGroup");
   permissionsList.add("AnotherUser");
   when(mockUserAccessor.hasMembership("IsAGroup", "KnownUser")).thenReturn(true);
   final Boolean canPerformAction =
       classUnderTest.isPermissionListEmptyOrContainsGivenUser(
           permissionsList, new DefaultUser("KnownUser"));
   assertEquals(Boolean.TRUE, canPerformAction);
 }
  @Test
  public void test_canCreatePage_success() {
    final Page contentEntityObject = new Page();

    when(mockUserManager.getRemoteUsername()).thenReturn(SOME_USER1.getName());
    when(mockUserAccessor.getUser(SOME_USER1.getName())).thenReturn(SOME_USER1);
    when(mockPermissionManager.hasPermission(SOME_USER1, Permission.EDIT, contentEntityObject))
        .thenReturn(true);

    final boolean result = classUnderTest.canCreatePage(contentEntityObject);
    assertThat(result, is(true));
  }
  @Test
  public void test_canAttachFile_success() {
    ContentEntityObject contentEntityObject = new Page();

    when(mockUserManager.getRemoteUsername()).thenReturn(SOME_USER1.getName());
    when(mockUserAccessor.getUser(SOME_USER1.getName())).thenReturn(SOME_USER1);
    when(mockPermissionManager.hasCreatePermission(
            SOME_USER1, contentEntityObject, Attachment.class))
        .thenReturn(true);

    boolean result = classUnderTest.canAttachFile(contentEntityObject);

    assertThat(result, is(true));
  }
  public Pager findUsersWhoseNameStartsWith(String partialName, ServiceContext context) {
    log.debug("findUsersWhoseNameStartsWith() called. partialName='" + partialName + "'");
    Pager pager = null;

    try {
      UserNameTermQuery query = new UserNameTermQuery(partialName, TermQuery.SUBSTRING_STARTS_WITH);
      SearchResult searchResult = userAccessor.findUsers(query);
      pager = searchResult.pager();
    } catch (EntityException e) {
      LogUtil.errorWithRemoteUserInfo(
          log, "Error finding username that starts with " + partialName, e);
    }

    return pager;
  }
  public AdvancedUserQueryResults findUsers(
      AdvancedUserQuery advancedUserQuery, ServiceContext context) throws FindException {
    log.debug("findUsers() called.");
    AdvancedUserQueryResults results = new AdvancedUserQueryResults();

    // TODO: this is really slow with osuser search. must use
    // http://confluence.atlassian.com/display/DOC/How+to+Improve+User+Search+Performance

    checkSearchForUnsupportedTerms(advancedUserQuery.getPartialSearchTerm(), results, context);

    Pager pager = new DefaultPager(new ArrayList());
    if (advancedUserQuery.isUsernameSearchDefined()) {
      try {
        UserNameTermQuery query =
            new UserNameTermQuery(
                advancedUserQuery.getPartialSearchTerm(),
                advancedUserQuery.getSubstringMatchType());
        SearchResult result = userAccessor.findUsers(query);
        pager = result.pager();
        // results.setMessage("" + PagerUtils.count(pager) + " returned");
      } catch (EntityException e) {
        LogUtil.warnWithRemoteUserInfo(log, "query by username failed due to EntityException", e);
        results.setMessage("" + e);
      } catch (IllegalArgumentException e) {
        // if search type is not allowed
        LogUtil.warnWithRemoteUserInfo(
            log,
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'",
            e);
        results.setMessage(
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'");
      }
    }

    if (advancedUserQuery.isFullnameSearchDefined()) {
      try {
        FullNameTermQuery query =
            new FullNameTermQuery(
                advancedUserQuery.getPartialSearchTerm(),
                advancedUserQuery.getSubstringMatchType());
        SearchResult result = userAccessor.findUsers(query);
        pager = result.pager();
        // results.setMessage("" + PagerUtils.count(pager) + " returned");
      } catch (EntityException e) {
        LogUtil.warnWithRemoteUserInfo(
            log, "query by user fullname failed due to EntityException", e);
        results.setMessage("" + e);
      } catch (IllegalArgumentException e) {
        // if search type is not allowed
        LogUtil.warnWithRemoteUserInfo(
            log,
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'",
            e);
        results.setMessage(
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'");
      }
    }

    if (advancedUserQuery.isEmailSearchDefined()) {
      try {
        EmailTermQuery query =
            new EmailTermQuery(
                advancedUserQuery.getPartialSearchTerm(),
                advancedUserQuery.getSubstringMatchType());
        SearchResult result = userAccessor.findUsers(query);
        pager = result.pager();
        // results.setMessage("" + PagerUtils.count(pager) + " returned");
      } catch (EntityException e) {
        LogUtil.warnWithRemoteUserInfo(log, "query by user email failed due to EntityException", e);
        results.setMessage("" + e);
      } catch (IllegalArgumentException e) {
        // if search type is not allowed
        LogUtil.warnWithRemoteUserInfo(
            log,
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'",
            e);
        results.setMessage(
            "Bad value '"
                + advancedUserQuery.getPartialSearchTerm()
                + "' for search type '"
                + advancedUserQuery.getSubstringMatchType()
                + "'");
      }
    }

    results.setUsers(pager);

    return results;
  }