Search in sources :

Example 36 with TransferTarget

use of org.alfresco.service.cmr.transfer.TransferTarget in project alfresco-repository by Alfresco.

the class TransferServiceToBeRefactoredTest method testReadOnlyFlag.

/**
 * Transfer with read only flag
 *
 * Node tree for this test
 * <pre>
 *           A (Folder)
 *   |                 |
 *   B (Content)   C (Folder)
 *                     |
 *                     D (Content)
 * </pre>
 * Step 1
 * transfer Nodes ABCD with read only flag set - content should all be locked on destination
 * <p>
 * Step 2
 * lock B (Content node) as user fred
 * transfer (read only) - destination lock should change to Admin
 * <p>
 * Step 3
 * lock C (Folder) as user fred
 * transfer (read only) - destination lock should change to Admin
 * <p>
 * Step 4
 * transfer without read only flag - locks should revert from Admin to Fred.
 * <p>
 * Step 5
 * remove locks on A and B - transfer without read only flag - content should all be unlocked.
 */
@Test
public void testReadOnlyFlag() throws Exception {
    final String CONTENT_TITLE = "ContentTitle";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "The quick brown fox";
    final Set<NodeRef> nodes = new HashSet<NodeRef>();
    final String USER_ONE = "TransferServiceImplTest";
    final String PASSWORD = "Password";
    final String targetName = "testReadOnlyFlag";
    class TestData {

        NodeRef nodeA;

        NodeRef nodeB;

        NodeRef nodeC;

        NodeRef nodeD;

        ChildAssociationRef child;

        @SuppressWarnings("unused")
        TransferTarget transferMe;
    }
    final TestData testData = new TestData();
    /**
     *  For unit test
     *  - replace the HTTP transport with the in-process transport
     *  - replace the node factory with one that will map node refs, paths etc.
     */
    TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(this.receiver, this.contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory);
    transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory);
    List<Pair<Path, Path>> pathMap = testNodeFactory.getPathMap();
    // Map company_home/guest_home to company_home so tranferred nodes and moved "up" one level.
    pathMap.add(new Pair<Path, Path>(PathHelper.stringToPath(GUEST_HOME_XPATH_QUERY), PathHelper.stringToPath(COMPANY_HOME_XPATH_QUERY)));
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node that we will read and write
             */
            String guid = GUID.generate();
            testData.child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(guid), ContentModel.TYPE_FOLDER);
            testData.nodeA = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeA, ContentModel.PROP_TITLE, guid);
            nodeService.setProperty(testData.nodeA, ContentModel.PROP_NAME, guid);
            nodes.add(testData.nodeA);
            testData.child = nodeService.createNode(testData.nodeA, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeB"), ContentModel.TYPE_CONTENT);
            testData.nodeB = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeB, ContentModel.PROP_TITLE, CONTENT_TITLE + "B");
            nodeService.setProperty(testData.nodeB, ContentModel.PROP_NAME, "DemoNodeB");
            {
                ContentWriter writer = contentService.getWriter(testData.nodeB, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
                nodes.add(testData.nodeB);
            }
            testData.child = nodeService.createNode(testData.nodeA, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeC"), ContentModel.TYPE_FOLDER);
            testData.nodeC = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeC, ContentModel.PROP_TITLE, "TestNodeC");
            nodeService.setProperty(testData.nodeC, ContentModel.PROP_NAME, "TestNodeC");
            nodes.add(testData.nodeC);
            testData.child = nodeService.createNode(testData.nodeC, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeD"), ContentModel.TYPE_CONTENT);
            testData.nodeD = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeD, ContentModel.PROP_TITLE, CONTENT_TITLE + "D");
            nodeService.setProperty(testData.nodeD, ContentModel.PROP_NAME, "DemoNodeD");
            {
                ContentWriter writer = contentService.getWriter(testData.nodeD, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
                nodes.add(testData.nodeD);
            }
            // Create users
            createUser(USER_ONE, USER_ONE, PASSWORD);
            /**
             * Now go ahead and create our first transfer target
             */
            if (!transferService.targetExists(targetName)) {
                testData.transferMe = createTransferTarget(targetName);
            } else {
                testData.transferMe = transferService.getTransferTarget(targetName);
            }
            return null;
        }
    });
    /**
     * Step 1.
     * transfer Nodes ABCD with read only flag set - content should all be locked on destination
     */
    logger.debug("transfer read only - step 1");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertTrue("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            return null;
        }
    });
    /**
     * Step 2
     * lock B (Content node) as user ONE
     * transfer (read only) - destination lock should change user to "Admin"
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            AuthenticationUtil.pushAuthentication();
            AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
            lockService.lock(testData.nodeB, LockType.READ_ONLY_LOCK);
            assertEquals("test error: dest node B lock ownership", nodeService.getProperty(testData.nodeB, ContentModel.PROP_LOCK_OWNER), USER_ONE);
            AuthenticationUtil.popAuthentication();
            return null;
        }
    });
    logger.debug("transfer read only - step 2");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertTrue("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            // check that the lock owner is no longer USER_ONE
            assertTrue("lock owner not changed", !USER_ONE.equalsIgnoreCase((String) nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.PROP_LOCK_OWNER)));
            return null;
        }
    });
    /**
     * Step 3
     * lock C (Folder node) as user ONE
     * transfer (read only) - destination lock should change to Admin
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            AuthenticationUtil.pushAuthentication();
            AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
            lockService.lock(testData.nodeC, LockType.READ_ONLY_LOCK);
            assertEquals("test error: dest node C lock ownership", nodeService.getProperty(testData.nodeC, ContentModel.PROP_LOCK_OWNER), USER_ONE);
            AuthenticationUtil.popAuthentication();
            return null;
        }
    });
    logger.debug("transfer read only - step 3");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertTrue("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            // check that the lock owner is no longer USER_ONE for content node B and folder node C
            assertTrue("lock owner not changed", !USER_ONE.equalsIgnoreCase((String) nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.PROP_LOCK_OWNER)));
            assertTrue("lock owner not changed", !USER_ONE.equalsIgnoreCase((String) nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.PROP_LOCK_OWNER)));
            return null;
        }
    });
    /**
     * Step 4
     * transfer without read only flag - locks should revert from Admin to USER_ONE.
     */
    logger.debug("transfer read only - step 4");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                // turn off read-only
                definition.setReadOnly(false);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are not locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertFalse("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            assertEquals("dest node B lock ownership", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.PROP_LOCK_OWNER), USER_ONE);
            assertEquals("dest node C lock ownership", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.PROP_LOCK_OWNER), USER_ONE);
            return null;
        }
    });
    /**
     * Step 5
     * remove locks on A and B - transfer without read only flag - content should all be unlocked.
     */
    logger.debug("transfer read only - step 5");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            lockService.unlock(testData.nodeB);
            lockService.unlock(testData.nodeC);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                // turn off read-only
                definition.setReadOnly(false);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are not locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertFalse("test fail: dest node B is still locked", nodeService.hasAspect(testData.nodeB, ContentModel.ASPECT_LOCKABLE));
            assertFalse("test fail: dest node C is still locked", nodeService.hasAspect(testData.nodeC, ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            return null;
        }
    });
}
Also used : Locale(java.util.Locale) Path(org.alfresco.service.cmr.repository.Path) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) DescriptorService(org.alfresco.service.descriptor.DescriptorService) HashSet(java.util.HashSet) Pair(org.alfresco.util.Pair) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 37 with TransferTarget

