use of org.alfresco.service.cmr.transfer.TransferEvent in project alfresco-repository by Alfresco.
the class XMLTransferReportWriter method writeTransferEvents.
/**
* Write the transfer manifest header
*/
public void writeTransferEvents(List<TransferEvent> events) throws SAXException {
writer.startElement(TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI, TransferReportModel.LOCALNAME_TRANSFER_EVENTS, PREFIX + ":" + TransferReportModel.LOCALNAME_TRANSFER_EVENTS, EMPTY_ATTRIBUTES);
for (TransferEvent event : events) {
writeTransferEvent(event);
}
writer.endElement(TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI, TransferReportModel.LOCALNAME_TRANSFER_EVENTS, PREFIX + ":" + TransferReportModel.LOCALNAME_TRANSFER_EVENTS);
}
use of org.alfresco.service.cmr.transfer.TransferEvent in project alfresco-repository by Alfresco.
the class TransferServiceCallbackTest method testErrorDuringCommit.
public void testErrorDuringCommit() {
Exception error = new TransferException("Commit failed");
TransferProgress status0 = new TransferProgress();
status0.setStatus(Status.COMMIT_REQUESTED);
status0.setCurrentPosition(0);
status0.setEndPosition(0);
TransferProgress status1 = new TransferProgress();
status1.setStatus(Status.COMMITTING);
status1.setCurrentPosition(0);
status1.setEndPosition(4);
TransferProgress status2 = new TransferProgress();
status2.setStatus(Status.COMMITTING);
status2.setCurrentPosition(3);
status2.setEndPosition(4);
TransferProgress status3 = new TransferProgress();
status3.setStatus(Status.COMMITTING);
status3.setCurrentPosition(5);
status3.setEndPosition(8);
TransferProgress status4 = new TransferProgress();
status4.setStatus(Status.ERROR);
status4.setCurrentPosition(8);
status4.setEndPosition(8);
status4.setError(error);
TransferProgress[] statuses = new TransferProgress[] { status0, status1, status2, status3, status4 };
configureBasicMockTransmitter(statuses);
when(mockedTransferTransmitter.begin(target, localRepositoryId, version)).thenReturn(transfer);
TransferDefinition transferDef = new TransferDefinition();
transferDef.setNodes(folder1, file1, file2, file3);
try {
transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
fail();
} catch (TransferFailureException ex) {
List<TransferEvent> expectedEvents = new ArrayList<TransferEvent>();
TransferEventImpl event;
event = new TransferEventEnterState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventBegin();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventSendingSnapshot();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventSendingContent();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventSendingContent();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventSendingContent();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.PREPARING);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.PREPARING);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventError();
event.setTransferState(TransferState.ERROR);
((TransferEventError) event).setException(error);
expectedEvents.add(event);
verifyCallback(expectedEvents);
}
}
use of org.alfresco.service.cmr.transfer.TransferEvent in project alfresco-repository by Alfresco.
the class TransferServiceCallbackTest method testSendContentFailed.
public void testSendContentFailed() {
TransferProgress status0 = new TransferProgress();
status0.setStatus(Status.CANCELLED);
status0.setCurrentPosition(0);
status0.setEndPosition(0);
TransferProgress[] statuses = new TransferProgress[] { status0 };
configureBasicMockTransmitter(statuses);
when(mockedTransferTransmitter.begin(target, localRepositoryId, version)).thenReturn(transfer);
doThrow(new TransferException("Simulate failure to write content")).when(mockedTransferTransmitter).sendManifest(any(Transfer.class), any(File.class), any(OutputStream.class));
when(mockedTransferTransmitter.getStatus(transfer)).thenReturn(statuses[0]);
TransferDefinition transferDef = new TransferDefinition();
transferDef.setNodes(folder1, file1, file2, file3);
try {
transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
fail("Transfer expected to throw an exception, but it didn't.");
} catch (TransferFailureException ex) {
List<TransferEvent> expectedEvents = new ArrayList<TransferEvent>();
TransferEventImpl event;
event = new TransferEventEnterState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventBegin();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventSendingSnapshot();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventError();
event.setTransferState(TransferState.ERROR);
((TransferEventError) event).setException((Exception) ex.getCause());
expectedEvents.add(event);
verifyCallback(expectedEvents);
}
}
use of org.alfresco.service.cmr.transfer.TransferEvent in project alfresco-repository by Alfresco.
the class TransferServiceCallbackTest method testCompleteSuccess.
public void testCompleteSuccess() {
TransferProgress status0 = new TransferProgress();
status0.setStatus(Status.COMMIT_REQUESTED);
status0.setCurrentPosition(0);
status0.setEndPosition(0);
TransferProgress status1 = new TransferProgress();
status1.setStatus(Status.COMMITTING);
status1.setCurrentPosition(0);
status1.setEndPosition(4);
TransferProgress status2 = new TransferProgress();
status2.setStatus(Status.COMMITTING);
status2.setCurrentPosition(3);
status2.setEndPosition(4);
TransferProgress status3 = new TransferProgress();
status3.setStatus(Status.COMMITTING);
status3.setCurrentPosition(5);
status3.setEndPosition(8);
TransferProgress status4 = new TransferProgress();
status4.setStatus(Status.COMPLETE);
status4.setCurrentPosition(8);
status4.setEndPosition(8);
TransferProgress[] statuses = new TransferProgress[] { status0, status1, status2, status3, status4 };
configureBasicMockTransmitter(statuses);
when(mockedTransferTransmitter.begin(target, localRepositoryId, version)).thenReturn(transfer);
TransferDefinition transferDef = new TransferDefinition();
transferDef.setNodes(folder1, file1, file2, file3);
transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
List<TransferEvent> expectedEvents = new ArrayList<TransferEvent>();
TransferEventImpl event;
event = new TransferEventEnterState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventBegin();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventSendingSnapshot();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.SENDING_SNAPSHOT);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventSendingContent();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventSendingContent();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventSendingContent();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.SENDING_CONTENT);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.PREPARING);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.PREPARING);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventCommittingStatus();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.SUCCESS);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.SUCCESS);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.SUCCESS);
expectedEvents.add(event);
event = new TransferEventSuccess();
event.setTransferState(TransferState.SUCCESS);
expectedEvents.add(event);
verifyCallback(expectedEvents);
}
use of org.alfresco.service.cmr.transfer.TransferEvent in project alfresco-repository by Alfresco.
the class TransferServiceImplTest method testAsyncCancel.
// test async callback
/**
* Test the transfer cancel 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 testAsyncCancel() throws Exception {
final int MAX_SLEEPS = 5;
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";
final NodeRef guestHome = repositoryHelper.getGuestHome();
class TestContext {
TransferTarget transferMe;
NodeRef nodeRefA = null;
NodeRef nodeRefB = null;
}
;
/**
* Unit test kludge to transfer from guest home to company home
*/
final UnitTestTransferManifestNodeFactory testNodeFactory = unitTestKludgeToTransferGuestHomeToCompanyHome();
RetryingTransactionCallback<TestContext> setupCB = new RetryingTransactionCallback<TestContext>() {
@Override
public TestContext execute() throws Throwable {
TestContext ctx = new TestContext();
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 = transactionService.getRetryingTransactionHelper().doInTransaction(setupCB, false, true);
/**
* The transfer report is a plain report of the transfer - no async shenanigans to worry about
*/
final List<TransferEvent> transferReport = new ArrayList<TransferEvent>(50);
RetryingTransactionCallback<Void> transferAsyncCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
/**
* The poison callback will cancel the transfer after
* the begin
*/
TransferCallback poison = new TransferCallback() {
String transferId = null;
public void processEvent(TransferEvent event) {
logger.debug(event.toString());
if (event instanceof TransferEventBegin) {
TransferEventBegin beginEvent = (TransferEventBegin) event;
transferId = beginEvent.getTransferId();
transferService.cancelAsync(transferId);
}
}
};
TestTransferCallback callback = new TestTransferCallback();
Set<TransferCallback> callbacks = new HashSet<TransferCallback>();
callbacks.add(callback);
callbacks.add(poison);
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 null;
}
};
transactionService.getRetryingTransactionHelper().doInTransaction(transferAsyncCB);
/**
* Now validate the transferReport
*/
assertTrue("transfer report is too small", transferReport.size() > 3);
assertTrue("transfer report does not start with START", transferReport.get(0).getTransferState().equals(TransferEvent.TransferState.START));
assertTrue("transfer report does not end with CANCELLED", transferReport.get(transferReport.size() - 1).getTransferState().equals(TransferEvent.TransferState.CANCELLED));
// last event is the transfer report event.
}
Aggregations