use of org.jbpm.integrationtests.handler.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessWorkItemTest method testWorkItem.
@Test
public void testWorkItem() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + " <header>\n" + " <variables>\n" + " <variable name=\"UserName\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>John Doe</value>\n" + " </variable>\n" + " <variable name=\"Person\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\" />\n" + " </variable>\n" + " <variable name=\"MyObject\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </variable>\n" + " <variable name=\"Number\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.IntegerDataType\" />\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"HumanTask\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>#{UserName}</value>\n" + " </parameter>\n" + " <parameter name=\"Content\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>#{Person.name}</value>\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something</value>\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Attachment\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " <mapping type=\"in\" from=\"MyObject\" to=\"Attachment\" />" + " <mapping type=\"in\" from=\"Person.name\" to=\"Comment\" />" + " <mapping type=\"out\" from=\"Result\" to=\"MyObject\" />" + " <mapping type=\"out\" from=\"Result.length()\" to=\"Number\" />" + " </workItem>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>");
kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
TestWorkItemHandler handler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("UserName", "John Doe");
Person person = new Person();
person.setName("John Doe");
parameters.put("Person", person);
WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.actions", parameters);
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("ActorId"));
assertEquals("John Doe", workItem.getParameter("Content"));
assertEquals("John Doe", workItem.getParameter("Comment"));
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
parameters = new HashMap<String, Object>();
parameters.put("UserName", "Jane Doe");
parameters.put("MyObject", "SomeString");
person = new Person();
person.setName("Jane Doe");
parameters.put("Person", person);
processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.actions", parameters);
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("Jane Doe", workItem.getParameter("ActorId"));
assertEquals("SomeString", workItem.getParameter("Attachment"));
assertEquals("Jane Doe", workItem.getParameter("Content"));
assertEquals("Jane Doe", workItem.getParameter("Comment"));
Map<String, Object> results = new HashMap<String, Object>();
results.put("Result", "SomeOtherString");
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), results);
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
assertEquals("SomeOtherString", processInstance.getVariable("MyObject"));
assertEquals(15, processInstance.getVariable("Number"));
}
use of org.jbpm.integrationtests.handler.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessMarshallingTest method testVariablePersistenceMarshallingStrategies.
@Test
public void testVariablePersistenceMarshallingStrategies() throws Exception {
String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + " <header>\n" + " <variables>\n" + " <variable name=\"myVariable\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>OldValue</value>\n" + " </variable>\n" + " <variable name=\"myPerson\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\"/>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"Email\" >\n" + " <work name=\"Report\" >\n" + " <parameter name=\"Subject\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Mail</value>\n" + " </parameter>\n" + " <parameter name=\"Subject\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Mail</value>\n" + " </parameter>\n" + " </work>\n" + " </workItem>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>";
builder.addProcessFromXml(new StringReader(process));
KieSession session = createKieSession(builder.getPackages());
TestWorkItemHandler handler = new TestWorkItemHandler();
session.getWorkItemManager().registerWorkItemHandler("Report", handler);
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("myVariable", "ThisIsMyValue");
Person myPerson = new Person("Nikola Tesla", 156);
variables.put("myPerson", myPerson);
session.startProcess("org.test.ruleflow", variables);
assertEquals(1, session.getProcessInstances().size());
assertTrue(handler.getWorkItem() != null);
session = getSerialisedStatefulKnowledgeSession(session);
assertEquals(1, session.getProcessInstances().size());
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstance) session.getProcessInstances().iterator().next()).getContextInstance(VariableScope.VARIABLE_SCOPE);
assertEquals("ThisIsMyValue", variableScopeInstance.getVariable("myVariable"));
assertEquals(myPerson, variableScopeInstance.getVariable("myPerson"));
session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
assertEquals(0, session.getProcessInstances().size());
}
use of org.jbpm.integrationtests.handler.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessUpgradeTest method testCompositeMappingUpgrade.
@Test
public void testCompositeMappingUpgrade() throws Exception {
String rule = "package org.test;\n";
rule += "import org.jbpm.integrationtests.test.Person\n";
rule += "global java.util.List list\n";
rule += "rule \"Rule 1\"\n";
rule += " ruleflow-group \"hello\"\n";
rule += "when\n";
rule += " $p : Person( ) \n";
rule += "then\n";
rule += " list.add( $p );\n";
rule += "end";
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(new ByteArrayResource(rule.getBytes()), ResourceType.DRL);
String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + " <header>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <composite id=\"2\" name=\"Composite\" >\n" + " <nodes>\n" + " <workItem id=\"1\" name=\"Hello\" >\n" + " <work name=\"Human Task\" >\n" + " </work>\n" + " </workItem>\n" + " </nodes>\n" + " <connections>\n" + " </connections>\n" + " <in-ports>\n" + " <in-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeInType=\"DROOLS_DEFAULT\" />\n" + " </in-ports>\n" + " <out-ports>\n" + " <out-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + " </out-ports>\n" + " </composite>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>";
builder.add(new ReaderResource(new StringReader(process)), ResourceType.DRF);
// RuleBaseConfiguration config = new RuleBaseConfiguration();
// config.setRuleBaseUpdateHandler(null);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(builder.getKnowledgePackages());
KieSession session = kbase.newKieSession();
TestWorkItemHandler handler = new TestWorkItemHandler();
session.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
List<String> list = new ArrayList<String>();
session.setGlobal("list", list);
Person p = new Person("bobba fet", 32);
session.insert(p);
ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.test.ruleflow");
assertEquals(1, session.getProcessInstances().size());
String process2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow2\" package-name=\"org.test\" >\n" + " <header>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <composite id=\"2\" name=\"Composite\" >\n" + " <nodes>\n" + " <workItem id=\"101\" name=\"Hello\" >\n" + " <work name=\"Human Task\" >\n" + " </work>\n" + " </workItem>\n" + " <actionNode id=\"2\" name=\"Action\" >" + " <action type=\"expression\" dialect=\"java\">System.out.println();\n" + "list.add(\"Executed\");</action>\n" + " </actionNode>\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"101\" to=\"2\"/>\n" + " </connections>\n" + " <in-ports>\n" + " <in-port type=\"DROOLS_DEFAULT\" nodeId=\"101\" nodeInType=\"DROOLS_DEFAULT\" />\n" + " </in-ports>\n" + " <out-ports>\n" + " <out-port type=\"DROOLS_DEFAULT\" nodeId=\"2\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + " </out-ports>\n" + " </composite>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>";
builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(new ReaderResource(new StringReader(process2)), ResourceType.DRF);
kbase.addPackages(builder.getKnowledgePackages());
Map<String, Long> mapping = new HashMap<String, Long>();
mapping.put("2:1", 101L);
WorkflowProcessInstanceUpgrader.upgradeProcessInstance(session, processInstance.getId(), "org.test.ruleflow2", mapping);
assertEquals("org.test.ruleflow2", processInstance.getProcessId());
session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
assertEquals(1, list.size());
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
use of org.jbpm.integrationtests.handler.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessUpgradeTest method testDefaultUpgrade.
@Test
public void testDefaultUpgrade() throws Exception {
String rule = "package org.test;\n";
rule += "import org.jbpm.integrationtests.test.Person\n";
rule += "global java.util.List list\n";
rule += "rule \"Rule 1\"\n";
rule += " ruleflow-group \"hello\"\n";
rule += "when\n";
rule += " $p : Person( ) \n";
rule += "then\n";
rule += " list.add( $p );\n";
rule += "end";
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(new ReaderResource(new StringReader(rule)), ResourceType.DRL);
String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + " <header>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"Hello\" >\n" + " <work name=\"Human Task\" >\n" + " </work>\n" + " </workItem>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>";
builder.add(new ReaderResource(new StringReader(process)), ResourceType.DRF);
// RuleBaseConfiguration config = new RuleBaseConfiguration();
// config.setRuleBaseUpdateHandler(null);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(builder.getKnowledgePackages());
KieSession session = kbase.newKieSession();
TestWorkItemHandler handler = new TestWorkItemHandler();
session.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
List<String> list = new ArrayList<String>();
session.setGlobal("list", list);
Person p = new Person("bobba fet", 32);
session.insert(p);
ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.test.ruleflow");
assertEquals(1, session.getProcessInstances().size());
String process2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.test.ruleflow2\" package-name=\"org.test\" >\n" + " <header>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"Hello\" >\n" + " <work name=\"Human Task\" >\n" + " </work>\n" + " </workItem>\n" + " <actionNode id=\"4\" name=\"Action\" >" + " <action type=\"expression\" dialect=\"java\">System.out.println();\n" + "list.add(\"Executed\");</action>\n" + " </actionNode>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"4\"/>\n" + " <connection from=\"4\" to=\"3\"/>\n" + " </connections>\n" + "</process>";
builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(new ReaderResource(new StringReader(process2)), ResourceType.DRF);
kbase.addPackages(builder.getKnowledgePackages());
WorkflowProcessInstanceUpgrader.upgradeProcessInstance(session, processInstance.getId(), "org.test.ruleflow2", new HashMap<String, Long>());
assertEquals("org.test.ruleflow2", processInstance.getProcessId());
session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
assertEquals(1, list.size());
}
use of org.jbpm.integrationtests.handler.TestWorkItemHandler in project jbpm by kiegroup.
the class ProcessDynamicNodeTest method TODOtestDynamicAsyncActions.
@Test
@Ignore
public void TODOtestDynamicAsyncActions() {
Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.drools.dynamic\" package-name=\"org.drools\" >\n" + "\n" + " <header>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <dynamic id=\"2\" name=\"DynamicNode\" >\n" + " <nodes>\n" + " <workItem id=\"1\" name=\"Work\" >\n" + " <work name=\"Work\" />\n" + " </workItem>\n" + " <actionNode id=\"2\" name=\"Action2\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action2\");\n" + "list.add(\"Action2\");</action>\n" + " </actionNode>\n" + " <actionNode id=\"3\" name=\"Action3\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action3\");\n" + "list.add(\"Action3\");</action>\n" + " </actionNode>\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"3\" />\n" + " </connections>\n" + " <in-ports/>\n" + " <out-ports/>\n" + " </dynamic>\n" + " <actionNode id=\"3\" name=\"Action4\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action4\");\n" + "list.add(\"Action4\");</action>\n" + " </actionNode>\n" + " <end id=\"4\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " <connection from=\"3\" to=\"4\" />\n" + " </connections>\n" + "</process>");
builder.addRuleFlow(source);
for (DroolsError error : builder.getErrors().getErrors()) {
logger.error(error.toString());
}
KieSession ksession = createKieSession(builder.getPackages());
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Work", testHandler);
ProcessInstance processInstance = (ProcessInstance) ksession.startProcess("org.drools.dynamic");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
assertEquals(1, list.size());
WorkItem workItem = testHandler.getWorkItem();
assertNotNull(workItem);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
assertEquals(3, list.size());
}
Aggregations