1 package org.jbpm.websale; 2 3 import java.util.Arrays ; 4 import java.util.HashMap ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import org.jbpm.context.exe.ContextInstance; 12 import org.jbpm.db.AbstractDbTestCase; 13 import org.jbpm.graph.def.ProcessDefinition; 14 import org.jbpm.graph.exe.ProcessInstance; 15 import org.jbpm.identity.Entity; 16 import org.jbpm.identity.hibernate.IdentitySession; 17 import org.jbpm.identity.xml.IdentityXmlParser; 18 import org.jbpm.security.Authentication; 19 import org.jbpm.taskmgmt.exe.TaskFormParameter; 20 import org.jbpm.taskmgmt.exe.TaskInstance; 21 import org.jbpm.taskmgmt.exe.TaskMgmtInstance; 22 23 public class WebsaleTest extends AbstractDbTestCase { 24 25 ProcessDefinition processDefinition = 26 ProcessDefinition.parseXmlResource("websale.par/processdefinition.xml"); 27 28 ProcessInstance processInstance = null; 29 ContextInstance contextInstance = null; 30 TaskMgmtInstance taskMgmtInstance = null; 31 32 public void setUp() { 33 super.setUp(); 34 35 graphSession.saveProcessDefinition(processDefinition); 36 37 processInstance = new ProcessInstance(processDefinition); 38 graphSession.saveProcessInstance(processInstance); 39 40 Entity[] entities = IdentityXmlParser.parseEntitiesResource("hsqldb/identity.db.xml"); 42 IdentitySession identitySession = new IdentitySession(jbpmSession.getSession()); 43 for (int i=0; i<entities.length; i++) { 44 identitySession.saveEntity(entities[i]); 45 } 46 47 newTransaction(); 48 49 processDefinition = graphSession.loadProcessDefinition(processDefinition.getId()); 50 processInstance = graphSession.loadProcessInstance(processInstance.getId()); 51 contextInstance = processInstance.getContextInstance(); 52 taskMgmtInstance = processInstance.getTaskMgmtInstance(); 53 } 54 55 public void testWebSaleOrderTaskParameters() { 56 TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance(); 57 assertEquals("create new web sale order", taskInstance.getName()); 58 59 Set expectedParameters = new HashSet (Arrays.asList(new String []{"item", "address", "quantity"})); 60 61 List taskFormParameters = taskInstance.getTaskFormParameters(); 62 Iterator iter = taskFormParameters.iterator(); 63 while (iter.hasNext()) { 64 TaskFormParameter tfp = (TaskFormParameter) iter.next(); 65 String label = tfp.getLabel(); 66 assertTrue("taskFormParameter '"+label+"' not in "+expectedParameters, (expectedParameters.contains(label))); 67 expectedParameters.remove(label); 68 } 69 70 if (!expectedParameters.isEmpty()) { 71 fail("following task form parameters were not present: "+expectedParameters); 72 } 73 } 74 75 public void testPerformWebSaleOrderTask() { 76 TaskInstance taskInstance = null; 77 78 Authentication.pushAuthenticatedActorId("cookie monster"); 80 try { 81 taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance(); 83 } finally { 84 Authentication.popAuthenticatedActorId(); 85 } 86 87 Map parameters = new HashMap (); 88 parameters.put("item", "cookies"); 89 parameters.put("quantity", "lots of them"); 90 parameters.put("address", "sesamestreet 46"); 91 92 taskInstance.submitParameters(parameters); 93 94 assertEquals("cookies", contextInstance.getVariable("item")); 95 assertEquals("lots of them", contextInstance.getVariable("quantity")); 96 assertEquals("sesamestreet 46", contextInstance.getVariable("address")); 97 assertEquals("cookie monster", taskMgmtInstance.getSwimlaneInstance("buyer").getActorId()); 98 } 99 100 public void testEvaluateAssignment() { 101 TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance(); 102 taskInstance.end(); 103 graphSession.saveProcessInstance(processInstance); 104 105 newTransaction(); 106 107 List erniesTasks = taskMgmtSession.findTaskInstances("ernie"); 108 assertEquals(1, erniesTasks.size()); 109 110 TaskInstance evaluateTaskInstance = (TaskInstance) erniesTasks.get(0); 111 assertEquals("ernie", evaluateTaskInstance.getActorId()); 112 assertEquals("evaluate web order", evaluateTaskInstance.getName()); 113 assertNotNull(evaluateTaskInstance.getToken()); 114 assertNotNull(evaluateTaskInstance.getCreate()); 115 assertNull(evaluateTaskInstance.getStart()); 116 assertNull(evaluateTaskInstance.getEnd()); 117 } 118 119 public void testEvaluateOk() { 120 TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance(); 121 taskInstance.end(); 122 graphSession.saveProcessInstance(processInstance); 123 124 newTransaction(); 125 126 TaskInstance evaluateTaskInstance = (TaskInstance) taskMgmtSession.findTaskInstances("ernie").get(0); 127 evaluateTaskInstance.end("ok"); 128 graphSession.saveProcessInstance(evaluateTaskInstance.getTaskMgmtInstance().getProcessInstance()); 129 130 newTransaction(); 131 132 List erniesTasks = taskMgmtSession.findTaskInstances("bert"); 133 assertEquals(1, erniesTasks.size()); 134 135 TaskInstance waitForMoneyTaskInstance = (TaskInstance) erniesTasks.get(0); 136 assertEquals("bert", waitForMoneyTaskInstance.getActorId()); 137 assertEquals("wait for money", waitForMoneyTaskInstance.getName()); 138 assertNotNull(waitForMoneyTaskInstance.getToken()); 139 assertNotNull(waitForMoneyTaskInstance.getCreate()); 140 assertNull(waitForMoneyTaskInstance.getStart()); 141 assertNull(waitForMoneyTaskInstance.getEnd()); 142 } 143 144 public void testMoreInfoNeeded() { 145 TaskInstance taskInstance = null; 146 Authentication.pushAuthenticatedActorId("cookie monster"); 148 try { 149 taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance(); 151 } finally { 152 Authentication.popAuthenticatedActorId(); 153 } 154 155 taskInstance.end(); 156 graphSession.saveProcessInstance(processInstance); 157 158 newTransaction(); 159 160 TaskInstance evaluateTaskInstance = (TaskInstance) taskMgmtSession.findTaskInstances("ernie").get(0); 161 evaluateTaskInstance.end("more info needed"); 162 graphSession.saveProcessInstance(evaluateTaskInstance.getTaskMgmtInstance().getProcessInstance()); 163 177 } 178 } 179 | Popular Tags |