Search in sources :

Example 11 with TransferTarget

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

the class TransferServiceImplTest method testTransferOneNode.

/**
 * Test the transfer method by sending one node (CRUD).
 *
 * Step 1: Create a new node (No content)
 * transfer
 *
 * Step 2: Update Node title property
 * transfer
 *
 * Step 3: Update Content property (add content)
 * transfer
 *
 * Step 4: Transfer again
 * transfer (Should transfer but not request the content item)
 *
 * Step 5: Update Content property (update content)
 *
 * Step 6: Delete the node
 *
 * Step 7: Negative test : transfer no nodes
 * transfer (should throw exception)
 *
 * Step 8: Negative test : transfer to a disabled transfer target
 * transfer (should throw exception)
 *
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testTransferOneNode() throws Exception {
    final String CONTENT_TITLE = "ContentTitle";
    final String CONTENT_TITLE_UPDATED = "ContentTitleUpdated";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello World";
    final String CONTENT_UPDATE_STRING = "Foo Bar";
    final String targetName = "testXferOneNode";
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    class TestContext {

        TransferTarget transferMe;

        NodeRef contentNodeRef;

        NodeRef destNodeRef;
    }
    ;
    /**
     * Unit test kludge to transfer from guest home to company home
     */
    final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {

        @Override
        public TestContext execute() throws Throwable {
            TestContext ctx = new TestContext();
            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);
            ctx.contentNodeRef = child.getChildRef();
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_NAME, name);
            if (!transferService.targetExists(targetName)) {
                ctx.transferMe = createTransferTarget(targetName);
            } else {
                ctx.transferMe = transferService.getTransferTarget(targetName);
            }
            transferService.enableTransferTarget(targetName, true);
            return ctx;
        }
    };
    final TestContext testContext = tran.doInTransaction(setupCB);
    /**
     * Step 1: Transfer our node which has no content
     */
    logger.debug("First transfer - create new node (no content yet)");
    RetryingTransactionCallback<Void> transferCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodes = new HashSet<NodeRef>();
            nodes.add(testContext.contentNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(transferCB);
    RetryingTransactionCallback<Void> validateStep1CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists and has similar properties to the source
            testContext.destNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
            assertFalse("unit test stuffed up - comparing with self", testContext.destNodeRef.equals(testContext.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testContext.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE);
            assertEquals("type is wrong", nodeService.getType(testContext.contentNodeRef), nodeService.getType(testContext.destNodeRef));
            // Check the modified time of the destination node is the same as the source node.
            Date destModifiedDate = (Date) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_MODIFIED);
            Date srcModifiedDate = (Date) nodeService.getProperty(testContext.contentNodeRef, ContentModel.PROP_MODIFIED);
            logger.debug("srcModifiedDate : " + srcModifiedDate + " destModifiedDate : " + destModifiedDate);
            assertTrue("dest modified date is not correct", destModifiedDate.compareTo(srcModifiedDate) == 0);
            Date destCreatedDate = (Date) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_CREATED);
            Date srcCreatedDate = (Date) nodeService.getProperty(testContext.contentNodeRef, ContentModel.PROP_CREATED);
            logger.debug("srcCreatedDate : " + srcCreatedDate + " destCreatedDate : " + destCreatedDate);
            assertTrue("dest created date is not correct", destCreatedDate.compareTo(srcCreatedDate) == 0);
            // Check injected transferred aspect.
            assertNotNull("transferredAspect", (String) nodeService.getProperty(testContext.destNodeRef, TransferModel.PROP_REPOSITORY_ID));
            // Now set up the next test which is to change the title
            nodeService.setProperty(testContext.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE_UPDATED);
            return null;
        }
    };
    tran.doInTransaction(validateStep1CB);
    /**
     * Step 2:
     * Transfer our node again - so this is an update of the title property
     */
    logger.debug("Second transfer - update title property (no content yet)");
    tran.doInTransaction(transferCB);
    RetryingTransactionCallback<Void> validateStep2CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists and has similar properties to the source
            assertFalse("unit test stuffed up - comparing with self", testContext.destNodeRef.equals(testContext.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testContext.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE_UPDATED);
            assertEquals("type is wrong", nodeService.getType(testContext.contentNodeRef), nodeService.getType(testContext.destNodeRef));
            // Check the modified time of the destination node is the same as the source node.
            Date destModifiedDate = (Date) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_MODIFIED);
            Date srcModifiedDate = (Date) nodeService.getProperty(testContext.contentNodeRef, ContentModel.PROP_MODIFIED);
            logger.debug("srcModifiedDate : " + srcModifiedDate + " destModifiedDate : " + destModifiedDate);
            assertTrue("after update, modified date is not correct", destModifiedDate.compareTo(srcModifiedDate) == 0);
            Date destCreatedDate = (Date) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_CREATED);
            Date srcCreatedDate = (Date) nodeService.getProperty(testContext.contentNodeRef, ContentModel.PROP_CREATED);
            logger.debug("srcCreatedDate : " + srcCreatedDate + " destCreatedDate : " + destCreatedDate);
            assertTrue("after update, created date is not correct", destCreatedDate.compareTo(srcCreatedDate) == 0);
            // Check injected transferred aspect.
            assertNotNull("transferredAspect", (String) nodeService.getProperty(testContext.destNodeRef, TransferModel.PROP_REPOSITORY_ID));
            return null;
        }
    };
    tran.doInTransaction(validateStep2CB);
    /**
     * Step 3 - update to add content
     */
    RetryingTransactionCallback<Void> step3WriteContentCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentWriter writer = contentService.getWriter(testContext.contentNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_STRING);
            return null;
        }
    };
    tran.doInTransaction(step3WriteContentCB);
    logger.debug("Transfer again - this is an update to add new content");
    tran.doInTransaction(transferCB);
    RetryingTransactionCallback<Void> validateStep3CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testContext.destNodeRef, ContentModel.PROP_CONTENT);
            assertNotNull("reader is null", reader);
            String contentStr = reader.getContentString();
            assertEquals("Content is wrong", contentStr, CONTENT_STRING);
            return null;
        }
    };
    tran.doInTransaction(validateStep3CB);
    /**
     * Step 4:
     * Now transfer nothing - content items do not need to be transferred since its already on
     * the destination.
     */
    logger.debug("Transfer again - with no new content");
    tran.doInTransaction(transferCB);
    RetryingTransactionCallback<Void> validateStep4CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node still exists and in particular that the old content is still there
            assertFalse("unit test stuffed up - comparing with self", testContext.destNodeRef.equals(testContext.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testContext.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testContext.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE_UPDATED);
            assertEquals("type is wrong", nodeService.getType(testContext.contentNodeRef), nodeService.getType(testContext.destNodeRef));
            ContentReader reader = contentService.getReader(testContext.destNodeRef, ContentModel.PROP_CONTENT);
            assertNotNull("reader is null", reader);
            String contentStr = reader.getContentString();
            assertEquals("Content is wrong", contentStr, CONTENT_STRING);
            // Check injected transferred aspect.
            assertNotNull("transferredAspect", (String) nodeService.getProperty(testContext.destNodeRef, TransferModel.PROP_REPOSITORY_ID));
            return null;
        }
    };
    tran.doInTransaction(validateStep4CB);
    /**
     * Step 5 - update content through transfer
     */
    RetryingTransactionCallback<Void> step5UpdateContentCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentWriter writer = contentService.getWriter(testContext.contentNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_UPDATE_STRING);
            return null;
        }
    };
    tran.doInTransaction(step5UpdateContentCB);
    logger.debug("Transfer again - this is an update to add new content");
    tran.doInTransaction(transferCB);
    RetryingTransactionCallback<Void> validateStep5CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testContext.destNodeRef, ContentModel.PROP_CONTENT);
            assertNotNull("reader is null", reader);
            String contentStr = reader.getContentString();
            assertEquals("Content is wrong", CONTENT_UPDATE_STRING, contentStr);
            return null;
        }
    };
    tran.doInTransaction(validateStep5CB);
    /**
     * Step 6
     * Delete the node through transfer of the archive node
     */
    logger.debug("Transfer again - to delete a node through transferring an archive node");
    RetryingTransactionCallback<Void> step6CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            nodeService.deleteNode(testContext.contentNodeRef);
            return null;
        }
    };
    tran.doInTransaction(step6CB);
    RetryingTransactionCallback<Void> transferDeletedCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef deletedContentNodeRef = new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, testContext.contentNodeRef.getId());
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodesToRemove = new HashSet<NodeRef>();
            nodesToRemove.add(deletedContentNodeRef);
            definition.setNodesToRemove(nodesToRemove);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(transferDeletedCB);
    RetryingTransactionCallback<Void> validateStep6CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            assertFalse("dest node still exists", nodeService.exists(testContext.destNodeRef));
            return null;
        }
    };
    tran.doInTransaction(validateStep6CB);
    /**
     * Step 7
     * Negative test transfer nothing
     */
    logger.debug("Transfer again - with no content - should throw exception");
    try {
        TransferDefinition definition = new TransferDefinition();
        Set<NodeRef> nodes = new HashSet<NodeRef>();
        definition.setNodes(nodes);
        transferService.transfer(targetName, definition);
        fail("exception not thrown");
    } catch (TransferException te) {
    // expect to go here
    }
    /**
     * Step 7: Negative test : transfer to a disabled transfer target
     * transfer (should throw exception)
     */
    logger.debug("Transfer again - with no content - should throw exception");
    try {
        transferService.enableTransferTarget(targetName, false);
        TransferDefinition definition = new TransferDefinition();
        Set<NodeRef> nodes = new HashSet<NodeRef>();
        nodes.add(testContext.contentNodeRef);
        definition.setNodes(nodes);
        transferService.transfer(targetName, definition);
        fail("target not enabled exception not thrown");
    } catch (TransferException te) {
        // expect to go here
        assertTrue("check contents of exception message :" + te.toString(), te.getCause().getMessage().contains("enabled"));
    }
}
Also used : Locale(java.util.Locale) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ContentReader(org.alfresco.service.cmr.repository.ContentReader) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Date(java.util.Date) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) DescriptorService(org.alfresco.service.descriptor.DescriptorService) HashSet(java.util.HashSet) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 12 with TransferTarget

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

