Search in sources :

Example 1 with DefaultProcessEventListener

use of io.automatiko.engine.api.event.process.DefaultProcessEventListener in project automatiko-engine by automatiko-io.

the class UserTaskTest method testBasicUserTaskProcess.

@Test
public void testBasicUserTaskProcess() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    final List<String> workItemTransitionEvents = new ArrayList<>();
    app.config().process().processEventListeners().listeners().add(new DefaultProcessEventListener() {

        @Override
        public void beforeWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("BEFORE:: " + event);
        }

        @Override
        public void afterWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("AFTER:: " + event);
        }
    });
    Process<? extends Model> p = app.processes().processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("FirstTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("SecondTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(workItemTransitionEvents).hasSize(8);
}
Also used : HashMap(java.util.HashMap) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) ArrayList(java.util.ArrayList) WorkItem(io.automatiko.engine.api.workflow.WorkItem) Model(io.automatiko.engine.api.Model) DefaultProcessEventListener(io.automatiko.engine.api.event.process.DefaultProcessEventListener) Application(io.automatiko.engine.api.Application) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultProcessEventListener

use of io.automatiko.engine.api.event.process.DefaultProcessEventListener in project automatiko-engine by automatiko-io.

the class VariableTagsTest method testProcessWithCustomVariableTag.

@Test
public void testProcessWithCustomVariableTag() throws Exception {
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler("Human Task");
    DefaultProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            if (event.hasTag("onlyAdmin")) {
                throw new VariableViolationException(event.getProcessInstance().getId(), event.getVariableId(), "Variable can only be set by admins");
            }
        }
    };
    ProcessConfig config = config(Collections.singletonList(workItemHandler), Collections.singletonList(listener));
    BpmnProcess process = create(config, "variable-tags/approval-with-custom-variable-tags.bpmn2");
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("approver", "john");
    assertThrows(VariableViolationException.class, () -> process.createInstance(BpmnVariables.create(parameters)));
}
Also used : TestWorkItemHandler(io.automatiko.engine.workflow.bpmn2.objects.TestWorkItemHandler) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) BpmnProcess(io.automatiko.engine.workflow.bpmn2.BpmnProcess) ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) HashMap(java.util.HashMap) VariableViolationException(io.automatiko.engine.api.workflow.VariableViolationException) DefaultProcessEventListener(io.automatiko.engine.api.event.process.DefaultProcessEventListener) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultProcessEventListener

use of io.automatiko.engine.api.event.process.DefaultProcessEventListener in project automatiko-engine by automatiko-io.

the class ActivityTest method testMinimalProcessMetaData.

@Test
public void testMinimalProcessMetaData() throws Exception {
    final List<String> list1 = new ArrayList<String>();
    final List<String> list2 = new ArrayList<String>();
    final List<String> list3 = new ArrayList<String>();
    final List<String> list4 = new ArrayList<String>();
    DefaultProcessEventListener listener = new DefaultProcessEventListener() {

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            logger.debug("before node");
            Map<String, Object> metaData = event.getNodeInstance().getNode().getMetaData();
            for (Map.Entry<String, Object> entry : metaData.entrySet()) {
                logger.debug(entry.getKey() + " " + entry.getValue());
            }
            String customTag = (String) metaData.get("customTag");
            if (customTag != null) {
                list1.add(customTag);
            }
            String customTag2 = (String) metaData.get("customTag2");
            if (customTag2 != null) {
                list2.add(customTag2);
            }
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            logger.debug("after variable");
            VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.base.core.impl.ProcessImpl) event.getProcessInstance().getProcess()).resolveContext(VariableScope.VARIABLE_SCOPE, event.getVariableId());
            if (variableScope == null) {
                return;
            }
            Map<String, Object> metaData = variableScope.findVariable(event.getVariableId()).getMetaData();
            for (Map.Entry<String, Object> entry : metaData.entrySet()) {
                logger.debug(entry.getKey() + " " + entry.getValue());
            }
            String customTag = (String) metaData.get("customTagVar");
            if (customTag != null) {
                list3.add(customTag);
            }
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            logger.debug("after process");
            Map<String, Object> metaData = event.getProcessInstance().getProcess().getMetaData();
            for (Map.Entry<String, Object> entry : metaData.entrySet()) {
                logger.debug(entry.getKey() + " " + entry.getValue());
            }
            String customTag = (String) metaData.get("customTagProcess");
            if (customTag != null) {
                list4.add(customTag);
            }
        }
    };
    ProcessConfig config = config(listener);
    BpmnProcess process = create(config, "BPMN2-MinimalProcessMetaData.bpmn2");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "krisv");
    ProcessInstance<BpmnVariables> instance = process.createInstance(BpmnVariables.create(params));
    instance.start();
    assertEquals(STATE_COMPLETED, instance.status());
    assertEquals(3, list1.size());
    assertEquals(2, list2.size());
    assertEquals(1, list3.size());
    assertEquals(1, list4.size());
}
Also used : ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) BpmnProcess(io.automatiko.engine.workflow.bpmn2.BpmnProcess) ProcessConfig(io.automatiko.engine.api.workflow.ProcessConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(io.automatiko.engine.api.event.process.DefaultProcessEventListener) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) HashMap(java.util.HashMap) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) BpmnVariables(io.automatiko.engine.workflow.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultProcessEventListener

use of io.automatiko.engine.api.event.process.DefaultProcessEventListener in project automatiko-engine by automatiko-io.

the class UserTaskTest method testBasicUserTaskProcessVersioned.

@Test
public void testBasicUserTaskProcessVersioned() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcessVersion.bpmn2", "usertask/UserTasksProcessVersion2.bpmn2");
    assertThat(app).isNotNull();
    final List<String> workItemTransitionEvents = new ArrayList<>();
    app.config().process().processEventListeners().listeners().add(new DefaultProcessEventListener() {

        @Override
        public void beforeWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("BEFORE:: " + event);
        }

        @Override
        public void afterWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("AFTER:: " + event);
        }
    });
    // verify that both versions have corresponding process definitions
    assertThat(app.processes().processById("UserTasksProcess_2")).isNotNull();
    Process<? extends Model> p = app.processes().processById("UserTasksProcess_1");
    assertThat(p).isNotNull();
    // verify that both version have corresponding data model classes
    Class<?> model1Class = Class.forName("org.kie.kogito.test.UserTasksProcess_1Model", true, testClassLoader());
    assertNotNull(model1Class);
    Class<?> model2Class = Class.forName("org.kie.kogito.test.UserTasksProcess_2Model", true, testClassLoader());
    assertNotNull(model2Class);
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("FirstTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("SecondTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(workItemTransitionEvents).hasSize(8);
}
Also used : HashMap(java.util.HashMap) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) ArrayList(java.util.ArrayList) WorkItem(io.automatiko.engine.api.workflow.WorkItem) Model(io.automatiko.engine.api.Model) DefaultProcessEventListener(io.automatiko.engine.api.event.process.DefaultProcessEventListener) Application(io.automatiko.engine.api.Application) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultProcessEventListener

