Search in sources :

Example 1 with NodeTarget

use of org.alfresco.rest.api.model.NodeTarget in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testMoveCopyBetweenSites.

/**
 * Tests move and copy folder between sites.
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/move}
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<nodeId>/copy}
 */
@Test
public void testMoveCopyBetweenSites() throws Exception {
    setRequestContext(user1);
    /*
         * Precondition - create two sites, invite users, create folders
         */
    // user1 creates a public site and adds user2 as a site collaborator
    String site1Title = "site-testMoveCopyBetweenSites1-" + RUNID;
    final String site1Id = createSite(site1Title, SiteVisibility.PUBLIC).getId();
    addSiteMember(site1Id, user2, SiteRole.SiteCollaborator);
    // Get user1Site's docLib node id
    final String user1SitetDocLibNodeId = getSiteContainerNodeId(site1Id, "documentLibrary");
    // user1 creates a folder in the docLib of his site (user1Site)
    String user1Folder = "folder" + RUNID + "_user1";
    String user1FolderNodeId = createFolder(user1SitetDocLibNodeId, user1Folder, null).getId();
    setRequestContext(user2);
    // user2 creates a public site and adds user1 as a site collaborator
    String site2Title = "site-testMoveCopyBetweenSites2--" + RUNID;
    final String site2Id = createSite(site2Title, SiteVisibility.PUBLIC).getId();
    addSiteMember(site2Id, user1, SiteRole.SiteCollaborator);
    // Get user2Site's docLib node id
    final String user2SitetDocLibNodeId = getSiteContainerNodeId(site2Id, "documentLibrary");
    // user2 creates 2 folders within the docLib of the user1Site
    String user2Folder1 = "folder1" + RUNID + "_user2";
    String user2FolderNodeId = createFolder(user1SitetDocLibNodeId, user2Folder1, null).getId();
    String user2Folder2 = "folder2" + RUNID + "_user2";
    String user2Folder2NodeId = createFolder(user1SitetDocLibNodeId, user2Folder2, null).getId();
    /*
         * Test move between sites
         */
    // user1 moves the folder created by user2 to the user2Site's docLib
    setRequestContext(user1);
    NodeTarget target = new NodeTarget();
    target.setTargetParentId(user2SitetDocLibNodeId);
    HttpResponse response = post("nodes/" + user2FolderNodeId + "/move", toJsonAsStringNonNull(target), null, 200);
    Folder moveFolderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(user2SitetDocLibNodeId, moveFolderResp.getParentId());
    // user1 tries to undo the move (moves back the folder to its original place)
    // as user1 is just a SiteCollaborator in the user2Site, he can't move the folder which he doesn't own - ACL access permission.
    target = new NodeTarget();
    target.setTargetParentId(user1SitetDocLibNodeId);
    post("nodes/" + user2FolderNodeId + "/move", toJsonAsStringNonNull(target), null, 403);
    // user1 moves the folder created by himself to the docLib of the user2Site
    target = new NodeTarget();
    target.setTargetParentId(user2SitetDocLibNodeId);
    response = post("nodes/" + user1FolderNodeId + "/move", toJsonAsStringNonNull(target), null, 200);
    moveFolderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(user2SitetDocLibNodeId, moveFolderResp.getParentId());
    // user1 tries to undo the move (moves back the folder to its original place)
    // The undo should be successful as user1 owns the folder
    target = new NodeTarget();
    target.setTargetParentId(user1SitetDocLibNodeId);
    response = post("nodes/" + user1FolderNodeId + "/move", toJsonAsStringNonNull(target), null, 200);
    moveFolderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(user1SitetDocLibNodeId, moveFolderResp.getParentId());
    /*
         * Test copy between sites
         */
    // user1 copies the folder created by user2 to the user2Site's docLib
    target = new NodeTarget();
    target.setTargetParentId(user2SitetDocLibNodeId);
    response = post("nodes/" + user2Folder2NodeId + "/copy", toJsonAsStringNonNull(target), null, 201);
    Folder copyFolderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    assertEquals(user2SitetDocLibNodeId, copyFolderResp.getParentId());
    // user1 tries to undo the copy (hard deletes the created copy)
    deleteNode(copyFolderResp.getId(), true, 204);
    // Check it's deleted
    getSingle("nodes", copyFolderResp.getId(), 404);
    // cleanup
    setRequestContext(user1);
    deleteSite(site1Id, true, 204);
    setRequestContext(user2);
    deleteSite(site2Id, true, 204);
}
Also used : NodeTarget(org.alfresco.rest.api.model.NodeTarget) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 2 with NodeTarget