use of org.alfresco.service.cmr.transfer.TransferTarget in project alfresco-repository by Alfresco.

the class TransferServiceToBeRefactoredTest method testTransferWithPermissions.

/**
 * Test the transfer method with regard to permissions on a node.
 * <p>
 * Step 1:
 * Create a node with a single permission
 *     Inherit:false
 *     Read, Admin, Allow
 *     Transfer
 * <p>
 * Step 2:
 * Update it to have several permissions
 *     Inherit:false
 *     Read, Everyone, DENY
 *     Read, Admin, Allow
 * <p>
 * Step 3:
 * Remove a permission
 *     Inherit:false
 *     Read, Admin, Allow
 * <p>
 * Step 4:
 * Revert to inherit all permissions
 *     Inherit:true
 * <p>
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testTransferWithPermissions() throws Exception {
    final String CONTENT_TITLE = "ContentTitle";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello";
    /**
     * For unit test - replace the HTTP transport with the in-process transport - replace the node factory with one
     * that will map node refs, paths etc.
     */
    TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(receiver, contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory);
    transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory);
    List<Pair<Path, Path>> pathMap = testNodeFactory.getPathMap();
    // Map company_home/guest_home to company_home so tranferred nodes and moved "up" one level.
    pathMap.add(new Pair<Path, Path>(PathHelper.stringToPath(GUEST_HOME_XPATH_QUERY), PathHelper.stringToPath(COMPANY_HOME_XPATH_QUERY)));
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    /**
     * Now go ahead and create our transfer target
     */
    final String targetName = "testTransferWithPermissions";
    class TestData {

        TransferTarget transferMe;

        NodeRef contentNodeRef;

        NodeRef destNodeRef;
    }
    final TestData testData = new TestData();
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node that we will read and write
             */
            String name = GUID.generate();
            ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_CONTENT);
            testData.contentNodeRef = child.getChildRef();
            nodeService.setProperty(testData.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testData.contentNodeRef, ContentModel.PROP_NAME, name);
            ContentWriter writer = contentService.getWriter(testData.contentNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_STRING);
            permissionService.setInheritParentPermissions(testData.contentNodeRef, false);
            permissionService.setPermission(testData.contentNodeRef, "admin", PermissionService.READ, true);
            if (!transferService.targetExists(targetName)) {
                testData.transferMe = createTransferTarget(targetName);
            } else {
                testData.transferMe = transferService.getTransferTarget(targetName);
            }
            return null;
        }
    });
    /**
     * Step 1
     */
    logger.debug("First transfer - create new node with inheritParent permission off");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            assertFalse("unit test stuffed up - comparing with self", testData.destNodeRef.equals(testData.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testData.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testData.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE);
            assertEquals("type is wrong", nodeService.getType(testData.contentNodeRef), nodeService.getType(testData.destNodeRef));
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertFalse("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertFalse("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                logger.debug("checking permission :" + p);
                assertTrue("permission is missing", destPerm.contains(p));
            }
            return null;
        }
    });
    /**
     * Step 2
     * Update it to have several permissions
     *     Inherit:false
     *     Read, Everyone, DENY
     *     Read, Admin, Allow
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            permissionService.setPermission(testData.contentNodeRef, "EVERYONE", PermissionService.READ, false);
            permissionService.setPermission(testData.contentNodeRef, "admin", PermissionService.FULL_CONTROL, true);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertFalse("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertFalse("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                logger.debug("checking permission :" + p);
                assertTrue("Step2, permission is missing", destPerm.contains(p));
            }
            return null;
        }
    });
    /**
     * Step 3 Remove a permission
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            permissionService.deletePermission(testData.contentNodeRef, "admin", PermissionService.FULL_CONTROL);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertFalse("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertFalse("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                logger.debug("checking permission :" + p);
                assertTrue("permission is missing", destPerm.contains(p));
            }
            return null;
        }
    });
    /**
     * Step 4 Revert to inherit all permissions
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            permissionService.setInheritParentPermissions(testData.contentNodeRef, true);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            assertFalse("unit test stuffed up - comparing with self", testData.destNodeRef.equals(testData.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testData.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testData.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE);
            assertEquals("type is wrong", nodeService.getType(testData.contentNodeRef), nodeService.getType(testData.destNodeRef));
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertTrue("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertTrue("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                if (p.isSetDirectly()) {
                    logger.debug("checking permission :" + p);
                    assertTrue("permission is missing:" + p, destPerm.contains(p));
                }
            }
            return null;
        }
    });
}
Also used : Locale(java.util.Locale) Path(org.alfresco.service.cmr.repository.Path) ResultSet(org.alfresco.service.cmr.search.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) DescriptorService(org.alfresco.service.descriptor.DescriptorService) Pair(org.alfresco.util.Pair) HashSet(java.util.HashSet) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 38 with TransferTarget

use of org.alfresco.service.cmr.transfer.TransferTarget in project alfresco-repository by Alfresco.

the class TransferServiceToBeRefactoredTest method createTransferTarget.

// test multi repo sync
// Utility methods below.
private TransferTarget createTransferTarget(String name) {
    String title = "title";
    String description = "description";
    String endpointProtocol = "http";
    String endpointHost = "MARKR02";
    int endpointPort = 7080;
    String endpointPath = "/alfresco/service/api/transfer";
    String username = "admin";
    char[] password = "admin".toCharArray();
    /**
     * Now go ahead and create our first transfer target
     */
    TransferTarget target = transferService.createAndSaveTransferTarget(name, title, description, endpointProtocol, endpointHost, endpointPort, endpointPath, username, password);
    return target;
}
Also used : TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget)