use of io.automatiko.engine.api.event.process.DefaultProcessEventListener in project automatiko-engine by automatiko-io.

the class UserTaskTest method testBasicUserTaskProcessClaimAndCompletePhasesWithIdentity.

@Test
public void testBasicUserTaskProcessClaimAndCompletePhasesWithIdentity() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    final List<String> workItemTransitionEvents = new ArrayList<>();
    ((DefaultProcessEventListenerConfig) app.config().process().processEventListeners()).listeners().add(new DefaultProcessEventListener() {

        @Override
        public void beforeWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("BEFORE:: " + event);
        }

        @Override
        public void afterWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("AFTER:: " + event);
        }
    });
    Process<? extends Model> p = app.processes().processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    WorkItem wi = workItems.get(0);
    assertEquals("FirstTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Claim.ID, Collections.singletonMap("test", "value"), securityPolicy));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    wi = workItems.get(0);
    assertEquals("FirstTask", wi.getName());
    assertEquals(Claim.ID, wi.getPhase());
    assertEquals(Claim.STATUS, wi.getPhaseStatus());
    assertEquals(2, wi.getResults().size());
    assertEquals("value", wi.getResults().get("test"));
    assertEquals("john", wi.getResults().get("ActorId"));
    processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Complete.ID, null, securityPolicy));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    wi = workItems.get(0);
    assertEquals("SecondTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    processInstance.abort();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
    assertThat(workItemTransitionEvents).hasSize(10);
}
Also used : HashMap(java.util.HashMap) ProcessWorkItemTransitionEvent(io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent) ArrayList(java.util.ArrayList) WorkItem(io.automatiko.engine.api.workflow.WorkItem) HumanTaskTransition(io.automatiko.engine.workflow.base.instance.impl.humantask.HumanTaskTransition) Model(io.automatiko.engine.api.Model) DefaultProcessEventListener(io.automatiko.engine.api.event.process.DefaultProcessEventListener) Application(io.automatiko.engine.api.Application) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultProcessEventListener (io.automatiko.engine.api.event.process.DefaultProcessEventListener)5 HashMap (java.util.HashMap)5 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 Application (io.automatiko.engine.api.Application)3 Model (io.automatiko.engine.api.Model)3 ProcessWorkItemTransitionEvent (io.automatiko.engine.api.event.process.ProcessWorkItemTransitionEvent)3 WorkItem (io.automatiko.engine.api.workflow.WorkItem)3 AbstractCodegenTest (io.automatiko.engine.codegen.AbstractCodegenTest)3 ProcessVariableChangedEvent (io.automatiko.engine.api.event.process.ProcessVariableChangedEvent)2 ProcessConfig (io.automatiko.engine.api.workflow.ProcessConfig)2 BpmnProcess (io.automatiko.engine.workflow.bpmn2.BpmnProcess)2 ProcessNodeTriggeredEvent (io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent)1 ProcessStartedEvent (io.automatiko.engine.api.event.process.ProcessStartedEvent)1 VariableViolationException (io.automatiko.engine.api.workflow.VariableViolationException)1 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)1 HumanTaskTransition (io.automatiko.engine.workflow.base.instance.impl.humantask.HumanTaskTransition)1 BpmnVariables (io.automatiko.engine.workflow.bpmn2.BpmnVariables)1 TestWorkItemHandler (io.automatiko.engine.workflow.bpmn2.objects.TestWorkItemHandler)1 Map (java.util.Map)1