use of org.alfresco.rest.api.model.NodeTarget in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testMove.

/**
 * Tests move (file or folder)
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/move}
 */
@Test
public void testMove() throws Exception {
    setRequestContext(user1);
    // create folder f0
    String folder0Name = "f0-testMove-" + RUNID;
    String f0Id = createFolder(Nodes.PATH_MY, folder0Name).getId();
    // create folder f1
    Folder folderResp = createFolder(f0Id, "f1");
    String f1Id = folderResp.getId();
    // create folder f2
    folderResp = createFolder(f0Id, "f2");
    String f2Id = folderResp.getId();
    // create doc d1
    String d1Name = "content" + RUNID + "_1";
    String d1Id = createTextFile(f1Id, d1Name, "The quick brown fox jumps over the lazy dog 1.").getId();
    // create doc d2
    String d2Name = "content" + RUNID + "_2";
    String d2Id = createTextFile(f2Id, d2Name, "The quick brown fox jumps over the lazy dog 2.").getId();
    // move file (without rename)
    NodeTarget tgt = new NodeTarget();
    tgt.setTargetParentId(f2Id);
    HttpResponse response = post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 200);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1Name, documentResp.getName());
    assertEquals(f2Id, documentResp.getParentId());
    // move file (with rename)
    String d1NewName = d1Name + " updated !!";
    tgt = new NodeTarget();
    tgt.setName(d1NewName);
    tgt.setTargetParentId(f1Id);
    response = post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1NewName, documentResp.getName());
    assertEquals(f1Id, documentResp.getParentId());
    // -ve tests
    // missing target
    tgt = new NodeTarget();
    tgt.setName("new name");
    post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 400);
    // name already exists
    tgt = new NodeTarget();
    tgt.setName(d2Name);
    tgt.setTargetParentId(f2Id);
    post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 409);
    // unknown source nodeId
    tgt = new NodeTarget();
    tgt.setTargetParentId(f2Id);
    post("nodes/" + UUID.randomUUID().toString() + "/move", toJsonAsStringNonNull(tgt), null, 404);
    // unknown target nodeId
    tgt = new NodeTarget();
    tgt.setTargetParentId(UUID.randomUUID().toString());
    post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 404);
    // target is not a folder
    tgt = new NodeTarget();
    tgt.setTargetParentId(d2Id);
    post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 400);
    String rootNodeId = getRootNodeId();
    // create folder f3 (sub-folder of f2)
    folderResp = createFolder(f2Id, "f3");
    String f3Id = folderResp.getId();
    // can't create cycle (move into own subtree)
    tgt = new NodeTarget();
    tgt.setTargetParentId(f3Id);
    post("nodes/" + f2Id + "/move", toJsonAsStringNonNull(tgt), null, 400);
    // no (write/create) permissions to move to target
    tgt = new NodeTarget();
    tgt.setTargetParentId(rootNodeId);
    post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 403);
    setRequestContext(user2);
    String my2NodeId = getMyNodeId();
    // no (write/delete) permissions to move source
    tgt = new NodeTarget();
    tgt.setTargetParentId(my2NodeId);
    post("nodes/" + f1Id + "/move", toJsonAsStringNonNull(tgt), null, 403);
    // -ve - cannot move (delete) Company Home root node
    setRequestContext(networkAdmin);
    post("nodes/" + rootNodeId + "/move", toJsonAsStringNonNull(tgt), null, 403);
    setRequestContext(user1);
    Map params = new HashMap<>();
    params.put(Nodes.PARAM_RELATIVE_PATH, "/Sites");
    response = getSingle(NodesEntityResource.class, rootNodeId, params, 200);
    Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    String sitesNodeId = nodeResp.getId();
    // -ve - cannot move (delete) Sites node
    setRequestContext(networkAdmin);
    post("nodes/" + sitesNodeId + "/move", toJsonAsStringNonNull(tgt), null, 403);
    setRequestContext(user1);
    params = new HashMap<>();
    params.put(Nodes.PARAM_RELATIVE_PATH, "/Data Dictionary");
    response = getSingle(NodesEntityResource.class, rootNodeId, params, 200);
    nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    String ddNodeId = nodeResp.getId();
    // -ve - cannot move (delete) Data Dictionary node
    setRequestContext(networkAdmin);
    post("nodes/" + ddNodeId + "/move", toJsonAsStringNonNull(tgt), null, 403);
    // -ve test - cannot move to multiple destinations in single POST call (unsupported)
    List<NodeTarget> nodeDestinations = new ArrayList<>(2);
    NodeTarget nodeTarget = new NodeTarget();
    nodeTarget.setTargetParentId(f1Id);
    nodeDestinations.add(nodeTarget);
    nodeTarget = new NodeTarget();
    nodeTarget.setTargetParentId(f2Id);
    nodeDestinations.add(nodeTarget);
    post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(nodeDestinations), null, 405);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.alfresco.rest.api.tests.client.data.Node) ArrayList(java.util.ArrayList) NodeTarget(org.alfresco.rest.api.model.NodeTarget) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) MultiValueMap(org.apache.commons.collections.map.MultiValueMap) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 3 with NodeTarget