the class TransferServiceImplTest method testEnableTransferTarget.

@Test
public void testEnableTransferTarget() throws Exception {
    String targetName = "enableMe";
    /**
     * Now go ahead and create our first transfer target
     */
    TransferTarget enableMe = createTransferTarget(targetName);
    try {
        /**
         * Check a new target is enabled
         */
        TransferTarget target = transferService.getTransferTarget(targetName);
        assertTrue("new target is not enabled", enableMe.isEnabled());
        /**
         * Diasble the target
         */
        transferService.enableTransferTarget(targetName, false);
        target = transferService.getTransferTarget(targetName);
        assertFalse("target is not disabled", target.isEnabled());
        /**
         * Now re-enable the target
         */
        transferService.enableTransferTarget(targetName, true);
        target = transferService.getTransferTarget(targetName);
        assertTrue("re-enabled target is not enabled", target.isEnabled());
    } finally {
        transferService.deleteTransferTarget(targetName);
    }
}
Also used : TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 13 with TransferTarget

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

the class TransferServiceImplTest method testEmptyContent.

// test big content
/**
 * Test the transfer method with regard to an empty content property.  ALF-4865
 *
 * Step 1: create a node with an empty content property
 * transfer
 *
 * Step 2: add non empty content property
 * transfer
 *
 * Step 3: update from non empty content to empty content property
 * transfer
 *
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testEmptyContent() throws Exception {
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    final String CONTENT_TITLE = "ContentTitle";
    final String CONTENT_TITLE_UPDATED = "ContentTitleUpdated";
    final Locale CONTENT_LOCALE = Locale.ENGLISH;
    final String CONTENT_ENCODING = "UTF-8";
    final String CONTENT_STRING = "The quick brown fox jumps over the lazy dog.";
    final String targetName = "testTransferEmptyContent";
    /**
     *  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.
     *
     *  Fake Repository Id
     */
    class TestContext {

        TransferTarget transferMe;

        NodeRef contentNodeRef;

        NodeRef savedDestinationNodeRef;
    }
    ;
    /**
     * Unit test kludge to transfer from guest home to company home
     */
    final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    TransferTarget transferMe;
    RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {

        @Override
        public TestContext execute() throws Throwable {
            TestContext ctx = new TestContext();
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node with an empty content that we will read and write
             */
            String name = GUID.generate();
            ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_CONTENT);
            ctx.contentNodeRef = child.getChildRef();
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_NAME, name);
            ContentData cd = new ContentData(null, null, 0, null);
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_CONTENT, cd);
            if (!transferService.targetExists(targetName)) {
                ctx.transferMe = createTransferTarget(targetName);
            } else {
                ctx.transferMe = transferService.getTransferTarget(targetName);
            }
            transferService.enableTransferTarget(targetName, true);
            return ctx;
        }
    };
    final TestContext testContext = tran.doInTransaction(setupCB);
    final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    /**
     * Step 1: Transfer our node which has empty content
     */
    logger.debug("testEmptyContent : First transfer - create new node (empty content)");
    RetryingTransactionCallback<Void> step1CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testContext.contentNodeRef, ContentModel.PROP_CONTENT);
            assertNull("test setup content reader not null", reader);
            Map<QName, Serializable> props = nodeService.getProperties(testContext.contentNodeRef);
            assertTrue(props.containsKey(ContentModel.PROP_CONTENT));
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodes = new HashSet<NodeRef>();
            nodes.add(testContext.contentNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(step1CB);
    RetryingTransactionCallback<Void> validateStep1CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            Serializable modifiedDate = nodeService.getProperty(testContext.contentNodeRef, ContentModel.PROP_MODIFIED);
            if (modifiedDate instanceof Date) {
                logger.debug("srcModified: " + SDF.format(modifiedDate));
            }
            NodeRef destinationNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
            testContext.savedDestinationNodeRef = destinationNodeRef;
            assertTrue("content node (dest) does not exist", nodeService.exists(destinationNodeRef));
            ContentReader reader = contentService.getReader(destinationNodeRef, ContentModel.PROP_CONTENT);
            assertNull("content reader not null", reader);
            Map<QName, Serializable> props = nodeService.getProperties(destinationNodeRef);
            assertTrue(props.containsKey(ContentModel.PROP_CONTENT));
            return null;
        }
    };
    tran.doInTransaction(validateStep1CB);
    /**
     * Step 2: replace empty content with new content
     */
    logger.debug("testEmptyContent : Second transfer - replace empty content with some content");
    RetryingTransactionCallback<Void> step2CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            Serializable modifiedDate = nodeService.getProperty(testContext.contentNodeRef, ContentModel.PROP_MODIFIED);
            if (modifiedDate instanceof Date) {
                logger.debug("srcModified: " + SDF.format(modifiedDate));
            }
            ContentWriter writer = contentService.getWriter(testContext.contentNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.setEncoding(CONTENT_ENCODING);
            writer.putContent(CONTENT_STRING);
            return null;
        }
    };
    tran.doInTransaction(step2CB);
    RetryingTransactionCallback<Void> step2TransferCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testContext.contentNodeRef, ContentModel.PROP_CONTENT);
            assertNotNull("test setup content reader not null", reader);
            Map<QName, Serializable> props = nodeService.getProperties(testContext.contentNodeRef);
            assertTrue(props.containsKey(ContentModel.PROP_CONTENT));
            /**
             * Step 2: replace empty content with new content
             */
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodes = new HashSet<NodeRef>();
            nodes.add(testContext.contentNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(step2TransferCB);
    RetryingTransactionCallback<Void> step2ValidateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef destinationNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
            assertEquals("test error destinationNodeRef not correct", testContext.savedDestinationNodeRef, destinationNodeRef);
            ContentReader reader = contentService.getReader(destinationNodeRef, ContentModel.PROP_CONTENT);
            assertNotNull("content reader is null", reader);
            assertTrue("content encoding is wrong", reader.getEncoding().equalsIgnoreCase(CONTENT_ENCODING));
            assertEquals("content locale is wrong", reader.getLocale(), CONTENT_LOCALE);
            assertTrue("content does not exist", reader.exists());
            String contentStr = reader.getContentString();
            assertEquals("Content is wrong", contentStr, CONTENT_STRING);
            return null;
        }
    };
    tran.doInTransaction(step2ValidateCB);
    /**
     * Step 3 - transition from a content property having content to one that is empty
     */
    logger.debug("testEmptyContent : Third transfer - remove existing content");
    RetryingTransactionCallback<Void> step3SetupCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentData cd = new ContentData(null, null, 0, null);
            nodeService.setProperty(testContext.contentNodeRef, ContentModel.PROP_CONTENT, cd);
            return null;
        }
    };
    tran.doInTransaction(step3SetupCB);
    RetryingTransactionCallback<Void> step3TransferCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testContext.contentNodeRef, ContentModel.PROP_CONTENT);
            assertNull("test setup content reader not null", reader);
            Map<QName, Serializable> props = nodeService.getProperties(testContext.contentNodeRef);
            assertTrue(props.containsKey(ContentModel.PROP_CONTENT));
            /**
             * Step 3: Transfer our node which has empty content to over-write existing
             * content
             */
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodes = new HashSet<NodeRef>();
            nodes.add(testContext.contentNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(step3TransferCB);
    RetryingTransactionCallback<Void> step3ValidateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef destinationNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
            assertTrue("content node (dest) does not exist", nodeService.exists(destinationNodeRef));
            ContentReader reader = contentService.getReader(destinationNodeRef, ContentModel.PROP_CONTENT);
            assertNull("content reader not null", reader);
            Map<QName, Serializable> props = nodeService.getProperties(destinationNodeRef);
            assertTrue(props.containsKey(ContentModel.PROP_CONTENT));
            return null;
        }
    };
    tran.doInTransaction(step3ValidateCB);
}
Also used : Locale(java.util.Locale) Serializable(java.io.Serializable) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Date(java.util.Date) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentData(org.alfresco.service.cmr.repository.ContentData) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) DescriptorService(org.alfresco.service.descriptor.DescriptorService) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 14 with TransferTarget

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

