Search in sources :

Example 6 with WorkItem

use of io.automatiko.engine.api.runtime.process.WorkItem 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 7 with WorkItem

use of io.automatiko.engine.api.runtime.process.WorkItem 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 8 with WorkItem

use of io.automatiko.engine.api.runtime.process.WorkItem in project automatiko-engine by automatiko-io.

the class VariableTagsTest method testProcessWithRequiredVariable.

@Test
public void testProcessWithRequiredVariable() throws Exception {
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler("Human Task");
    ProcessConfig config = config(workItemHandler);
    BpmnProcess process = create(config, "variable-tags/approval-with-required-variable-tags.bpmn2");
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("approver", "john");
    ProcessInstance<BpmnVariables> instance = process.createInstance(BpmnVariables.create(parameters));
    instance.start();
    assertEquals(STATE_ACTIVE, instance.status());
    WorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    instance.completeWorkItem(workItem.getId(), null);
    workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    instance.completeWorkItem(workItem.getId(), null);
    assertEquals(STATE_COMPLETED, instance.status());
}
Also used : TestWorkItemHandler(io.automatiko.engine.workflow.bpmn2.objects.TestWorkItemHandler) BpmnProcess(io.automatiko.engine.workflow.bpmn2.BpmnProcess) ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) HashMap(java.util.HashMap) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) BpmnVariables(io.automatiko.engine.workflow.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 9 with WorkItem

use of io.automatiko.engine.api.runtime.process.WorkItem in project automatiko-engine by automatiko-io.

the class VariableTagsTest method testProcessWithReadonlyVariable.

@Test
public void testProcessWithReadonlyVariable() throws Exception {
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler("Human Task");
    ProcessConfig config = config(workItemHandler);
    BpmnProcess process = create(config, "variable-tags/approval-with-readonly-variable-tags.bpmn2");
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("approver", "john");
    ProcessInstance<BpmnVariables> instance = process.createInstance(BpmnVariables.create(parameters));
    instance.start();
    assertEquals(STATE_ACTIVE, instance.status());
    WorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertThrows(VariableViolationException.class, () -> instance.completeWorkItem(workItem.getId(), Collections.singletonMap("ActorId", "john")));
    instance.abort();
    assertEquals(STATE_ABORTED, instance.status());
}
Also used : TestWorkItemHandler(io.automatiko.engine.workflow.bpmn2.objects.TestWorkItemHandler) BpmnProcess(io.automatiko.engine.workflow.bpmn2.BpmnProcess) ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) HashMap(java.util.HashMap) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem) BpmnVariables(io.automatiko.engine.workflow.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 10 with WorkItem

use of io.automatiko.engine.api.runtime.process.WorkItem in project automatiko-engine by automatiko-io.

the class TestWorkItemHandler method getWorkItem.

public WorkItem getWorkItem() {
    if (workItems.size() == 0) {
        return null;
    }
    if (workItems.size() == 1) {
        WorkItem result = workItems.get(0);
        this.workItems.clear();
        return result;
    } else {
        throw new IllegalArgumentException("More than one work item active");
    }
}
Also used : HumanTaskWorkItem(io.automatiko.engine.api.runtime.process.HumanTaskWorkItem) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem)

Aggregations

WorkItem (io.automatiko.engine.api.runtime.process.WorkItem)22 ProcessConfig (io.automatiko.engine.api.workflow.ProcessConfig)7 Test (org.junit.jupiter.api.Test)7 WorkItemHandler (io.automatiko.engine.api.runtime.process.WorkItemHandler)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 WorkItemManager (io.automatiko.engine.api.runtime.process.WorkItemManager)5 ClassPathResource (io.automatiko.engine.services.io.ClassPathResource)5 DefaultWorkItemHandlerConfig (io.automatiko.engine.workflow.DefaultWorkItemHandlerConfig)5 ProcessInstance (io.automatiko.engine.api.runtime.process.ProcessInstance)4 ArrayList (java.util.ArrayList)4 HumanTaskWorkItem (io.automatiko.engine.api.runtime.process.HumanTaskWorkItem)3 HashMap (java.util.HashMap)3 DelayedExecution (io.automatiko.engine.api.event.process.DelayedExecution)2 ProcessCompletedEvent (io.automatiko.engine.api.event.process.ProcessCompletedEvent)2 ProcessEventListener (io.automatiko.engine.api.event.process.ProcessEventListener)2 ProcessNodeInstanceFailedEvent (io.automatiko.engine.api.event.process.ProcessNodeInstanceFailedEvent)2 ProcessNodeLeftEvent (io.automatiko.engine.api.event.process.ProcessNodeLeftEvent)2