Search in sources :

Example 1 with DefaultWorkItemHandlerConfig

use of io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig in project automatiko-engine by automatiko-io.

the class OperationStateWorkflowsTest method testRestOperationStateWorkflow.

@Test
public void testRestOperationStateWorkflow() throws Exception {
    ProcessConfig processConfig = ServerlessProcess.processConfig();
    ((DefaultWorkItemHandlerConfig) processConfig.workItemHandlers()).register("Service Task", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            System.out.println(workItem.getParameters());
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode data = mapper.createObjectNode();
            data.put("greeting", "test");
            manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    ServerlessProcess process = ServerlessProcess.from(processConfig, new ClassPathResource("examples/greeting.json")).get(0);
    assertThat(process).isNotNull();
    JsonNode data = new ObjectMapper().readTree("{\n" + "  \"person\": {\"name\" : \"john\"}\n" + "}");
    ServerlessProcessInstance pi = (ServerlessProcessInstance) process.createInstance(ServerlessModel.from(data));
    pi.start();
    assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(pi.variables().toMap()).hasSize(2).containsKey("greeting").extracting("greeting").isEqualTo(new TextNode("test"));
}
Also used : ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) DefaultWorkItemHandlerConfig(io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig) ClassPathResource(io.automatiko.engine.services.io.ClassPathResource) WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) WorkItemManager(io.automatiko.engine.api.runtime.process.WorkItemManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultWorkItemHandlerConfig

use of io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig in project automatiko-engine by automatiko-io.

the class SwitchStateWorkflowsTest method testSwitchWithDataConditions.

@Test
public void testSwitchWithDataConditions() throws Exception {
    ProcessConfig processConfig = ServerlessProcess.processConfig();
    ((DefaultWorkItemHandlerConfig) processConfig.workItemHandlers()).register("Service Task", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode data = mapper.createObjectNode();
            if ("emailStart".equals(workItem.getParameter("Operation"))) {
                data.put("status", "accepted");
                manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
            } else if ("emailRejection".equals(workItem.getParameter("Operation"))) {
                data.put("status", "rejected");
                manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
            }
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    ServerlessProcess process = ServerlessProcess.from(processConfig, new ClassPathResource("switch-state/data-condition.json")).get(0);
    assertThat(process).isNotNull();
    JsonNode data = new ObjectMapper().readTree("{\"applicant\" : {\"name\":\"John\", \"age\":20}}");
    ServerlessProcessInstance pi = (ServerlessProcessInstance) process.createInstance(ServerlessModel.from(data));
    pi.start();
    assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(pi.variables().toMap()).hasSize(2).containsKey("status").extracting("status").isEqualTo(new TextNode("accepted"));
    data = new ObjectMapper().readTree("{\"applicant\" : {\"name\":\"Mary\", \"age\":10}}");
    pi = (ServerlessProcessInstance) process.createInstance(ServerlessModel.from(data));
    pi.start();
    assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(pi.variables().toMap()).hasSize(2).containsKey("status").extracting("status").isEqualTo(new TextNode("rejected"));
}
Also used : ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) DefaultWorkItemHandlerConfig(io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig) ClassPathResource(io.automatiko.engine.services.io.ClassPathResource) WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) WorkItemManager(io.automatiko.engine.api.runtime.process.WorkItemManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultWorkItemHandlerConfig

use of io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig 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 4 with DefaultWorkItemHandlerConfig

use of io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig in project automatiko-engine by automatiko-io.

the class ServerlessProcess method processConfig.

public static ProcessConfig processConfig() {
    DefaultWorkItemHandlerConfig workItemHandlerConfig = new DefaultWorkItemHandlerConfig();
    DefaultProcessEventListenerConfig processEventListenerConfig = new DefaultProcessEventListenerConfig();
    return new StaticProcessConfig(workItemHandlerConfig, processEventListenerConfig, new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()), null, new DefaultVariableInitializer(), null);
}
Also used : StaticProcessConfig(io.automatiko.engine.workflow.StaticProcessConfig) CollectingUnitOfWorkFactory(io.automatiko.engine.services.uow.CollectingUnitOfWorkFactory) DefaultProcessEventListenerConfig(io.automatiko.engine.workflow.DefaultProcessEventListenerConfig) DefaultUnitOfWorkManager(io.automatiko.engine.services.uow.DefaultUnitOfWorkManager) DefaultVariableInitializer(io.automatiko.engine.workflow.base.instance.context.variable.DefaultVariableInitializer) DefaultWorkItemHandlerConfig(io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig)