use of org.alfresco.rest.api.model.NodeTarget in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testCopy.

/**
 * Tests copy (file or folder)
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/copy}
 */
@Test
public void testCopy() throws Exception {
    setRequestContext(user1);
    // create folder
    Folder folderResp = createFolder(Nodes.PATH_MY, "fsource");
    String sourceId = folderResp.getId();
    // create folder
    folderResp = createFolder(Nodes.PATH_MY, "ftarget");
    String targetId = folderResp.getId();
    // create doc d1
    String d1Name = "content" + RUNID + "_1";
    String d1Id = createTextFile(sourceId, d1Name, "The quick brown fox jumps over the lazy dog 1.").getId();
    // create doc d2
    String d2Name = "content" + RUNID + "_2";
    String d2Id = createTextFile(sourceId, d2Name, "The quick brown fox jumps over the lazy dog 2.").getId();
    Map<String, String> body = new HashMap<>();
    body.put("targetParentId", targetId);
    HttpResponse response = post(URL_NODES, d1Id, "copy", toJsonAsStringNonNull(body).getBytes(), null, null, 201);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1Name, documentResp.getName());
    assertEquals(targetId, documentResp.getParentId());
    // copy file (with rename)
    String newD2Name = d2Name + " updated !!";
    body = new HashMap<>();
    body.put("targetParentId", targetId);
    body.put("name", newD2Name);
    response = post(URL_NODES, d2Id, "copy", toJsonAsStringNonNull(body).getBytes(), null, null, 201);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(newD2Name, documentResp.getName());
    assertEquals(targetId, documentResp.getParentId());
    // -ve tests
    // missing target
    NodeTarget tgt = new NodeTarget();
    tgt.setName("new name");
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 400);
    // name already exists - different parent
    tgt = new NodeTarget();
    tgt.setName(newD2Name);
    tgt.setTargetParentId(targetId);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 409);
    // name already exists - same parent
    tgt = new NodeTarget();
    tgt.setTargetParentId(sourceId);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 409);
    // unknown source nodeId
    tgt = new NodeTarget();
    tgt.setTargetParentId(targetId);
    post("nodes/" + UUID.randomUUID().toString() + "/copy", toJsonAsStringNonNull(tgt), null, 404);
    // unknown target nodeId
    tgt = new NodeTarget();
    tgt.setTargetParentId(UUID.randomUUID().toString());
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 404);
    // target is not a folder
    tgt = new NodeTarget();
    tgt.setTargetParentId(d2Id);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 400);
    String rootNodeId = getRootNodeId();
    // no (write/create) permissions to copy to target
    tgt = new NodeTarget();
    tgt.setTargetParentId(rootNodeId);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(tgt), null, 403);
    // -ve test - cannot copy to multiple destinations in single POST call (unsupported)
    List<NodeTarget> nodeDestinations = new ArrayList<>(2);
    NodeTarget nodeTarget = new NodeTarget();
    nodeTarget.setTargetParentId(sourceId);
    nodeDestinations.add(nodeTarget);
    nodeTarget = new NodeTarget();
    nodeTarget.setTargetParentId(targetId);
    nodeDestinations.add(nodeTarget);
    post("nodes/" + d1Id + "/copy", toJsonAsStringNonNull(nodeDestinations), null, 405);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) NodeTarget(org.alfresco.rest.api.model.NodeTarget) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 4 with NodeTarget

