Search in sources :

Example 6 with UnitOfWork

use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.

the class PublishEventTest method testProcessWithMilestoneEvents.

@Test
public void testProcessWithMilestoneEvents() throws Exception {
    Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
    assertThat(app).isNotNull();
    TestEventPublisher publisher = new TestEventPublisher();
    app.unitOfWorkManager().eventManager().setService("http://myhost");
    app.unitOfWorkManager().eventManager().addPublisher(publisher);
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    Process<? extends Model> p = app.processes().processById("TestCase.SimpleMilestone_1_0");
    ProcessInstance<?> processInstance = p.createInstance(p.createModel());
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    uow.end();
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    DataEvent<?> event = events.get(0);
    assertThat(event).isInstanceOf(ProcessInstanceDataEvent.class);
    ProcessInstanceDataEvent processDataEvent = (ProcessInstanceDataEvent) event;
    assertThat(processDataEvent.getSource()).isEqualTo("http://myhost/SimpleMilestone");
    Set<MilestoneEventBody> milestones = ((ProcessInstanceDataEvent) event).getData().getMilestones();
    assertThat(milestones).hasSize(2).extracting(e -> e.getName(), e -> e.getStatus()).containsExactlyInAnyOrder(tuple("AutoStartMilestone", Status.COMPLETED.name()), tuple("SimpleMilestone", Status.COMPLETED.name()));
}
Also used : AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) UserTaskInstanceEventBody(io.automatiko.engine.services.event.impl.UserTaskInstanceEventBody) MilestoneEventBody(io.automatiko.engine.services.event.impl.MilestoneEventBody) SecurityPolicy(io.automatiko.engine.api.auth.SecurityPolicy) ProcessInstanceEventBody(io.automatiko.engine.services.event.impl.ProcessInstanceEventBody) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProcessErrors(io.automatiko.engine.api.workflow.ProcessErrors) HashMap(java.util.HashMap) DataEvent(io.automatiko.engine.api.event.DataEvent) ArrayList(java.util.ArrayList) Application(io.automatiko.engine.api.Application) UserTaskInstanceDataEvent(io.automatiko.engine.services.event.UserTaskInstanceDataEvent) ProcessInstanceDataEvent(io.automatiko.engine.services.event.ProcessInstanceDataEvent) Map(java.util.Map) StaticIdentityProvider(io.automatiko.engine.services.identity.StaticIdentityProvider) Process(io.automatiko.engine.api.workflow.Process) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EventPublisher(io.automatiko.engine.api.event.EventPublisher) Model(io.automatiko.engine.api.Model) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Collection(java.util.Collection) Status(io.automatiko.engine.api.workflow.flexible.ItemDescription.Status) Set(java.util.Set) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) Test(org.junit.jupiter.api.Test) List(java.util.List) WorkItem(io.automatiko.engine.api.workflow.WorkItem) UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) MilestoneEventBody(io.automatiko.engine.services.event.impl.MilestoneEventBody) DataEvent(io.automatiko.engine.api.event.DataEvent) UserTaskInstanceDataEvent(io.automatiko.engine.services.event.UserTaskInstanceDataEvent) ProcessInstanceDataEvent(io.automatiko.engine.services.event.ProcessInstanceDataEvent) ProcessInstanceDataEvent(io.automatiko.engine.services.event.ProcessInstanceDataEvent) Application(io.automatiko.engine.api.Application) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Example 7 with UnitOfWork

use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.

the class UnitOfWorkExecutor method executeInUnitOfWork.

public static <T> T executeInUnitOfWork(UnitOfWorkManager uowManager, Supplier<T> supplier) {
    T result = null;
    UnitOfWork uow = uowManager.newUnitOfWork();
    try {
        uow.start();
        result = supplier.get();
        uow.end();
        return result;
    } catch (ProcessInstanceExecutionException e) {
        uow.end();
        throw e;
    } catch (ConflictingVersionException e) {
        LOGGER.warn("A conflict was identified for current unit of work with message '{}', aborting current unit of work and retrying", e.getMessage());
        uow.abort();
        return executeInUnitOfWork(uowManager, supplier);
    } catch (Exception e) {
        e.printStackTrace();
        uow.abort();
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    } finally {
        // reset identity provider upon completion of the unit of work
        IdentityProvider.set(null);
    }
}
Also used : UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) ProcessInstanceExecutionException(io.automatiko.engine.api.workflow.ProcessInstanceExecutionException) ProcessInstanceExecutionException(io.automatiko.engine.api.workflow.ProcessInstanceExecutionException) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException)

