use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class SingleSessionCommandServiceTest method testPersistenceSubProcess.
@Test
public void testPersistenceSubProcess() {
setUp();
Properties properties = new Properties();
properties.setProperty("drools.commandService", PersistableRunner.class.getName());
properties.setProperty("drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
properties.setProperty("drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
properties.setProperty("drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
SessionConfiguration config = SessionConfiguration.newInstance(properties);
InternalKnowledgeBase ruleBase = KnowledgeBaseFactory.newKnowledgeBase();
KiePackage pkg = getProcessSubProcess();
ruleBase.addPackages((Collection) Arrays.asList(pkg));
PersistableRunner service = new PersistableRunner(ruleBase, config, env);
Long sessionId = service.getSessionId();
StartProcessCommand startProcessCommand = new StartProcessCommand();
startProcessCommand.setProcessId("org.drools.test.TestProcess");
RuleFlowProcessInstance processInstance = (RuleFlowProcessInstance) service.execute(startProcessCommand);
logger.info("Started process instance {}", processInstance.getId());
long processInstanceId = processInstance.getId();
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
service.dispose();
service = new PersistableRunner(sessionId, ruleBase, config, env);
GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
assertNotNull(processInstance);
Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
assertEquals(1, nodeInstances.size());
SubProcessNodeInstance subProcessNodeInstance = (SubProcessNodeInstance) nodeInstances.iterator().next();
long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
RuleFlowProcessInstance subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
assertNotNull(subProcessInstance);
service.dispose();
service = new PersistableRunner(sessionId, ruleBase, config, env);
CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
completeWorkItemCommand.setWorkItemId(workItem.getId());
service.execute(completeWorkItemCommand);
service.dispose();
service = new PersistableRunner(sessionId, ruleBase, config, env);
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
assertNull(subProcessInstance);
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
assertNull(processInstance);
service.dispose();
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class VariablePersistenceStrategyTest method testAbortWorkItemWithVariablePersistence.
@Test
public void testAbortWorkItemWithVariablePersistence() throws Exception {
MyEntity myEntity = new MyEntity("This is a test Entity");
MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
EntityManager em = emf.createEntityManager();
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
utx.begin();
em.joinTransaction();
em.persist(myEntity);
utx.commit();
em.close();
Environment env = createEnvironment();
KieBase kbase = createKnowledgeBase("VPSProcessWithWorkItems.rf");
KieSession ksession = createSession(kbase, env);
logger.debug("### Starting process ###");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("x", "SomeString");
parameters.put("y", myEntity);
parameters.put("z", myVariableSerializable);
long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
logger.debug("### Retrieving process instance ###");
ksession = reloadSession(ksession, kbase, env);
WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
assertEquals("SomeString", processInstance.getVariable("x"));
assertEquals("This is a test Entity", ((MyEntity) processInstance.getVariable("y")).getTest());
assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
assertNull(processInstance.getVariable("a"));
assertNull(processInstance.getVariable("b"));
assertNull(processInstance.getVariable("c"));
logger.debug("### Completing first work item ###");
Map<String, Object> results = new HashMap<String, Object>();
results.put("zeta", processInstance.getVariable("z"));
results.put("equis", processInstance.getVariable("x") + "->modifiedResult");
// we simulate a failure here, aborting the work item
ksession.getWorkItemManager().abortWorkItem(workItem.getId());
workItem = handler.getWorkItem();
assertNotNull(workItem);
logger.debug("### Retrieving process instance ###");
ksession = reloadSession(ksession, kbase, env);
processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
logger.debug("######## Getting the already Persisted Variables #########");
// we expect the variables to be unmodifed
assertEquals("SomeString", processInstance.getVariable("x"));
assertEquals("This is a test Entity", ((MyEntity) processInstance.getVariable("y")).getTest());
assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
assertEquals("Some new String", processInstance.getVariable("a"));
assertEquals("This is a new test Entity", ((MyEntity) processInstance.getVariable("b")).getTest());
assertEquals("This is a new test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("c")).getText());
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class VariablePersistenceStrategyTest method testPersistenceVariablesSubProcess.
@Test
public void testPersistenceVariablesSubProcess() throws NamingException, NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
MyEntity myEntity = new MyEntity("This is a test Entity with annotation in fields");
MyEntityMethods myEntityMethods = new MyEntityMethods("This is a test Entity with annotations in methods");
MyEntityOnlyFields myEntityOnlyFields = new MyEntityOnlyFields("This is a test Entity with annotations in fields and without accesors methods");
MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
EntityManager em = emf.createEntityManager();
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
utx.begin();
em.joinTransaction();
em.persist(myEntity);
em.persist(myEntityMethods);
em.persist(myEntityOnlyFields);
utx.commit();
em.close();
Environment env = createEnvironment();
KieBase kbase = createKnowledgeBase("VariablePersistenceStrategySubProcess.rf");
KieSession ksession = createSession(kbase, env);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("x", "SomeString");
parameters.put("y", myEntity);
parameters.put("m", myEntityMethods);
parameters.put("f", myEntityOnlyFields);
parameters.put("z", myVariableSerializable);
long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
ksession = reloadSession(ksession, kbase, env);
ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
ksession = reloadSession(ksession, kbase, env);
processInstance = ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
ksession = reloadSession(ksession, kbase, env);
processInstance = ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNull(workItem);
ksession = reloadSession(ksession, kbase, env);
processInstance = ksession.getProcessInstance(processInstanceId);
assertNull(processInstance);
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessFactoryTest method testBoundaryTimerTimeCycle.
@Test(timeout = 10000)
public void testBoundaryTimerTimeCycle() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("BoundaryTimerEvent", 1);
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
factory.name("My process").packageName("org.jbpm").startNode(1).name("Start").done().humanTaskNode(2).name("Task").actorId("john").taskName("MyTask").done().endNode(3).name("End1").terminate(false).done().boundaryEventNode(4).name("BoundaryTimerEvent").attachedTo(2).timeCycle("1s###5s").cancelActivity(false).done().endNode(5).name("End2").terminate(false).done().connection(1, 2).connection(2, 3).connection(4, 5);
RuleFlowProcess process = factory.validate().getProcess();
Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
// source path or target path must be set to be added into kbase
res.setSourcePath("/tmp/processFactory.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(res);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
ksession.addEventListener(countDownListener);
ProcessInstance pi = ksession.startProcess("org.jbpm.process");
assertProcessInstanceActive(pi);
// wait for boundary timer firing
countDownListener.waitTillCompleted();
assertNodeTriggered(pi.getId(), "End2");
// still active because CancelActivity = false
assertProcessInstanceActive(pi);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceCompleted(pi);
ksession.dispose();
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessFactoryTest method testBoundaryTimerTimeDuration.
@Test(timeout = 10000)
public void testBoundaryTimerTimeDuration() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("BoundaryTimerEvent", 1);
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
factory.name("My process").packageName("org.jbpm").startNode(1).name("Start").done().humanTaskNode(2).name("Task").actorId("john").taskName("MyTask").done().endNode(3).name("End1").terminate(false).done().boundaryEventNode(4).name("BoundaryTimerEvent").attachedTo(2).timeDuration("1s").cancelActivity(false).done().endNode(5).name("End2").terminate(false).done().connection(1, 2).connection(2, 3).connection(4, 5);
RuleFlowProcess process = factory.validate().getProcess();
Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
// source path or target path must be set to be added into kbase
res.setSourcePath("/tmp/processFactory.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(res);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
ksession.addEventListener(countDownListener);
ProcessInstance pi = ksession.startProcess("org.jbpm.process");
assertProcessInstanceActive(pi);
// wait for boundary timer firing
countDownListener.waitTillCompleted();
assertNodeTriggered(pi.getId(), "End2");
// still active because CancelActivity = false
assertProcessInstanceActive(pi);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceCompleted(pi);
ksession.dispose();
}
Aggregations