PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ojc-core/workflowse/workflowseimpl/test/com/sun/jbi/engine/workflow/process/EscalationHandlerTest.java

https://bitbucket.org/pymma/openesb-components
Java | 337 lines | 166 code | 84 blank | 87 comment | 17 complexity | 9b214ec383d60e019eb05063d76ad93b MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)EscalationHandlerTest.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.jbi.engine.workflow.process;
  30. import java.util.Calendar;
  31. import java.util.Collection;
  32. import java.util.HashMap;
  33. import java.util.Iterator;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import javax.xml.transform.Source;
  38. import junit.framework.TestCase;
  39. import org.apache.xmlbeans.XmlDateTime;
  40. import org.w3c.dom.Element;
  41. import com.sun.jbi.engine.workflow.clientapi.Util;
  42. import com.sun.jbi.engine.workflow.common.model.TaskInput;
  43. import com.sun.jbi.engine.workflow.common.model.TaskModelFactory;
  44. import com.sun.jbi.engine.workflow.common.model.TaskPrincipal;
  45. import com.sun.jbi.engine.workflow.runtime.model.DefaultRuntimeTaskTimer;
  46. import com.sun.jbi.engine.workflow.runtime.model.RuntimeTask;
  47. import com.sun.jbi.engine.workflow.runtime.model.RuntimeTaskTimer;
  48. import com.sun.jbi.engine.workflow.runtime.model.TaskManager;
  49. import com.sun.jbi.engine.workflow.runtime.model.TaskManagerFactory;
  50. import com.sun.jbi.workflow.model.Escalation;
  51. import com.sun.jbi.workflow.model.Task;
  52. /**
  53. *
  54. *
  55. */
  56. public class EscalationHandlerTest extends TestCase {
  57. public EscalationHandlerTest(String testName) {
  58. super(testName);
  59. }
  60. protected void setUp() throws Exception {
  61. Util.initTaskManager();
  62. }
  63. protected void tearDown() throws Exception {
  64. }
  65. /**
  66. * Test of execute method, of class com.sun.jbi.engine.workflow.process.EscalationHandler.
  67. */
  68. public void testDeadline() throws Exception {
  69. System.out.println("testDeadline");
  70. TaskManager taskManager = TaskManagerFactory.getInstance().getTaskManager();
  71. //load task definition
  72. String wfFileName = "/com/sun/jbi/engine/workflow/clientapi/testMessages/purchaseOrder/ApprovePurchaseEscalationDeadlineTest.wf";
  73. Element root = Util.loadElement(wfFileName);
  74. //define 1 second deadline from current date
  75. Calendar c1 = Calendar.getInstance();
  76. c1.add(Calendar.SECOND, 4);
  77. XmlDateTime d1 = XmlDateTime.Factory.newInstance();
  78. d1.setCalendarValue(c1);
  79. Map<String, String> prefixToNSMap = new HashMap<String, String>();
  80. prefixToNSMap.put("wf", "http://jbi.com.sun/wfse");
  81. Util.replaceTextContent(root, "/wf:task/wf:escalation/wf:deadline",
  82. "'" + d1.getStringValue() + "'", prefixToNSMap);
  83. Task task = Util.loadTask(root);
  84. assertNotNull("task should not be null "+ task);
  85. //get first escalation
  86. List<Escalation> escalations = task.getTaskEscalations();
  87. assertEquals("should have 1 escalation", 1, escalations.size());
  88. //load task input
  89. String taskInputFileName = "/com/sun/jbi/engine/workflow/runtime/model/testData/purchaseOrderInput.xml";
  90. Source taskInput = Util.loadTaskInput(taskInputFileName);
  91. TaskInput tInput = TaskModelFactory.getInstance().createTaskInput(taskInput);
  92. //create task
  93. RuntimeTask rTask = taskManager.createTask("exchange1", tInput, task);
  94. // //add RuntimeTaskTimer
  95. // RuntimeTaskTimer rtt1 = new DefaultRuntimeTaskTimer(Long.valueOf("1"), escalation1, rTask);
  96. // rTask.addTaskTimer(rtt1);
  97. //
  98. //
  99. // //escalate task
  100. // EscalationHandler instance = new EscalationHandler(rtt1, taskManager);
  101. //
  102. // instance.execute();
  103. Thread.sleep(500);
  104. rTask = taskManager.getTask(rTask.getId());
  105. assertTrue("task state should not be escalated" ,rTask.getState() != RuntimeTask.TaskState.ESCALATED);
  106. Collection<TaskPrincipal> assignedTo = rTask.getAssignedTo();
  107. assertTrue("should have zero task principal ", assignedTo.size() == 0);
  108. List<RuntimeTaskTimer> rtts = taskManager.getActiveTimers(rTask.getId());
  109. assertTrue("task should have one active escalation timer" , rtts.size() == 1);
  110. Thread.sleep(5000);
  111. rTask = taskManager.getTask(rTask.getId());
  112. assertTrue("task state should be escalated" ,rTask.getState() == RuntimeTask.TaskState.ESCALATED);
  113. assignedTo = rTask.getAssignedTo();
  114. TaskPrincipal expectedPrincipal = TaskModelFactory.getInstance().createPrincipal("rwaldorf", TaskPrincipal.PrincipalType.User);
  115. assertTrue("should have one task principal ", assignedTo.size() == 1);
  116. assertEquals("task expected principal" ,expectedPrincipal, assignedTo.iterator().next());
  117. rtts = taskManager.getActiveTimers(rTask.getId());
  118. assertTrue("task should have zero active escalation timer" , rtts.size() == 0);
  119. }
  120. public void testTaskCompletedBeforeDeadline() throws Exception {
  121. System.out.println("testTaskCompletedBeforeDeadline");
  122. TaskManager taskManager = TaskManagerFactory.getInstance().getTaskManager();
  123. //load task definition
  124. String wfFileName = "/com/sun/jbi/engine/workflow/clientapi/testMessages/purchaseOrder/ApprovePurchaseEscalationDeadlineTest.wf";
  125. Element root = Util.loadElement(wfFileName);
  126. //define 1 second deadline from current date
  127. Calendar c1 = Calendar.getInstance();
  128. c1.add(Calendar.SECOND, 2);
  129. XmlDateTime d1 = XmlDateTime.Factory.newInstance();
  130. d1.setCalendarValue(c1);
  131. Map<String, String> prefixToNSMap = new HashMap<String, String>();
  132. prefixToNSMap.put("wf", "http://jbi.com.sun/wfse");
  133. Util.replaceTextContent(root, "/wf:task/wf:escalation/wf:deadline",
  134. "'" + d1.getStringValue() + "'", prefixToNSMap);
  135. Task task = Util.loadTask(root);
  136. assertNotNull("task should not be null "+ task);
  137. //get first escalation
  138. List<Escalation> escalations = task.getTaskEscalations();
  139. assertTrue("should have 1 escalation", escalations.size() == 1);
  140. //load task input
  141. String taskInputFileName = "/com/sun/jbi/engine/workflow/runtime/model/testData/purchaseOrderInput.xml";
  142. Source taskInput = Util.loadTaskInput(taskInputFileName);
  143. TaskInput tInput = TaskModelFactory.getInstance().createTaskInput(taskInput);
  144. //create task
  145. RuntimeTask rTask = taskManager.createTask("exchange2", tInput, task);
  146. // //add RuntimeTaskTimer
  147. // RuntimeTaskTimer rtt1 = new DefaultRuntimeTaskTimer(Long.valueOf("1"), escalation1, rTask);
  148. // rTask.addTaskTimer(rtt1);
  149. //
  150. //
  151. // //escalate task
  152. // EscalationHandler instance = new EscalationHandler(rtt1, taskManager);
  153. //
  154. // instance.execute();
  155. Thread.sleep(500);
  156. rTask = taskManager.getTask(rTask.getId());
  157. assertTrue("task state should not be escalated" ,rTask.getState() != RuntimeTask.TaskState.ESCALATED);
  158. Collection<TaskPrincipal> assignedTo = rTask.getAssignedTo();
  159. assertTrue("should have zero task principal ", assignedTo.size() == 0);
  160. List<RuntimeTaskTimer> rtts = taskManager.getActiveTimers(rTask.getId());
  161. assertTrue("task should have one active escalation timer" , rtts.size() == 1);
  162. rTask.execute();
  163. Thread.sleep(200);
  164. TaskPrincipal claimPrincipal = TaskModelFactory.getInstance().createPrincipal("radval", TaskPrincipal.PrincipalType.User);
  165. taskManager.claimTask(rTask.getId(), claimPrincipal);
  166. Thread.sleep(200);
  167. taskManager.completeTask(rTask.getId(), claimPrincipal);
  168. Thread.sleep(1500);
  169. rTask = taskManager.getTask(rTask.getId());
  170. assertTrue("task state should be completed" ,rTask.getState() == RuntimeTask.TaskState.COMPLETED);
  171. // assignedTo = rTask.getAssignedTo();
  172. // TaskPrincipal expectedPrincipal = TaskModelFactory.getInstance().createPrincipal("rwaldorf", TaskPrincipal.PrincipalType.User);
  173. //
  174. // assertTrue("should have one task principal ", assignedTo.size() == 1);
  175. // assertEquals("task expected principal" ,expectedPrincipal, assignedTo.iterator().next());
  176. //
  177. Thread.sleep(4000);
  178. rtts = taskManager.getActiveTimers(rTask.getId());
  179. assertTrue("task should have zero active escalation timer" , rtts.size() == 0);
  180. }
  181. public void testDuration() throws Exception {
  182. System.out.println("duration");
  183. TaskManager taskManager = TaskManagerFactory.getInstance().getTaskManager();
  184. //load task definition
  185. String wfFileName = "/com/sun/jbi/engine/workflow/clientapi/testMessages/purchaseOrder/ApprovePurchaseEscalationDurationTest.wf";
  186. Task task = Util.loadTask(wfFileName);
  187. assertNotNull("task should not be null "+ task);
  188. //get second escalation
  189. List<Escalation> escalations = task.getTaskEscalations();
  190. assertTrue("should have 2 escalation", escalations.size() == 1);
  191. String taskInputFileName = "/com/sun/jbi/engine/workflow/runtime/model/testData/purchaseOrderInput.xml";
  192. Source taskInput = Util.loadTaskInput(taskInputFileName);
  193. TaskInput tInput = TaskModelFactory.getInstance().createTaskInput(taskInput);
  194. //create task
  195. RuntimeTask rTask = taskManager.createTask("exchange3", tInput, task);
  196. //
  197. // //add RuntimeTaskTimer
  198. // RuntimeTaskTimer rtt2 = new DefaultRuntimeTaskTimer(Long.valueOf("2"), escalation2, rTask);
  199. // rTask.addTaskTimer(rtt2);
  200. //
  201. // //escalate task
  202. // EscalationHandler instance = new EscalationHandler(rtt2, taskManager);
  203. //
  204. // instance.execute();
  205. Thread.sleep(4000);
  206. rTask = taskManager.getTask(rTask.getId());
  207. assertTrue("task state should be escalated" ,rTask.getState() == RuntimeTask.TaskState.ESCALATED);
  208. Collection<TaskPrincipal> assignedTo = rTask.getAssignedTo();
  209. assertTrue("should have one task principal ", assignedTo.size() == 1);
  210. TaskPrincipal expectedPrincipal = TaskModelFactory.getInstance().createPrincipal("rwaldorf", TaskPrincipal.PrincipalType.User);
  211. assertEquals("task expected principal" ,expectedPrincipal, assignedTo.iterator().next());
  212. }
  213. public void testDeadlineNegative() throws Exception {
  214. System.out.println("testDeadline");
  215. TaskManager taskManager = TaskManagerFactory.getInstance().getTaskManager();
  216. //load task definition
  217. String wfFileName = "/com/sun/jbi/engine/workflow/clientapi/testMessages/purchaseOrder/ApprovePurchaseEscalationDeadlineNegativeTest.wf";
  218. Task task = Util.loadTask(wfFileName);
  219. assertNotNull("task should not be null "+ task);
  220. //get first escalation
  221. List<Escalation> escalations = task.getTaskEscalations();
  222. assertTrue("should have 1 escalation", escalations.size() == 1);
  223. //load task input
  224. String taskInputFileName = "/com/sun/jbi/engine/workflow/runtime/model/testData/purchaseOrderInput.xml";
  225. Source taskInput = Util.loadTaskInput(taskInputFileName);
  226. TaskInput tInput = TaskModelFactory.getInstance().createTaskInput(taskInput);
  227. try {
  228. //create task
  229. taskManager.createTask("exchange4", tInput, task);
  230. } catch(Exception ex) {
  231. assertTrue("should fail because deadline has elapsed", true);
  232. }
  233. }
  234. public void testDurationNegative() throws Exception {
  235. System.out.println("duration");
  236. TaskManager taskManager = TaskManagerFactory.getInstance().getTaskManager();
  237. //load task definition
  238. String wfFileName = "/com/sun/jbi/engine/workflow/clientapi/testMessages/purchaseOrder/ApprovePurchaseEscalationDurationNegativeTest.wf";
  239. Task task = Util.loadTask(wfFileName);
  240. assertNotNull("task should not be null "+ task);
  241. //get second escalation
  242. List<Escalation> escalations = task.getTaskEscalations();
  243. assertTrue("should have 1 escalation", escalations.size() == 1);
  244. String taskInputFileName = "/com/sun/jbi/engine/workflow/runtime/model/testData/purchaseOrderInput.xml";
  245. Source taskInput = Util.loadTaskInput(taskInputFileName);
  246. TaskInput tInput = TaskModelFactory.getInstance().createTaskInput(taskInput);
  247. try {
  248. taskManager.createTask("exchange5", tInput, task);
  249. } catch(Exception ex) {
  250. assertTrue("should fail because duration has elapsed", true);
  251. }
  252. }
  253. }