the class TransferServiceImplTest method testCreateTargetSyntax2.

/**
 * Test create target via in memory data object.
 *
 * @throws Exception
 */
@Test
public void testCreateTargetSyntax2() throws Exception {
    String name = "Test Transfer Target " + GUID.generate();
    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();
    /**
     * Now go ahead and create our first transfer target
     */
    TransferTarget newValue = transferService.createTransferTarget(name);
    newValue.setDescription(description);
    newValue.setEndpointHost(endpointHost);
    newValue.setEndpointPort(endpointPort);
    newValue.setEndpointPath(endpointPath);
    newValue.setEndpointProtocol(endpointProtocol);
    newValue.setPassword(password);
    newValue.setTitle(title);
    newValue.setUsername(username);
    TransferTarget ret = transferService.saveTransferTarget(newValue);
    assertNotNull("return value is null", ret);
    assertNotNull("node ref is null", ret.getNodeRef());
    // titled aspect
    assertEquals("name not equal", ret.getName(), name);
    assertEquals("title not equal", ret.getTitle(), title);
    assertEquals("description not equal", ret.getDescription(), description);
    // endpoint
    assertEquals("endpointProtocol not equal", ret.getEndpointProtocol(), endpointProtocol);
    assertEquals("endpointHost not equal", ret.getEndpointHost(), endpointHost);
    assertEquals("endpointPort not equal", ret.getEndpointPort(), endpointPort);
    assertEquals("endpointPath not equal", ret.getEndpointPath(), endpointPath);
    // authentication
    assertEquals("username not equal", ret.getUsername(), username);
    char[] password2 = ret.getPassword();
    assertEquals(password.length, password2.length);
    for (int i = 0; i < password.length; i++) {
        if (password[i] != password2[i]) {
            fail("password not equal:" + new String(password) + new String(password2));
        }
    }
    /**
     * Negative test - try to create a transfer target with a name that's already used.
     */
    try {
        transferService.createAndSaveTransferTarget(name, title, description, endpointProtocol, endpointHost, endpointPort, endpointPath, username, password);
        fail("duplicate name not detected");
    } catch (TransferException e) {
    // expect to go here
    }
}
Also used : TransferException(org.alfresco.service.cmr.transfer.TransferException) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 15 with TransferTarget

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