Example 8 with UnitOfWork

use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.

the class FileSystemProcessInstancesTest method testBasicFlowControlledByUnitOfWork.

@Test
void testBasicFlowControlledByUnitOfWork() {
    UnitOfWorkManager uowManager = new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory());
    ProcessConfig config = new StaticProcessConfig(new DefaultWorkItemHandlerConfig(), new DefaultProcessEventListenerConfig(), uowManager, null, new DefaultVariableInitializer(), new FileSystemProcessInstancesFactory());
    BpmnProcess process = createProcess(config, "BPMN2-UserTask.bpmn2");
    process.configure();
    ProcessInstance<BpmnVariables> processInstance = process.createInstance(BpmnVariables.create(Collections.singletonMap("test", "test")));
    UnitOfWork uow = uowManager.newUnitOfWork();
    uow.start();
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(STATE_ACTIVE);
    assertThat(processInstance.description()).isEqualTo("User Task");
    assertThat(process.instances().values(1, 10)).hasSize(1);
    FileSystemProcessInstances fileSystemBasedStorage = (FileSystemProcessInstances) process.instances();
    verify(fileSystemBasedStorage, times(2)).create(any(), any());
    verify(fileSystemBasedStorage, times(1)).setMetadata(any(), eq(FileSystemProcessInstances.PI_DESCRIPTION), eq("User Task"));
    verify(fileSystemBasedStorage, times(1)).setMetadata(any(), eq(FileSystemProcessInstances.PI_STATUS), eq("1"));
    String testVar = (String) processInstance.variables().get("test");
    assertThat(testVar).isEqualTo("test");
    assertThat(processInstance.description()).isEqualTo("User Task");
    WorkItem workItem = processInstance.workItems(securityPolicy).get(0);
    assertThat(workItem).isNotNull();
    assertThat(workItem.getParameters().get("ActorId")).isEqualTo("john");
    uow = uowManager.newUnitOfWork();
    uow.start();
    processInstance.completeWorkItem(workItem.getId(), null, securityPolicy);
    uow.end();
    assertThat(processInstance.status()).isEqualTo(STATE_COMPLETED);
    fileSystemBasedStorage = (FileSystemProcessInstances) process.instances();
    verify(fileSystemBasedStorage, times(1)).remove(any(), any());
    assertThat(fileSystemBasedStorage.size()).isZero();
}
Also used : BpmnProcess(io.automatiko.engine.workflow.bpmn2.BpmnProcess) UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) StaticProcessConfig(io.automatiko.engine.workflow.StaticProcessConfig) ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) StaticProcessConfig(io.automatiko.engine.workflow.StaticProcessConfig) DefaultVariableInitializer(io.automatiko.engine.workflow.base.instance.context.variable.DefaultVariableInitializer) WorkItem(io.automatiko.engine.api.workflow.WorkItem) DefaultWorkItemHandlerConfig(io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig) DefaultUnitOfWorkManager(io.automatiko.engine.services.uow.DefaultUnitOfWorkManager) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) CollectingUnitOfWorkFactory(io.automatiko.engine.services.uow.CollectingUnitOfWorkFactory) DefaultProcessEventListenerConfig(io.automatiko.engine.workflow.DefaultProcessEventListenerConfig) DefaultUnitOfWorkManager(io.automatiko.engine.services.uow.DefaultUnitOfWorkManager) BpmnVariables(io.automatiko.engine.workflow.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 9 with UnitOfWork

use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.

the class WorkflowThreadContextProvider method currentContext.

@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    UnitOfWork capturedUnitOfWork = DefaultUnitOfWorkManager.getUnitOfWork();
    IdentityProvider capturedIdentity = IdentityProvider.isSet() ? IdentityProvider.get() : null;
    return () -> {
        UnitOfWork currentUnitOfWork = DefaultUnitOfWorkManager.getUnitOfWork();
        IdentityProvider currentIdentity = IdentityProvider.isSet() ? IdentityProvider.get() : null;
        if (currentUnitOfWork != capturedUnitOfWork) {
            DefaultUnitOfWorkManager.setUnitOfWork(capturedUnitOfWork);
        }
        if (currentIdentity != capturedIdentity) {
            IdentityProvider.set(capturedIdentity);
        }
        return () -> {
            DefaultUnitOfWorkManager.setUnitOfWork(currentUnitOfWork);
            IdentityProvider.set(currentIdentity);
        };
    };
}
Also used : UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) IdentityProvider(io.automatiko.engine.api.auth.IdentityProvider)

