Search in sources :

Example 1 with AssocTarget

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

the class NodeAssociationsApiTest method testNodePeerAssocsPermissions.

/**
 * Tests base permissions for managing (adding, listing and removing) peer associations.
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets}
 *
 * <p>DELETE:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets/<targetNodeId>}
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets}
 * {
 */
@Test
public void testNodePeerAssocsPermissions() throws Exception {
    setRequestContext(user1);
    // as user 1 - create folder in "Shared Files" area and content within the folder
    String sharedFolderNodeId = getSharedNodeId();
    String sf1Id = createFolder(sharedFolderNodeId, "shared folder " + RUNID).getId();
    Node n = new Node();
    n.setName("shared content " + RUNID);
    n.setNodeType(TYPE_CM_CONTENT);
    n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
    HttpResponse response = post(getNodeChildrenUrl(sf1Id), toJsonAsStringNonNull(n), 201);
    String so1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    // as user 1 - create folder in user's home (My Files) area and content within the folder
    String u1myNodeId = getMyNodeId();
    String u1f1Id = createFolder(u1myNodeId, "f1").getId();
    n = new Node();
    n.setName("o1");
    n.setNodeType(TYPE_CM_CONTENT);
    n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
    response = post(getNodeChildrenUrl(u1f1Id), toJsonAsStringNonNull(n), 201);
    String u1o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    // as user 2 - create folder in user's home (My Files) area and content within the folder
    setRequestContext(user2);
    String u2myNodeId = getMyNodeId();
    String u2f1Id = createFolder(u2myNodeId, "f1").getId();
    n = new Node();
    n.setName("o1");
    n.setNodeType(TYPE_CM_CONTENT);
    n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
    response = post(getNodeChildrenUrl(u2f1Id), toJsonAsStringNonNull(n), 201);
    String u2o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    try {
        Paging paging = getPaging(0, 100);
        // empty lists - before
        setRequestContext(user1);
        response = getAll(getNodeTargetsUrl(u1f1Id), paging, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        setRequestContext(user2);
        response = getAll(getNodeTargetsUrl(u2f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // Create some assocs
        setRequestContext(user1);
        AssocTarget tgt = new AssocTarget(u1o1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(u1f1Id), toJsonAsStringNonNull(tgt), 201);
        setRequestContext(user2);
        tgt = new AssocTarget(u2o1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(u2f1Id), toJsonAsStringNonNull(tgt), 201);
        setRequestContext(user1);
        response = getAll(getNodeTargetsUrl(u1f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        setRequestContext(user2);
        response = getAll(getNodeTargetsUrl(u2f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        // -ve tests
        {
            // source/target not readable
            setRequestContext(user2);
            // list
            getAll(getNodeTargetsUrl(u1f1Id), paging, null, 403);
            getAll(getNodeSourcesUrl(u1o1Id), paging, null, 403);
            setRequestContext(user1);
            // create
            tgt = new AssocTarget(u2o1Id, ASSOC_TYPE_CM_REFERENCES);
            post(getNodeTargetsUrl(u1f1Id), toJsonAsStringNonNull(tgt), 403);
            tgt = new AssocTarget(u1o1Id, ASSOC_TYPE_CM_REFERENCES);
            post(getNodeTargetsUrl(u2f1Id), toJsonAsStringNonNull(tgt), 403);
            setRequestContext(user2);
            // remove
            delete(getNodeTargetsUrl(u1f1Id), u2o1Id, null, 403);
            delete(getNodeTargetsUrl(u2f1Id), u1o1Id, null, 404);
        }
        setRequestContext(user1);
        // Test listing targets (with permissions applied)
        // update permission
        // TODO refactor with remote permission api calls (use v0 until we have v1 ?) (RA-1085)
        AuthenticationUtil.setFullyAuthenticatedUser(user1);
        permissionService.setPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, sf1Id), user2, PermissionService.EDITOR, true);
        setRequestContext(networkAdmin);
        response = publicApiClient.get(getScope(), "nodes/" + sf1Id + "/targets", null, null, null, createParams(paging, null));
        checkStatus(200, response.getStatusCode());
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // user 1
        setRequestContext(user1);
        tgt = new AssocTarget(u1o1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(sf1Id), toJsonAsStringNonNull(tgt), 201);
        // user 2
        setRequestContext(user2);
        tgt = new AssocTarget(u2o1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(sf1Id), toJsonAsStringNonNull(tgt), 201);
        setRequestContext(networkAdmin);
        response = publicApiClient.get(getScope(), "nodes/" + sf1Id + "/targets", null, null, null, createParams(paging, null));
        checkStatus(200, response.getStatusCode());
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        setRequestContext(user1);
        response = getAll(getNodeTargetsUrl(sf1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(u1o1Id, nodes.get(0).getId());
        setRequestContext(user2);
        response = getAll(getNodeTargetsUrl(sf1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(u2o1Id, nodes.get(0).getId());
        // Test listing sources (with permissions applied)
        // update permission
        // TODO refactor with remote permission api calls (use v0 until we have v1 ?)
        AuthenticationUtil.setFullyAuthenticatedUser(user1);
        permissionService.setPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, sf1Id), user2, PermissionService.EDITOR, true);
        setRequestContext(networkAdmin);
        response = publicApiClient.get(getScope(), "nodes/" + so1Id + "/sources", null, null, null, createParams(paging, null));
        checkStatus(200, response.getStatusCode());
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // user 1
        setRequestContext(user1);
        tgt = new AssocTarget(so1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(u1f1Id), toJsonAsStringNonNull(tgt), 201);
        // user 2
        setRequestContext(user2);
        tgt = new AssocTarget(so1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(u2f1Id), toJsonAsStringNonNull(tgt), 201);
        setRequestContext(networkAdmin);
        response = publicApiClient.get(getScope(), "nodes/" + so1Id + "/sources", null, null, null, createParams(paging, null));
        checkStatus(200, response.getStatusCode());
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        setRequestContext(user1);
        response = getAll(getNodeSourcesUrl(so1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(u1f1Id, nodes.get(0).getId());
        setRequestContext(user2);
        response = getAll(getNodeSourcesUrl(so1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(u2f1Id, nodes.get(0).getId());
    } finally {
        // some cleanup
        setRequestContext(user1);
        deleteNode(u1f1Id, true, 204);
        deleteNode(sf1Id, true, 204);
        setRequestContext(user2);
        deleteNode(u2f1Id, true, 204);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Node(org.alfresco.rest.api.tests.client.data.Node) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) AssocTarget(org.alfresco.rest.api.model.AssocTarget) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 2 with AssocTarget

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

the class NodesImpl method addTargets.

public List<AssocTarget> addTargets(String sourceNodeId, List<AssocTarget> entities) {
    List<AssocTarget> result = new ArrayList<>(entities.size());
    NodeRef srcNodeRef = validateNode(sourceNodeId);
    for (AssocTarget assoc : entities) {
        String targetNodeId = assoc.getTargetId();
        if (targetNodeId == null) {
            throw new InvalidArgumentException("Missing targetId");
        }
        String assocTypeStr = assoc.getAssocType();
        QName assocTypeQName = getAssocType(assocTypeStr);
        try {
            NodeRef tgtNodeRef = validateNode(targetNodeId);
            nodeAssocService.createAssociation(srcNodeRef, tgtNodeRef, assocTypeQName);
        } catch (AssociationExistsException aee) {
            throw new ConstraintViolatedException("Node association '" + assocTypeStr + "' already exists from " + sourceNodeId + " to " + targetNodeId);
        } catch (IllegalArgumentException iae) {
            // note: for now, we assume it is invalid assocType - alternatively, we could attempt to pre-validate via dictionary.getAssociation
            throw new InvalidArgumentException(sourceNodeId + "," + assocTypeStr + "," + targetNodeId);
        }
        result.add(assoc);
    }
    return result;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) AssocTarget(org.alfresco.rest.api.model.AssocTarget) AssociationExistsException(org.alfresco.service.cmr.repository.AssociationExistsException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 3 with AssocTarget

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

the class NodeAssociationsApiTest method testCreateNodeWithAssocs.

/**
 * Test ability to create a node and optionally specify one or more associations (to other existing nodes) at time of create.
 *
 * @throws Exception
 */
@Test
public void testCreateNodeWithAssocs() throws Exception {
    // as user 1
    setRequestContext(user1);
    String myFolderNodeId = getMyNodeId();
    // create node with some assocs in a single call
    // create folder
    Node n = new Node();
    n.setName("f1");
    n.setNodeType(TYPE_CM_FOLDER);
    n.setAspectNames(Arrays.asList(ASPECT_CM_PREFERENCES));
    HttpResponse response = post(getNodeChildrenUrl(myFolderNodeId), toJsonAsStringNonNull(n), 201);
    String f1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    // create content node
    String o1Name = "o1";
    n = new Node();
    n.setName(o1Name);
    n.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
    String o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    String o2Name = "o2";
    n = new Node();
    n.setName(o2Name);
    n.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
    String o2Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    // create folder node with some assocs
    String f2Name = "f2";
    n = new Node();
    n.setName(f2Name);
    n.setNodeType(TYPE_CM_FOLDER);
    AssocChild secChild = new AssocChild(o1Id, ASSOC_TYPE_CM_CONTAINS);
    n.setSecondaryChildren(Collections.singletonList(secChild));
    AssocTarget tgt = new AssocTarget(o2Id, ASSOC_TYPE_CM_REFERENCES);
    n.setTargets(Collections.singletonList(tgt));
    response = post(getNodeChildrenUrl(myFolderNodeId), toJsonAsStringNonNull(n), 201);
    String f2Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    String f3Id = createFolder(myFolderNodeId, "f3").getId();
    try {
        Paging paging = getPaging(0, 100);
        response = getAll(getNodeSecondaryChildrenUrl(f2Id), paging, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o1Id, nodes.get(0).getId());
        response = getAll(getNodeTargetsUrl(f2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o2Id, nodes.get(0).getId());
        // TODO test model with mandatory aspect
        // -ve test -  minor: error code if creating a cyclic child assoc (REPO-475)
        n = new Node();
        n.setName("my-folder");
        n.setNodeType(TYPE_CM_FOLDER);
        AssocChild assocChild = new AssocChild(myFolderNodeId, "cm:contains");
        n.setSecondaryChildren(Collections.singletonList(assocChild));
        post(getNodeChildrenUrl(myFolderNodeId), RestApiUtil.toJsonAsStringNonNull(n), 400);
        // -ve tests - missing targetId / childId or assocType
        n = new Node();
        n.setName("my-folder");
        n.setNodeType(TYPE_CM_FOLDER);
        assocChild = new AssocChild(null, ASSOC_TYPE_CM_CONTAINS);
        n.setSecondaryChildren(Collections.singletonList(assocChild));
        post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
        n = new Node();
        n.setName("my-folder");
        n.setNodeType(TYPE_CM_FOLDER);
        assocChild = new AssocChild(f2Id, null);
        n.setSecondaryChildren(Collections.singletonList(assocChild));
        post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
        n = new Node();
        n.setName("my-folder");
        n.setNodeType(TYPE_CM_FOLDER);
        tgt = new AssocTarget(null, ASSOC_TYPE_CM_REFERENCES);
        n.setTargets(Collections.singletonList(tgt));
        post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
        n = new Node();
        n.setName("my-folder");
        n.setNodeType(TYPE_CM_FOLDER);
        tgt = new AssocTarget(f2Id, null);
        n.setTargets(Collections.singletonList(tgt));
        post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
    } finally {
        // some cleanup
        setRequestContext(user1);
        deleteNode(f1Id, true, 204);
        deleteNode(f2Id, true, 204);
        deleteNode(f3Id, true, 204);
    }
}
Also used : Node(org.alfresco.rest.api.tests.client.data.Node) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) AssocChild(org.alfresco.rest.api.model.AssocChild) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) AssocTarget(org.alfresco.rest.api.model.AssocTarget) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 4 with AssocTarget

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

the class NodeAssociationsApiTest method testDeleteAndRestoreNodeWithAssocs.

/**
 * Test ability to delete a node with associations (to and from the node) and then restore it.
 * Only the primary parent/child assoc(s) for the deleted node(s) is/are restored.
 *
 * @throws Exception
 */
@Test
public void testDeleteAndRestoreNodeWithAssocs() throws Exception {
    // as user 1 ...
    setRequestContext(user1);
    String f1Id = null;
    String f2Id = null;
    String f3Id = null;
    try {
        String myFolderNodeId = getMyNodeId();
        // create primary parent-child hierarchy
        f1Id = createFolder(myFolderNodeId, "f1").getId();
        String f1bId = createFolder(f1Id, "f1b").getId();
        String f1cId = createFolder(f1bId, "f1c").getId();
        String f1dId = createFolder(f1cId, "f1d").getId();
        String c1eId = createTextFile(f1dId, "c1e", "some text content").getId();
        f2Id = createFolder(myFolderNodeId, "f2").getId();
        f3Id = createFolder(myFolderNodeId, "f3").getId();
        HttpResponse response = getAll(getNodeParentsUrl(f1bId), null, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1Id, nodes.get(0).getId());
        response = getAll(getNodeParentsUrl(f1dId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1cId, nodes.get(0).getId());
        response = getAll(getNodeSourcesUrl(c1eId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // add some secondary parent/child assocs outside of the hierarchy
        AssocChild secChild = new AssocChild(f1bId, ASSOC_TYPE_CM_CONTAINS);
        post(getNodeSecondaryChildrenUrl(f2Id), toJsonAsStringNonNull(secChild), 201);
        secChild = new AssocChild(f1bId, ASSOC_TYPE_CM_CONTAINS);
        post(getNodeSecondaryChildrenUrl(f3Id), toJsonAsStringNonNull(secChild), 201);
        secChild = new AssocChild(f1dId, ASSOC_TYPE_CM_CONTAINS);
        post(getNodeSecondaryChildrenUrl(f2Id), toJsonAsStringNonNull(secChild), 201);
        secChild = new AssocChild(f1dId, ASSOC_TYPE_CM_CONTAINS);
        post(getNodeSecondaryChildrenUrl(f3Id), toJsonAsStringNonNull(secChild), 201);
        // also add a secondary parent/child assoc within the hierarchy
        secChild = new AssocChild(f1dId, ASSOC_TYPE_CM_CONTAINS);
        post(getNodeSecondaryChildrenUrl(f1bId), toJsonAsStringNonNull(secChild), 201);
        // add some peer assocs outside of the hierarchy
        AssocTarget tgt = new AssocTarget(c1eId, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(f2Id), toJsonAsStringNonNull(tgt), 201);
        tgt = new AssocTarget(c1eId, ASSOC_TYPE_CM_PARTS);
        post(getNodeTargetsUrl(f3Id), toJsonAsStringNonNull(tgt), 201);
        // also add a peer assoc within the hierarchy
        tgt = new AssocTarget(c1eId, ASSOC_TYPE_CM_PARTS);
        post(getNodeTargetsUrl(f1cId), toJsonAsStringNonNull(tgt), 201);
        // double-check the secondary parent/child assocs
        response = getAll(getNodeParentsUrl(f1bId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(3, nodes.size());
        response = getAll(getNodeParentsUrl(f1dId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(4, nodes.size());
        response = getAll(getNodeSecondaryChildrenUrl(f2Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        List<String> nodeIds = Arrays.asList(new String[] { nodes.get(0).getId(), nodes.get(1).getId() });
        assertTrue(nodeIds.contains(f1bId));
        assertTrue(nodeIds.contains(f1dId));
        response = getAll(getNodeSecondaryChildrenUrl(f3Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        nodeIds = Arrays.asList(new String[] { nodes.get(0).getId(), nodes.get(1).getId() });
        assertTrue(nodeIds.contains(f1bId));
        assertTrue(nodeIds.contains(f1dId));
        response = getAll(getNodeSecondaryChildrenUrl(f1bId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1dId, nodes.get(0).getId());
        // double-check the peer assocs
        response = getAll(getNodeSourcesUrl(c1eId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(3, nodes.size());
        response = getAll(getNodeTargetsUrl(f2Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(c1eId, nodes.get(0).getId());
        response = getAll(getNodeTargetsUrl(f3Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(c1eId, nodes.get(0).getId());
        response = getAll(getNodeTargetsUrl(f1cId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(c1eId, nodes.get(0).getId());
        // ... delete to trashcan/archive ...
        deleteNode(f1bId);
        getSingle(NodesEntityResource.class, f1bId, null, 404);
        response = getAll(getNodeTargetsUrl(f2Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(f3Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // ... and then restore again ...
        post(URL_DELETED_NODES + "/" + f1bId + "/restore", null, null, 200);
        // check primary parent-child hierarchy is restored
        // but not the secondary parents or peer assocs of the deleted nodes (outside or within the hierarchy)
        response = getSingle(NodesEntityResource.class, f1bId, null, 200);
        Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals(f1Id, nodeResp.getParentId());
        response = getSingle(NodesEntityResource.class, f1cId, null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals(f1bId, nodeResp.getParentId());
        response = getSingle(NodesEntityResource.class, f1dId, null, 200);
        nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
        assertEquals(f1cId, nodeResp.getParentId());
        // secondary child assocs have not been restored
        response = getAll(getNodeParentsUrl(f1bId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1Id, nodes.get(0).getId());
        response = getAll(getNodeParentsUrl(f1cId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1bId, nodes.get(0).getId());
        response = getAll(getNodeParentsUrl(f1dId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1cId, nodes.get(0).getId());
        response = getAll(getNodeSecondaryChildrenUrl(f2Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSecondaryChildrenUrl(f3Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // peer assocs have not been restored
        response = getAll(getNodeSourcesUrl(c1eId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(f1cId), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(f2Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(f3Id), null, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
    } finally {
        // some cleanup
        setRequestContext(user1);
        if (f1Id != null) {
            deleteNode(f1Id, true, 204);
        }
        if (f2Id != null) {
            deleteNode(f2Id, true, 204);
        }
        if (f3Id != null) {
            deleteNode(f3Id, true, 204);
        }
    }
}
Also used : Node(org.alfresco.rest.api.tests.client.data.Node) AssocChild(org.alfresco.rest.api.model.AssocChild) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) AssocTarget(org.alfresco.rest.api.model.AssocTarget) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 5 with AssocTarget

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

the class NodeAssociationsApiTest method testNodePeerAssocs.

/**
 * Tests basic api to manage (add, list, remove) node peer associations (ie. source node -> target node)
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets}
 *
 * <p>DELETE:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets/<targetNodeId>}
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets}
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<targetNodeId>/sources}
 */
@Test
public void testNodePeerAssocs() throws Exception {
    setRequestContext(user1);
    String myFolderNodeId = getMyNodeId();
    // create folder
    String f1Id = createFolder(myFolderNodeId, "f1").getId();
    // create content node
    Node n = new Node();
    n.setName("o1");
    n.setNodeType(TYPE_CM_CONTENT);
    n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
    HttpResponse response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
    String o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    // create ano' folder
    String f2Id = createFolder(myFolderNodeId, "f2").getId();
    // create ano' content node
    n = new Node();
    n.setName("o2");
    n.setNodeType(TYPE_CM_CONTENT);
    n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
    response = post(getNodeChildrenUrl(f2Id), toJsonAsStringNonNull(n), 201);
    String o2Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    try {
        // As user 1 ...
        Paging paging = getPaging(0, 100);
        // empty lists - before
        response = getAll(getNodeTargetsUrl(o1Id), paging, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSourcesUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSourcesUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // create two assocs in one direction (from src to tgt)
        AssocTarget tgt = new AssocTarget(o2Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(o1Id), toJsonAsStringNonNull(tgt), 201);
        tgt = new AssocTarget(o2Id, ASSOC_TYPE_CM_PARTS);
        post(getNodeTargetsUrl(o1Id), toJsonAsStringNonNull(tgt), 201);
        response = getAll(getNodeTargetsUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeSourcesUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSourcesUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        // create two assocs in the other direction (from tgt to src)
        tgt = new AssocTarget(o1Id, ASSOC_TYPE_CM_REFERENCES);
        post(getNodeTargetsUrl(o2Id), toJsonAsStringNonNull(tgt), 201);
        tgt = new AssocTarget(o1Id, ASSOC_TYPE_CM_PARTS);
        post(getNodeTargetsUrl(o2Id), toJsonAsStringNonNull(tgt), 201);
        response = getAll(getNodeTargetsUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeSourcesUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeTargetsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeSourcesUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        // test basic list filter
        Map<String, String> params = new HashMap<>(1);
        params.put("where", "(assocType='" + ASSOC_TYPE_CM_REFERENCES + "')");
        response = getAll(getNodeTargetsUrl(o1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o2Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_REFERENCES, nodes.get(0).getAssociation().getAssocType());
        response = getAll(getNodeSourcesUrl(o1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o2Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_REFERENCES, nodes.get(0).getAssociation().getAssocType());
        params = new HashMap<>(1);
        params.put("where", "(assocType='" + ASSOC_TYPE_CM_PARTS + "')");
        response = getAll(getNodeTargetsUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o1Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_PARTS, nodes.get(0).getAssociation().getAssocType());
        response = getAll(getNodeSourcesUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o1Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_PARTS, nodes.get(0).getAssociation().getAssocType());
        // remove assocs - specific type - in one direction
        params = new HashMap<>(1);
        params.put(PARAM_ASSOC_TYPE, ASSOC_TYPE_CM_REFERENCES);
        delete(getNodeTargetsUrl(o1Id), o2Id, params, 204);
        response = getAll(getNodeTargetsUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        response = getAll(getNodeSourcesUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeTargetsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeSourcesUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        params = new HashMap<>(1);
        params.put(PARAM_ASSOC_TYPE, ASSOC_TYPE_CM_PARTS);
        delete(getNodeTargetsUrl(o1Id), o2Id, params, 204);
        response = getAll(getNodeTargetsUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSourcesUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeTargetsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        response = getAll(getNodeSourcesUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // remove assocs - both types at once (ie. no assocType param) - in the other direction
        delete(getNodeTargetsUrl(o2Id), o1Id, 204);
        // empty lists - after
        response = getAll(getNodeTargetsUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSourcesUrl(o1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeTargetsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeSourcesUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // 
        // -ve tests - add assoc
        // 
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            tgt = new AssocTarget(o2Id, ASSOC_TYPE_CM_REFERENCES);
            post(getNodeTargetsUrl(o1Id), toJsonAsStringNonNull(tgt), 401);
            setRequestContext(user1);
            // -ve test - model integrity
            tgt = new AssocTarget(f2Id, ASSOC_TYPE_CM_REFERENCES);
            post(getNodeTargetsUrl(o1Id), toJsonAsStringNonNull(tgt), 422);
            // -ve test - duplicate assoc
            tgt = new AssocTarget(o1Id, ASSOC_TYPE_CM_REFERENCES);
            post(getNodeTargetsUrl(o2Id), toJsonAsStringNonNull(tgt), 201);
            post(getNodeTargetsUrl(o2Id), toJsonAsStringNonNull(tgt), 409);
            tgt = new AssocTarget(o1Id, "cm:unknowntype");
            post(getNodeTargetsUrl(o2Id), toJsonAsStringNonNull(tgt), 400);
        }
        // 
        // -ve test - list assocs
        // 
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            getAll(getNodeTargetsUrl(f1Id), paging, null, 401);
            getAll(getNodeSourcesUrl(f1Id), paging, null, 401);
            setRequestContext(user1);
            getAll(getNodeTargetsUrl(UUID.randomUUID().toString()), paging, null, 404);
            getAll(getNodeSourcesUrl(UUID.randomUUID().toString()), paging, null, 404);
            params = new HashMap<>(1);
            params.put("where", "(assocType='cm:unknownassoctype')");
            getAll(getNodeTargetsUrl(o1Id), paging, params, 400);
            getAll(getNodeSourcesUrl(o1Id), paging, params, 400);
        // TODO paging - in-built sort order ? (RA-926, RA-927)
        }
        // 
        // -ve test - remove assoc(s)
        // 
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            delete(getNodeTargetsUrl(o1Id), o2Id, 401);
            setRequestContext(user1);
            delete(getNodeTargetsUrl(UUID.randomUUID().toString()), o2Id, null, 404);
            delete(getNodeTargetsUrl(o1Id), UUID.randomUUID().toString(), null, 404);
            // -ve test -nothing to delete - for any assoc type
            delete(getNodeTargetsUrl(o1Id), o2Id, null, 404);
            // -ve test - nothing to delete - for given assoc type
            params = new HashMap<>(1);
            params.put(PARAM_ASSOC_TYPE, ASSOC_TYPE_CM_REFERENCES);
            delete(getNodeTargetsUrl(o1Id), o2Id, params, 404);
            // -ve test - unknown assoc type
            params = new HashMap<>(1);
            params.put(PARAM_ASSOC_TYPE, "cm:unknowntype");
            delete(getNodeTargetsUrl(o1Id), o2Id, params, 400);
        }
    } finally {
        // some cleanup
        setRequestContext(user1);
        deleteNode(f1Id, true, 204);
        deleteNode(f2Id, true, 204);
    }
}
Also used : HashMap(java.util.HashMap) Node(org.alfresco.rest.api.tests.client.data.Node) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) AssocTarget(org.alfresco.rest.api.model.AssocTarget) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Aggregations

AssocTarget (org.alfresco.rest.api.model.AssocTarget)5 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)4 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)4 Node (org.alfresco.rest.api.tests.client.data.Node)4 Test (org.junit.Test)4 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 AssocChild (org.alfresco.rest.api.model.AssocChild)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 AssociationExistsException (org.alfresco.service.cmr.repository.AssociationExistsException)1 QName (org.alfresco.service.namespace.QName)1