use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.
the class TransferServiceImplTest method testTransferOneNodeWithBigContent.
/**
* Test the transfer method with regard to big content.
*
* This test takes a long time to run and is by default not run in the overnight build.
*
* Turn it on by turning debug logging on for this class or by changing the "runTest" value;
*/
@Test
public void testTransferOneNodeWithBigContent() throws Exception {
/**
* This test takes a long time to run - so switch it on and off here.
*/
boolean runTest = false;
if (runTest || logger.isDebugEnabled()) {
final String CONTENT_TITLE = "ContentTitle";
final String CONTENT_NAME = "BigContent";
final Locale CONTENT_LOCALE = Locale.UK;
logger.debug("testTransferOneNodeWithBigContent starting");
final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
/**
* Unit test kludge to transfer from guest home to company home
*/
final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
final String targetName = "testTransferOneNodeWithBigContent";
class TestContext {
TransferTarget transferMe;
NodeRef contentNodeRef;
NodeRef destNodeRef;
}
;
RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {
@Override
public TestContext execute() throws Throwable {
TestContext ctx = new TestContext();
NodeRef guestHome = repositoryHelper.getGuestHome();
ctx.contentNodeRef = nodeService.getChildByName(guestHome, ContentModel.ASSOC_CONTAINS, CONTENT_NAME);
if (ctx.contentNodeRef == null) {
/**
* Create a test node that we will read and write
*/
ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(CONTENT_NAME), ContentModel.TYPE_CONTENT);
File tempFile = TempFileProvider.createTempFile("test", ".dat");
FileWriter fw = new FileWriter(tempFile);
for (int i = 0; i < 100000000; i++) {
fw.write("hello world this is my text, I wonder how much text I can transfer?" + i);
}
System.out.println("Temp File Size is:" + tempFile.length());
fw.close();
ctx.contentNodeRef = child.getChildRef();
ContentWriter writer = contentService.getWriter(ctx.contentNodeRef, ContentModel.PROP_CONTENT, true);
writer.setLocale(CONTENT_LOCALE);
writer.setMimetype("application/data");
writer.putContent(tempFile);
tempFile.delete();
nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(ctx.contentNodeRef, ContentModel.PROP_NAME, CONTENT_NAME);
}
if (!transferService.targetExists(targetName)) {
createTransferTarget(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();
Set<NodeRef> nodes = new HashSet<NodeRef>();
nodes.add(testContext.contentNodeRef);
definition.setNodes(nodes);
transferService.transfer(targetName, definition);
return null;
}
};
RetryingTransactionCallback<Void> finishCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
NodeRef oldDestNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef);
ContentReader source = contentService.getReader(testContext.contentNodeRef, ContentModel.PROP_CONTENT);
ContentReader destination = contentService.getReader(oldDestNodeRef, ContentModel.PROP_CONTENT);
assertNotNull("source is null", source);
assertNotNull("destination is null", destination);
assertEquals("size different", source.getSize(), destination.getSize());
/**
* Now get rid of the transferred node so that the test can run again.
*/
nodeService.deleteNode(oldDestNodeRef);
return null;
}
};
/**
* This is the test
*/
tran.doInTransaction(transferCB);
tran.doInTransaction(finishCB);
} else {
System.out.println("test supressed");
}
}
use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.
the class TransferServiceImplTest method testManyNodes.
// test move node
/**
* Test the transfer method by sending a graph of nodes.
*
* This is a unit test so it does some shenanigans to send to he same instance of alfresco.
*/
@Test
public void testManyNodes() throws Exception {
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 nodeB = null;
NodeRef nodeAA = null;
NodeRef nodeAB = null;
NodeRef nodeABA = null;
NodeRef nodeABB = null;
NodeRef nodeABC = null;
}
;
/**
* Unit test kludge to transfer from guest home to company home
*/
final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
TransferTarget transferMe;
RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {
@Override
public TestContext execute() throws Throwable {
TestContext ctx = new TestContext();
NodeRef guestHome = repositoryHelper.getGuestHome();
/**
* Create a test node that we will read and write
*/
String guid = GUID.generate();
/**
* Create a tree
* ManyNodesRoot
* AC (Content Node)
* A (Folder)
* --AA
* --AB (Folder)
* ----ABA (Folder)
* -------- 100+ nodes
* ----ABB
* ----ABC
* B
*/
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("testNodeAC"), ContentModel.TYPE_CONTENT);
NodeRef nodeAC = child.getChildRef();
nodeService.setProperty(nodeAC, ContentModel.PROP_TITLE, CONTENT_TITLE + "AC");
nodeService.setProperty(nodeAC, ContentModel.PROP_NAME, "DemoNodeAC");
{
ContentWriter writer = contentService.getWriter(nodeAC, ContentModel.PROP_CONTENT, true);
writer.setLocale(CONTENT_LOCALE);
writer.putContent(CONTENT_STRING);
nodes.add(nodeAC);
}
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);
child = nodeService.createNode(testRootNode, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeB"), ContentModel.TYPE_FOLDER);
ctx.nodeB = child.getChildRef();
nodeService.setProperty(ctx.nodeB, ContentModel.PROP_TITLE, "TestNodeB");
nodeService.setProperty(ctx.nodeB, ContentModel.PROP_NAME, "TestNodeB");
nodes.add(ctx.nodeB);
child = nodeService.createNode(ctx.nodeA, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeAA"), ContentModel.TYPE_FOLDER);
ctx.nodeAA = child.getChildRef();
nodeService.setProperty(ctx.nodeAA, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(ctx.nodeAA, ContentModel.PROP_NAME, "DemoNodeAA");
nodes.add(ctx.nodeAA);
child = nodeService.createNode(ctx.nodeA, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeAB"), ContentModel.TYPE_FOLDER);
ctx.nodeAB = child.getChildRef();
nodeService.setProperty(ctx.nodeAB, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(ctx.nodeAB, ContentModel.PROP_NAME, "DemoNodeAB");
nodes.add(ctx.nodeAB);
child = nodeService.createNode(ctx.nodeAB, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeABA"), ContentModel.TYPE_FOLDER);
ctx.nodeABA = child.getChildRef();
nodeService.setProperty(ctx.nodeABA, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(ctx.nodeABA, ContentModel.PROP_NAME, "DemoNodeABA");
nodes.add(ctx.nodeABA);
child = nodeService.createNode(ctx.nodeAB, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeABB"), ContentModel.TYPE_FOLDER);
ctx.nodeABB = child.getChildRef();
nodeService.setProperty(ctx.nodeABB, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(ctx.nodeABB, ContentModel.PROP_NAME, "DemoNodeABB");
nodes.add(ctx.nodeABB);
child = nodeService.createNode(ctx.nodeAB, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeABC"), ContentModel.TYPE_FOLDER);
ctx.nodeABC = child.getChildRef();
nodeService.setProperty(ctx.nodeABC, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(ctx.nodeABC, ContentModel.PROP_NAME, "DemoNodeABC");
nodes.add(ctx.nodeABC);
/**
* 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> checkTransferCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
NodeRef destNodeA;
NodeRef destNodeB;
NodeRef destNodeAA;
NodeRef destNodeAB;
NodeRef destNodeABA;
NodeRef destNodeABB;
NodeRef destNodeABC;
// Now validate that the target node exists and has similar properties to the source
destNodeA = testNodeFactory.getMappedNodeRef(testContext.nodeA);
assertFalse("unit test stuffed up - comparing with self", destNodeA.equals(testContext.transferMe.getNodeRef()));
assertTrue("dest node ref A does not exist", nodeService.exists(destNodeA));
assertEquals("title is wrong", (String) nodeService.getProperty(destNodeA, ContentModel.PROP_TITLE), "TestNodeA");
assertEquals("type is wrong", nodeService.getType(testContext.nodeA), nodeService.getType(destNodeA));
destNodeB = testNodeFactory.getMappedNodeRef(testContext.nodeB);
assertTrue("dest node B does not exist", nodeService.exists(destNodeB));
destNodeAA = testNodeFactory.getMappedNodeRef(testContext.nodeAA);
assertTrue("dest node AA ref does not exist", nodeService.exists(destNodeAA));
destNodeAB = testNodeFactory.getMappedNodeRef(testContext.nodeAB);
assertTrue("dest node AB ref does not exist", nodeService.exists(destNodeAB));
destNodeABA = testNodeFactory.getMappedNodeRef(testContext.nodeABA);
assertTrue("dest node ABA ref does not exist", nodeService.exists(destNodeABA));
destNodeABB = testNodeFactory.getMappedNodeRef(testContext.nodeABB);
assertTrue("dest node ABB ref does not exist", nodeService.exists(destNodeABB));
destNodeABC = testNodeFactory.getMappedNodeRef(testContext.nodeABC);
assertTrue("dest node ABC ref does not exist", nodeService.exists(destNodeABC));
return null;
}
};
tran.doInTransaction(checkTransferCB);
/**
* Update a single node (NodeAB) from the middle of the tree
*/
RetryingTransactionCallback<Void> updateSingleNodeCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
nodeService.setProperty(testContext.nodeAB, ContentModel.PROP_TITLE, CONTENT_TITLE_UPDATED);
TransferDefinition definition = new TransferDefinition();
Set<NodeRef> toUpdate = new HashSet<NodeRef>();
toUpdate.add(testContext.nodeAB);
definition.setNodes(toUpdate);
transferService.transfer(targetName, definition);
return null;
}
};
tran.doInTransaction(updateSingleNodeCB);
/**
* Now generate a large number of nodes
*/
RetryingTransactionCallback<Void> generateLotsOfNodesCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
for (int i = 0; i < 100; i++) {
ChildAssociationRef child = nodeService.createNode(testContext.nodeABA, ContentModel.ASSOC_CONTAINS, QName.createQName(GUID.generate() + i), ContentModel.TYPE_CONTENT);
NodeRef nodeX = child.getChildRef();
nodeService.setProperty(nodeX, ContentModel.PROP_TITLE, CONTENT_TITLE + i);
nodeService.setProperty(nodeX, ContentModel.PROP_NAME, CONTENT_NAME + i);
nodes.add(nodeX);
ContentWriter writer = contentService.getWriter(nodeX, ContentModel.PROP_CONTENT, true);
writer.setLocale(CONTENT_LOCALE);
writer.putContent(CONTENT_STRING + i);
}
return null;
}
};
tran.doInTransaction(generateLotsOfNodesCB);
/**
* Transfer our transfer target nodes
*/
tran.doInTransaction(transferCB);
}
use of org.alfresco.service.cmr.transfer.TransferDefinition 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);
}
Aggregations