Search in sources :

Example 31 with TransferDefinition

use of org.alfresco.service.cmr.transfer.TransferDefinition 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)

Example 32 with TransferDefinition

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

the class TransferServiceImplTest method testPeerAssocs.

// test replace node
// /**
// * Test the transfer method with regard to obscure paths.
// *
// * This is a unit test so it does some shenanigans to send to he same instance of alfresco.
// */
// public void testHorriblePaths() throws Exception
// {
// setDefaultRollback(false);
// 
// final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
// 
// final String CONTENT_TITLE = "ContentTitle";
// final String CONTENT_TITLE_UPDATED = "ContentTitleUpdated";
// final String CONTENT_NAME = "Demo Node 1";
// final Locale CONTENT_LOCALE = Locale.GERMAN;
// final String CONTENT_STRING = "The quick brown fox";
// final Set<NodeRef>nodes = new HashSet<NodeRef>();
// 
// final String targetName = "testManyNodes";
// 
// class TestContext
// {
// TransferTarget transferMe;
// NodeRef nodeA = null;
// NodeRef childNode = null;
// };
// 
// /**
// * Unit test kludge to transfer from guest home to company home
// */
// final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
// 
// TransferTarget transferMe;
// 
// final QName[] difficult = { QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"testNodeB"),
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"with.dot"),
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"8332"),
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"&#~@"),
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"_-+ )"),
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"with space"),
// // A, e with accent
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "\u0041\u00E9"),
// // Greek Alpha, Omega
// QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "\u0391\u03A9")
// };
// 
// RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>()
// {
// @Override
// public TestContext execute() throws Throwable
// {
// TestContext ctx = new TestContext();
// 
// /**
// * Get guest home
// */
// String guestHomeQuery = "/app:company_home/app:guest_home";
// ResultSet guestHomeResult = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_XPATH, guestHomeQuery);
// assertEquals("", 1, guestHomeResult.length());
// NodeRef guestHome = guestHomeResult.getNodeRef(0);
// 
// /**
// * Create a test node that we will read and write
// */
// String guid = GUID.generate();
// 
// /**
// * Create a tree with "difficult" characters in the path
// * ManyNodesRoot
// * A (Folder)
// * ... childNode
// */
// ChildAssociationRef child;
// 
// child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(guid), ContentModel.TYPE_FOLDER);
// NodeRef testRootNode = child.getChildRef();
// nodeService.setProperty(testRootNode , ContentModel.PROP_TITLE, guid);
// nodeService.setProperty(testRootNode , ContentModel.PROP_NAME, guid);
// nodes.add(testRootNode);
// 
// child = nodeService.createNode(testRootNode, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,"testNodeA"), ContentModel.TYPE_FOLDER);
// ctx.nodeA = child.getChildRef();
// nodeService.setProperty(ctx.nodeA , ContentModel.PROP_TITLE, "TestNodeA");
// nodeService.setProperty(ctx.nodeA , ContentModel.PROP_NAME, "TestNodeA");
// nodes.add(ctx.nodeA);
// 
// NodeRef current = ctx.nodeA;
// 
// for(QName name : difficult)
// {
// child = nodeService.createNode(current, ContentModel.ASSOC_CONTAINS, name, ContentModel.TYPE_FOLDER);
// current = child.getChildRef();
// nodeService.setProperty(current , ContentModel.PROP_TITLE, name);
// nodeService.setProperty(current , ContentModel.PROP_NAME, "testName");
// nodes.add(current);
// }
// 
// child = nodeService.createNode(current, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeAC"), ContentModel.TYPE_CONTENT);
// ctx.childNode = child.getChildRef();
// nodeService.setProperty(  ctx.childNode , ContentModel.PROP_TITLE, CONTENT_TITLE + "AC");
// nodeService.setProperty(  ctx.childNode , ContentModel.PROP_NAME, "DemoNodeAC");
// 
// {
// ContentWriter writer = contentService.getWriter(  ctx.childNode , ContentModel.PROP_CONTENT, true);
// writer.setLocale(CONTENT_LOCALE);
// writer.putContent(CONTENT_STRING);
// nodes.add(  ctx.childNode);
// }
// 
// 
// /**
// * Now go ahead and create our first transfer target
// */
// if(!transferService.targetExists(targetName))
// {
// ctx.transferMe = createTransferTarget(targetName);
// }
// else
// {
// ctx.transferMe = transferService.getTransferTarget(targetName);
// }
// 
// return ctx;
// }
// };
// 
// final TestContext testContext = tran.doInTransaction(setupCB);
// 
// RetryingTransactionCallback<Void> transferCB = new RetryingTransactionCallback<Void>() {
// 
// @Override
// public Void execute() throws Throwable
// {
// TransferDefinition definition = new TransferDefinition();
// definition.setNodes(nodes);
// transferService.transfer(targetName, definition);
// 
// return null;
// }
// };
// 
// tran.doInTransaction(transferCB);
// 
// RetryingTransactionCallback<Void> check1CB = new RetryingTransactionCallback<Void>() {
// 
// @Override
// public Void execute() throws Throwable
// {
// NodeRef destChildNode = testNodeFactory.getMappedNodeRef(testContext.childNode);
// assertTrue("dest node does not exist", nodeService.exists(destChildNode));
// 
// /**
// * Step through source and dest trees on nodes comparing the path as we go.
// */
// Path srcPath = nodeService.getPath(testContext.childNode);
// Path destPath = nodeService.getPath(destChildNode);
// 
// int srcSize = srcPath.size();
// int destSize = destPath.size();
// 
// Path dest = destPath.subPath(2, destSize-1);
// Path src = srcPath.subPath(3, srcSize-1);
// 
// //                System.out.println("src=" + src);
// //                System.out.println("dest=" + dest);
// assertEquals("paths are different", src.toString(), dest.toString());
// 
// return null;
// }
// };
// 
// tran.doInTransaction(check1CB);
// 
// RetryingTransactionCallback<Void> updateCB = new RetryingTransactionCallback<Void>() {
// 
// @Override
// public Void execute() throws Throwable
// {
// 
// return null;
// }
// };
// 
// tran.doInTransaction(updateCB);
// 
// tran.doInTransaction(transferCB);
// 
// RetryingTransactionCallback<Void> check2CB = new RetryingTransactionCallback<Void>() {
// 
// @Override
// public Void execute() throws Throwable
// {
// assertTrue("dest node does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testContext.childNode)));
// 
// return null;
// }
// };
// tran.doInTransaction(check2CB);
// 
// } // horrible paths
/**
 * ALF-6174
 * Test transfer of peer associations
 *
 * Step 1 : Create 2 nodes
 * Add a peer assoc
 * Transfer
 *
 * Step 2: Add another peer assoc
 * Transfer
 *
 * Step 3: Remove a peer assoc
 * Transfer
 *
 * Step 4: Remove a peer assoc
 * Transfer
 *
 * @throws Exception
 */
