Search in sources :

Example 1 with AssocChild

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

the class TestDownloads method setupTest.

@Before
public void setupTest() throws IOException, Exception {
    nodesApi = applicationContext.getBean("Nodes", org.alfresco.rest.api.Nodes.class);
    setRequestContext(user1);
    Document zippableDoc1 = createTextFile(tDocLibNodeId, ZIPPABLE_DOC1_NAME, DUMMY_CONTENT);
    zippableDocId1 = zippableDoc1.getId();
    zippableDocId2 = createTextFile(tDocLibNodeId, "docTest2", DUMMY_CONTENT).getId();
    Folder zippableFolder1 = createFolder(tDocLibNodeId, FOLDER1_NAME);
    zippableFolderId1 = zippableFolder1.getId();
    zippableDocId3_InFolder1 = createTextFile(zippableFolderId1, DOC3_NAME, DUMMY_CONTENT).getId();
    Folder zippableFolder2_InFolder1 = createFolder(zippableFolderId1, SUB_FOLDER1_NAME);
    zippableFolderId2_InFolder1 = zippableFolder2_InFolder1.getId();
    zippableDocId4_InFolder2 = createTextFile(zippableFolderId2_InFolder1, DOC4_NAME, DUMMY_CONTENT).getId();
    Folder zippableFolder3 = createFolder(tDocLibNodeId, FOLDER3_NAME);
    zippableFolderId3 = zippableFolder3.getId();
    setRequestContext(user2);
    String user2Site = createSite("TestSite B - " + RUNID, SiteVisibility.PRIVATE).getId();
    String user2DocLib = getSiteContainerNodeId(user2Site, "documentLibrary");
    zippableDoc_user2 = createTextFile(user2DocLib, "user2doc", DUMMY_CONTENT).getId();
    setRequestContext(user1);
    AssocChild secChild = new AssocChild(zippableDoc1.getId(), ASSOC_TYPE_CM_CONTAINS);
    post(format(NODES_SECONDARY_CHILDREN, zippableFolder3.getId()), toJsonAsStringNonNull(secChild), HttpServletResponse.SC_CREATED);
}
Also used : AssocChild(org.alfresco.rest.api.model.AssocChild) Document(org.alfresco.rest.api.tests.client.data.Document) Folder(org.alfresco.rest.api.tests.client.data.Folder) Before(org.junit.Before)

Example 2 with AssocChild

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

the class NodeAssociationsApiTest method testNodeSecondaryChildAssocs.

/**
 * Tests basic api to manage (add, list, remove) node secondary child associations (ie. parent node -> child node)
 *
 * Note: refer to NodeApiTest for tests for primary child association
 *
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<parentNodeId>/secondary-children}
 *
 * <p>DELETE:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<parentNodeId>/secondary-children/<childNodeId>}
 *
 * <p>GET:</p>
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<parentNodeId>/secondary-children}
 * {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<childNodeId>/parents}
 */