Example 39 with TransferTarget

use of org.alfresco.service.cmr.transfer.TransferTarget in project alfresco-repository by Alfresco.

the class ManifestIntegrationTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    // Snapshot a transfer node
    String CONTENT_STRING = "hello world";
    Locale CONTENT_LOCALE = Locale.TAIWAN;
    String CONTENT_TITLE = "the title";
    // nasty name for XML
    String CONTENT_NAME = "&the name <\\*";
    String CONTENT_ASSOC_NAME = "&hell+-1we";
    String snapshotMe = "snapshotMe";
    String title = "title";
    String description = "description";
    String endpointProtocol = "http";
    String endpointHost = "localhost";
    int endpointPort = 8080;
    String endpointPath = "rhubarb";
    String username = "admin";
    char[] password = "password".toCharArray();
    Map<NodeRef, TransferManifestNode> sentNodes = new HashMap<NodeRef, TransferManifestNode>();
    TransferManifestNodeFactoryImpl nodeFactory = new TransferManifestNodeFactoryImpl();
    nodeFactory.setNodeService(nodeService);
    nodeFactory.setPermissionService(permissionService);
    nodeFactory.setDictionaryService(dictionaryService);
    nodeFactory.setMlAwareNodeService(mlAwareNodeService);
    /**
     * Create our transfer target
     */
    TransferTarget target = transferService.createAndSaveTransferTarget(snapshotMe, title, description, endpointProtocol, endpointHost, endpointPort, endpointPath, username, password);
    File snapshotFile = null;
    try {
        /**
         * Create a test node that we will read and write
         */
        ChildAssociationRef child = nodeService.createNode(target.getNodeRef(), ContentModel.ASSOC_CONTAINS, QName.createQName(CONTENT_ASSOC_NAME), ContentModel.TYPE_CONTENT);
        NodeRef childNodeRef = child.getChildRef();
        ContentWriter writer = contentService.getWriter(childNodeRef, ContentModel.PROP_CONTENT, true);
        writer.setLocale(CONTENT_LOCALE);
        writer.putContent(CONTENT_STRING);
        nodeService.setProperty(childNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
        nodeService.setProperty(childNodeRef, ContentModel.PROP_NAME, CONTENT_NAME);
        snapshotFile = TempFileProvider.createTempFile("xxx", ".xml");
        Writer snapshotWriter = new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8");
        Set<NodeRef> nodes = new HashSet<NodeRef>();
        /**
         * Write three nodes
         * a: the root node of the workspace store
         * b: the target node
         * c: child of the target node
         */
        nodes.add(nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE));
        nodes.add(target.getNodeRef());
        nodes.add(childNodeRef);
        TransferManifestWriter formatter = new XMLTransferManifestWriter();
        TransferManifestHeader header = new TransferManifestHeader();
        header.setNodeCount(nodes.size());
        header.setCreatedDate(new Date());
        formatter.startTransferManifest(snapshotWriter);
        formatter.writeTransferManifestHeader(header);
        for (NodeRef nodeRef : nodes) {
            TransferManifestNode node = nodeFactory.createTransferManifestNode(nodeRef, null, new TransferContext());
            formatter.writeTransferManifestNode(node);
            sentNodes.put(nodeRef, node);
        }
        formatter.endTransferManifest();
        snapshotWriter.close();
        // Show the snapshot file (For dev purposes)
        outputFile(snapshotFile);
        /**
         * Now read the snapshot file
         */
        TestTransferManifestProcessor processor = new TestTransferManifestProcessor();
        XMLTransferManifestReader reader = new XMLTransferManifestReader(processor);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        parser.parse(snapshotFile, reader);
        /**
         * Now validate that we read back what we write out
         */
        assertEquals("did not get back the same number of nodes", nodes.size(), processor.getNodes().size());
        assertNotNull("header is null", processor.getHeader());
        for (NodeRef nodeId : nodes) {
            System.out.println("Processing node:" + nodeId);
            TransferManifestNormalNode readNode = (TransferManifestNormalNode) processor.getNodes().get(nodeId);
            TransferManifestNormalNode writeNode = (TransferManifestNormalNode) sentNodes.get(nodeId);
            assertNotNull("readNode is null", readNode);
            assertNotNull("writeNode is null", writeNode);
            assertEquals("type is different", writeNode.getType(), readNode.getType());
            assertEquals("nodeRef is different", writeNode.getNodeRef(), readNode.getNodeRef());
            assertEquals("parent node ref is different", writeNode.getPrimaryParentAssoc(), readNode.getPrimaryParentAssoc());
            if (writeNode.getParentPath() != null) {
                assertEquals("parent path is different", writeNode.getParentPath().toString(), readNode.getParentPath().toString());
            }
            assertEquals("aspects array different size", writeNode.getAspects().size(), readNode.getAspects().size());
            for (QName aspect : writeNode.getAspects()) {
                assertTrue("missing aspect", readNode.getAspects().contains(aspect));
            }
            assertEquals("properties array different size", writeNode.getProperties().size(), readNode.getProperties().size());
            for (QName prop : writeNode.getProperties().keySet()) {
                assertTrue("missing property", readNode.getProperties().containsKey(prop));
            }
            assertEquals("child assocs different", writeNode.getChildAssocs().size(), readNode.getChildAssocs().size());
            assertEquals("parent assocs different", writeNode.getParentAssocs().size(), readNode.getParentAssocs().size());
            assertEquals("source assocs different", writeNode.getSourceAssocs().size(), readNode.getSourceAssocs().size());
            assertEquals("target assocs different", writeNode.getTargetAssocs().size(), readNode.getTargetAssocs().size());
            if (readNode.getNodeRef().equals(childNodeRef)) {
                /**
                 * Check the child node since we created it at the start of this test this test
                 */
                ContentData data = (ContentData) readNode.getProperties().get(ContentModel.PROP_CONTENT);
                assertEquals("content data wrong size", data.getSize(), CONTENT_STRING.length());
                assertEquals("content locale wrong", data.getLocale(), CONTENT_LOCALE);
                String childTitle = ((MLText) readNode.getProperties().get(ContentModel.PROP_TITLE)).getDefaultValue();
                assertEquals("content title wrong", childTitle, CONTENT_TITLE);
                String childName = (String) readNode.getProperties().get(ContentModel.PROP_NAME);
                assertEquals("content name wrong", childName, CONTENT_NAME);
                /**
                 * Check the parent associations, there should be only one primary
                 */
                assertTrue("one parent assoc", readNode.getParentAssocs().size() == 1);
                assertTrue("isPrimary", readNode.getParentAssocs().get(0).isPrimary());
                assertEquals("parent q name", readNode.getParentAssocs().get(0).getQName(), QName.createQName(CONTENT_ASSOC_NAME));
                assertEquals("parent type q name", readNode.getParentAssocs().get(0).getTypeQName(), ContentModel.ASSOC_CONTAINS);
                assertEquals("child node ref", readNode.getParentAssocs().get(0).getChildRef(), childNodeRef);
                assertEquals("parent node ref", readNode.getParentAssocs().get(0).getParentRef(), target.getNodeRef());
                assertTrue("zero child assoc", readNode.getChildAssocs().size() == 0);
                /**
                 * Test Node Helper
                 */
                assertEquals(readNode.getParentAssocs().get(0), TransferManifestNodeHelper.getPrimaryParentAssoc(readNode));
                Set<ContentData> content = TransferManifestNodeHelper.getContentData(readNode);
                assertEquals("content not found", content.size(), 1);
            }
        }
    } finally {
        if (snapshotFile != null) {
            snapshotFile.delete();
        }
        transferService.deleteTransferTarget(snapshotMe);
    }
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) TransferContext(org.alfresco.repo.transfer.TransferContext) MLText(org.alfresco.service.cmr.repository.MLText) SAXParser(javax.xml.parsers.SAXParser) HashSet(java.util.HashSet) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Date(java.util.Date) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) SAXParserFactory(javax.xml.parsers.SAXParserFactory) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 40 with TransferTarget