@Test
public void testPeerAssocs() throws Exception {
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    final String CONTENT_TITLE = "ContentTitle";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_ENCODING = "UTF-8";
    /**
     *  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
     */
    final TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(receiver, contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory);
    transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory);
    final 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)));
    final String targetName = "testPeerAssocs";
    class TestContext {

        TransferTarget transferMe;

        NodeRef folderNodeRef;

        NodeRef sourceNodeRef;

        NodeRef targetNodeRef;

        NodeRef destSourceNodeRef;

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

        @Override
        public TestContext execute() throws Throwable {
            TestContext testContext = new TestContext();
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node that we will read and write
             */
            String name = GUID.generate();
            TransferDefinition def = new TransferDefinition();
            ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_FOLDER);
            testContext.folderNodeRef = child.getChildRef();
            nodeService.setProperty(testContext.folderNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.folderNodeRef, ContentModel.PROP_NAME, name);
            // Side effect - initialisee nodeid mapping
            testNodeFactory.createTransferManifestNode(testContext.folderNodeRef, def, new TransferContext());
            child = nodeService.createNode(testContext.folderNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("source"), ContentModel.TYPE_CONTENT);
            testContext.sourceNodeRef = child.getChildRef();
            nodeService.setProperty(testContext.sourceNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.sourceNodeRef, ContentModel.PROP_NAME, "source");
            // Side effect - initialise nodeid mapping
            testNodeFactory.createTransferManifestNode(testContext.sourceNodeRef, def, new TransferContext());
            child = nodeService.createNode(testContext.folderNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("target"), ContentModel.TYPE_CONTENT);
            testContext.targetNodeRef = child.getChildRef();
            nodeService.setProperty(testContext.targetNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testContext.targetNodeRef, ContentModel.PROP_NAME, "target");
            testNodeFactory.createTransferManifestNode(testContext.folderNodeRef, def, new TransferContext());
            nodeService.createAssociation(testContext.sourceNodeRef, testContext.targetNodeRef, ContentModel.ASSOC_REFERENCES);
            // Side effect - initialise nodeid mapping
            testNodeFactory.createTransferManifestNode(testContext.targetNodeRef, def, new TransferContext());
            /**
             * 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> addPeerAssocCB = new RetryingTransactionCallback<Void>() {

        public QName assocQName = ContentModel.ASSOC_ATTACHMENTS;

        @Override
        public Void execute() throws Throwable {
            nodeService.createAssociation(testContext.sourceNodeRef, testContext.targetNodeRef, assocQName);
            return null;
        }
    };
    RetryingTransactionCallback<Void> removePeerAssocCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            List<AssociationRef> refs = nodeService.getTargetAssocs(testContext.sourceNodeRef, RegexQNamePattern.MATCH_ALL);
            if (refs.size() > 0) {
                AssociationRef ref = refs.get(0);
                nodeService.removeAssociation(ref.getSourceRef(), ref.getTargetRef(), ref.getTypeQName());
            }
            return null;
        }
    };
    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.sourceNodeRef);
            nodes.add(testContext.targetNodeRef);
            nodes.add(testContext.folderNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(targetName, definition);
            return null;
        }
    };
    RetryingTransactionCallback<List<AssociationRef>> readAssocsCB = new RetryingTransactionCallback<List<AssociationRef>>() {

        @Override
        public List<AssociationRef> execute() throws Throwable {
            List<AssociationRef> source = nodeService.getSourceAssocs(testContext.sourceNodeRef, RegexQNamePattern.MATCH_ALL);
            List<AssociationRef> target = nodeService.getTargetAssocs(testContext.sourceNodeRef, RegexQNamePattern.MATCH_ALL);
            NodeRef destNode = testNodeFactory.getMappedNodeRef(testContext.sourceNodeRef);
            List<AssociationRef> destSource = nodeService.getSourceAssocs(destNode, RegexQNamePattern.MATCH_ALL);
            List<AssociationRef> destTarget = nodeService.getTargetAssocs(destNode, RegexQNamePattern.MATCH_ALL);
            assertEquals("source peers different sizes", destSource.size(), source.size());
            assertEquals("target peers different sizes", destTarget.size(), target.size());
            if (destSource.size() == 1) {
                assertEquals(destSource.get(0).getTypeQName(), source.get(0).getTypeQName());
            }
            if (destTarget.size() == 1) {
                assertEquals(destTarget.get(0).getTypeQName(), target.get(0).getTypeQName());
            }
            return destTarget;
        }
    };
    /**
     * This is the test
     */
    tran.doInTransaction(transferCB);
    List<AssociationRef> assocs = tran.doInTransaction(readAssocsCB);
    assertEquals("assocs not one", 1, assocs.size());
    tran.doInTransaction(addPeerAssocCB);
    tran.doInTransaction(transferCB);
    assocs = tran.doInTransaction(readAssocsCB);
    assertEquals("assocs not two", 2, assocs.size());
    tran.doInTransaction(removePeerAssocCB);
    tran.doInTransaction(transferCB);
    tran.doInTransaction(removePeerAssocCB);
    tran.doInTransaction(transferCB);
}
Also used : Locale(java.util.Locale) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) List(java.util.List) ArrayList(java.util.ArrayList) Pair(org.alfresco.util.Pair) HashSet(java.util.HashSet) Path(org.alfresco.service.cmr.repository.Path) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 33 with TransferDefinition

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

