Search in sources :

Example 6 with TransferCallback

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

the class TransferServiceImplTest method testAsyncCallback.

// Path based update
/**
 * Test the transfer method when it is running async.
 *
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testAsyncCallback() throws Exception {
    final int MAX_SLEEPS = 5;
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    /**
     * Unit test kludge to transfer from guest home to company home
     */
    final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
    /**
     * This needs to be committed before we can call transfer asycnc.
     */
    final String CONTENT_TITLE = "ContentTitle";
    final String CONTENT_NAME_A = "Demo Node A";
    final String CONTENT_NAME_B = "Demo Node B";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello";
    final String targetName = "testAsyncCallback";
    class TestContext {

        TransferTarget transferMe;

        NodeRef nodeRefA = null;

        NodeRef nodeRefB = null;
    }
    ;
    RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {

        @Override
        public TestContext execute() throws Throwable {
            TestContext ctx = new TestContext();
            final NodeRef guestHome = repositoryHelper.getGuestHome();
            ctx.nodeRefA = nodeService.getChildByName(guestHome, ContentModel.ASSOC_CONTAINS, CONTENT_NAME_A);
            if (ctx.nodeRefA == null) {
                /**
                 * Create a test node that we will read and write
                 */
                ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(GUID.generate()), ContentModel.TYPE_CONTENT);
                ctx.nodeRefA = child.getChildRef();
                nodeService.setProperty(ctx.nodeRefA, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(ctx.nodeRefA, ContentModel.PROP_NAME, CONTENT_NAME_A);
                ContentWriter writer = contentService.getWriter(ctx.nodeRefA, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
            }
            ctx.nodeRefB = nodeService.getChildByName(guestHome, ContentModel.ASSOC_CONTAINS, CONTENT_NAME_B);
            if (ctx.nodeRefB == null) {
                ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(GUID.generate()), ContentModel.TYPE_CONTENT);
                ctx.nodeRefB = child.getChildRef();
                nodeService.setProperty(ctx.nodeRefB, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(ctx.nodeRefB, ContentModel.PROP_NAME, CONTENT_NAME_B);
                ContentWriter writer = contentService.getWriter(ctx.nodeRefB, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
            }
            /**
             * Now go ahead and create our first transfer target
             */
            if (!transferService.targetExists(targetName)) {
                createTransferTarget(targetName);
            } else {
                transferService.getTransferTarget(targetName);
            }
            return ctx;
        }
    };
    final TestContext testContext = tran.doInTransaction(setupCB);
    RetryingTransactionCallback<List<TransferEvent>> transferCB = new RetryingTransactionCallback<List<TransferEvent>>() {

        @Override
        public List<TransferEvent> execute() throws Throwable {
            List<TransferEvent> transferReport = new ArrayList<TransferEvent>(50);
            TestTransferCallback callback = new TestTransferCallback();
            Set<TransferCallback> callbacks = new HashSet<TransferCallback>();
            callbacks.add(callback);
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodes = new HashSet<NodeRef>();
            nodes.add(testContext.nodeRefA);
            nodes.add(testContext.nodeRefB);
            definition.setNodes(nodes);
            transferService.transferAsync(targetName, definition, callbacks);
            logger.debug("transfer async has returned");
            /**
             * Need to poll the transfer events here until callback receives the last event
             */
            Queue<TransferEvent> events = callback.getEvents();
            int sleepCount = MAX_SLEEPS;
            boolean ended = false;
            TransferEvent event = events.poll();
            while (!ended) {
                logger.debug("polling loop:" + sleepCount);
                while (event != null) {
                    /**
                     * Got an event - reset the sleep counter
                     */
                    sleepCount = MAX_SLEEPS;
                    logger.debug("Got an event" + event.toString());
                    /**
                     * Squirrel away the event for analysis later
                     */
                    transferReport.add(event);
                    /**
                     * If we read the last record which will either be SUCCESS or ERROR then we we have finished
                     */
                    if (event.isLast()) {
                        logger.debug("got last event");
                        ended = true;
                    }
                    /**
                     * Try to get the next event
                     */
                    event = events.poll();
                }
                if (event == null && !ended) {
                    if (sleepCount <= 0) {
                        fail("timed out without receiving last event");
                        ended = true;
                    } else {
                        /**
                         *  No content - go to sleep to wait for some more
                         */
                        if (sleepCount-- > 0) {
                            // Sleep for 5 second
                            Thread.sleep(5000);
                        }
                    }
                    /**
                     * Try to get the next event
                     */
                    event = events.poll();
                }
            }
            return transferReport;
        }
    };
    /**
     * The transfer report is a plain report of the transfer - no async shenanigans to worry about
     */
    final List<TransferEvent> transferReport = tran.doInTransaction(transferCB);
    /**
     * Now validate the transferReport
     */
    assertTrue("transfer report is too small", transferReport.size() > 2);
    assertTrue("transfer report does not start with START", transferReport.get(0).getTransferState().equals(TransferEvent.TransferState.START));
    boolean success = false;
    for (TransferEvent event : transferReport) {
        if (event.getTransferState() == TransferEvent.TransferState.SUCCESS) {
            success = true;
        }
    }
    assertTrue("transfer report does not contain SUCCESS", success);
}
Also used : Locale(java.util.Locale) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ArrayList(java.util.ArrayList) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TransferEvent(org.alfresco.service.cmr.transfer.TransferEvent) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TransferCallback(org.alfresco.service.cmr.transfer.TransferCallback) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Aggregations

TransferCallback (org.alfresco.service.cmr.transfer.TransferCallback)6 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)5 TransferEvent (org.alfresco.service.cmr.transfer.TransferEvent)5 TransferTarget (org.alfresco.service.cmr.transfer.TransferTarget)4 HashSet (java.util.HashSet)3 Locale (java.util.Locale)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)3 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)2 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)2 TransferEventBegin (org.alfresco.service.cmr.transfer.TransferEventBegin)2 TransferException (org.alfresco.service.cmr.transfer.TransferException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1