Search in sources :

Example 1 with RuntimeActionService

use of org.alfresco.repo.action.RuntimeActionService in project alfresco-repository by Alfresco.

the class ReplicationServiceIntegrationTest method testBasicExecution.

/**
 * Test that the action service can find the executor
 *  for us, and that it has everything it needs
 */
public void testBasicExecution() throws Exception {
    // We need the test transfer target for this test
    makeTransferTarget();
    // Ensure the destination is empty
    // (don't want to get confused with older runs)
    assertEquals(0, nodeService.getChildAssocs(destinationFolder).size());
    // First one with no target, which isn't allowed
    ReplicationDefinition rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted with no Target defined");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Now no payload, also not allowed
    rd.setTargetName(TRANSFER_TARGET);
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted with no payload defined");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Invalid Transfer Target, not allowed
    rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    rd.setTargetName("I am an invalid target that isn't there");
    rd.getPayload().add(folder1);
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted with an invalid transfer target");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Can't send Folder2a if Folder2 isn't there, as it
    // won't have anywhere to put it
    rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    rd.setTargetName(TRANSFER_TARGET);
    rd.getPayload().add(folder2a);
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be able to send Folder2a when Folder2 is missing!");
    } catch (ReplicationServiceException e) {
    }
    txn.rollback();
    // Next a proper one with a transient definition,
    // and a sensible set of folders
    rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    rd.setTargetName(TRANSFER_TARGET);
    rd.getPayload().add(folder1);
    // A deleted folder is fine, will be skipped
    rd.getPayload().add(deletedFolder);
    // Will execute without error
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
    } catch (ReplicationServiceException e) {
        // This shouldn't happen normally! Something is wrong!
        // Tidy up before we throw the exception
        txn.rollback();
        throw e;
    }
    txn.commit();
    // Now with one that's in the repo
    ReplicationDefinition rd2 = replicationService.createReplicationDefinition(ACTION_NAME2, "Test");
    rd2.setTargetName(TRANSFER_TARGET);
    rd2.getPayload().add(folder2);
    replicationService.saveReplicationDefinition(rd2);
    rd2 = replicationService.loadReplicationDefinition(ACTION_NAME2);
    // Again no errors
    txn = transactionService.getUserTransaction();
    txn.begin();
    actionService.executeAction(rd2, replicationRoot);
    txn.commit();
    // Now disabled, not allowed
    assertEquals(true, rd.isEnabled());
    rd.setEnabled(false);
    assertEquals(false, rd.isEnabled());
    txn = transactionService.getUserTransaction();
    txn.begin();
    try {
        actionService.executeAction(rd, replicationRoot);
        fail("Shouldn't be permitted when disabled");
    } catch (ReplicationServiceException e) {
        // check if throwed exception is of expected type
        assertTrue(e instanceof DisabledReplicationJobException);
        assertTrue(actionService instanceof RuntimeActionService);
        if (actionService instanceof RuntimeActionService) {
            RuntimeActionService runtimeActionService = (RuntimeActionService) actionService;
            // check if throwed exception is considered handled
            assertTrue(runtimeActionService.onLogException(rd, log, e, e.getMessage()));
        }
    }
    txn.rollback();
    rd.setEnabled(true);
    // Schedule it for 0.5 seconds into the future
    // Ensure that it is run to completion
    txn = transactionService.getUserTransaction();
    txn.begin();
    ((ActionImpl) rd2).setExecutionStatus(ActionStatus.New);
    replicationService.enableScheduling(rd2);
    rd2.setScheduleStart(new Date(System.currentTimeMillis() + 500));
    replicationService.saveReplicationDefinition(rd2);
    txn.commit();
    // Wait for it to run
    Thread.sleep(2000);
    for (int i = 0; i < 100; i++) {
        txn = transactionService.getUserTransaction();
        txn.begin();
        rd2 = replicationService.loadReplicationDefinition(ACTION_NAME2);
        txn.commit();
        if (rd2.getExecutionStatus().equals(ActionStatus.New) || rd2.getExecutionStatus().equals(ActionStatus.Pending) || rd2.getExecutionStatus().equals(ActionStatus.Running)) {
            Thread.sleep(50);
        }
    }
    // Check it worked
    assertEquals(ActionStatus.Completed, rd2.getExecutionStatus());
}
Also used : UserTransaction(javax.transaction.UserTransaction) DisabledReplicationJobException(org.alfresco.service.cmr.replication.DisabledReplicationJobException) ReplicationServiceException(org.alfresco.service.cmr.replication.ReplicationServiceException) ScriptReplicationDefinition(org.alfresco.repo.replication.script.ScriptReplicationDefinition) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) RuntimeActionService(org.alfresco.repo.action.RuntimeActionService) ActionImpl(org.alfresco.repo.action.ActionImpl) Date(java.util.Date)

Aggregations

Date (java.util.Date)1 UserTransaction (javax.transaction.UserTransaction)1 ActionImpl (org.alfresco.repo.action.ActionImpl)1 RuntimeActionService (org.alfresco.repo.action.RuntimeActionService)1 ScriptReplicationDefinition (org.alfresco.repo.replication.script.ScriptReplicationDefinition)1 DisabledReplicationJobException (org.alfresco.service.cmr.replication.DisabledReplicationJobException)1 ReplicationDefinition (org.alfresco.service.cmr.replication.ReplicationDefinition)1 ReplicationServiceException (org.alfresco.service.cmr.replication.ReplicationServiceException)1