the class TransferServiceImplTest method testPathBasedUpdate.

// end many nodes
/**
 * Test the path based update.
 *
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testPathBasedUpdate() throws Exception {
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    final String CONTENT_TITLE = "ContentTitle";
    final String CONTENT_TITLE_UPDATED = "ContentTitleUpdated";
    final String CONTENT_NAME = GUID.generate();
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello";
    final QName TEST_QNAME = QName.createQName(CONTENT_NAME);
    class TestContext {

        String targetName;

        NodeRef contentNodeRef;

        NodeRef newContentNodeRef;

        NodeRef guestHome;

        ChildAssociationRef child;
    }
    ;
    RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {

        @Override
        public TestContext execute() throws Throwable {
            TestContext ctx = new TestContext();
            ctx.targetName = GUID.generate();
            ctx.guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node that we will transfer.   Its path is what is important
             */
            ctx.child = nodeService.createNode(ctx.guestHome, ContentModel.ASSOC_CONTAINS, TEST_QNAME, ContentModel.TYPE_CONTENT);
            ctx.contentNodeRef = ctx.child.getChildRef();
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_NAME, CONTENT_NAME);
            ContentWriter writer = contentService.getWriter(ctx.contentNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_STRING);
            /**
             * Now go ahead and create our first transfer target
             */
            if (!transferService.targetExists(ctx.targetName)) {
                createTransferTarget(ctx.targetName);
            } else {
                transferService.getTransferTarget(ctx.targetName);
            }
            return ctx;
        }
    };
    final TestContext testContext = tran.doInTransaction(setupCB);
    /**
     *  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);
    RetryingTransactionCallback<Void> step1CB = 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(testContext.contentNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(testContext.targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(step1CB);
    RetryingTransactionCallback<Void> transfer1CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists and has similar properties to the source
            NodeRef destNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
            assertFalse("unit test stuffed up - comparing with self", destNodeRef.equals(testContext.contentNodeRef));
            assertTrue("dest node ref does not exist", nodeService.exists(destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE);
            assertEquals("type is wrong", nodeService.getType(testContext.contentNodeRef), nodeService.getType(destNodeRef));
            /**
             * Now delete the content node and re-create another one with the old path
             */
            nodeService.deleteNode(testContext.contentNodeRef);
            testContext.child = nodeService.createNode(testContext.guestHome, ContentModel.ASSOC_CONTAINS, TEST_QNAME, ContentModel.TYPE_CONTENT);
            testContext.newContentNodeRef = testContext.child.getChildRef();
            nodeService.setProperty(testContext.newContentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE_UPDATED);
            return null;
        }
    };
    tran.doInTransaction(transfer1CB);
    RetryingTransactionCallback<Void> transfer1CB2 = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our node which is a new node (so will not exist on the back end) with a path that already has a node.
             */
            TransferDefinition definition = new TransferDefinition();
            Set<NodeRef> nodes = new HashSet<NodeRef>();
            nodes.add(testContext.newContentNodeRef);
            definition.setNodes(nodes);
            transferService.transfer(testContext.targetName, definition);
            return null;
        }
    };
    tran.doInTransaction(transfer1CB2);
    RetryingTransactionCallback<Void> validateStep1CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef oldDestNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
            NodeRef newDestNodeRef = testNodeFactory.getMappedNodeRef(testContext.newContentNodeRef);
            // Now validate that the target node does not exist - it should have
            // been updated by path.
            assertFalse("unit test stuffed up - comparing with self", oldDestNodeRef.equals(newDestNodeRef));
            assertFalse("new dest node ref exists", nodeService.exists(newDestNodeRef));
            assertTrue("old dest node does not exists", nodeService.exists(oldDestNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(oldDestNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE_UPDATED);
            // assertEquals("type is wrong", nodeService.getType(contentNodeRef), nodeService.getType(destNodeRef));
            return null;
        }
    };
    tran.doInTransaction(validateStep1CB);
}
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) 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) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) 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 34 with TransferDefinition

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

