/**
   * Most of the shared credentials container tests work on the test one, so that things are in a
   * known and empty state. We have this one test that uses the real shared container, just to check
   * that it's correctly setup and available
   */
  @Test
  public void testRealSharedCredentialsContainer() throws Exception {
    // Create a new instance, using the real container
    RemoteCredentialsServiceImpl realService = new RemoteCredentialsServiceImpl();
    realService.setDictionaryService(DICTIONARY_SERVICE);
    realService.setNamespaceService(NAMESPACE_SERVICE);
    realService.setNodeService(PUBLIC_NODE_SERVICE);
    realService.setRepositoryHelper(REPOSITORY_HELPER);

    for (Entry<QName, RemoteCredentialsInfoFactory> e :
        ((RemoteCredentialsServiceImpl) PRIVATE_REMOTE_CREDENTIALS_SERVICE)
            .getCredentialsFactories()
            .entrySet()) {
      realService.registerCredentialsFactory(e.getKey(), e.getValue());
    }

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

    // Do a create / fetch / delete step
    PasswordCredentialsInfoImpl pwCredI = new PasswordCredentialsInfoImpl();
    pwCredI.setRemoteUsername(TEST_REMOTE_USERNAME_ONE);
    pwCredI.setRemotePassword(TEST_USER_THREE);
    BaseCredentialsInfo credentials = null;

    try {
      // Create
      credentials = realService.createSharedCredentials(TEST_REMOTE_SYSTEM_ONE, pwCredI);
      assertEquals(TEST_REMOTE_USERNAME_ONE, credentials.getRemoteUsername());

      // Update
      ((PasswordCredentialsInfoImpl) credentials).setRemoteUsername(TEST_REMOTE_USERNAME_TWO);
      ((PasswordCredentialsInfoImpl) credentials).setRemotePassword(TEST_USER_ONE);
      credentials = realService.updateCredentials(credentials);
      assertEquals(TEST_REMOTE_USERNAME_TWO, credentials.getRemoteUsername());

      // Delete
      realService.deleteCredentials(credentials);

      // Tidy, and zap the test parent
      PUBLIC_NODE_SERVICE.deleteNode(credentials.getRemoteSystemContainerNodeRef());
      credentials = null;
    } finally {
      // Tidy up if needed
      if (credentials != null) {
        AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();

        // Zap the credentials themselves
        PUBLIC_NODE_SERVICE.deleteNode(credentials.getNodeRef());

        // And their test parent
        PUBLIC_NODE_SERVICE.deleteNode(credentials.getRemoteSystemContainerNodeRef());
      }
    }
  }
  @BeforeClass
  public static void initTestsContext() throws Exception {
    ApplicationContext testContext = APP_CONTEXT_INIT.getApplicationContext();

    PRIVATE_REMOTE_CREDENTIALS_SERVICE =
        (RemoteCredentialsService) testContext.getBean("remoteCredentialsService");
    REMOTE_CREDENTIALS_SERVICE =
        (RemoteCredentialsService) testContext.getBean("RemoteCredentialsService");

    AUTHENTICATION_SERVICE =
        (MutableAuthenticationService) testContext.getBean("authenticationService");
    BEHAVIOUR_FILTER = (BehaviourFilter) testContext.getBean("policyBehaviourFilter");
    DICTIONARY_SERVICE = (DictionaryService) testContext.getBean("dictionaryService");
    NAMESPACE_SERVICE = (NamespaceService) testContext.getBean("namespaceService");
    REPOSITORY_HELPER = (Repository) testContext.getBean("repositoryHelper");
    NODE_SERVICE = (NodeService) testContext.getBean("nodeService");
    PUBLIC_NODE_SERVICE = (NodeService) testContext.getBean("NodeService");
    PERSON_SERVICE = (PersonService) testContext.getBean("personService");
    TRANSACTION_HELPER =
        (RetryingTransactionHelper) testContext.getBean("retryingTransactionHelper");
    TRANSACTION_SERVICE = (TransactionService) testContext.getBean("TransactionService");
    PERMISSION_SERVICE = (PermissionService) testContext.getBean("permissionService");

    // Switch to a test shared system container
    RemoteCredentialsServiceImpl.setSharedCredentialsSystemContainerName(
        SHARED_SYSTEM_CONTAINER_NAME);
  }