Search in sources :

Example 1 with TestWorkItemHandler

use of org.jbpm.process.test.TestWorkItemHandler in project jbpm by kiegroup.

the class CompensationTest method runCompensationBoundaryEventGeneralTest.

public static void runCompensationBoundaryEventGeneralTest(KieSession ksession, RuleFlowProcess process, String processId, String[] workItemNames, List<String> eventList, String compensationEvent) {
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    for (String workItem : workItemNames) {
        ksession.getWorkItemManager().registerWorkItemHandler(workItem, workItemHandler);
    }
    ProcessInstance processInstance = ksession.startProcess(processId);
    // general compensation should not cause anything to happen
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should not have fired yet.", 0, eventList.size());
    // complete work 1 & 2
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertEquals("Compensation should not have fired yet.", 0, eventList.size());
    // general compensation should now cause the compensation handlers to fire in reverse order
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should have fired.", 2, eventList.size());
    // complete work 3 and finish
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.process.test.TestWorkItemHandler) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 2 with TestWorkItemHandler

use of org.jbpm.process.test.TestWorkItemHandler in project jbpm by kiegroup.

the class CompensationTest method runCompensationBoundaryEventSpecificTest.

public static void runCompensationBoundaryEventSpecificTest(KieSession ksession, RuleFlowProcess process, String processId, String[] workItemNames, List<String> eventList, String compensationEvent) {
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    for (String workItem : workItemNames) {
        ksession.getWorkItemManager().registerWorkItemHandler(workItem, workItemHandler);
    }
    ProcessInstance processInstance = ksession.startProcess(processId);
    // call compensation on the uncompleted work 1 (which should not fire)
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should not have fired yet.", 0, eventList.size());
    // complete work 1
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    // call compensation on work 1, which should now fire
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should have fired.", 1, eventList.size());
    // complete work 2 & 3
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.process.test.TestWorkItemHandler) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 3 with TestWorkItemHandler

use of org.jbpm.process.test.TestWorkItemHandler in project jbpm by kiegroup.

the class CompensationTest method runCompensationEventSubProcessGeneralTest.