Example 10 with UnitOfWork

use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.

the class PublishEventTest method testServiceTaskProcessWithError.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testServiceTaskProcessWithError() throws Exception {
    Application app = generateCodeProcessesOnly("servicetask/ServiceProcessDifferentOperations.bpmn2");
    assertThat(app).isNotNull();
    TestEventPublisher publisher = new TestEventPublisher();
    app.unitOfWorkManager().eventManager().setService("http://myhost");
    app.unitOfWorkManager().eventManager().addPublisher(publisher);
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    Process<? extends Model> p = app.processes().processById("ServiceProcessDifferentOperations_1_0");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance processInstance = p.createInstance(m);
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ERROR);
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
    assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("StartNode", "WorkItemNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    // human task is active
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
    // thus null for leave
    // time
    assertThat(body.getErrors()).hasSize(1);
    assertThat(body.getErrors().get(0).getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
    parameters.put("s", "john");
    m.fromMap(parameters);
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    processInstance.updateVariables(m);
    uow.end();
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
    assertThat(body.getErrors()).hasSize(1);
    assertThat(body.getErrors().get(0).getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    if (processInstance.errors().isPresent()) {
        ((ProcessErrors) processInstance.errors().get()).retrigger();
    }
    uow.end();
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 2);
    assertThat(body.getErrors()).isEmpty();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKeys("s");
    assertThat(result.toMap().get("s")).isNotNull().isEqualTo("Goodbye Hello john!!");
}
Also used : UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) HashMap(java.util.HashMap) ProcessInstanceEventBody(io.automatiko.engine.services.event.impl.ProcessInstanceEventBody) DataEvent(io.automatiko.engine.api.event.DataEvent) UserTaskInstanceDataEvent(io.automatiko.engine.services.event.UserTaskInstanceDataEvent) ProcessInstanceDataEvent(io.automatiko.engine.services.event.ProcessInstanceDataEvent) ProcessErrors(io.automatiko.engine.api.workflow.ProcessErrors) Model(io.automatiko.engine.api.Model) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) Application(io.automatiko.engine.api.Application) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Aggregations

UnitOfWork (io.automatiko.engine.api.uow.UnitOfWork)18 Test (org.junit.jupiter.api.Test)16 Application (io.automatiko.engine.api.Application)9 Model (io.automatiko.engine.api.Model)9 AbstractCodegenTest (io.automatiko.engine.codegen.AbstractCodegenTest)9 DataEvent (io.automatiko.engine.api.event.DataEvent)8 ProcessInstanceDataEvent (io.automatiko.engine.services.event.ProcessInstanceDataEvent)8 UserTaskInstanceDataEvent (io.automatiko.engine.services.event.UserTaskInstanceDataEvent)8 HashMap (java.util.HashMap)8 ProcessInstanceEventBody (io.automatiko.engine.services.event.impl.ProcessInstanceEventBody)7 WorkItem (io.automatiko.engine.api.workflow.WorkItem)6 CollectingUnitOfWork (io.automatiko.engine.services.uow.CollectingUnitOfWork)6 ManagedUnitOfWork (io.automatiko.engine.services.uow.ManagedUnitOfWork)6 PassThroughUnitOfWork (io.automatiko.engine.services.uow.PassThroughUnitOfWork)6 StaticIdentityProvider (io.automatiko.engine.services.identity.StaticIdentityProvider)4 BaseWorkUnit (io.automatiko.engine.services.uow.BaseWorkUnit)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ProcessErrors (io.automatiko.engine.api.workflow.ProcessErrors)2 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)2 IdentityProvider (io.automatiko.engine.api.auth.IdentityProvider)1