the class TransferServiceImplTest method testReplaceNode.

// test repeat update content
/**
 * Test the transfer method with regard to replacing a node.  ALF-5109
 *
 * Step 1: Create a new parent node and child node
 * transfer
 *
 * Step 2: Delete the parent node
 * transfer
 *
 * Step 3: Create new parent child node with same names and assocs.
 * transfer
 *
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testReplaceNode() throws Exception {
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    final String CONTENT_TITLE = "ContentTitle";
    final String CONTENT_TITLE_UPDATED = "ContentTitleUpdated";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello World";
    final String CONTENT_UPDATE_STRING = "Foo Bar";
    /**
     *  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.
     *
     *  Fake Repository Id
     */
    TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(receiver, contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    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);
    final NodeRef guestHome = repositoryHelper.getGuestHome();
    final String targetName = "testRepeatUpdateOfContent";
    class TestContext {

        TransferTarget transferMe;

        NodeRef parentNodeRef;

        NodeRef middleNodeRef;

        NodeRef childNodeRef;

        QName parentName;

        QName middleName;

        QName childName;
    }
    ;
    RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {

        @Override
        public TestContext execute() throws Throwable {
            TestContext testContext = new TestContext();
            /**
             * Create a test node that we will read and write
             */
            String name = GUID.generate();
            testContext.parentName = QName.createQName(name);
            testContext.childName = QName.createQName("Ermintrude");
            testContext.middleName = QName.createQName("Matilda");
            ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, testContext.parentName, ContentModel.TYPE_FOLDER);
            testContext.parentNodeRef = child.getChildRef();
            logger.debug("parentNodeRef created:" + testContext.parentNodeRef);
            nodeService.setProperty(testContext.parentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.parentNodeRef, ContentModel.PROP_NAME, testContext.parentName.getLocalName());
            ChildAssociationRef child2 = nodeService.createNode(testContext.parentNodeRef, ContentModel.ASSOC_CONTAINS, testContext.childName, ContentModel.TYPE_FOLDER);
            testContext.middleNodeRef = child2.getChildRef();
            logger.debug("middleNodeRef created:" + testContext.middleNodeRef);
            nodeService.setProperty(testContext.middleNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.middleNodeRef, ContentModel.PROP_NAME, testContext.childName.getLocalName());
            ChildAssociationRef child3 = nodeService.createNode(testContext.middleNodeRef, ContentModel.ASSOC_CONTAINS, testContext.childName, ContentModel.TYPE_CONTENT);
            testContext.childNodeRef = child3.getChildRef();
            logger.debug("childNodeRef created:" + testContext.childNodeRef);
            nodeService.setProperty(testContext.childNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.childNodeRef, ContentModel.PROP_NAME, testContext.childName.getLocalName());
            /**
             * Make sure the transfer target exists and is enabled.
             */
            if (!transferService.targetExists(targetName)) {
                testContext.transferMe = createTransferTarget(targetName);
            } else {
                testContext.transferMe = transferService.getTransferTarget(targetName);
            }
            transferService.enableTransferTarget(targetName, true);
            return testContext;
        }
    };
    final TestContext testContext = tran.doInTransaction(setupCB);
    RetryingTransactionCallback<Void> transferCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            TransferDefinition definition = new TransferDefinition();
            Collection<NodeRef> nodes = new ArrayList<NodeRef>();
            nodes.add(testContext.childNodeRef);
            nodes.add(testContext.parentNodeRef);
            nodes.add(testContext.middleNodeRef);
            definition.setSync(true);
            definition.setNodes(nodes);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    RetryingTransactionCallback<Void> checkTransferCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            return null;
        }
    };
    RetryingTransactionCallback<Void> replaceNodesCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Delete the old nodes
            nodeService.deleteNode(testContext.middleNodeRef);
            logger.debug("deleted node");
            ChildAssociationRef child2 = nodeService.createNode(testContext.parentNodeRef, ContentModel.ASSOC_CONTAINS, testContext.childName, ContentModel.TYPE_FOLDER);
            testContext.middleNodeRef = child2.getChildRef();
            logger.debug("middleNodeRef created:" + testContext.middleNodeRef);
            nodeService.setProperty(testContext.middleNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.middleNodeRef, ContentModel.PROP_NAME, testContext.childName.getLocalName());
            ChildAssociationRef child3 = nodeService.createNode(testContext.middleNodeRef, ContentModel.ASSOC_CONTAINS, testContext.childName, ContentModel.TYPE_CONTENT);
            testContext.childNodeRef = child3.getChildRef();
            logger.debug("childNodeRef created:" + testContext.childNodeRef);
            nodeService.setProperty(testContext.childNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.childNodeRef, ContentModel.PROP_NAME, testContext.childName.getLocalName());
            return null;
        }
    };
    // This is the test
    tran.doInTransaction(transferCB);
    tran.doInTransaction(replaceNodesCB);
    tran.doInTransaction(transferCB);
    tran.doInTransaction(checkTransferCB);
}
Also used : Locale(java.util.Locale) Path(org.alfresco.service.cmr.repository.Path) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) 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) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) DescriptorService(org.alfresco.service.descriptor.DescriptorService) Pair(org.alfresco.util.Pair) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

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