Search in sources :

Example 1 with NodeHierarchyWalker

use of org.alfresco.repo.node.db.NodeHierarchyWalker in project alfresco-repository by Alfresco.

the class NodeServiceTest method testNodeHierarchyWalker.

/**
 * @see NodeHierarchyWalker
 */
@Test
public void testNodeHierarchyWalker() throws Exception {
    final NodeRef workspaceRootNodeRef = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
    final NodeRef[] nodes = new NodeRef[6];
    buildNodeHierarchy(workspaceRootNodeRef, nodes);
    // Hook up some associations
    nodeService.addAspect(nodes[1], ContentModel.ASPECT_COPIEDFROM, null);
    // Peer n1-n0
    nodeService.createAssociation(nodes[1], nodes[0], ContentModel.ASSOC_ORIGINAL);
    // Secondary child n0-n2
    nodeService.addChild(nodes[0], nodes[2], ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.ALFRESCO_URI, "testNodeHierarchyWalker"));
    // Walk the hierarchy
    NodeHierarchyWalker walker = txnService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeHierarchyWalker>() {

        @Override
        public NodeHierarchyWalker execute() throws Throwable {
            Pair<Long, NodeRef> parentNodePair = nodeDAO.getNodePair(nodes[0]);
            Pair<Long, ChildAssociationRef> parentAssocPair = nodeDAO.getPrimaryParentAssoc(parentNodePair.getFirst());
            NodeHierarchyWalker walker = new NodeHierarchyWalker(nodeDAO);
            walker.walkHierarchy(parentNodePair, parentAssocPair);
            return walker;
        }
    }, true);
    List<VisitedNode> nodesLeafFirst = walker.getNodes(true);
    assertEquals("Unexpected number of nodes visited", 6, nodesLeafFirst.size());
    assertEquals("Incorrect order ", nodesLeafFirst.get(0).nodeRef, nodes[5]);
    assertEquals("Incorrect order ", nodesLeafFirst.get(5).nodeRef, nodes[0]);
    List<VisitedNode> nodesParentFirst = walker.getNodes(false);
    assertEquals("Unexpected number of nodes visited", 6, nodesParentFirst.size());
    assertEquals("Incorrect order ", nodesParentFirst.get(0).nodeRef, nodes[0]);
    assertEquals("Incorrect order ", nodesParentFirst.get(5).nodeRef, nodes[5]);
    // Check primary parent links
    assertEquals(workspaceRootNodeRef, nodesParentFirst.get(0).primaryParentAssocPair.getSecond().getParentRef());
    assertEquals(nodes[0], nodesParentFirst.get(1).primaryParentAssocPair.getSecond().getParentRef());
    assertEquals(nodes[4], nodesParentFirst.get(5).primaryParentAssocPair.getSecond().getParentRef());
    // Check secondary parent links
    assertEquals(0, nodesParentFirst.get(0).secondaryParentAssocs.size());
    assertEquals(nodes[0], nodesParentFirst.get(2).secondaryParentAssocs.get(0).getSecond().getParentRef());
    assertEquals(0, nodesParentFirst.get(1).secondaryParentAssocs.size());
    assertEquals(1, nodesParentFirst.get(2).secondaryParentAssocs.size());
    assertEquals(0, nodesParentFirst.get(3).secondaryParentAssocs.size());
    // Check secondary child links
    assertEquals(1, nodesParentFirst.get(0).secondaryChildAssocs.size());
    assertEquals(nodes[2], nodesParentFirst.get(0).secondaryChildAssocs.get(0).getSecond().getChildRef());
    assertEquals(0, nodesParentFirst.get(1).secondaryChildAssocs.size());
    // Check target assocs
    assertEquals(0, nodesParentFirst.get(0).targetAssocs.size());
    assertEquals(1, nodesParentFirst.get(1).targetAssocs.size());
    assertEquals(nodes[0], nodesParentFirst.get(1).targetAssocs.get(0).getSecond().getTargetRef());
    // Check source assocs
    assertEquals(1, nodesParentFirst.get(0).sourceAssocs.size());
    assertEquals(nodes[1], nodesParentFirst.get(0).sourceAssocs.get(0).getSecond().getSourceRef());
    assertEquals(0, nodesParentFirst.get(1).sourceAssocs.size());
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) NodeHierarchyWalker(org.alfresco.repo.node.db.NodeHierarchyWalker) VisitedNode(org.alfresco.repo.node.db.NodeHierarchyWalker.VisitedNode) Pair(org.alfresco.util.Pair) CannedQueryDAOTest(org.alfresco.repo.domain.query.CannedQueryDAOTest) Test(org.junit.Test)

Aggregations

CannedQueryDAOTest (org.alfresco.repo.domain.query.CannedQueryDAOTest)1 NodeHierarchyWalker (org.alfresco.repo.node.db.NodeHierarchyWalker)1 VisitedNode (org.alfresco.repo.node.db.NodeHierarchyWalker.VisitedNode)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 Pair (org.alfresco.util.Pair)1 Test (org.junit.Test)1