/** Dedicated permissions and paging tests */
  @Test
  public void testListingPermissionsAndPaging() throws Exception {
    PagingResults<String> systems = null;
    PagingResults<? extends BaseCredentialsInfo> creds = null;

    // Run as a test user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);

    // Initially both should be empty empty
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());

    // Create some credentials as the first user, for systems One and Two
    PasswordCredentialsInfoImpl pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_ONE);
    REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_ONE, pwCred);

    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_ONE, pwCred);

    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_TWO, pwCred);

    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_TWO, pwCred);

    // Switch to the second user, create some credentials on Two and Three
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);

    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_TWO, pwCred);

    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_THREE);
    REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_THREE, pwCred);

    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_THREE);
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_THREE, pwCred);

    // Check the listings of remote systems for each user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());
    assertEquals(TEST_REMOTE_SYSTEM_TWO, systems.getPage().get(0));

    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());
    assertEquals(TEST_REMOTE_SYSTEM_THREE, systems.getPage().get(0));

    // Check the listings of remote systems that are shared - shouldn't matter which user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(3, systems.getPage().size());

    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(3, systems.getPage().size());

    // Check the listings of the credentials by user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_SYSTEM_TWO, creds.getPage().get(0).getRemoteSystemName());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());

    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_SYSTEM_THREE, creds.getPage().get(0).getRemoteSystemName());

    // Check the shared listing of credentials, same for both users
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());

    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());

    // Check the paging of remote systems
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(3, systems.getPage().size());
    assertEquals(false, systems.hasMoreItems());
    assertEquals(3, systems.getTotalResultCount().getFirst().intValue());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(1));
    assertEquals(1, systems.getPage().size());
    assertEquals(true, systems.hasMoreItems());
    assertEquals(3, systems.getTotalResultCount().getFirst().intValue());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(1, 2));
    assertEquals(2, systems.getPage().size());
    assertEquals(false, systems.hasMoreItems());
    assertEquals(3, systems.getTotalResultCount().getFirst().intValue());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(2, 2));
    assertEquals(1, systems.getPage().size());
    assertEquals(false, systems.hasMoreItems());
    assertEquals(3, systems.getTotalResultCount().getFirst().intValue());

    // Check the paging of credentials
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    assertEquals(false, creds.hasMoreItems());
    assertEquals(2, creds.getTotalResultCount().getFirst().intValue());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(1));
    assertEquals(1, creds.getPage().size());
    assertEquals(true, creds.hasMoreItems());
    assertEquals(2, creds.getTotalResultCount().getFirst().intValue());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(1, 1));
    assertEquals(1, creds.getPage().size());
    assertEquals(false, creds.hasMoreItems());
    assertEquals(2, creds.getTotalResultCount().getFirst().intValue());

    // Tweak shared permissions
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());

    // Systems One and Two were created by users one
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(1));
    NodeRef sharedS1 = creds.getPage().get(0).getRemoteSystemContainerNodeRef();
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(1));
    NodeRef sharedS2 = creds.getPage().get(0).getRemoteSystemContainerNodeRef();

    AuthenticationUtil.setFullyAuthenticatedUser(ADMIN_USER);
    PERMISSION_SERVICE.setInheritParentPermissions(sharedS1, false);
    PERMISSION_SERVICE.setInheritParentPermissions(sharedS2, false);

    // Should then only be able to see Three, the one they created
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());

    // User One won't be able to see User Two's shared credentials under S2 under the new
    // permissions
    // They can still see their own credentials for S1 and S2, plus all in S3 (permissions
    // unchanged)
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(2, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_THREE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
  }
  /** Test CRUD on person credentials, with listing */
  @Test
  public void testPersonCredentialsCRUD() throws Exception {
    PagingResults<String> systems = null;
    PagingResults<? extends BaseCredentialsInfo> creds = null;

    // Run as a test user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);

    // Initially both should be empty empty
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());

    // Create for a person
    PasswordCredentialsInfoImpl pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_ONE);
    BaseCredentialsInfo credentials =
        REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_ONE, pwCred);

    // Check the new object was populated properly
    assertNotNull(credentials);
    assertNotNull(credentials.getNodeRef());
    assertNotNull(credentials.getRemoteSystemContainerNodeRef());
    assertEquals(TEST_REMOTE_SYSTEM_ONE, credentials.getRemoteSystemName());
    assertEquals(TEST_REMOTE_USERNAME_ONE, credentials.getRemoteUsername());
    assertEquals(
        RemoteCredentialsModel.TYPE_PASSWORD_CREDENTIALS, credentials.getCredentialsType());

    // Fetch and re-check
    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertNotNull(credentials);
    assertNotNull(credentials.getNodeRef());
    assertNotNull(credentials.getRemoteSystemContainerNodeRef());
    assertEquals(TEST_REMOTE_SYSTEM_ONE, credentials.getRemoteSystemName());
    assertEquals(TEST_REMOTE_USERNAME_ONE, credentials.getRemoteUsername());
    assertEquals(
        RemoteCredentialsModel.TYPE_PASSWORD_CREDENTIALS, credentials.getCredentialsType());

    // Won't be there for non-existent systems
    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_TWO);
    assertEquals(null, credentials);
    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_THREE);
    assertEquals(null, credentials);

    // Update
    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertEquals(PasswordCredentialsInfoImpl.class, credentials.getClass());
    pwCred = (PasswordCredentialsInfoImpl) credentials;

    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    pwCred.setRemotePassword("testing");

    credentials = REMOTE_CREDENTIALS_SERVICE.updateCredentials(pwCred);
    assertNotNull(credentials);
    assertEquals(TEST_REMOTE_USERNAME_TWO, credentials.getRemoteUsername());

    // Fetch and re-check
    pwCred =
        (PasswordCredentialsInfoImpl)
            REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertNotNull(pwCred);
    assertEquals(TEST_REMOTE_USERNAME_TWO, pwCred.getRemoteUsername());
    assertEquals("testing", pwCred.getRemotePassword());

    // Update the auth worked flag
    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertEquals(true, credentials.getLastAuthenticationSucceeded());

    // To the same thing
    credentials =
        REMOTE_CREDENTIALS_SERVICE.updateCredentialsAuthenticationSucceeded(true, credentials);
    assertEquals(true, credentials.getLastAuthenticationSucceeded());

    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertEquals(true, credentials.getLastAuthenticationSucceeded());

    // To a different things
    credentials =
        REMOTE_CREDENTIALS_SERVICE.updateCredentialsAuthenticationSucceeded(false, credentials);
    assertEquals(false, credentials.getLastAuthenticationSucceeded());

    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertEquals(false, credentials.getLastAuthenticationSucceeded());

    // And back
    credentials =
        REMOTE_CREDENTIALS_SERVICE.updateCredentialsAuthenticationSucceeded(true, credentials);
    assertEquals(true, credentials.getLastAuthenticationSucceeded());

    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertEquals(true, credentials.getLastAuthenticationSucceeded());

    // List remote systems
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    // List the credentials
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_TWO, creds.getPage().get(0).getRemoteUsername());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listAllCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_TWO, creds.getPage().get(0).getRemoteUsername());

    // Delete
    REMOTE_CREDENTIALS_SERVICE.deleteCredentials(credentials);

    credentials = REMOTE_CREDENTIALS_SERVICE.getPersonCredentials(TEST_REMOTE_SYSTEM_ONE);
    assertEquals(null, credentials);

    // List again - credentials should have gone, but system remains
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listAllCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    // Create credentials of Password, OAuth1 and OAuth2 types
    pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_ONE);
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_TWO, pwCred);

    OAuth1CredentialsInfoImpl oa1Cred = new OAuth1CredentialsInfoImpl();
    oa1Cred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    oa1Cred.setOAuthToken("test");
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_TWO, oa1Cred);

    OAuth2CredentialsInfoImpl oa2Cred = new OAuth2CredentialsInfoImpl();
    oa2Cred.setRemoteUsername(TEST_REMOTE_USERNAME_THREE);
    oa2Cred.setOauthAccessToken("testA");
    oa2Cred.setOauthRefreshToken("testR");
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_TWO, oa2Cred);

    // List, should see all three sets of credentials
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(3, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listAllCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(3, creds.getPage().size());

    // List the systems, still only system one and two
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());

    // Check we can filter credentials by type
    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO, null, new PagingRequest(10));
    assertEquals(3, creds.getPage().size());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO,
            RemoteCredentialsModel.TYPE_PASSWORD_CREDENTIALS,
            new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_ONE, creds.getPage().get(0).getRemoteUsername());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO,
            RemoteCredentialsModel.TYPE_OAUTH1_CREDENTIALS,
            new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_TWO, creds.getPage().get(0).getRemoteUsername());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_TWO,
            RemoteCredentialsModel.TYPE_OAUTH2_CREDENTIALS,
            new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_THREE, creds.getPage().get(0).getRemoteUsername());
  }
  /** Test CRUD on shared credentials, with listing */
  @Test
  public void testSharedCredentialsCRUD() throws Exception {
    PagingResults<String> systems = null;
    PagingResults<? extends BaseCredentialsInfo> creds = null;

    // Run as a test user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);

    // Initially both should be empty empty
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());

    // Create shared
    PasswordCredentialsInfoImpl pwCred = new PasswordCredentialsInfoImpl();
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_ONE);
    BaseCredentialsInfo credentials =
        REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_ONE, pwCred);

    // Check the new object was populated properly
    assertNotNull(credentials);
    assertNotNull(credentials.getNodeRef());
    assertNotNull(credentials.getRemoteSystemContainerNodeRef());
    assertEquals(TEST_REMOTE_SYSTEM_ONE, credentials.getRemoteSystemName());
    assertEquals(TEST_REMOTE_USERNAME_ONE, credentials.getRemoteUsername());
    assertEquals(
        RemoteCredentialsModel.TYPE_PASSWORD_CREDENTIALS, credentials.getCredentialsType());

    // Update
    pwCred = (PasswordCredentialsInfoImpl) credentials;
    pwCred.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    credentials = REMOTE_CREDENTIALS_SERVICE.updateCredentials(pwCred);

    assertNotNull(credentials);
    assertNotNull(credentials.getNodeRef());
    assertNotNull(credentials.getRemoteSystemContainerNodeRef());
    assertEquals(TEST_REMOTE_SYSTEM_ONE, credentials.getRemoteSystemName());
    assertEquals(TEST_REMOTE_USERNAME_TWO, credentials.getRemoteUsername());
    assertEquals(
        RemoteCredentialsModel.TYPE_PASSWORD_CREDENTIALS, credentials.getCredentialsType());

    // List
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_TWO, creds.getPage().get(0).getRemoteUsername());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listAllCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(1, creds.getPage().size());
    assertEquals(TEST_REMOTE_USERNAME_TWO, creds.getPage().get(0).getRemoteUsername());

    // Delete
    REMOTE_CREDENTIALS_SERVICE.deleteCredentials(credentials);

    // List, system remains, credentials gone
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());
    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    creds =
        REMOTE_CREDENTIALS_SERVICE.listPersonCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listSharedCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
    creds =
        REMOTE_CREDENTIALS_SERVICE.listAllCredentials(
            TEST_REMOTE_SYSTEM_ONE, null, new PagingRequest(10));
    assertEquals(0, creds.getPage().size());
  }
  /**
   * Creating shared and personal credentials, then checking how this affects the listing of Remote
   * Systems
   */
  @Test
  public void testCreateCredentialsAndSystemListing() throws Exception {
    PagingResults<String> systems = null;

    // Run as a test user
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);

    // Initially both should be empty
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(
        "No systems should be found, got " + systems.getPage(), 0, systems.getPage().size());

    // Create one for the person
    PasswordCredentialsInfoImpl credentials = new PasswordCredentialsInfoImpl();
    credentials.setRemoteUsername(TEST_REMOTE_USERNAME_ONE);
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_ONE, credentials);

    // Check it shows up
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals("Unexpected systems " + systems.getPage(), 1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals("Unexpected systems " + systems.getPage(), 0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals("Unexpected systems " + systems.getPage(), 1, systems.getPage().size());

    // Switch to another user, check it doesn't
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);

    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());

    // Create both personal and shared ones as the current user
    credentials = new PasswordCredentialsInfoImpl();
    credentials.setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_TWO, credentials);

    credentials = new PasswordCredentialsInfoImpl();
    credentials.setRemoteUsername(TEST_REMOTE_USERNAME_THREE);
    REMOTE_CREDENTIALS_SERVICE.createPersonCredentials(TEST_REMOTE_SYSTEM_THREE, credentials);

    credentials = new PasswordCredentialsInfoImpl();
    credentials.setRemoteUsername(TEST_REMOTE_USERNAME_THREE);
    BaseCredentialsInfo cc =
        REMOTE_CREDENTIALS_SERVICE.createSharedCredentials(TEST_REMOTE_SYSTEM_THREE, credentials);

    // Check as the user who created these
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());

    // Check as the first user, they should see the shared one
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);

    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());

    // Change the shared permissions, see it goes away
    AuthenticationUtil.setFullyAuthenticatedUser(ADMIN_USER);
    PERMISSION_SERVICE.setInheritParentPermissions(cc.getRemoteSystemContainerNodeRef(), false);

    // Check as the owning user, will still see all of them
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());

    // Check as the other user, shared will have gone as we lost read permissions
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_ONE);

    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(0, systems.getPage().size());

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());

    // Finally, check the listings have the correct things in them
    AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER_TWO);
    systems = REMOTE_CREDENTIALS_SERVICE.listPersonRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());
    assertEquals(true, systems.getPage().contains(TEST_REMOTE_SYSTEM_TWO));
    assertEquals(true, systems.getPage().contains(TEST_REMOTE_SYSTEM_THREE));

    systems = REMOTE_CREDENTIALS_SERVICE.listSharedRemoteSystems(new PagingRequest(10));
    assertEquals(1, systems.getPage().size());
    assertEquals(true, systems.getPage().contains(TEST_REMOTE_SYSTEM_THREE));

    systems = REMOTE_CREDENTIALS_SERVICE.listAllRemoteSystems(new PagingRequest(10));
    assertEquals(2, systems.getPage().size());
    assertEquals(true, systems.getPage().contains(TEST_REMOTE_SYSTEM_TWO));
    assertEquals(true, systems.getPage().contains(TEST_REMOTE_SYSTEM_THREE));
  }