the class TransferServiceToBeRefactoredTest method testCopyTransferredNode.

/**
 * Test the behaviour with regard to copying transferred nodes.
 * <p>
 * Transfer node read only
 * <p>
 * Copy transferred node.
 * <p>
 * New node should not be locked and should not be transferred.
 * <p>
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testCopyTransferredNode() throws Exception {
    final String CONTENT_TITLE = "ContentTitle";
    /**
     * Now go ahead and create our transfer target
     */
    final String targetName = "testCopyTransferredNode";
    class TestData {

        @SuppressWarnings("unused")
        TransferTarget transferMe;

        NodeRef S0NodeRef;

        NodeRef A1NodeRef;

        NodeRef A2NodeRef;

        NodeRef A3NodeRef;

        NodeRef B1NodeRef;

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

        @Override
        public Void execute() throws Throwable {
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Node Source - located under guest home
             */
            {
                String name = GUID.generate();
                ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_FOLDER);
                testData.S0NodeRef = child.getChildRef();
                nodeService.setProperty(testData.S0NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.S0NodeRef, ContentModel.PROP_NAME, name);
            }
            {
                // Node A1
                ChildAssociationRef child = nodeService.createNode(testData.S0NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A1"), ContentModel.TYPE_FOLDER);
                testData.A1NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A1NodeRef, ContentModel.PROP_TITLE, "A1");
                nodeService.setProperty(testData.A1NodeRef, ContentModel.PROP_NAME, "A1");
            }
            {
                // Node A2
                ChildAssociationRef child = nodeService.createNode(testData.A1NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A2"), ContentModel.TYPE_FOLDER);
                testData.A2NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A2NodeRef, ContentModel.PROP_TITLE, "A2");
                nodeService.setProperty(testData.A2NodeRef, ContentModel.PROP_NAME, "A2");
            }
            {
                // Node A3
                ChildAssociationRef child = nodeService.createNode(testData.A2NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A3"), ContentModel.TYPE_FOLDER);
                testData.A3NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A3NodeRef, ContentModel.PROP_TITLE, "A3");
                nodeService.setProperty(testData.A3NodeRef, ContentModel.PROP_NAME, "A3");
            }
            {
                // Node B1
                ChildAssociationRef child = nodeService.createNode(testData.S0NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("B1"), ContentModel.TYPE_FOLDER);
                testData.B1NodeRef = child.getChildRef();
                nodeService.setProperty(testData.B1NodeRef, ContentModel.PROP_TITLE, "B1");
                nodeService.setProperty(testData.B1NodeRef, ContentModel.PROP_NAME, "B1");
            }
            {
                // Node B2
                ChildAssociationRef child = nodeService.createNode(testData.S0NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("B2"), ContentModel.TYPE_FOLDER);
                testData.B2NodeRef = child.getChildRef();
                nodeService.setProperty(testData.B1NodeRef, ContentModel.PROP_TITLE, "B2");
                nodeService.setProperty(testData.B1NodeRef, ContentModel.PROP_NAME, "B2");
            }
            if (!transferService.targetExists(targetName)) {
                testData.transferMe = createTransferTarget(targetName);
            } else {
                testData.transferMe = transferService.getTransferTarget(targetName);
            }
            return null;
        }
    });
    /**
     *  For unit test
     *  - replace the HTTP transport with the in-process transport
     *  - Map path from A1 to B1 (So transfer will transfer by path)
     */
    TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(receiver, contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory);
    transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory);
    final List<Pair<Path, Path>> pathMap = testNodeFactory.getPathMap();
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Map Project A to Project B
            pathMap.add(new Pair<Path, Path>(nodeService.getPath(testData.A1NodeRef), nodeService.getPath(testData.B1NodeRef)));
            return null;
        }
    });
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    /**
     * Step 1
     */
    logger.debug("First transfer - ");
    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.A2NodeRef);
                nodes.add(testData.A3NodeRef);
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                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
            NodeRef A2destNodeRef = testNodeFactory.getMappedNodeRef(testData.A2NodeRef);
            assertTrue("dest node ref does not exist", nodeService.exists(A2destNodeRef));
            /**
             * Copy the node A2 Dest
             */
            NodeRef copiedNode = copyService.copy(A2destNodeRef, testData.B2NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A2Copy"));
            assertTrue("copied node does not exist", nodeService.exists(copiedNode));
            System.out.println("copied node is " + copiedNode);
            assertFalse("copied node still has transferred aspect", nodeService.hasAspect(copiedNode, TransferModel.ASPECT_TRANSFERRED));
            assertNull("copied node still has from repository id", nodeService.getProperty(copiedNode, TransferModel.PROP_FROM_REPOSITORY_ID));
            assertNull("copied node still has original repository id", nodeService.getProperty(copiedNode, TransferModel.PROP_REPOSITORY_ID));
            @SuppressWarnings("unused") Set<QName> aspects = nodeService.getAspects(copiedNode);
            /**
             * Copy a chain of transferred nodes - well A2dest and A3dest
             */
            copiedNode = copyService.copy(A2destNodeRef, testData.B2NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A2Copy2"), true);
            assertTrue("copied node does not exist", nodeService.exists(copiedNode));
            System.out.println("copied node is " + copiedNode);
            assertFalse("copied node still has transferred aspect", nodeService.hasAspect(copiedNode, TransferModel.ASPECT_TRANSFERRED));
            assertNull("copied node still has from repository id", nodeService.getProperty(copiedNode, TransferModel.PROP_FROM_REPOSITORY_ID));
            assertNull("copied node still has original repository id", nodeService.getProperty(copiedNode, TransferModel.PROP_REPOSITORY_ID));
            List<ChildAssociationRef> children = nodeService.getChildAssocs(copiedNode);
            for (ChildAssociationRef child : children) {
                assertFalse("copied node still has transferred aspect", nodeService.hasAspect(child.getChildRef(), TransferModel.ASPECT_TRANSFERRED));
                assertNull("copied node still has from repository id", nodeService.getProperty(child.getChildRef(), TransferModel.PROP_FROM_REPOSITORY_ID));
                assertNull("copied node still has original repository id", nodeService.getProperty(child.getChildRef(), TransferModel.PROP_REPOSITORY_ID));
            }
            return null;
        }
    });
}
Also used : ResultSet(org.alfresco.service.cmr.search.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) 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) List(java.util.List) ArrayList(java.util.ArrayList) 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 35 with TransferDefinition

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