@Test
public void testNodeSecondaryChildAssocs() throws Exception {
    setRequestContext(user1);
    String myFolderNodeId = getMyNodeId();
    // 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();
    // create ano' folder
    String f2Id = createFolder(myFolderNodeId, "f2").getId();
    // create ano' content node
    String o2Name = "o2";
    n = new Node();
    n.setName(o2Name);
    n.setNodeType(TYPE_CM_CONTENT);
    response = post(getNodeChildrenUrl(f2Id), toJsonAsStringNonNull(n), 201);
    String o2Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    String f3Id = createFolder(myFolderNodeId, "f3").getId();
    String f4Id = createFolder(myFolderNodeId, "f4").getId();
    try {
        // As user 1 ...
        Paging paging = getPaging(0, 100);
        // lists - before
        response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
        List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        // primary parent only
        response = getAll(getNodeParentsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f2Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_CONTAINS, nodes.get(0).getAssociation().getAssocType());
        assertTrue(nodes.get(0).getAssociation().getIsPrimary());
        // create secondary child assoc
        AssocChild secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_CONTAINS);
        post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 201);
        // create ano' secondary child assoc (different type) between the same two nodes
        secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_PREFERENCE_IMAGE);
        post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 201);
        response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        int i = 0;
        for (Node node : nodes) {
            Association nodeAssoc = node.getAssociation();
            if (nodeAssoc.getAssocType().equals(ASSOC_TYPE_CM_CONTAINS)) {
                i++;
            } else if (nodeAssoc.getAssocType().equals(ASSOC_TYPE_CM_PREFERENCE_IMAGE)) {
                i++;
            }
            assertEquals(o2Id, node.getId());
            assertFalse(nodeAssoc.getIsPrimary());
        }
        assertEquals(2, i);
        response = getAll(getNodeParentsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        i = 0;
        for (Node node : nodes) {
            String nodeId = node.getId();
            Association nodeAssoc = node.getAssociation();
            if (nodeId.equals(f2Id)) {
                assertEquals(ASSOC_TYPE_CM_CONTAINS, nodeAssoc.getAssocType());
                assertTrue(nodeAssoc.getIsPrimary());
                i++;
            } else if (nodeId.equals(f1Id)) {
                if (nodeAssoc.getAssocType().equals(ASSOC_TYPE_CM_CONTAINS)) {
                    i++;
                } else if (nodeAssoc.getAssocType().equals(ASSOC_TYPE_CM_PREFERENCE_IMAGE)) {
                    i++;
                }
                assertFalse(nodeAssoc.getIsPrimary());
            }
        }
        assertEquals(3, i);
        // test list filter - assocType (/secondary-children & /parents)
        Map<String, String> params = new HashMap<>(1);
        params.put("where", "(assocType='" + ASSOC_TYPE_CM_CONTAINS + "')");
        response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o2Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_CONTAINS, nodes.get(0).getAssociation().getAssocType());
        response = getAll(getNodeParentsUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        i = 0;
        for (Node node : nodes) {
            String nodeId = node.getId();
            Association nodeAssoc = node.getAssociation();
            if (nodeId.equals(f2Id)) {
                assertEquals(ASSOC_TYPE_CM_CONTAINS, nodeAssoc.getAssocType());
                assertTrue(nodeAssoc.getIsPrimary());
                i++;
            } else if (nodeId.equals(f1Id)) {
                assertEquals(ASSOC_TYPE_CM_CONTAINS, nodeAssoc.getAssocType());
                assertFalse(nodeAssoc.getIsPrimary());
                i++;
            }
        }
        assertEquals(2, i);
        params = new HashMap<>(1);
        params.put("where", "(assocType='" + ASSOC_TYPE_CM_PREFERENCE_IMAGE + "')");
        response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o2Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_PREFERENCE_IMAGE, nodes.get(0).getAssociation().getAssocType());
        assertFalse(nodes.get(0).getAssociation().getIsPrimary());
        response = getAll(getNodeParentsUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1Id, nodes.get(0).getId());
        assertEquals(ASSOC_TYPE_CM_PREFERENCE_IMAGE, nodes.get(0).getAssociation().getAssocType());
        assertFalse(nodes.get(0).getAssociation().getIsPrimary());
        // test list filter - isPrimary (/children)
        // note: see NodeApiTest for other filters related to /nodes/{parentId}/children filters
        // note: currently collapses same nodeIds (o2Id x 2) into one - makes sense in terms of File/Folder to avoid duplicate names
        response = getAll(getNodeChildrenUrl(f1Id), paging, 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(o1Id));
        assertTrue(nodeIds.contains(o2Id));
        params = new HashMap<>(1);
        params.put("where", "(isPrimary=true)");
        response = getAll(getNodeChildrenUrl(f1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o1Id, nodes.get(0).getId());
        params = new HashMap<>(1);
        params.put("where", "(isPrimary=false)");
        // note: currently collapses same nodeIds (o2Id x 2) into one - makes sense in terms of File/Folder to avoid duplicate names
        response = getAll(getNodeChildrenUrl(f1Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(o2Id, nodes.get(0).getId());
        // test list filter - isPrimary (/parents)
        response = getAll(getNodeParentsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(3, nodes.size());
        params = new HashMap<>(1);
        params.put("where", "(isPrimary=true)");
        response = getAll(getNodeParentsUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f2Id, nodes.get(0).getId());
        params = new HashMap<>(1);
        params.put("where", "(isPrimary=false)");
        response = getAll(getNodeParentsUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        i = 0;
        for (Node node : nodes) {
            String nodeId = node.getId();
            Association nodeAssoc = node.getAssociation();
            if (nodeId.equals(f1Id)) {
                if (nodeAssoc.getAssocType().equals(ASSOC_TYPE_CM_CONTAINS)) {
                    i++;
                } else if (nodeAssoc.getAssocType().equals(ASSOC_TYPE_CM_PREFERENCE_IMAGE)) {
                    i++;
                }
                assertFalse(nodeAssoc.getIsPrimary());
            }
        }
        assertEquals(2, i);
        // combined filter
        params = new HashMap<>(1);
        params.put("where", "(isPrimary=false and assocType='" + ASSOC_TYPE_CM_PREFERENCE_IMAGE + "')");
        response = getAll(getNodeParentsUrl(o2Id), paging, params, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        assertEquals(f1Id, nodes.get(0).getId());
        assertFalse(nodes.get(0).getAssociation().getIsPrimary());
        assertEquals(ASSOC_TYPE_CM_PREFERENCE_IMAGE, nodes.get(0).getAssociation().getAssocType());
        // remove one secondary child assoc
        params = new HashMap<>(1);
        params.put(PARAM_ASSOC_TYPE, ASSOC_TYPE_CM_CONTAINS);
        delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, params, 204);
        response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        response = getAll(getNodeParentsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(2, nodes.size());
        params = new HashMap<>(1);
        params.put(PARAM_ASSOC_TYPE, ASSOC_TYPE_CM_PREFERENCE_IMAGE);
        // remove other secondary child assoc
        delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, params, 204);
        response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(0, nodes.size());
        response = getAll(getNodeParentsUrl(o2Id), paging, null, 200);
        nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
        assertEquals(1, nodes.size());
        {
            // test removal of multiple secondary child assocs (if assoc type is not specified)
            response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(0, nodes.size());
            // re-create secondary child assoc
            secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_CONTAINS);
            post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 201);
            // re-create ano' secondary child assoc (different type) between the same two nodes
            secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_PREFERENCE_IMAGE);
            post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 201);
            response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(2, nodes.size());
            assertEquals(o2Id, nodes.get(0).getId());
            assertEquals(o2Id, nodes.get(1).getId());
            // now remove both secondary child assocs
            delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, null, 204);
            response = getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(0, nodes.size());
        }
        {
            // sanity check paging of list of secondary children
            paging = getPaging(0, 100);
            response = getAll(getNodeSecondaryChildrenUrl(f3Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(0, nodes.size());
            int childCnt = 6;
            String[] childIds = new String[childCnt];
            for (int j = 0; j < childCnt; j++) {
                String childName = "child " + j;
                n = new Node();
                n.setName(childName);
                n.setNodeType(TYPE_CM_CONTENT);
                response = post(getNodeChildrenUrl(f2Id), toJsonAsStringNonNull(n), 201);
                childIds[j] = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
                secChild = new AssocChild(childIds[j], ASSOC_TYPE_CM_CONTAINS);
                post(getNodeSecondaryChildrenUrl(f3Id), toJsonAsStringNonNull(secChild), 201);
            }
            int skipCount = 0;
            int maxItems = 100;
            paging = getPaging(skipCount, maxItems);
            response = getAll(getNodeSecondaryChildrenUrl(f3Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(childCnt, nodes.size());
            PublicApiClient.ExpectedPaging expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
            assertEquals(childCnt, expectedPaging.getCount().intValue());
            assertEquals(skipCount, expectedPaging.getSkipCount().intValue());
            assertEquals(maxItems, expectedPaging.getMaxItems().intValue());
            assertFalse(expectedPaging.getHasMoreItems().booleanValue());
            skipCount = 1;
            maxItems = 3;
            paging = getPaging(skipCount, maxItems);
            response = getAll(getNodeSecondaryChildrenUrl(f3Id), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(maxItems, nodes.size());
            assertEquals(childIds[1], nodes.get(0).getId());
            assertEquals(childIds[2], nodes.get(1).getId());
            assertEquals(childIds[3], nodes.get(2).getId());
            expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
            assertEquals(maxItems, expectedPaging.getCount().intValue());
            assertEquals(skipCount, expectedPaging.getSkipCount().intValue());
            assertEquals(maxItems, expectedPaging.getMaxItems().intValue());
            assertTrue(expectedPaging.getHasMoreItems().booleanValue());
        }
        {
            // sanity check paging of list of parents
            String childName = "child with many parents";
            n = new Node();
            n.setName(childName);
            n.setNodeType(TYPE_CM_CONTENT);
            response = post(getNodeChildrenUrl(f4Id), toJsonAsStringNonNull(n), 201);
            String childId = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
            paging = getPaging(0, 100);
            response = getAll(getNodeParentsUrl(childId), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(1, nodes.size());
            assertEquals(f4Id, nodes.get(0).getId());
            int parentCnt = 5;
            String[] parentIds = new String[parentCnt];
            for (int j = 0; j < parentCnt; j++) {
                String parentName = "parent " + j;
                parentIds[j] = createFolder(f4Id, parentName).getId();
                secChild = new AssocChild(childId, ASSOC_TYPE_CM_CONTAINS);
                post(getNodeSecondaryChildrenUrl(parentIds[j]), toJsonAsStringNonNull(secChild), 201);
            }
            int skipCount = 0;
            int maxItems = 100;
            int expectedCnt = parentCnt + 1;
            paging = getPaging(skipCount, maxItems);
            response = getAll(getNodeParentsUrl(childId), paging, null, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(expectedCnt, nodes.size());
            PublicApiClient.ExpectedPaging expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
            assertEquals(expectedCnt, expectedPaging.getCount().intValue());
            assertEquals(skipCount, expectedPaging.getSkipCount().intValue());
            assertEquals(maxItems, expectedPaging.getMaxItems().intValue());
            assertFalse(expectedPaging.getHasMoreItems().booleanValue());
            params = new HashMap<>(1);
            params.put("where", "(isPrimary=false)");
            // TBC - order is currently undefined
            List<String> expectedIds = new ArrayList<>(5);
            expectedIds.addAll(Arrays.asList(parentIds));
            skipCount = 0;
            maxItems = 2;
            paging = getPaging(skipCount, maxItems);
            response = getAll(getNodeParentsUrl(childId), paging, params, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(maxItems, nodes.size());
            for (Node node : nodes) {
                expectedIds.remove(node.getId());
            }
            assertEquals(3, expectedIds.size());
            expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
            assertEquals(maxItems, expectedPaging.getCount().intValue());
            assertEquals(skipCount, expectedPaging.getSkipCount().intValue());
            assertEquals(maxItems, expectedPaging.getMaxItems().intValue());
            assertTrue(expectedPaging.getHasMoreItems().booleanValue());
            skipCount = 2;
            maxItems = 2;
            paging = getPaging(skipCount, maxItems);
            response = getAll(getNodeParentsUrl(childId), paging, params, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(maxItems, nodes.size());
            for (Node node : nodes) {
                expectedIds.remove(node.getId());
            }
            assertEquals(1, expectedIds.size());
            expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
            assertEquals(maxItems, expectedPaging.getCount().intValue());
            assertEquals(skipCount, expectedPaging.getSkipCount().intValue());
            assertEquals(maxItems, expectedPaging.getMaxItems().intValue());
            assertTrue(expectedPaging.getHasMoreItems().booleanValue());
            skipCount = 4;
            maxItems = 2;
            paging = getPaging(skipCount, maxItems);
            response = getAll(getNodeParentsUrl(childId), paging, params, 200);
            nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
            assertEquals(1, nodes.size());
            for (Node node : nodes) {
                expectedIds.remove(node.getId());
            }
            assertEquals(0, expectedIds.size());
            expectedPaging = RestApiUtil.parsePaging(response.getJsonResponse());
            assertEquals(1, expectedPaging.getCount().intValue());
            assertEquals(skipCount, expectedPaging.getSkipCount().intValue());
            assertEquals(maxItems, expectedPaging.getMaxItems().intValue());
            assertFalse(expectedPaging.getHasMoreItems().booleanValue());
        }
        // 
        // -ve tests - add assoc
        // 
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_CONTAINS);
            post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 401);
            setRequestContext(user1);
            // -ve test - model integrity
            secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_CONTAINS);
            post(getNodeSecondaryChildrenUrl(o1Id), toJsonAsStringNonNull(secChild), 422);
            // -ve test - duplicate assoc
            secChild = new AssocChild(o2Id, ASSOC_TYPE_CM_CONTAINS);
            post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 201);
            post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 409);
            // cleanup
            delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, null, 204);
            secChild = new AssocChild(o2Id, "cm:unknowntype");
            post(getNodeSecondaryChildrenUrl(f1Id), toJsonAsStringNonNull(secChild), 400);
        }
        // 
        // -ve test - list assocs
        // 
        {
            setRequestContext(null);
            // -ve test - unauthenticated - belts-and-braces ;-)
            getAll(getNodeSecondaryChildrenUrl(f1Id), paging, null, 401);
            getAll(getNodeParentsUrl(o2Id), paging, null, 401);
            setRequestContext(user1);
            getAll(getNodeSecondaryChildrenUrl(UUID.randomUUID().toString()), paging, null, 404);
            getAll(getNodeParentsUrl(UUID.randomUUID().toString()), paging, null, 404);
            params = new HashMap<>(1);
            params.put("where", "(assocType='cm:unknownassoctype')");
            getAll(getNodeSecondaryChildrenUrl(o1Id), paging, params, 400);
            getAll(getNodeParentsUrl(o1Id), paging, params, 400);
        }
        // 
        // -ve test - remove assoc(s)
        // 
        {
            setRequestContext(null);
            // unauthenticated - belts-and-braces ;-)
            delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, null, 401);
            setRequestContext(user1);
            delete(getNodeSecondaryChildrenUrl(UUID.randomUUID().toString()), o2Id, null, 404);
            delete(getNodeSecondaryChildrenUrl(f1Id), UUID.randomUUID().toString(), null, 404);
            // nothing to remove - for any assoc type
            delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, null, 404);
            // nothing to remove - for given assoc type
            params = new HashMap<>(1);
            params.put(PARAM_ASSOC_TYPE, ASSOC_TYPE_CM_PREFERENCE_IMAGE);
            delete(getNodeSecondaryChildrenUrl(f1Id), o2Id, params, 404);
            // unknown assoc type
            params = new HashMap<>(1);
            params.put(PARAM_ASSOC_TYPE, "cm:unknowntype");
            delete(getNodeSecondaryChildrenUrl(o1Id), o2Id, params, 400);
            // do not allow delete of primary child (via secondary child removal)
            params = new HashMap<>(1);
            params.put(PARAM_ASSOC_TYPE, "cm:contains");
            delete(getNodeSecondaryChildrenUrl(f1Id), o1Id, params, 400);
        }
    } finally {
        // some cleanup
        setRequestContext(user1);
        deleteNode(f1Id, true, 204);
        deleteNode(f2Id, true, 204);
        deleteNode(f3Id, true, 204);
        deleteNode(f4Id, 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) AssocChild(org.alfresco.rest.api.model.AssocChild) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Association(org.alfresco.rest.api.tests.client.data.Association) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 3 with AssocChild

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

the class NodeAssociationsApiTest method testListChildrenConsistentParentIdWithSecondaryAssociations.

/**
 * Regardless of which parent is used to list children of a node, the node information
 * should consistently present the primary parent as a node's {@code parentId}.
 * <p>
 * See REPO-1780
 */
@Test
public void testListChildrenConsistentParentIdWithSecondaryAssociations() throws Exception {
    setRequestContext(user1);
    String myNodeId = getMyNodeId();
    // Create primary folder
    String primaryFolderName = "primary folder " + RUNID;
    String primaryFolderId = createFolder(myNodeId, primaryFolderName, null).getId();
    // Create content file
    String contentFileName = "content" + RUNID + " in folder";
    String contentId = createTextFile(primaryFolderId, contentFileName, "The quick brown fox jumps over the lazy dog.").getId();
    // Add a secondary parent for content file
    Node n = new Node();
    n.setName("secondary folder " + RUNID);
    n.setNodeType(TYPE_CM_FOLDER);
    n.setAspectNames(Arrays.asList(ASPECT_CM_PREFERENCES));
    HttpResponse response = post(getNodeChildrenUrl(myNodeId), toJsonAsStringNonNull(n), 201);
    String secondaryFolderId = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
    AssocChild secChild = new AssocChild(contentId, ASSOC_TYPE_CM_CONTAINS);
    post(getNodeSecondaryChildrenUrl(secondaryFolderId), toJsonAsStringNonNull(secChild), 201);
    Paging paging = getPaging(0, 100);
    // Order by folders and modified date first
    Map<String, String> orderBy = Collections.singletonMap("orderBy", "isFolder DESC,modifiedAt DESC");
    // Retrieve the node via the primary parent
    String primaryChildrenUrl = getNodeChildrenUrl(primaryFolderId);
    response = getAll(primaryChildrenUrl, paging, orderBy, 200);
    List<Document> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    Document node = nodes.get(0);
    assertEquals(contentId, node.getId());
    assertEquals(primaryFolderId, node.getParentId());
    // Retrieve the node via the secondary parent
    String secondaryChildrenUrl = getNodeChildrenUrl(secondaryFolderId);
    response = getAll(secondaryChildrenUrl, paging, orderBy, 200);
    nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Document.class);
    node = nodes.get(0);
    assertEquals(contentId, node.getId());
    // The parent ID must STILL be the primary parent, even when the info
    // is retrieved via the secondary parent.
    assertEquals(primaryFolderId, node.getParentId());
}
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) Document(org.alfresco.rest.api.tests.client.data.Document) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 4 with AssocChild

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

