use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class DynamicProcessTest method testDynamicProcess.
@Test
public void testDynamicProcess() throws Exception {
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
factory.name("HelloWorldProcess").version("1.0").packageName("org.jbpm").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
final RuleFlowProcess process = factory.validate().getProcess();
Resource resource = ResourceFactory.newByteArrayResource(XmlRuleFlowProcessDumper.INSTANCE.dump(process).getBytes());
// source path or target path must be set to be added into kbase
resource.setSourcePath("/tmp/dynamicProcess.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(resource);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
ksession.addEventListener(new ProcessEventListener() {
public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
}
public void beforeProcessStarted(ProcessStartedEvent arg0) {
logger.info("{}", arg0);
}
public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
logger.info("{}", arg0);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
logger.info("{}", arg0);
}
public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
logger.info("{}", arg0);
}
public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
}
public void afterProcessStarted(ProcessStartedEvent arg0) {
}
public void afterProcessCompleted(ProcessCompletedEvent arg0) {
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
}
public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
}
});
final ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.startProcess("org.jbpm.HelloWorld");
HumanTaskNode node = new HumanTaskNode();
node.setName("Task2");
node.setId(4);
insertNodeInBetween(process, 2, 3, node);
((CommandBasedStatefulKnowledgeSession) ksession).getRunner().execute(new ExecutableCommand<Void>() {
public Void execute(Context context) {
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
((ProcessInstanceImpl) ksession.getProcessInstance(processInstance.getId())).updateProcess(process);
return null;
}
});
assertProcessInstanceActive(processInstance);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceActive(processInstance);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
ksession.dispose();
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testPersistenceVariables.
@Test
public void testPersistenceVariables() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("VariablesProcess.rf"), ResourceType.DRF);
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
logger.debug(error.toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
long id = ksession.getIdentifier();
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("name", "John Doe");
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess", parameters);
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("name"));
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("text"));
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNull(workItem);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testPersistenceEvents.
@Test
public void testPersistenceEvents() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("EventsProcess.rf"), ResourceType.DRF);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
long id = ksession.getIdentifier();
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
logger.debug("Started process instance {}", processInstance.getId());
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession.signalEvent("MyEvent1", null, processInstance.getId());
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession.signalEvent("MyEvent2", null, processInstance.getId());
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class VariablePersistenceStrategyTest method testPersistenceVariables.
@Test
public void testPersistenceVariables() throws NamingException, NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
EntityManager em = emf.createEntityManager();
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
if (utx.getStatus() == Status.STATUS_NO_TRANSACTION) {
utx.begin();
em.joinTransaction();
}
int origNumMyEntities = em.createQuery("select i from MyEntity i").getResultList().size();
int origNumMyEntityMethods = em.createQuery("select i from MyEntityMethods i").getResultList().size();
int origNumMyEntityOnlyFields = em.createQuery("select i from MyEntityOnlyFields i").getResultList().size();
if (utx.getStatus() == Status.STATUS_ACTIVE) {
utx.commit();
}
// Setup entities
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");
// persist entities
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();
// More setup
Environment env = createEnvironment();
KieBase kbase = createKnowledgeBase("VariablePersistenceStrategyProcess.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("m", myEntityMethods);
parameters.put("f", myEntityOnlyFields);
parameters.put("z", myVariableSerializable);
// Start process
long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
// Test results
List<?> result = emf.createEntityManager().createQuery("select i from MyEntity i").getResultList();
assertEquals(origNumMyEntities + 1, result.size());
result = emf.createEntityManager().createQuery("select i from MyEntityMethods i").getResultList();
assertEquals(origNumMyEntityMethods + 1, result.size());
result = emf.createEntityManager().createQuery("select i from MyEntityOnlyFields i").getResultList();
assertEquals(origNumMyEntityOnlyFields + 1, result.size());
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 with annotation in fields", ((MyEntity) processInstance.getVariable("y")).getTest());
assertEquals("This is a test Entity with annotations in methods", ((MyEntityMethods) processInstance.getVariable("m")).getTest());
assertEquals("This is a test Entity with annotations in fields and without accesors methods", ((MyEntityOnlyFields) processInstance.getVariable("f")).test);
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 ###");
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
logger.debug("### Retrieving process instance ###");
ksession = reloadSession(ksession, kbase, env);
processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
assertEquals("SomeString", processInstance.getVariable("x"));
assertEquals("This is a test Entity with annotation in fields", ((MyEntity) processInstance.getVariable("y")).getTest());
assertEquals("This is a test Entity with annotations in methods", ((MyEntityMethods) processInstance.getVariable("m")).getTest());
assertEquals("This is a test Entity with annotations in fields and without accesors methods", ((MyEntityOnlyFields) processInstance.getVariable("f")).test);
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());
logger.debug("### Completing second work item ###");
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
logger.debug("### Retrieving process instance ###");
ksession = reloadSession(ksession, kbase, env);
processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
assertEquals("SomeString", processInstance.getVariable("x"));
assertEquals("This is a test Entity with annotation in fields", ((MyEntity) processInstance.getVariable("y")).getTest());
assertEquals("This is a test Entity with annotations in methods", ((MyEntityMethods) processInstance.getVariable("m")).getTest());
assertEquals("This is a test Entity with annotations in fields and without accesors methods", ((MyEntityOnlyFields) processInstance.getVariable("f")).test);
assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
assertEquals("Some changed String", processInstance.getVariable("a"));
assertEquals("This is a changed test Entity", ((MyEntity) processInstance.getVariable("b")).getTest());
assertEquals("This is a changed test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("c")).getText());
logger.debug("### Completing third work item ###");
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNull(workItem);
ksession = reloadSession(ksession, kbase, env);
processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNull(processInstance);
}
use of org.jbpm.persistence.session.objects.TestWorkItemHandler in project jbpm by kiegroup.
the class VariablePersistenceStrategyTest method testWorkItemWithVariablePersistence.
@Test
public void testWorkItemWithVariablePersistence() 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");
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), results);
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 #########");
assertEquals("SomeString->modifiedResult", 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());
logger.debug("### Completing second work item ###");
results = new HashMap<String, Object>();
results.put("zeta", processInstance.getVariable("z"));
results.put("equis", processInstance.getVariable("x"));
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), results);
workItem = handler.getWorkItem();
assertNotNull(workItem);
logger.debug("### Retrieving process instance ###");
ksession = reloadSession(ksession, kbase, env);
processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNotNull(processInstance);
assertEquals("SomeString->modifiedResult", 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 changed String", processInstance.getVariable("a"));
assertEquals("This is a changed test Entity", ((MyEntity) processInstance.getVariable("b")).getTest());
assertEquals("This is a changed test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("c")).getText());
logger.debug("### Completing third work item ###");
results = new HashMap<String, Object>();
results.put("zeta", processInstance.getVariable("z"));
results.put("equis", processInstance.getVariable("x"));
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), results);
workItem = handler.getWorkItem();
assertNull(workItem);
ksession = reloadSession(ksession, kbase, env);
processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
assertNull(processInstance);
}
Aggregations