the class TransferServiceToBeRefactoredTest method testTwoRepoSync.

// end test read only flag
/**
 * Transfer sync from multiple repos.
 *
 * This is a unit test so does lots of shenanigans to fake transfer from three repositories on a single repo.
 *
 * Multi-repo sync depends upon the following pieces of functionality
 * a) transferred nodes are tagged with a trx:transferred aspect containing the originating repository
 * id and the from repository id
 * b) to support hub and spoke - when syncing don't imply delete nodes that are not "from" the transferring system
 *
 *      * Tree of nodes
 *      A1
 *   |      |
 *   A2     A3 (Content Node) B6 (Content Node)
 *   |
 * A4 A5 B7
 *
 * Step 1
 * Hub and Spoke Sync
 * create Tree A1...A5
 * transfer (sync)
 * check the transfered aspects on destination
 * create node B6.  Fake its transfered aspect to be from Repo B.
 * transfer (sync)
 *
 * Step 2
 * Chain Sync
 * Create Node A7 "from repo B".
 * Change Nodes A1 ... A5 source to be received "from repo B"
 * transfer
 */
@Test
public void testTwoRepoSync() throws Exception {
    /**
     * Step 1
     * create Tree A1...A6
     * transfer (sync)
     * check the transfered aspect
     * create node B6.  Fake its transfered aspect to be from Repo B, Non Alien.
     * transfer (sync)
     */
    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);
    final String repositoryId = REPO_ID_A;
    /**
     * Now go ahead and create our first transfer target
     */
    final String targetName = "testTransferSyncNodes";
    class TestData {

        @SuppressWarnings("unused")
        TransferTarget transferMe;

        NodeRef A1NodeRef;

        NodeRef A2NodeRef;

        NodeRef A3NodeRef;

        NodeRef A4NodeRef;

        NodeRef A5NodeRef;

        NodeRef B6NodeRef;

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

        @Override
        public Void execute() throws Throwable {
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test nodes A1 through A5 that we will read and write
             */
            {
                // Node A1
                String name = GUID.generate();
                ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_FOLDER);
                testData.A1NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A1NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.A1NodeRef, ContentModel.PROP_NAME, name);
            }
            {
                // Node A2
                ChildAssociationRef child = nodeService.createNode(testData.A1NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A2"), ContentModel.TYPE_FOLDER);
                testData.A2NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A2NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.A2NodeRef, ContentModel.PROP_NAME, "A2");
            }
            {
                // Node A3
                ChildAssociationRef child = nodeService.createNode(testData.A1NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A3"), ContentModel.TYPE_CONTENT);
                testData.A3NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A3NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.A3NodeRef, ContentModel.PROP_NAME, "A3");
                ContentWriter writer = contentService.getWriter(testData.A3NodeRef, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
            }
            {
                // Node A4
                ChildAssociationRef child = nodeService.createNode(testData.A2NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A4"), ContentModel.TYPE_CONTENT);
                testData.A4NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A4NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.A4NodeRef, ContentModel.PROP_NAME, "A4");
                ContentWriter writer = contentService.getWriter(testData.A4NodeRef, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
            }
            {
                // Node A5
                ChildAssociationRef child = nodeService.createNode(testData.A2NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A5"), ContentModel.TYPE_CONTENT);
                testData.A5NodeRef = child.getChildRef();
                nodeService.setProperty(testData.A5NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
                nodeService.setProperty(testData.A5NodeRef, ContentModel.PROP_NAME, "A5");
                ContentWriter writer = contentService.getWriter(testData.A5NodeRef, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
            }
            // Create the transfer target if it does not already exist
            if (!transferService.targetExists(targetName)) {
                testData.transferMe = createTransferTarget(targetName);
            } else {
                testData.transferMe = transferService.getTransferTarget(targetName);
            }
            return null;
        }
    });
    final Set<NodeRef> nodes = new HashSet<NodeRef>();
    nodes.add(testData.A1NodeRef);
    nodes.add(testData.A2NodeRef);
    nodes.add(testData.A3NodeRef);
    nodes.add(testData.A4NodeRef);
    nodes.add(testData.A5NodeRef);
    /**
     * transfer (sync)
     * check the transfered aspect
     * create node B6.  Fake its transfered aspect to be from Repo B, Non Alien.
     * transfer (sync)
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

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

        @Override
        public Void execute() throws Throwable {
            // Node B6 - faked transfer from repository B. Child of Destination node A1
            NodeRef a1Dest = testNodeFactory.getMappedNodeRef(testData.A1NodeRef);
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.A1NodeRef)));
            assertEquals("dest node A1 From RepositoryId", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A1NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A1 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A1NodeRef), TransferModel.PROP_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A2 From RepositoryId", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A2NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A2 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A2NodeRef), TransferModel.PROP_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A3 From RepositoryId", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A3NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A3 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A3NodeRef), TransferModel.PROP_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A4 From RepositoryId", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A4NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A4 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A4NodeRef), TransferModel.PROP_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A5 From RepositoryId", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A5NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A5 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A5NodeRef), TransferModel.PROP_REPOSITORY_ID), repositoryId);
            ChildAssociationRef child = nodeService.createNode(a1Dest, ContentModel.ASSOC_CONTAINS, QName.createQName("B6"), ContentModel.TYPE_CONTENT);
            testData.B6NodeRef = child.getChildRef();
            nodeService.setProperty(testData.B6NodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testData.B6NodeRef, ContentModel.PROP_NAME, "B6");
            /**
             * The first tranfer was mocked to repository A - this is repository B.
             */
            // This is repository B so there's no need to fake it
            // nodeService.setProperty(testData.B6NodeRef, TransferModel.PROP_FROM_REPOSITORY_ID, REPO_ID_B);
            // nodeService.setProperty(testData.B6NodeRef, TransferModel.PROP_REPOSITORY_ID, REPO_ID_B);
            ContentWriter writer = contentService.getWriter(testData.B6NodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_STRING);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

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

        @Override
        public Void execute() throws Throwable {
            // Does node B6 still exist ?
            assertTrue("dest node B6 does not exist", nodeService.exists(testData.B6NodeRef));
            assertTrue("B6 not alien", nodeService.hasAspect(testData.B6NodeRef, TransferModel.ASPECT_ALIEN));
            return null;
        }
    });
    /**
     * Step 2
     * Chain Sync
     * Change Nodes A1 ... A5 source to be received "from repo B"
     * Create Node A7 - Fake it to be received "from repo B"
     * transfer
     */
    final String NEW_TITLE = "Chain sync";
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            nodeService.setProperty(testData.A1NodeRef, ContentModel.PROP_TITLE, NEW_TITLE);
            nodeService.setProperty(testData.A1NodeRef, TransferModel.PROP_FROM_REPOSITORY_ID, REPO_ID_B);
            nodeService.setProperty(testData.A1NodeRef, TransferModel.PROP_REPOSITORY_ID, REPO_ID_B);
            nodeService.setProperty(testData.A2NodeRef, ContentModel.PROP_TITLE, NEW_TITLE);
            nodeService.setProperty(testData.A2NodeRef, TransferModel.PROP_FROM_REPOSITORY_ID, REPO_ID_B);
            nodeService.setProperty(testData.A2NodeRef, TransferModel.PROP_REPOSITORY_ID, REPO_ID_B);
            nodeService.setProperty(testData.A3NodeRef, ContentModel.PROP_TITLE, NEW_TITLE);
            nodeService.setProperty(testData.A3NodeRef, TransferModel.PROP_FROM_REPOSITORY_ID, REPO_ID_B);
            nodeService.setProperty(testData.A3NodeRef, TransferModel.PROP_REPOSITORY_ID, REPO_ID_B);
            /**
             * The repository was mocked to repoistory A. This is repository B
             */
            ChildAssociationRef child = nodeService.createNode(testData.A2NodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("A7"), ContentModel.TYPE_CONTENT);
            testData.A7NodeRef = child.getChildRef();
            nodeService.setProperty(testData.A7NodeRef, ContentModel.PROP_TITLE, NEW_TITLE);
            nodeService.setProperty(testData.A7NodeRef, ContentModel.PROP_NAME, "A7");
            nodeService.setProperty(testData.A7NodeRef, ContentModel.PROP_TITLE, NEW_TITLE);
            nodeService.setProperty(testData.A7NodeRef, TransferModel.PROP_FROM_REPOSITORY_ID, REPO_ID_B);
            nodeService.setProperty(testData.A7NodeRef, TransferModel.PROP_REPOSITORY_ID, REPO_ID_B);
            ContentWriter writer = contentService.getWriter(testData.A3NodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_STRING);
            return null;
        }
    });
    nodes.add(testData.A7NodeRef);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

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

        @Override
        public Void execute() throws Throwable {
            assertTrue("dest node A7 does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.A7NodeRef)));
            assertEquals("dest node A1 Title", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A1NodeRef), ContentModel.PROP_TITLE), NEW_TITLE);
            assertEquals("dest node A1 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A1NodeRef), TransferModel.PROP_REPOSITORY_ID), REPO_ID_B);
            assertEquals("dest node A1 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A1NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A2 Title", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A2NodeRef), ContentModel.PROP_TITLE), NEW_TITLE);
            assertEquals("dest node A2 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A2NodeRef), TransferModel.PROP_REPOSITORY_ID), REPO_ID_B);
            assertEquals("dest node A2 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A2NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            assertEquals("dest node A3 Title", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A3NodeRef), ContentModel.PROP_TITLE), NEW_TITLE);
            assertEquals("dest node A3 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A3NodeRef), TransferModel.PROP_REPOSITORY_ID), REPO_ID_B);
            assertEquals("dest node A3 Repository Id", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.A3NodeRef), TransferModel.PROP_FROM_REPOSITORY_ID), repositoryId);
            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) Pair(org.alfresco.util.Pair) HashSet(java.util.HashSet) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Aggregations

TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)38 NodeRef (org.alfresco.service.cmr.repository.NodeRef)31 TransferTarget (org.alfresco.service.cmr.transfer.TransferTarget)25 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)24 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)24 Test (org.junit.Test)24 HashSet (java.util.HashSet)21 Locale (java.util.Locale)19 DescriptorService (org.alfresco.service.descriptor.DescriptorService)18 ArrayList (java.util.ArrayList)17 Pair (org.alfresco.util.Pair)16 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)14 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)13 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)11 List (java.util.List)10 Path (org.alfresco.service.cmr.repository.Path)10 TransferEvent (org.alfresco.service.cmr.transfer.TransferEvent)8 Collection (java.util.Collection)7 QName (org.alfresco.service.namespace.QName)7 ContentReader (org.alfresco.service.cmr.repository.ContentReader)5