/** @return */
  private TransferManifestNormalNode createFolderNode(NodeRef parentFolder, String nodeName)
      throws Exception {
    TransferManifestNormalNode node = new TransferManifestNormalNode();
    String uuid = GUID.generate();
    NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, uuid);
    node.setNodeRef(nodeRef);
    node.setUuid(uuid);
    node.setType(ContentModel.TYPE_FOLDER);
    node.setAncestorType(ContentModel.TYPE_FOLDER);

    /** Get guest home */
    List<ChildAssociationRef> parents = new ArrayList<ChildAssociationRef>();
    ChildAssociationRef primaryAssoc =
        new ChildAssociationRef(
            ContentModel.ASSOC_CONTAINS,
            parentFolder,
            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, nodeName),
            node.getNodeRef(),
            true,
            -1);
    parents.add(primaryAssoc);
    node.setParentAssocs(parents);
    node.setParentPath(null);
    node.setPrimaryParentAssoc(primaryAssoc);

    Map<QName, Serializable> props = new HashMap<QName, Serializable>();
    props.put(ContentModel.PROP_NODE_UUID, uuid);
    props.put(ContentModel.PROP_NAME, nodeName);
    node.setProperties(props);

    return node;
  }
  public void testBasicRenameContentAndMove() throws Exception {
    startNewTransaction();
    TransferManifestNormalNode node = null;

    try {
      String transferId = ftTransferReceiver.start("1234", true, ftTransferReceiver.getVersion());

      try {
        // get back the node in the list
        node = (TransferManifestNormalNode) staticNodes.get(0);
        // modify the node name
        node.getProperties().put(ContentModel.PROP_NAME, "New_Name.txt");

        // create a folder
        TransferManifestNormalNode folderNode = this.createFolderNode(companytHome, "FOLDER_1");
        staticNodes.add(folderNode);

        // modify the parent
        modifyParentNode(folderNode.getNodeRef(), node);

        // set the root
        // NodeRef parentRef = node.getPrimaryParentAssoc().getParentRef();

        String snapshot = createSnapshot(staticNodes, false);
        ftTransferReceiver.setFileTransferRootNodeFileFileSystem(this.companytHome.toString());
        ftTransferReceiver.saveSnapshot(
            transferId, new ByteArrayInputStream(snapshot.getBytes("UTF-8")));
        ftTransferReceiver.commit(transferId);

      } catch (Exception ex) {
        ftTransferReceiver.end(transferId);
        throw ex;
      }
    } finally {
      endTransaction();
    }
    // check that the temporary folder where orphan are put in do not exist anymore
    File tempFolder =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "T_V_R_1234432123478");
    assertFalse(tempFolder.exists());
    // check that content exist
    // get the name of the node
    String nodeName = (String) node.getProperties().get(ContentModel.PROP_NAME);
    File transferedNode =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOLDER_1" + "/" + nodeName);
    assertTrue(transferedNode.exists());
    // check content itself
    byte byteArray[] = readBytesFromFile(transferedNode);
    // transform to string
    ByteArrayOutputStream bs = new ByteArrayOutputStream();

    bs.write(byteArray, 0, byteArray.length);
    String content = bs.toString();
    assertEquals(content, dummyContent);
  }
  public void testCreateFoldersAndContentAndMove() throws Exception {
    startNewTransaction();

    try {
      String transferId = ftTransferReceiver.start("1234", true, ftTransferReceiver.getVersion());

      try {

        // create a folder
        TransferManifestNormalNode folderNode = this.createFolderNode(companytHome, "FOLDER_2");
        // move all the nodes to that newly created folder
        for (TransferManifestNode curNode : staticNodes) {
          modifyParentNode(folderNode.getNodeRef(), (TransferManifestNormalNode) curNode);
        }
        staticNodes.add(folderNode);

        String snapshot = createSnapshot(staticNodes, false);
        ftTransferReceiver.setFileTransferRootNodeFileFileSystem(this.companytHome.toString());
        ftTransferReceiver.saveSnapshot(
            transferId, new ByteArrayInputStream(snapshot.getBytes("UTF-8")));
        ftTransferReceiver.commit(transferId);

      } catch (Exception ex) {
        ftTransferReceiver.end(transferId);
        throw ex;
      }
    } finally {
      endTransaction();
    }
    // check that the temporary folder where orphan are put in do not exist anymore
    File tempFolder =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "T_V_R_1234432123478");
    assertFalse(tempFolder.exists());
    // check that content exist
    // get the name of the node
    File transferedNode = new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOLDER_2");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOLDER_2" + "/" + "FOLDER_1");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOLDER_2" + "/" + "New_Name.txt");
    assertTrue(transferedNode.exists());
  }
  /** @return */
  private void modifyParentNode(NodeRef parentFolder, TransferManifestNormalNode nodeToMove)
      throws Exception {

    String nodeName = (String) nodeToMove.getProperties().get(ContentModel.PROP_NAME);

    List<ChildAssociationRef> parents = new ArrayList<ChildAssociationRef>();
    ChildAssociationRef primaryAssoc =
        new ChildAssociationRef(
            ContentModel.ASSOC_CONTAINS,
            parentFolder,
            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, nodeName),
            nodeToMove.getNodeRef(),
            true,
            -1);
    parents.add(primaryAssoc);
    nodeToMove.setParentAssocs(parents);
    nodeToMove.setParentPath(null);
    nodeToMove.setPrimaryParentAssoc(primaryAssoc);
  }
  /** @throws Exception */
  public void testSyncMode() throws Exception {
    startNewTransaction();

    try {
      String transferId = ftTransferReceiver.start("1234", true, ftTransferReceiver.getVersion());

      try {

        // create a folders
        TransferManifestNormalNode folderNode0 = this.createFolderNode(companytHome, "FOL_0");
        TransferManifestNormalNode folderNode1 =
            this.createFolderNode(folderNode0.getNodeRef(), "FOL_1");
        TransferManifestNormalNode folderNode2 =
            this.createFolderNode(folderNode0.getNodeRef(), "FOL_2");
        TransferManifestNormalNode folderNode11 =
            this.createFolderNode(folderNode1.getNodeRef(), "FOL_11");
        TransferManifestNormalNode folderNode12 =
            this.createFolderNode(folderNode1.getNodeRef(), "FOL_12");
        TransferManifestNormalNode folderNode21 =
            this.createFolderNode(folderNode2.getNodeRef(), "FOL_21");
        TransferManifestNormalNode folderNode22 =
            this.createFolderNode(folderNode2.getNodeRef(), "FOL_22");

        staticNodes = new ArrayList<TransferManifestNode>();
        staticNodes.add(folderNode0);
        staticNodes.add(folderNode1);
        staticNodes.add(folderNode2);
        staticNodes.add(folderNode11);
        staticNodes.add(folderNode12);
        staticNodes.add(folderNode21);
        staticNodes.add(folderNode22);

        // sync mode true
        String snapshot = createSnapshot(staticNodes, true);
        ftTransferReceiver.setFileTransferRootNodeFileFileSystem(this.companytHome.toString());
        ftTransferReceiver.saveSnapshot(
            transferId, new ByteArrayInputStream(snapshot.getBytes("UTF-8")));
        ftTransferReceiver.commit(transferId);

      } catch (Exception ex) {
        ftTransferReceiver.end(transferId);
        throw ex;
      }
    } finally {
      endTransaction();
    }
    // check that the temporary folder where orphan are put in do not exist anymore
    File tempFolder =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "T_V_R_1234432123478");
    assertFalse(tempFolder.exists());
    // check that content exist
    // get the name of the node
    File transferedNode = new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOL_0");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOL_0" + "/" + "FOL_1");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOL_0" + "/" + "FOL_2");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_1"
                + "/"
                + "FOL_11");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_1"
                + "/"
                + "FOL_12");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_2"
                + "/"
                + "FOL_21");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_2"
                + "/"
                + "FOL_22");
    assertTrue(transferedNode.exists());

    // remove folderNode1 and sync
    try {
      String transferId = ftTransferReceiver.start("1234", true, ftTransferReceiver.getVersion());

      try {

        // remove FOL_1
        staticNodes.remove(1);

        // sync mode true
        String snapshot = createSnapshot(staticNodes, true);
        ftTransferReceiver.setFileTransferRootNodeFileFileSystem(this.companytHome.toString());
        ftTransferReceiver.saveSnapshot(
            transferId, new ByteArrayInputStream(snapshot.getBytes("UTF-8")));
        ftTransferReceiver.commit(transferId);

      } catch (Exception ex) {
        ftTransferReceiver.end(transferId);
        throw ex;
      }
    } finally {
      endTransaction();
    }

    transferedNode = new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOL_0");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOL_0" + "/" + "FOL_1");
    assertFalse(transferedNode.exists());

    transferedNode =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "FOL_0" + "/" + "FOL_2");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_1"
                + "/"
                + "FOL_11");
    assertFalse(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_1"
                + "/"
                + "FOL_12");
    assertFalse(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_2"
                + "/"
                + "FOL_21");
    assertTrue(transferedNode.exists());

    transferedNode =
        new File(
            ftTransferReceiver.getDefaultReceivingroot()
                + "/"
                + "FOL_0"
                + "/"
                + "FOL_2"
                + "/"
                + "FOL_22");
    assertTrue(transferedNode.exists());
  }
  /** @throws Exception */
  public void testCreateStuctureWithSynonymsRenameAndReverseIt() throws Exception {
    // Create A/A/A/A/A ... 10 time
    // reverse
    startNewTransaction();
    ArrayList<TransferManifestNode> copy = new ArrayList<TransferManifestNode>();

    try {
      String transferId = ftTransferReceiver.start("1234", true, ftTransferReceiver.getVersion());

      try {
        NodeRef parent = companytHome;
        staticNodes = new ArrayList<TransferManifestNode>();
        staticNodes2 = new ArrayList<TransferManifestNode>();
        for (int i = 1; i <= 20; i++) {
          TransferManifestNormalNode folderNode = this.createFolderNode(parent, "A");
          staticNodes.add(folderNode);
          String fName = "F_" + i;
          TransferManifestNormalNode folderNode2 =
              this.createFolderNode(folderNode.getNodeRef(), fName);
          staticNodes2.add(folderNode2);
          parent = folderNode.getNodeRef();
        }

        copy = copyList(staticNodes);
        Collections.copy(copy, staticNodes);
        copy.addAll(staticNodes2);
        // transferring node in random order
        Collections.shuffle(copy);

        String snapshot = createSnapshot(copy, false);
        ftTransferReceiver.setFileTransferRootNodeFileFileSystem(this.companytHome.toString());
        ftTransferReceiver.saveSnapshot(
            transferId, new ByteArrayInputStream(snapshot.getBytes("UTF-8")));
        ftTransferReceiver.commit(transferId);

      } catch (Exception ex) {
        ftTransferReceiver.end(transferId);
        throw ex;
      }
    } finally {
      endTransaction();
    }
    // check that the temporary folder where orphan are put in do not exist anymore
    File tempFolder =
        new File(ftTransferReceiver.getDefaultReceivingroot() + "/" + "T_V_R_1234432123478");
    assertFalse(tempFolder.exists());

    String curPath = ftTransferReceiver.getDefaultReceivingroot();
    for (int i = 1; i <= 20; i++) {
      String curPath1 = curPath + "/" + "A";
      File transferedNode = new File(curPath1);
      assertTrue(transferedNode.exists());
      String curPath2 = curPath1 + "/" + "F_" + i;
      transferedNode = new File(curPath2);
      assertTrue(transferedNode.exists());
      curPath += "/" + "A";
    }

    // reverse the structure
    Collections.reverse(staticNodes);

    // modify the parents
    NodeRef parent = companytHome;
    for (int i = 0; i < 20; i++) {
      TransferManifestNormalNode curNode = (TransferManifestNormalNode) staticNodes.get(i);
      modifyParentNode(parent, curNode);
      parent = curNode.getNodeRef();
    }

    staticNodes.addAll(staticNodes2);

    try {
      String transferId = ftTransferReceiver.start("1234", true, ftTransferReceiver.getVersion());

      try {

        // transferring node in random order
        Collections.shuffle(copy);

        String snapshot = createSnapshot(copy, false);
        ftTransferReceiver.setFileTransferRootNodeFileFileSystem(this.companytHome.toString());
        ftTransferReceiver.saveSnapshot(
            transferId, new ByteArrayInputStream(snapshot.getBytes("UTF-8")));
        ftTransferReceiver.commit(transferId);

      } catch (Exception ex) {
        ftTransferReceiver.end(transferId);
        throw ex;
      }
    } finally {
      endTransaction();
    }

    // check result on file system
    curPath = ftTransferReceiver.getDefaultReceivingroot();
    for (int i = 20; i > 0; i--) {
      String curPath1 = curPath + "/" + "A";
      File transferedNode = new File(curPath1);
      assertTrue(transferedNode.exists());
      String curPath2 = curPath1 + "/" + "F_" + i;
      transferedNode = new File(curPath2);
      assertTrue(transferedNode.exists());
      curPath += "/" + "A";
    }
  }