@Test
  public void testGetUserInvalid() throws Exception {
    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    StringBuilder sb = new StringBuilder();
    sb.append("http://localhost:");
    sb.append(
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY));
    sb.append("/user/iamaninvaliduser");

    DefaultHttpClientAndContext clientAndContext =
        RestAuthUtils.httpClientSetup(irodsAccount, testingProperties);
    try {

      HttpGet httpget = new HttpGet(sb.toString());
      httpget.addHeader("accept", "application/xml");

      HttpResponse response =
          clientAndContext.getHttpClient().execute(httpget, clientAndContext.getHttpContext());
      HttpEntity entity = response.getEntity();
      Assert.assertEquals(500, response.getStatusLine().getStatusCode());
      EntityUtils.consume(entity);

    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      clientAndContext.getHttpClient().getConnectionManager().shutdown();
    }
  }
  @Test(expected = CollectionNotMountedException.class)
  public void testCreateAMountedFileSystemPhyPathMissing() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      throw new CollectionNotMountedException("thrown to honor contracts");
    }

    String targetCollectionName = "testCreateAMountedFileSystemPhyPathMissing";

    String localCollectionAbsolutePath = "/i/dont/exist";

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    // do an initial unmount
    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.unmountACollection(
        targetIrodsCollection, irodsAccount.getDefaultStorageResource());

    mountedCollectionAO.createMountedFileSystemCollection(
        localCollectionAbsolutePath,
        targetIrodsCollection,
        irodsAccount.getDefaultStorageResource());
  }
  @Test
  public void testAddUserByAdminInvalidMessage() throws Exception {

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    StringBuilder sb = new StringBuilder();
    sb.append("http://localhost:");
    sb.append(
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY));
    sb.append("/user/");

    DefaultHttpClientAndContext clientAndContext =
        RestAuthUtils.httpClientSetup(irodsAccount, testingProperties);
    try {

      HttpPut httpPut = new HttpPut(sb.toString());
      httpPut.addHeader("accept", "application/json");
      httpPut.addHeader("Content-Type", "application/json");
      String body = "I am not valid json";
      httpPut.setEntity(new StringEntity(body));

      HttpResponse response =
          clientAndContext.getHttpClient().execute(httpPut, clientAndContext.getHttpContext());
      Assert.assertEquals(400, response.getStatusLine().getStatusCode());

    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      clientAndContext.getHttpClient().getConnectionManager().shutdown();
    }
  }
  /**
   * create a soft link where the source file is a data object
   *
   * @throws Exception
   */
  @Test(expected = JargonException.class)
  public final void testCreateASoftLinkSourceIsIRODSFile() throws Exception {

    String sourceCollectionName = "testCreateASoftLinkSourceIsIRODSFile.txt";
    String targetCollectionName = "testCreateASoftLinkSourceNotExistsTarget";

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String sourceIrodsFile =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + sourceCollectionName);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    IRODSFile sourceAsFile =
        irodsFileSystem.getIRODSFileFactory(irodsAccount).instanceIRODSFile(sourceIrodsFile);
    sourceAsFile.createNewFile();

    // create the soft link

    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);
    mountedCollectionAO.createASoftLink(sourceIrodsFile, targetIrodsCollection);
  }
  @Test(expected = IllegalArgumentException.class)
  public void testCreateAndRemoveMountedFileSystemNullResource() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      throw new IllegalArgumentException("thrown to honor contracts");
    }

    String targetCollectionName = "testCreateAndRemoveMountedFileSystem";
    String localMountDir = "testCreateAndRemoveMountedFileSystemLocal";

    String localCollectionAbsolutePath =
        scratchFileUtils.createAndReturnAbsoluteScratchPath(
            IRODS_TEST_SUBDIR_PATH + '/' + localMountDir);

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.createMountedFileSystemCollection(
        localCollectionAbsolutePath, targetIrodsCollection, null);
  }
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
   TestingPropertiesHelper testingPropertiesLoader = new TestingPropertiesHelper();
   testingProperties = testingPropertiesLoader.getTestProperties();
   scratchFileUtils = new ScratchFileUtils(testingProperties);
   scratchFileUtils.clearAndReinitializeScratchDirectory(IRODS_TEST_SUBDIR_PATH);
   irodsTestSetupUtilities = new IRODSTestSetupUtilities();
   irodsTestSetupUtilities.initializeIrodsScratchDirectory();
   irodsTestSetupUtilities.initializeDirectoryForTest(IRODS_TEST_SUBDIR_PATH);
   irodsFileSystem = IRODSFileSystem.instance();
 }
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
   irodsFileSystem = IRODSFileSystem.instance();
   org.irods.jargon.testutils.TestingPropertiesHelper testingPropertiesLoader =
       new TestingPropertiesHelper();
   testingProperties = testingPropertiesLoader.getTestProperties();
   irodsTestSetupUtilities = new org.irods.jargon.testutils.IRODSTestSetupUtilities();
   irodsTestSetupUtilities.initializeIrodsScratchDirectory();
   irodsTestSetupUtilities.initializeDirectoryForTest(IRODS_TEST_SUBDIR_PATH);
   SettableJargonProperties settableJargonProperties =
       new SettableJargonProperties(irodsFileSystem.getJargonProperties());
   jargonOriginalProperties = settableJargonProperties;
 }
  @Test
  public void testAddUserByAdminBlankPassword() throws Exception {

    String testUserName = "******";
    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    StringBuilder sb = new StringBuilder();
    sb.append("http://localhost:");
    sb.append(
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY));
    sb.append("/user/");

    DefaultHttpClientAndContext clientAndContext =
        RestAuthUtils.httpClientSetup(irodsAccount, testingProperties);
    try {

      HttpPut httpPut = new HttpPut(sb.toString());
      httpPut.addHeader("accept", "application/json");
      httpPut.addHeader("Content-Type", "application/json");

      ObjectMapper mapper = new ObjectMapper();
      UserAddByAdminRequest addRequest = new UserAddByAdminRequest();
      addRequest.setDistinguishedName("dn here");
      addRequest.setTempPassword("");
      addRequest.setUserName(testUserName);
      String body = mapper.writeValueAsString(addRequest);
      httpPut.setEntity(new StringEntity(body));

      HttpResponse response =
          clientAndContext.getHttpClient().execute(httpPut, clientAndContext.getHttpContext());
      HttpEntity entity = response.getEntity();
      Assert.assertEquals(200, response.getStatusLine().getStatusCode());
      String entityData = EntityUtils.toString(entity);
      UserAddActionResponse actual = mapper.readValue(entityData, UserAddActionResponse.class);
      Assert.assertEquals(
          UserAddActionResponse.UserAddActionResponseCode.ATTRIBUTES_MISSING,
          actual.getUserAddActionResponse());
      Assert.assertEquals(
          UserAddActionResponse.UserAddActionResponseCode.ATTRIBUTES_MISSING.ordinal(),
          actual.getUserAddActionResponseNumericCode());

    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      clientAndContext.getHttpClient().getConnectionManager().shutdown();
    }
  }
  @Test(expected = IllegalArgumentException.class)
  public void testCreateAndRemoveMountedFileSystemNullTarget() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      throw new IllegalArgumentException("thrown to honor contracts");
    }

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.createMountedFileSystemCollection("source", null, "resc");
  }
  @Before
  public void setUp() throws Exception {

    if (server != null) {
      return;
    }

    int port =
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY);
    server = new TJWSEmbeddedJaxrsServer();
    server.setPort(port);
    ResteasyDeployment deployment = server.getDeployment();

    server.start();
    Dispatcher dispatcher = deployment.getDispatcher();
    SpringBeanProcessor processor =
        new SpringBeanProcessor(
            dispatcher, deployment.getRegistry(), deployment.getProviderFactory());
    ((ConfigurableApplicationContext) applicationContext).addBeanFactoryPostProcessor(processor);

    SpringResourceFactory noDefaults =
        new SpringResourceFactory("userService", applicationContext, UserService.class);
    dispatcher.getRegistry().addResourceFactory(noDefaults);
  }
  @Test
  public void testCreateAMountedFileSystemPhyPathMissingButUnderConfiguredPath() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      return;
    }

    String targetCollectionName =
        "testCreateAMountedFileSystemPhyPathMissingButUnderConfiguredPath";
    String localMountDir = "testCreateAMountedFileSystemPhyPathMissingButUnderConfiguredPathLocal";

    String localCollectionAbsolutePath =
        testingProperties.getProperty(TestingPropertiesHelper.IRODS_REG_BASEDIR)
            + "/"
            + localMountDir;

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    // do an initial unmount
    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.unmountACollection(
        targetIrodsCollection, irodsAccount.getDefaultStorageResource());

    mountedCollectionAO.createMountedFileSystemCollection(
        localCollectionAbsolutePath,
        targetIrodsCollection,
        irodsAccount.getDefaultStorageResource());

    // now get an objstat it should exist

    CollectionAndDataObjectListAndSearchAO searchAO =
        irodsFileSystem
            .getIRODSAccessObjectFactory()
            .getCollectionAndDataObjectListAndSearchAO(irodsAccount);
    ObjStat objStat = searchAO.retrieveObjectStatForPath(targetIrodsCollection);

    Assert.assertEquals(
        "did not get spec col in objStat", SpecColType.MOUNTED_COLL, objStat.getSpecColType());
  }
  /**
   * Unmount a soft link that does not exist
   *
   * @throws Exception
   */
  @Test
  public final void testUnmountSoftLinkNotExists() throws Exception {
    String targetCollectionName = "testUnmountSoftLinkNotExists";

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    // do an initial unmount
    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);
    boolean success = mountedCollectionAO.unmountACollection(targetIrodsCollection, "");
    Assert.assertFalse("should get no success", success);
  }
  @Test(expected = CollectionNotMountedException.class)
  public void testCreateMountedFileSystemTwice() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      throw new CollectionNotMountedException("thrown to honor contracts");
    }

    String targetCollectionName = "testCreateMountedFileSystemTwice";
    String localMountDir = "testCreateMountedFileSystemTwiceLocal";

    String localCollectionAbsolutePath =
        scratchFileUtils.createAndReturnAbsoluteScratchPath(
            IRODS_TEST_SUBDIR_PATH + '/' + localMountDir);

    FileGenerator.generateManyFilesInParentCollectionByAbsolutePath(
        localCollectionAbsolutePath, "testCreateMountedFileSystemTwice", ".txt", 10, 1, 2);

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    // do an initial unmount
    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.unmountACollection(
        targetIrodsCollection, irodsAccount.getDefaultStorageResource());

    mountedCollectionAO.createMountedFileSystemCollection(
        localCollectionAbsolutePath,
        targetIrodsCollection,
        irodsAccount.getDefaultStorageResource());

    mountedCollectionAO.createMountedFileSystemCollection(
        localCollectionAbsolutePath,
        targetIrodsCollection,
        irodsAccount.getDefaultStorageResource());

    // no errors means success. will test further in
    // listing methods

  }
  @Test
  public void testGetUserJSON() throws Exception {

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);
    StringBuilder sb = new StringBuilder();
    sb.append("http://localhost:");
    sb.append(
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY));
    sb.append("/user/");
    sb.append(testingProperties.get(TestingPropertiesHelper.IRODS_USER_KEY));
    // contentType doesn't really work in test container, set in the header
    sb.append("?contentType=application/json");

    DefaultHttpClientAndContext clientAndContext =
        RestAuthUtils.httpClientSetup(irodsAccount, testingProperties);
    try {

      HttpGet httpget = new HttpGet(sb.toString());
      httpget.addHeader("accept", "application/json");

      HttpResponse response =
          clientAndContext.getHttpClient().execute(httpget, clientAndContext.getHttpContext());
      HttpEntity entity = response.getEntity();
      Assert.assertEquals(200, response.getStatusLine().getStatusCode());
      Assert.assertNotNull(entity);
      String entityData = EntityUtils.toString(entity);
      Assert.assertFalse(
          "did not get json with user name",
          entityData.indexOf(
                  "\"name\":\""
                      + testingProperties.get(TestingPropertiesHelper.IRODS_USER_KEY)
                      + "\"")
              == -1);

      EntityUtils.consume(entity);

    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      clientAndContext.getHttpClient().getConnectionManager().shutdown();
    }
  }
  /**
   * create a soft link where the target file is blank
   *
   * @throws Exception
   */
  @Test(expected = IllegalArgumentException.class)
  public final void testCreateASoftLinkTargetIsBlank() throws Exception {

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);
    mountedCollectionAO.createASoftLink("hello", "");
  }
  @Test(expected = IllegalArgumentException.class)
  public void testCreateAndRemoveMountedFileSystemBlankSource() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      throw new IllegalArgumentException("thrown to honor contracts");
    }

    String targetCollectionName = "testCreateAndRemoveMountedFileSystem";

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.createMountedFileSystemCollection("", targetIrodsCollection, "");
  }
  @Test
  public void testCreateAndRemoveMountedFileSystem() throws Exception {

    if (!testingPropertiesHelper.isTestFileSystemMount(testingProperties)) {
      return;
    }

    String targetCollectionName = "testCreateAndRemoveMountedFileSystem";
    String localMountDir = "testCreateAndRemoveMountedFileSystemLocal";

    String localCollectionAbsolutePath =
        testingProperties.getProperty(TestingPropertiesHelper.IRODS_REG_BASEDIR)
            + "/"
            + localMountDir;

    File localFile = new File(localCollectionAbsolutePath);
    localFile.mkdirs();

    FileGenerator.generateManyFilesInParentCollectionByAbsolutePath(
        localCollectionAbsolutePath, "testCreateAndRemoveMountedFileSystem", ".txt", 10, 1, 2);

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    // do an initial unmount
    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.unmountACollection(
        targetIrodsCollection, irodsAccount.getDefaultStorageResource());

    mountedCollectionAO.createMountedFileSystemCollection(
        localCollectionAbsolutePath,
        targetIrodsCollection,
        irodsAccount.getDefaultStorageResource());
  }
  @Test
  public void testHandleNoObjStatUnderRootOrHomeByLookingForPublicAndHome() throws Exception {
    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    SettableJargonProperties props =
        new SettableJargonProperties(irodsFileSystem.getJargonProperties());
    props.setDefaultToPublicIfNothingUnderRootWhenListing(true);
    irodsFileSystem.getIrodsSession().setJargonProperties(props);

    CollectionAndDataObjectListAndSearchAO collectionAndDataObjectListAndSearchAO =
        irodsFileSystem
            .getIRODSAccessObjectFactory()
            .getCollectionAndDataObjectListAndSearchAO(irodsAccount);
    CollectionListingUtils listingUtils =
        new CollectionListingUtils(collectionAndDataObjectListAndSearchAO);

    String path = "/";
    ObjStat objStat = listingUtils.handleNoObjStatUnderRootOrHomeByLookingForPublicAndHome(path);
    Assert.assertNotNull(objStat);
    Assert.assertTrue(objStat.isStandInGeneratedObjStat());
    Assert.assertEquals(path, objStat.getAbsolutePath());
  }
  /**
   * Create a soft link to an iRODS collection in nominal mode, target does not exist and will be
   * created
   *
   * @throws Exception
   */
  @Test
  public final void testCreateASoftLink() throws Exception {

    String sourceCollectionName = "testCreateASoftLinkSource";
    String targetCollectionName = "testCreateASoftLinkTarget";
    String subfileName = "testCreateASoftLink.txt";

    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    String sourceIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + sourceCollectionName);

    String targetIrodsCollection =
        testingPropertiesHelper.buildIRODSCollectionAbsolutePathFromTestProperties(
            testingProperties, IRODS_TEST_SUBDIR_PATH + '/' + targetCollectionName);

    // do an initial unmount
    MountedCollectionAO mountedCollectionAO =
        irodsFileSystem.getIRODSAccessObjectFactory().getMountedCollectionAO(irodsAccount);

    mountedCollectionAO.unmountACollection(
        targetIrodsCollection, irodsAccount.getDefaultStorageResource());

    // set up source collection
    IRODSFile sourceFile =
        irodsFileSystem.getIRODSFileFactory(irodsAccount).instanceIRODSFile(sourceIrodsCollection);
    sourceFile.mkdirs();
    IRODSFile subFile =
        irodsFileSystem
            .getIRODSFileFactory(irodsAccount)
            .instanceIRODSFile(sourceIrodsCollection, subfileName);
    subFile.createNewFile();

    // add a subfile to this collection

    // create the soft link

    mountedCollectionAO.createASoftLink(sourceIrodsCollection, targetIrodsCollection);

    IRODSFile mountedCollectionTargetFile =
        irodsFileSystem.getIRODSFileFactory(irodsAccount).instanceIRODSFile(targetIrodsCollection);
    Assert.assertTrue("target collection does not exist", mountedCollectionTargetFile.exists());
    String softLinkedSourceFileName =
        mountedCollectionTargetFile.getAbsolutePath() + "/" + subFile.getName();

    CollectionAndDataObjectListAndSearchAO listAndSearchAO =
        irodsFileSystem
            .getIRODSAccessObjectFactory()
            .getCollectionAndDataObjectListAndSearchAO(irodsAccount);

    ObjStat statForSoftLinkedFile =
        listAndSearchAO.retrieveObjectStatForPath(softLinkedSourceFileName);

    Assert.assertEquals(
        "did not set the objPath",
        targetIrodsCollection,
        statForSoftLinkedFile.getCollectionPath());
    Assert.assertEquals(
        "did not identify as a linked coll",
        ObjStat.SpecColType.LINKED_COLL,
        statForSoftLinkedFile.getSpecColType());

    Assert.assertTrue(
        "did not get the soft linked file",
        statForSoftLinkedFile.getObjectType() == ObjectType.DATA_OBJECT);
  }
  @Test
  public void testAddUserByAdmin() throws Exception {

    String testUser = "******";
    String testPassword = "******";
    String testDn = "testDNForaddubyAdminRest";
    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    IRODSAccessObjectFactory accessObjectFactory = irodsFileSystem.getIRODSAccessObjectFactory();

    UserAO userAO = accessObjectFactory.getUserAO(irodsAccount);
    userAO.deleteUser(testUser);

    StringBuilder sb = new StringBuilder();
    sb.append("http://localhost:");
    sb.append(
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY));
    sb.append("/user/");

    DefaultHttpClientAndContext clientAndContext =
        RestAuthUtils.httpClientSetup(irodsAccount, testingProperties);
    try {

      HttpPut httpPut = new HttpPut(sb.toString());
      httpPut.addHeader("accept", "application/json");
      httpPut.addHeader("Content-Type", "application/json");

      ObjectMapper mapper = new ObjectMapper();
      UserAddByAdminRequest addRequest = new UserAddByAdminRequest();
      addRequest.setDistinguishedName(testDn);
      addRequest.setTempPassword(testPassword);
      addRequest.setUserName(testUser);
      String body = mapper.writeValueAsString(addRequest);

      System.out.println(body);

      httpPut.setEntity(new StringEntity(body));

      HttpResponse response =
          clientAndContext.getHttpClient().execute(httpPut, clientAndContext.getHttpContext());
      HttpEntity entity = response.getEntity();
      Assert.assertEquals(200, response.getStatusLine().getStatusCode());
      String entityData = EntityUtils.toString(entity);

      System.out.println(entityData);

      UserAddActionResponse actual = mapper.readValue(entityData, UserAddActionResponse.class);
      Assert.assertEquals(testUser, actual.getUserName());
      Assert.assertEquals(
          UserAddActionResponse.UserAddActionResponseCode.SUCCESS,
          actual.getUserAddActionResponse());
      Assert.assertEquals(
          UserAddActionResponse.UserAddActionResponseCode.SUCCESS.ordinal(),
          actual.getUserAddActionResponseNumericCode());

    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      clientAndContext.getHttpClient().getConnectionManager().shutdown();
    }

    User user = userAO.findByName(testUser);
    Assert.assertNotNull("user not added", user);
    Assert.assertEquals("dn not set", testDn, user.getUserDN());
  }
  @Test
  public void testAddUserByAdminLongDn() throws Exception {

    String testUser = "******";
    String testPassword = "******";
    String testDN =
        "/CN=xxxxxyyxx-64f0-4d49-a0a9-508a8a5328cd/emailAddress=xxxxxxxxxxthismaynotshowup";
    IRODSAccount irodsAccount =
        testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

    IRODSAccount userAccount =
        testingPropertiesHelper.buildIRODSAccountForIRODSUserFromTestPropertiesForGivenUser(
            testingProperties, testUser, testPassword);

    IRODSAccessObjectFactory accessObjectFactory = irodsFileSystem.getIRODSAccessObjectFactory();

    UserAO userAO = accessObjectFactory.getUserAO(irodsAccount);

    try {
      userAO.findByName(testUser);
      String homeDir = MiscIRODSUtils.computeHomeDirectoryForIRODSAccount(userAccount);
      IRODSFile userHomeDir =
          irodsFileSystem.getIRODSFileFactory(userAccount).instanceIRODSFile(homeDir);

      for (File homeDirFile : userHomeDir.listFiles()) {
        homeDirFile.delete();
      }
      userAO.deleteUser(testUser);

    } catch (DataNotFoundException dnf) {
      // OK
    }

    StringBuilder sb = new StringBuilder();
    sb.append("http://localhost:");
    sb.append(
        testingPropertiesHelper.getPropertyValueAsInt(
            testingProperties, RestTestingProperties.REST_PORT_PROPERTY));
    sb.append("/user/");

    DefaultHttpClientAndContext clientAndContext =
        RestAuthUtils.httpClientSetup(irodsAccount, testingProperties);
    try {

      HttpPut httpPut = new HttpPut(sb.toString());
      httpPut.addHeader("accept", "application/json");
      httpPut.addHeader("Content-Type", "application/json");

      ObjectMapper mapper = new ObjectMapper();
      UserAddByAdminRequest addRequest = new UserAddByAdminRequest();
      addRequest.setDistinguishedName(testDN);
      addRequest.setTempPassword(testPassword);
      addRequest.setUserName(testUser);
      String body = mapper.writeValueAsString(addRequest);

      System.out.println(body);

      httpPut.setEntity(new StringEntity(body));

      HttpResponse response =
          clientAndContext.getHttpClient().execute(httpPut, clientAndContext.getHttpContext());
      HttpEntity entity = response.getEntity();
      Assert.assertEquals(200, response.getStatusLine().getStatusCode());
      String entityData = EntityUtils.toString(entity);

      System.out.println(entityData);

      UserAddActionResponse actual = mapper.readValue(entityData, UserAddActionResponse.class);
      Assert.assertEquals(testUser, actual.getUserName());
      Assert.assertEquals(
          UserAddActionResponse.UserAddActionResponseCode.SUCCESS,
          actual.getUserAddActionResponse());
      Assert.assertEquals(
          UserAddActionResponse.UserAddActionResponseCode.SUCCESS.ordinal(),
          actual.getUserAddActionResponseNumericCode());

    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      clientAndContext.getHttpClient().getConnectionManager().shutdown();
    }

    User user = userAO.findByName(testUser);
    Assert.assertNotNull("user not added", user);
    Assert.assertEquals("dn not set", testDN, user.getUserDN());
  }
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
   TestingPropertiesHelper testingPropertiesLoader = new TestingPropertiesHelper();
   testingProperties = testingPropertiesLoader.getTestProperties();
   irodsFileSystem = IRODSFileSystem.instance();
 }