use of org.alfresco.service.cmr.transfer.TransferTarget in project alfresco-repository by Alfresco.

the class ScriptTransferServiceTest method testJSAPI.

// == Test the JavaScript API ==
@Test
public void testJSAPI() throws Exception {
    /**
     * Prepare some dummy data for tests
     */
    TransferTargetImpl dummyTarget = new TransferTargetImpl();
    dummyTarget.setName("dummyTarget");
    dummyTarget.setNodeRef(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "4"));
    Set<TransferTarget> dummyTargets = new HashSet<TransferTarget>();
    dummyTargets.add(dummyTarget);
    TransferService mockedTransferService = Mockito.mock(TransferService.class);
    scriptTransferService.setTransferService(mockedTransferService);
    // When the transfer method is called return a node ref - mocks a good call.
    // When the transfer method is called with a transfer name of exception - throw a transferException
    Mockito.when(mockedTransferService.transfer(Matchers.anyString(), Matchers.isA(TransferDefinition.class))).thenReturn(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "123"));
    Mockito.when(mockedTransferService.transfer(Matchers.eq("exception"), Matchers.isA(TransferDefinition.class))).thenThrow(new TransferException("mocked transfer exception"));
    // When getTransferTarget called return a TransferTarget
    Mockito.when(mockedTransferService.getTransferTarget(Matchers.anyString())).thenReturn(dummyTarget);
    Mockito.when(mockedTransferService.getTransferTargets(Matchers.anyString())).thenReturn(dummyTargets);
    Mockito.when(mockedTransferService.getTransferTargets()).thenReturn(dummyTargets);
    // Execute the unit test script
    Map<String, Object> model = new HashMap<String, Object>(1);
    ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/transfer/script/test_transferService.js");
    this.scriptService.executeScript(location, model);
}
Also used : HashMap(java.util.HashMap) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ScriptLocation(org.alfresco.service.cmr.repository.ScriptLocation) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) TransferException(org.alfresco.service.cmr.transfer.TransferException) ClasspathScriptLocation(org.alfresco.repo.jscript.ClasspathScriptLocation) TransferService(org.alfresco.service.cmr.transfer.TransferService) TransferTargetImpl(org.alfresco.repo.transfer.TransferTargetImpl) HashSet(java.util.HashSet) Test(org.junit.Test) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest)

Aggregations

TransferTarget (org.alfresco.service.cmr.transfer.TransferTarget)48 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)30 Test (org.junit.Test)30 NodeRef (org.alfresco.service.cmr.repository.NodeRef)29 TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)26 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)25 HashSet (java.util.HashSet)22 Locale (java.util.Locale)18 DescriptorService (org.alfresco.service.descriptor.DescriptorService)16 Pair (org.alfresco.util.Pair)14 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)13 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)12 TransferException (org.alfresco.service.cmr.transfer.TransferException)12 ArrayList (java.util.ArrayList)10 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)10 IOException (java.io.IOException)8 Path (org.alfresco.service.cmr.repository.Path)8 List (java.util.List)7 QName (org.alfresco.service.namespace.QName)7 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)7