the class NodesImpl method addChildren.

public List<AssocChild> addChildren(String parentNodeId, List<AssocChild> entities) {
    NodeRef parentNodeRef = validateNode(parentNodeId);
    List<AssocChild> result = new ArrayList<>(entities.size());
    for (AssocChild assoc : entities) {
        String childId = assoc.getChildId();
        if (childId == null) {
            throw new InvalidArgumentException("Missing childId");
        }
        QName assocTypeQName = getAssocType(assoc.getAssocType());
        try {
            NodeRef childNodeRef = validateNode(childId);
            String nodeName = (String) nodeService.getProperty(childNodeRef, ContentModel.PROP_NAME);
            QName assocChildQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(nodeName));
            nodeService.addChild(parentNodeRef, childNodeRef, assocTypeQName, assocChildQName);
        } catch (AssociationExistsException aee) {
            throw new ConstraintViolatedException(aee.getMessage());
        } catch (DuplicateChildNodeNameException dcne) {
            throw new ConstraintViolatedException(dcne.getMessage());
        }
        result.add(assoc);
    }
    return result;
}
Also used : DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) AssocChild(org.alfresco.rest.api.model.AssocChild) ArrayList(java.util.ArrayList) AssociationExistsException(org.alfresco.service.cmr.repository.AssociationExistsException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 5 with AssocChild

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

the class AbstractNodeRelation method listNodeChildAssocs.

protected CollectionWithPagingInfo<Node> listNodeChildAssocs(List<ChildAssociationRef> childAssocRefs, Parameters parameters, Boolean isPrimary, boolean returnChild) {
    Map<QName, String> qnameMap = new HashMap<>(3);
    Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
    List<String> includeParam = parameters.getInclude();
    List<Node> collection = new ArrayList<Node>(childAssocRefs.size());
    for (ChildAssociationRef childAssocRef : childAssocRefs) {
        if (isPrimary == null || (isPrimary == childAssocRef.isPrimary())) {
            // minimal info by default (unless "include"d otherwise)
            NodeRef nodeRef = (returnChild ? childAssocRef.getChildRef() : childAssocRef.getParentRef());
            Node node = nodes.getFolderOrDocument(nodeRef, null, null, includeParam, mapUserInfo);
            QName assocTypeQName = childAssocRef.getTypeQName();
            if (!EXCLUDED_NS.contains(assocTypeQName.getNamespaceURI())) {
                String assocType = qnameMap.get(assocTypeQName);
                if (assocType == null) {
                    assocType = assocTypeQName.toPrefixString(namespaceService);
                    qnameMap.put(assocTypeQName, assocType);
                }
                node.setAssociation(new AssocChild(assocType, childAssocRef.isPrimary()));
                collection.add(node);
            }
        }
    }
    return listPage(collection, parameters.getPaging());
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Node(org.alfresco.rest.api.model.Node) ArrayList(java.util.ArrayList) AssocChild(org.alfresco.rest.api.model.AssocChild) UserInfo(org.alfresco.rest.api.model.UserInfo) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef)

Aggregations

AssocChild (org.alfresco.rest.api.model.AssocChild)9 ArrayList (java.util.ArrayList)4 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 QName (org.alfresco.service.namespace.QName)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 AssocTarget (org.alfresco.rest.api.model.AssocTarget)2 Node (org.alfresco.rest.api.model.Node)2 Document (org.alfresco.rest.api.tests.client.data.Document)2 Serializable (java.io.Serializable)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ActivityType (org.alfresco.repo.activities.ActivityType)1 FilterPropBoolean (org.alfresco.repo.node.getchildren.FilterPropBoolean)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1