Search in sources :

Example 6 with TransferEventReport

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

the class TransferServiceToBeRefactoredTest method testTransferReport.

/**
 * Test the transfer report.
 *
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testTransferReport() throws Exception {
    final NodeRef guestHome = repositoryHelper.getGuestHome();
    /**
     *  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);
    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 first transfer target
     * This needs to be committed before we can call transfer asycnc.
     */
    final String CONTENT_TITLE = "ContentTitle";
    final String CONTENT_NAME_A = "Report Node A";
    final String CONTENT_NAME_B = "Report Node B";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello";
    class TestData {

        NodeRef nodeRefA;

        NodeRef nodeRefB;

        NodeRef testFolder;

        NodeRef transferReport;

        NodeRef transferDestReport;
    }
    final TestData testData = new TestData();
    final String targetName = "testTransferReport";
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            {
                String name = GUID.generate();
                ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_FOLDER);
                testData.testFolder = child.getChildRef();
                nodeService.setProperty(testData.testFolder, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.testFolder, ContentModel.PROP_NAME, name);
            }
            {
                /**
                 * Create a test node that we will read and write
                 */
                ChildAssociationRef child = nodeService.createNode(testData.testFolder, ContentModel.ASSOC_CONTAINS, QName.createQName(GUID.generate()), ContentModel.TYPE_CONTENT);
                testData.nodeRefA = child.getChildRef();
                nodeService.setProperty(testData.nodeRefA, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.nodeRefA, ContentModel.PROP_NAME, CONTENT_NAME_A);
                ContentWriter writer = contentService.getWriter(testData.nodeRefA, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
            }
            {
                ChildAssociationRef child = nodeService.createNode(testData.testFolder, ContentModel.ASSOC_CONTAINS, QName.createQName(GUID.generate()), ContentModel.TYPE_CONTENT);
                testData.nodeRefB = child.getChildRef();
                nodeService.setProperty(testData.nodeRefB, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.nodeRefB, ContentModel.PROP_NAME, CONTENT_NAME_B);
                ContentWriter writer = contentService.getWriter(testData.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);
            }
            return null;
        }
    });
    /**
     * Step 1.
     * Call the transfer method. to get a failed transfer - orphan nodes exist
     */
    TestTransaction.start();
    try {
        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(testData.nodeRefA);
        nodes.add(testData.nodeRefB);
        // missing the folder node (testFolder)
        definition.setNodes(nodes);
        try {
            transferService.transfer(targetName, definition, callbacks);
            fail("transfer should have failed with an orphan not found exception");
        } catch (TransferException te) {
            logger.debug("deliberatly caught and ignored exception");
        }
        // Can't dirty read transfer report here
        boolean foundSourceReport = false;
        boolean foundDestReport = false;
        for (TransferEvent event : callback.getEvents()) {
            if (event instanceof TransferEventReport) {
                TransferEventReport reportEvent = (TransferEventReport) event;
                switch(reportEvent.getReportType()) {
                    case DESTINATION:
                        foundDestReport = true;
                        testData.transferDestReport = reportEvent.getNodeRef();
                        assertNotNull("dest transfer nodeId null", testData.transferDestReport);
                        break;
                    case SOURCE:
                        foundSourceReport = true;
                        testData.transferReport = reportEvent.getNodeRef();
                        break;
                }
            }
        }
        assertTrue("source report not found", foundSourceReport);
        assertTrue("dest report not found", foundDestReport);
    } finally {
        TestTransaction.end();
    }
    /**
     * Now validate the client side error transfer report against the xsd file
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testData.transferReport, ContentModel.PROP_CONTENT);
            assertNotNull("transfer reader is null", reader);
            assertEquals("client report mimetype not set", reader.getMimetype(), MimetypeMap.MIMETYPE_XML);
            String name = (String) nodeService.getProperty(testData.transferReport, ContentModel.PROP_NAME);
            assertTrue("client report does not end with .xml", name.endsWith(".xml"));
            logger.debug("This report should have failed");
            if (logger.isDebugEnabled()) {
                dumpToSystemOut(testData.transferReport);
            }
            // Now validate the client side transfer report against the XSD
            Source transferReportSource = new StreamSource(reader.getContentInputStream());
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final String TRANSFER_REPORT_SCHEMA_LOCATION = "classpath:org/alfresco/repo/transfer/report/TransferReport2.xsd";
            Schema schema = sf.newSchema(ResourceUtils.getURL(TRANSFER_REPORT_SCHEMA_LOCATION));
            Validator validator = schema.newValidator();
            try {
                validator.validate(transferReportSource);
            } catch (Exception e) {
                fail(e.getMessage());
            }
            return null;
        }
    });
    /**
     * Step 2
     * Call the transfer method to get a good success transfer report
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            {
                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(testData.nodeRefA);
                nodes.add(testData.nodeRefB);
                nodes.add(testData.testFolder);
                definition.setNodes(nodes);
                testData.transferReport = transferService.transfer(targetName, definition, callbacks);
                assertNotNull("transfer report is null", testData.transferReport);
                // Can't dirty read transfer report here
                boolean foundSourceReport = false;
                boolean foundDestReport = false;
                for (TransferEvent event : callback.getEvents()) {
                    if (event instanceof TransferEventReport) {
                        TransferEventReport reportEvent = (TransferEventReport) event;
                        switch(reportEvent.getReportType()) {
                            case DESTINATION:
                                foundDestReport = true;
                                testData.transferDestReport = reportEvent.getNodeRef();
                                assertNotNull("dest transfer nodeId null", testData.transferDestReport);
                                assertFalse("dest transfer nodeId not correct", testData.transferReport.equals(testData.transferDestReport));
                                break;
                            case SOURCE:
                                foundSourceReport = true;
                                assertEquals("source transfer nodeId not correct", testData.transferReport, reportEvent.getNodeRef());
                                break;
                        }
                    }
                }
                assertTrue("source report not found", foundSourceReport);
                assertTrue("dest report not found", foundDestReport);
            }
            return null;
        }
    });
    /**
     * Now validate the client side transfer report against the xsd file
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testData.transferReport, ContentModel.PROP_CONTENT);
            assertNotNull("transfer reader is null", reader);
            logger.debug("This report should succeed");
            if (logger.isDebugEnabled()) {
                dumpToSystemOut(testData.transferReport);
            }
            // Now validate the client side transfer report against the XSD
            Source transferReportSource = new StreamSource(reader.getContentInputStream());
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final String TRANSFER_REPORT_SCHEMA_LOCATION = "classpath:org/alfresco/repo/transfer/report/TransferReport2.xsd";
            Schema schema = sf.newSchema(ResourceUtils.getURL(TRANSFER_REPORT_SCHEMA_LOCATION));
            Validator validator = schema.newValidator();
            try {
                validator.validate(transferReportSource);
            } catch (Exception e) {
                fail(e.getMessage());
            }
            return null;
        }
    });
    /**
     * Now validate the destination side transfer report against its xsd file
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testData.transferDestReport, ContentModel.PROP_CONTENT);
            assertNotNull("transfer reader is null", reader);
            assertEquals("dest report mimetype not set", reader.getMimetype(), MimetypeMap.MIMETYPE_XML);
            String name = (String) nodeService.getProperty(testData.transferReport, ContentModel.PROP_NAME);
            assertTrue("dest report does not end with .xml", name.endsWith(".xml"));
            if (logger.isDebugEnabled()) {
                dumpToSystemOut(testData.transferDestReport);
            }
            // Now validate the destination side transfer report against the XSD
            Source transferReportSource = new StreamSource(reader.getContentInputStream());
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final String TRANSFER_REPORT_SCHEMA_LOCATION = "classpath:org/alfresco/repo/transfer/reportd/TransferDestinationReport.xsd";
            Schema schema = sf.newSchema(ResourceUtils.getURL(TRANSFER_REPORT_SCHEMA_LOCATION));
            Validator validator = schema.newValidator();
            try {
                validator.validate(transferReportSource);
            } catch (Exception e) {
                fail("Destination Transfer Report " + e.getMessage());
            }
            return null;
        }
    });
    /**
     * Now validate all transfer reports.
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            String query = "TYPE:\"trx:transferReportDest\"";
            ResultSet results = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_LUCENE, query);
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final String TRANSFER_REPORT_SCHEMA_LOCATION = "classpath:org/alfresco/repo/transfer/reportd/TransferDestinationReport.xsd";
            Schema schema = sf.newSchema(ResourceUtils.getURL(TRANSFER_REPORT_SCHEMA_LOCATION));
            Validator validator = schema.newValidator();
            for (ResultSetRow result : results) {
                NodeRef reportNode = result.getNodeRef();
                logger.debug("validating  reportNode " + reportNode);
                // Now validate the destination side transfer report against the XSD
                ContentReader reader = contentService.getReader(reportNode, ContentModel.PROP_CONTENT);
                assertNotNull("transfer reader is null", reader);
                if (reader.getMimetype().equals(MimetypeMap.MIMETYPE_XML)) {
                    Source transferReportSource = new StreamSource(reader.getContentInputStream());
                    try {
                        validator.validate(transferReportSource);
                    } catch (Exception e) {
                        fail("Destination Transfer Report reportNode:" + reportNode + " message :" + e.getMessage());
                    }
                }
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            logger.debug("now delete the target:" + targetName);
            transferService.deleteTransferTarget(targetName);
            return null;
        }
    });
}
Also used : Locale(java.util.Locale) Schema(javax.xml.validation.Schema) TransferEventReport(org.alfresco.service.cmr.transfer.TransferEventReport) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ResultSet(org.alfresco.service.cmr.search.ResultSet) DescriptorService(org.alfresco.service.descriptor.DescriptorService) Pair(org.alfresco.util.Pair) HashSet(java.util.HashSet) Path(org.alfresco.service.cmr.repository.Path) SchemaFactory(javax.xml.validation.SchemaFactory) ContentReader(org.alfresco.service.cmr.repository.ContentReader) StreamSource(javax.xml.transform.stream.StreamSource) TransferEvent(org.alfresco.service.cmr.transfer.TransferEvent) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TransferCallback(org.alfresco.service.cmr.transfer.TransferCallback) TransferException(org.alfresco.service.cmr.transfer.TransferException) IOException(java.io.IOException) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) Validator(javax.xml.validation.Validator) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Aggregations

TransferEventReport (org.alfresco.service.cmr.transfer.TransferEventReport)6 TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)5 TransferEvent (org.alfresco.service.cmr.transfer.TransferEvent)5 ArrayList (java.util.ArrayList)4 TransferEventEndState (org.alfresco.service.cmr.transfer.TransferEventEndState)4 TransferEventEnterState (org.alfresco.service.cmr.transfer.TransferEventEnterState)4 TransferException (org.alfresco.service.cmr.transfer.TransferException)4 List (java.util.List)3 TransferEventBegin (org.alfresco.service.cmr.transfer.TransferEventBegin)3 TransferEventError (org.alfresco.service.cmr.transfer.TransferEventError)3 TransferEventSendingSnapshot (org.alfresco.service.cmr.transfer.TransferEventSendingSnapshot)3 TransferFailureException (org.alfresco.service.cmr.transfer.TransferFailureException)3 TransferProgress (org.alfresco.service.cmr.transfer.TransferProgress)3 TransferEventCommittingStatus (org.alfresco.service.cmr.transfer.TransferEventCommittingStatus)2 TransferEventSendingContent (org.alfresco.service.cmr.transfer.TransferEventSendingContent)2 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1