public static void runCompensationEventSubProcessGeneralTest(KieSession ksession, RuleFlowProcess process, String processId, String[] workItemNames, List<String> eventList, String compensationEvent) {
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    for (String workItem : workItemNames) {
        ksession.getWorkItemManager().registerWorkItemHandler(workItem, workItemHandler);
    }
    ProcessInstance processInstance = ksession.startProcess(processId);
    // pre and sub process work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    // Call general compensation
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should have fired once.", 1, eventList.size());
    // post work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.process.test.TestWorkItemHandler) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 4 with TestWorkItemHandler

use of org.jbpm.process.test.TestWorkItemHandler in project jbpm by kiegroup.

the class CompensationTest method runCompensationEventSubProcessSpecificTest.

public static void runCompensationEventSubProcessSpecificTest(KieSession ksession, RuleFlowProcess process, String processId, String[] workItemNames, List<String> eventList, String compensationEvent) {
    // run process
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    for (String workItem : workItemNames) {
        ksession.getWorkItemManager().registerWorkItemHandler(workItem, workItemHandler);
    }
    ProcessInstance processInstance = ksession.startProcess(processId);
    // call compensation on the uncompleted work 1 (which should not fire)
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should not have fired yet.", 0, eventList.size());
    // pre work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    // sub-process is active, but not complete
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should not have fired yet.", 0, eventList.size());
    // sub process work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    // sub-process has completed
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals("Compensation should have fired once.", 1, eventList.size());
    // post work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.process.test.TestWorkItemHandler) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 5 with TestWorkItemHandler

use of org.jbpm.process.test.TestWorkItemHandler in project jbpm by kiegroup.

the class EventSubProcessTest method testNestedEventSubProcess.

@Test
public void testNestedEventSubProcess() throws Exception {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setAutoComplete(true);
    String processId = "org.jbpm.process.event.subprocess";
    process.setId(processId);
    process.setName("Event SubProcess Process");
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("event");
    ObjectDataType personDataType = new ObjectDataType();
    personDataType.setClassName("org.drools.Person");
    variable.setType(personDataType);
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
    NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
    NodeCreator<CompositeNode> compNodeCreator = new NodeCreator<CompositeNode>(process, CompositeNode.class);
    // outer process
    StartNode startNode = startNodeCreator.createNode("start0");
    CompositeNode compositeNode = compNodeCreator.createNode("comp0");
    connect(startNode, compositeNode);
    EndNode endNode = endNodeCreator.createNode("end0");
    connect(compositeNode, endNode);
    // 1rst level nested subprocess
    startNodeCreator.setNodeContainer(compositeNode);
    endNodeCreator.setNodeContainer(compositeNode);
    compNodeCreator.setNodeContainer(compositeNode);
    startNode = startNodeCreator.createNode("start1");
    compositeNode = compNodeCreator.createNode("comp1");
    connect(startNode, compositeNode);
    endNode = endNodeCreator.createNode("end1");
    connect(compositeNode, endNode);
    // 2nd level subprocess
    startNodeCreator.setNodeContainer(compositeNode);
    endNodeCreator.setNodeContainer(compositeNode);
    NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(compositeNode, WorkItemNode.class);
    startNode = startNodeCreator.createNode("start2");
    WorkItemNode workItemNode = workItemNodeCreator.createNode("workItem2");
    Work work = new WorkImpl();
    String workItemName = "play";
    work.setName(workItemName);
    workItemNode.setWork(work);
    connect(startNode, workItemNode);
    endNode = endNodeCreator.createNode("end2");
    connect(workItemNode, endNode);
    // (3rd level) event sub process in 2nd level subprocess
    NodeCreator<EventSubProcessNode> espNodeCreator = new NodeCreator<EventSubProcessNode>(compositeNode, EventSubProcessNode.class);
    EventSubProcessNode espNode = espNodeCreator.createNode("eventSub2");
    EventTypeFilter eventFilter = new EventTypeFilter();
    String EVENT_NAME = "subEvent";
    eventFilter.setType(EVENT_NAME);
    espNode.addEvent(eventFilter);
    startNodeCreator.setNodeContainer(espNode);
    endNodeCreator.setNodeContainer(espNode);
    NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(espNode, ActionNode.class);
    startNode = startNodeCreator.createNode("start3*");
    ActionNode actionNode = actionNodeCreator.createNode("print3*");
    actionNode.setName("Print");
    final List<String> eventList = new ArrayList<String>();
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            eventList.add("Executed action");
        }
    });
    actionNode.setAction(action);
    connect(startNode, actionNode);
    endNode = endNodeCreator.createNode("end3*");
    connect(actionNode, endNode);
    // run process
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler(workItemName, workItemHandler);
    ProcessInstance processInstance = ksession.startProcess(processId);
    processInstance.signalEvent(EVENT_NAME, null);
    assertEquals("Event " + EVENT_NAME + " did not fire!", 1, eventList.size());
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    verifyEventHistory(nestedEventOrder, procEventListener.getEventHistory());
}
Also used : TestWorkItemHandler(org.jbpm.process.test.TestWorkItemHandler) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) Variable(org.jbpm.process.core.context.variable.Variable) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) ArrayList(java.util.ArrayList) ActionNode(org.jbpm.workflow.core.node.ActionNode) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ProcessContext(org.kie.api.runtime.process.ProcessContext) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Work(org.jbpm.process.core.Work) WorkImpl(org.jbpm.process.core.impl.WorkImpl) KieSession(org.kie.api.runtime.KieSession) DroolsAction(org.jbpm.workflow.core.DroolsAction) StartNode(org.jbpm.workflow.core.node.StartNode) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EndNode(org.jbpm.workflow.core.node.EndNode) NodeCreator(org.jbpm.process.test.NodeCreator) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

TestWorkItemHandler (org.jbpm.process.test.TestWorkItemHandler)5 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)5 ArrayList (java.util.ArrayList)1 Work (org.jbpm.process.core.Work)1 Variable (org.jbpm.process.core.context.variable.Variable)1 ObjectDataType (org.jbpm.process.core.datatype.impl.type.ObjectDataType)1 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)1 WorkImpl (org.jbpm.process.core.impl.WorkImpl)1 Action (org.jbpm.process.instance.impl.Action)1 NodeCreator (org.jbpm.process.test.NodeCreator)1 TestProcessEventListener (org.jbpm.process.test.TestProcessEventListener)1 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)1 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)1 DroolsAction (org.jbpm.workflow.core.DroolsAction)1 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)1 ActionNode (org.jbpm.workflow.core.node.ActionNode)1 CompositeNode (org.jbpm.workflow.core.node.CompositeNode)1 EndNode (org.jbpm.workflow.core.node.EndNode)1 EventSubProcessNode (org.jbpm.workflow.core.node.EventSubProcessNode)1 StartNode (org.jbpm.workflow.core.node.StartNode)1