Example 5 with DefaultWorkItemHandlerConfig

use of io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig in project automatiko-engine by automatiko-io.

the class EventStateWorkflowsTest method testEventStateWorkflow.

@Test
public void testEventStateWorkflow() throws Exception {
    ProcessConfig processConfig = ServerlessProcess.processConfig();
    ((DefaultWorkItemHandlerConfig) processConfig.workItemHandlers()).register("Service Task", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            System.out.println(workItem.getParameters());
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode data = mapper.createObjectNode();
            data.put("greeting", "Hello " + ((TextNode) workItem.getParameter("name")).asText());
            manager.completeWorkItem(workItem.getId(), Collections.singletonMap(JsonVariableScope.WORKFLOWDATA_KEY, data));
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    ServerlessProcess process = ServerlessProcess.from(processConfig, new ClassPathResource("event-state/event-state-greeting2.json")).get(0);
    assertThat(process).isNotNull();
    ServerlessProcessInstance pi = (ServerlessProcessInstance) process.createInstance();
    pi.start();
    JsonNode data = new ObjectMapper().readTree("{\n" + "  \"data\": {\"greet\" : { \"name\" : \"john\"}}\n" + "}");
    pi.send(Sig.of("Message-GreetingEvent", data));
    assertThat(pi.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(pi.variables().toMap()).hasSize(3).containsKey("greeting").extracting("greeting").isEqualTo(new TextNode("Hello john"));
}
Also used : ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) DefaultWorkItemHandlerConfig(io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig) ClassPathResource(io.automatiko.engine.services.io.ClassPathResource) WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) WorkItemManager(io.automatiko.engine.api.runtime.process.WorkItemManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultWorkItemHandlerConfig (io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig)8 ProcessConfig (io.automatiko.engine.api.workflow.ProcessConfig)7 Test (org.junit.jupiter.api.Test)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 TextNode (com.fasterxml.jackson.databind.node.TextNode)5 WorkItem (io.automatiko.engine.api.runtime.process.WorkItem)5 WorkItemHandler (io.automatiko.engine.api.runtime.process.WorkItemHandler)5 WorkItemManager (io.automatiko.engine.api.runtime.process.WorkItemManager)5 ClassPathResource (io.automatiko.engine.services.io.ClassPathResource)5 CollectingUnitOfWorkFactory (io.automatiko.engine.services.uow.CollectingUnitOfWorkFactory)3 DefaultUnitOfWorkManager (io.automatiko.engine.services.uow.DefaultUnitOfWorkManager)3 DefaultProcessEventListenerConfig (io.automatiko.engine.workflow.DefaultProcessEventListenerConfig)3 StaticProcessConfig (io.automatiko.engine.workflow.StaticProcessConfig)3 DefaultVariableInitializer (io.automatiko.engine.workflow.base.instance.context.variable.DefaultVariableInitializer)3 UnitOfWork (io.automatiko.engine.api.uow.UnitOfWork)1 UnitOfWorkManager (io.automatiko.engine.api.uow.UnitOfWorkManager)1 WorkItem (io.automatiko.engine.api.workflow.WorkItem)1 BpmnProcess (io.automatiko.engine.workflow.bpmn2.BpmnProcess)1