use of org.alfresco.rest.api.model.NodeTarget in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testMoveFileCreatedByDeletedUser.

@Test
public void testMoveFileCreatedByDeletedUser() throws Exception {
    // Create temp user
    String user = createUser("userTstMove-" + RUNID, "userRNDPassword", networkOne);
    setRequestContext(user);
    // create folder f0
    String folder0Name = "f0-testMove-" + RUNID;
    String f0Id = createFolder(Nodes.PATH_MY, folder0Name).getId();
    // create folder f1
    Folder folderResp = createFolder(f0Id, "f1");
    String f1Id = folderResp.getId();
    // create folder f2
    folderResp = createFolder(f0Id, "f2");
    String f2Id = folderResp.getId();
    // create doc d1
    String d1Name = "content" + RUNID + "_1";
    String d1Id = createTextFile(f1Id, d1Name, "The quick brown fox jumps over the lazy dog 1.").getId();
    setRequestContext(networkAdmin);
    transactionHelper.doInTransaction(() -> {
        deleteUser(user, networkOne);
        return null;
    });
    // move file (without rename)
    NodeTarget tgt = new NodeTarget();
    tgt.setTargetParentId(f2Id);
    HttpResponse response = post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 200);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1Name, documentResp.getName());
    assertEquals(f2Id, documentResp.getParentId());
    // Get node info (ensure rollback didn't happen)
    response = getSingle(NodesEntityResource.class, d1Id, null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(f2Id, documentResp.getParentId());
    // move file (with rename)
    String d1NewName = d1Name + " updated !!";
    tgt = new NodeTarget();
    tgt.setName(d1NewName);
    tgt.setTargetParentId(f1Id);
    response = post("nodes/" + d1Id + "/move", toJsonAsStringNonNull(tgt), null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1NewName, documentResp.getName());
    assertEquals(f1Id, documentResp.getParentId());
    // Get node info (ensure rollback didn't happen)
    response = getSingle(NodesEntityResource.class, d1Id, null, 200);
    documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(f1Id, documentResp.getParentId());
}
Also used : NodeTarget(org.alfresco.rest.api.model.NodeTarget) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Aggregations

AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)4 NodeTarget (org.alfresco.rest.api.model.NodeTarget)4 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)4 Folder (org.alfresco.rest.api.tests.client.data.Folder)4 Test (org.junit.Test)4 Document (org.alfresco.rest.api.tests.client.data.Document)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)2 Map (java.util.Map)1 MimetypeMap (org.alfresco.repo.content.MimetypeMap)1 Node (org.alfresco.rest.api.tests.client.data.Node)1 MultiValueMap (org.apache.commons.collections.map.MultiValueMap)1