View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.actions;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.actionitem.ActionItem;
20  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
21  import org.kuali.rice.kew.api.WorkflowDocument;
22  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
23  import org.kuali.rice.kew.api.action.ActionRequest;
24  import org.kuali.rice.kew.api.action.ActionRequestStatus;
25  import org.kuali.rice.kew.api.action.ActionRequestType;
26  import org.kuali.rice.kew.api.action.AdHocToPrincipal;
27  import org.kuali.rice.kew.api.exception.WorkflowException;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  import org.kuali.rice.kew.test.TestUtilities;
31  import org.kuali.rice.kim.api.KimConstants;
32  import org.kuali.rice.kim.api.group.Group;
33  import org.kuali.rice.kim.api.identity.Person;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  import org.kuali.rice.test.BaselineTestCase;
36  
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import static org.junit.Assert.*;
43  
44  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
45  public class AdHocRouteTest extends KEWTestCase {
46  
47  	private static final String ADHOC_DOC = "AdhocRouteTest";
48  	private static final String ADHOC_NO_NODE_DOC = "AdHocNoNodeTest";
49      private static final String ADHOC_PRIORITY_PARALLEL_DOC = "AdhocRouteTest-PriorityParallel";
50  	private String docId;
51  
52      protected void loadTestData() throws Exception {
53          loadXmlFile("ActionsConfig.xml");
54      }
55  
56      @Test
57      public void testRequestLabel() throws Exception{
58      	String note = "test note";
59      	Person per = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("rkirkend");
60      	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(per.getPrincipalId(), ADHOC_DOC);
61  
62      	docId = doc.getDocumentId();
63  
64      	doc.adHocToPrincipal(ActionRequestType.FYI, "AdHoc", "annotation1", getPrincipalIdForName("dewey"), "respDesc1", false, note);
65  
66      	doc = getDocument(per.getPrincipalId(), docId);
67      	List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(docId);
68      	for(ActionRequestValue arv : actionRequests){
69      		assertTrue("The note we passed in should equal the one we get out. note=["+ note +"]", note.equals(arv.getRequestLabel()));
70      	}
71      }
72  
73  	@Test
74  	public void testParallelAdHocRouting() throws Exception {
75      	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
76      	docId = doc.getDocumentId();
77      	doc.adHocToPrincipal(ActionRequestType.APPROVE, "AdHoc", "annotation1", getPrincipalIdForName("dewey"), "respDesc1", false);
78  
79      	doc = getDocument("dewey");
80      	assertFalse("User andlee should not have an approve request yet.  Document not yet routed.", doc.isApprovalRequested());
81  
82      	doc.adHocToGroup(ActionRequestType.APPROVE, "AdHoc", "annotation2", getGroupIdForName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "WorkflowAdmin"), "respDesc2", true);
83  
84      	doc = getDocument("quickstart");
85      	assertFalse("User should not have approve request yet.  Document not yet routed.", doc.isApprovalRequested());
86  
87      	doc = getDocument("rkirkend");
88      	doc.route("");
89  
90      	// there should be two adhoc requests
91      	List<ActionRequest> actionRequests = doc.getRootActionRequests();
92      	for (ActionRequest actionRequest : actionRequests) {
93      		assertTrue("Request should be an adhoc request.", actionRequest.isAdHocRequest());
94      	}
95  
96      	//all users should now have active approvals
97      	WorkflowDocument deweyDoc = getDocument("dewey");
98      	assertTrue("Dewey should have an approve request", deweyDoc.isApprovalRequested());
99  
100 		doc = getDocument("ewestfal");// test that more than 1 member got
101 										// requests
102     	assertTrue("WorkflowAdmin should have an approve request", doc.isApprovalRequested());
103 
104     	deweyDoc.approve("");
105     	doc.approve("");
106     	doc = getDocument("user1");//this dude has a rule in ActionsConfig.xml
107     	doc.approve("");
108     	assertTrue("The document should be final", doc.isFinal());
109     }
110 
111     /**
112 	 * Test generation of an initial ad-hoc request to initiator prior to
113 	 * routing.
114      *
115      * This test will fail until EN-643 is resolved.
116      */
117 	@Test
118 	public void testAdHocToInitiator() throws Exception {
119         final String ADHOC_NODE = "AdHoc";
120         WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
121         docId = doc.getDocumentId();
122         doc.adHocToPrincipal(ActionRequestType.APPROVE, ADHOC_NODE, "annotation1", getPrincipalIdForName("rkirkend"), "", true);
123 
124         doc.route("");
125         assertTrue(doc.isEnroute());
126 
127         doc = getDocument("rkirkend");
128         assertTrue("rkirkend should have an approval request on the document", doc.isApprovalRequested());
129         TestUtilities.assertAtNodeNew(doc, ADHOC_NODE);
130 
131         // now try it with force action=false
132         doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
133         docId = doc.getDocumentId();
134         doc.adHocToPrincipal(ActionRequestType.APPROVE, ADHOC_NODE, "annotation1", getPrincipalIdForName("rkirkend"), "", false);
135 
136         doc.route("");
137         assertTrue(doc.isEnroute());
138 
139         doc = getDocument("rkirkend");
140         assertFalse("rkirkend should NOT have an approval request on the document", doc.isApprovalRequested());
141         TestUtilities.assertAtNodeNew(doc, "One");
142         doc = getDocument("user1");
143         assertTrue("user1 should have an approval request on the document", doc.isApprovalRequested());
144     }
145 
146 	@Test
147 	public void testSerialAdHocRouting() throws Exception {
148     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
149     	docId = doc.getDocumentId();
150     	doc.route("");
151     	doc = getDocument("user1");
152     	doc.adHocToPrincipal(ActionRequestType.APPROVE, "One", "annotation1", getPrincipalIdForName("user2"), "", false);
153     	doc.adHocToPrincipal(ActionRequestType.APPROVE, "One", "annotation1", getPrincipalIdForName("rkirkend"), "", true);
154     	doc.approve("");
155     	doc = getDocument("rkirkend");
156     	assertFalse("rkirkend should not have the document at this point 'S' activation", doc.isApprovalRequested());
157     	doc = getDocument("user2");
158     	assertTrue("user2 should have an approve request", doc.isApprovalRequested());
159     	doc.approve("");
160     	doc = getDocument("rkirkend");
161     	doc.approve("");
162     	assertTrue("The document should be final", doc.isFinal());
163     }
164 
165 	@Test
166     public void testRepeatedAdHocRouting() throws Exception {
167         final String ADHOC_NODE = "AdHoc";
168         WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
169         docId = doc.getDocumentId();
170         doc.adHocToPrincipal(ActionRequestType.APPROVE, ADHOC_NODE, "annotation1", getPrincipalIdForName("user2"), "", false);
171         doc.route("");
172         doc = getDocument("rkirkend");
173         assertFalse("rkirkend should NOT have an approval request on the document", doc.isApprovalRequested());
174         // user1 shouldn't have an approve request YET - to prove that we have not yet advanced past this initial AdHoc node
175         doc = getDocument("user1");
176         assertFalse("user1 should NOT have an approval request on the document", doc.isApprovalRequested());
177 
178         doc = getDocument("user2");
179         assertTrue("user2 should have an approval request on document", doc.isApprovalRequested());
180         doc.adHocToPrincipal(ActionRequestType.APPROVE, ADHOC_NODE, "annotation1", getPrincipalIdForName("user3"), "", false);
181         doc.approve("");
182         doc = getDocument("user2");
183         assertFalse("user2 should NOT have an approval request on the document", doc.isApprovalRequested());
184 
185         doc = getDocument("user3");
186         assertTrue("user3 should have an approval request on document", doc.isApprovalRequested());
187         doc.adHocToPrincipal(ActionRequestType.APPROVE, ADHOC_NODE, "annotation1", getPrincipalIdForName("rkirkend"), "", true);
188         doc.approve("");
189         doc = getDocument("user3");
190         assertFalse("user3 should NOT have an approval request on the document", doc.isApprovalRequested());
191 
192         doc = getDocument("rkirkend");
193         assertTrue("rkirkend should have an approval request on document", doc.isApprovalRequested());
194         doc.approve("");
195         doc = getDocument("rkirkend");
196         assertFalse("rkirkend should NOT have an approval request on the document", doc.isApprovalRequested());
197 
198         // the last node that is defined on the doc goes to user1
199         doc = getDocument("user1");
200         assertTrue("user1 should have an approval request on document", doc.isApprovalRequested());
201         doc.approve("");
202         doc = getDocument("user1");
203         assertFalse("user1 should NOT have an approval request on the document", doc.isApprovalRequested());
204 
205         // just to be extra sure...let's double check those other ad-hoc-ees
206         doc = getDocument("rkirkend");
207         assertFalse("rkirkend should NOT have an approval request on the document", doc.isApprovalRequested());
208         doc = getDocument("user2");
209         assertFalse("user2 should NOT have an approval request on the document", doc.isApprovalRequested());
210         doc = getDocument("user3");
211         assertFalse("user3 should NOT have an approval request on the document", doc.isApprovalRequested());
212 
213         assertTrue("The document should be final", doc.isFinal());
214     }
215 
216     @Test public void testAdHocWhenDocumentIsInitiated() throws Exception {
217     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TakeWorkgroupAuthorityDoc");
218         document.saveDocumentData();
219         assertTrue(document.isInitiated());
220 
221         document.adHocToPrincipal(ActionRequestType.APPROVE, "My Annotation", getPrincipalIdForName("rkirkend"), "", true);
222         document.adHocToPrincipal(ActionRequestType.FYI, "My Annotation", getPrincipalIdForName("user1"), "", true);
223 
224         // this is an initiated document, the requests should not be activated yet
225         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
226         assertFalse(document.isApprovalRequested());
227         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
228         assertFalse(document.isFYIRequested());
229 
230         // now route the document, the requests should be activated
231         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
232         document.route("");
233         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
234         assertTrue(document.isApprovalRequested());
235         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
236         assertTrue(document.isFYIRequested());
237     }
238 
239     @Test public void testAdHocWhenDocumentIsFinal() throws Exception {
240         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TakeWorkgroupAuthorityDoc");
241         document.route("");
242         TestUtilities.assertAtNodeNew(document, "WorkgroupByDocument");
243 
244         try {
245         	document.adHocToPrincipal(ActionRequestType.APPROVE, "AdHoc", "", getPrincipalIdForName("ewestfal"), "", true);
246         	fail("document should not be allowed to route to nodes that are complete");
247 		} catch (Exception e) {
248 		}
249 
250 
251         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
252         assertTrue("rkirkend should have request", document.isApprovalRequested());
253         document.approve("");
254 
255         assertTrue("Document should be final", document.isFinal());
256 
257         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
258         List<ActionRequest> requests = document.getRootActionRequests();
259         for (ActionRequest request : requests) {
260 			if (request.isActivated()) {
261 				fail("Active requests should not be present on a final document");
262 			}
263 		}
264 
265         // try and adhoc a request to a final document, should blow up
266         try {
267         	document.adHocToPrincipal(ActionRequestType.APPROVE, "WorkgroupByDocument", "", getPrincipalIdForName("ewestfal"), "", true);
268         	fail("Should not be allowed to adhoc approve to a final document.");
269         } catch (Exception e) {
270 
271         }
272 
273         // it should be legal to adhoc an FYI on a FINAL document
274         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
275         assertFalse("rkirkend should not have an FYI request.", document.isFYIRequested());
276     	document.adHocToPrincipal(ActionRequestType.FYI, "WorkgroupByDocument", "", getPrincipalIdForName("rkirkend"), "", true);
277     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
278     	assertTrue("rkirkend should have an FYI request", document.isFYIRequested());
279     }
280 
281     @Test public void testAdHocWhenDocumentIsSaved() throws Exception {
282     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TakeWorkgroupAuthorityDoc");
283         document.saveDocument("");
284 
285         // TODO test adhocing of approve requests
286 
287         assertTrue("Document should be saved.", document.isSaved());
288     	document.adHocToPrincipal(ActionRequestType.FYI, "AdHoc", "", getPrincipalIdForName("rkirkend"), "", true);
289     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
290     	assertTrue("rkirkend should have an FYI request", document.isFYIRequested());
291     }
292 
293     @Test public void testAdHocFieldsSavedCorrectly() throws Exception  {
294     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
295     	docId = doc.getDocumentId();
296     	doc.route("");
297     	// find all current request and get a list of ids for elimination purposes later
298     	List<String> oldRequestIds = new ArrayList<String>();
299         for (Iterator iterator = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(doc.getDocumentId()).iterator(); iterator.hasNext();) {
300             oldRequestIds.add(((ActionRequestValue) iterator.next()).getActionRequestId());
301         }
302 
303         // send the adhoc route request
304         doc = getDocument("user1");
305     	doc.adHocToPrincipal(ActionRequestType.APPROVE, "One", "annotation1", getPrincipalIdForName("user2"), "respDesc", false);
306 
307     	// adhoc request should be only new request on document
308     	ActionRequestValue request = null;
309     	for (Iterator iter = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(doc.getDocumentId()).iterator(); iter.hasNext();) {
310             ActionRequestValue actionRequest = (ActionRequestValue) iter.next();
311             if (!oldRequestIds.contains(actionRequest.getActionRequestId())) {
312                 request = actionRequest;
313                 break;
314             }
315         }
316     	assertNotNull("Could not find adhoc routed action request", request);
317 
318     	// verify request data
319         assertEquals("wrong person", request.getPrincipalId(), getPrincipalIdForName("user2"));
320     	assertEquals("annotation incorrect", "annotation1", request.getAnnotation());
321     	assertEquals("action requested code incorrect", request.getActionRequested(), ActionRequestType.APPROVE.getCode());
322     	assertEquals("responsibility desc incorrect", request.getResponsibilityDesc(), "respDesc");
323     	assertEquals("wrong force action", request.getForceAction(), Boolean.FALSE);
324 
325     }
326 
327 	@Test
328 	public void testAdHocDissaprovedDocument() throws Exception {
329     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), ADHOC_DOC);
330     	docId = doc.getDocumentId();
331     	doc.route("");
332 
333     	doc = getDocument("user1");
334     	TestUtilities.assertAtNodeNew(doc, "One");
335     	doc.disapprove("");
336     	TestUtilities.assertAtNodeNew(doc, "One");
337     	//adhoc an ack and fyi
338 
339     	doc.adHocToPrincipal(ActionRequestType.FYI, "One", "", getPrincipalIdForName("rkirkend"), "", true);
340     	doc.adHocToPrincipal(ActionRequestType.ACKNOWLEDGE, "One", "", getPrincipalIdForName("user2"), "", true);
341 
342     	doc = getDocument("rkirkend");
343     	assertTrue(doc.isFYIRequested());
344     	doc.fyi();
345     	doc = getDocument("user2");
346     	assertTrue(doc.isAcknowledgeRequested());
347     	doc.acknowledge("");
348 
349     	//make sure we cant ad hoc approves or completes
350     	doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), ADHOC_DOC);
351     	docId = doc.getDocumentId();
352     	doc.route("");
353 
354     	doc = getDocument("user1");
355     	doc.disapprove("");
356 
357     	// try to ad hoc an approve request
358     	try {
359     		doc.adHocToPrincipal(ActionRequestType.APPROVE, "One", "", getPrincipalIdForName("rkirkend"), "", true);
360     		fail("should have thrown exception cant adhoc approvals on dissaproved documents");
361     	} catch (Exception e) {
362 
363     	}
364 
365     	// try to ad hoc a complete request
366     	try {
367     		doc.adHocToPrincipal(ActionRequestType.COMPLETE, "One", "", getPrincipalIdForName("rkirkend"), "", true);
368     		fail("should have thrown exception cant ad hoc completes on dissaproved documents");
369     	} catch (Exception e) {
370 
371     	}
372 
373         // try to ad hoc an ack request
374         try {
375             doc.adHocToPrincipal(ActionRequestType.ACKNOWLEDGE, "", getPrincipalIdForName("user1"), "", true);
376         } catch (Exception e) {
377             e.printStackTrace();
378             fail("should have thrown exception cant ad hoc completes on dissaproved documents");
379     }
380 
381         // try to ad hoc a fyi request
382         try {
383             doc.adHocToPrincipal(ActionRequestType.FYI, "", getPrincipalIdForName("user1"), "", true);
384         } catch (Exception e) {
385             e.printStackTrace();
386             fail("should have thrown exception cant ad hoc completes on dissaproved documents");
387         }
388 
389     }
390 
391     /**
392      *
393      * @throws Exception
394      */
395 	@Test
396 	public void testAdHocNoNodeName() throws Exception {
397     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), ADHOC_DOC);
398     	docId = doc.getDocumentId();
399 
400 		// do an appspecific route to jitrue and NonSIT (w/ force action =
401 		// false), should end up at the current node
402 
403     	doc.adHocToPrincipal(ActionRequestType.APPROVE, "", getPrincipalIdForName("jitrue"), "", false);
404     	doc.adHocToGroup(ActionRequestType.APPROVE, "", getGroupIdForName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "NonSIT"), "", false);
405     	doc.route("");
406 
407 		// user1 should not have a request, his action should have counted for
408 		// the ad hoc request to the workgroup
409     	doc = getDocument("user1");
410     	assertFalse(doc.isApprovalRequested());
411     	doc = getDocument("jitrue");
412     	assertTrue(doc.isApprovalRequested());
413 		// we should still be at the "AdHoc" node because we sent an ad hoc
414 		// request to jitrue and the workgroup
415     	TestUtilities.assertAtNodeNew(doc, "AdHoc");
416 
417     	// now disapprove as jitrue
418     	doc = getDocument("jitrue");
419     	TestUtilities.assertAtNodeNew(doc, "AdHoc");
420     	doc.disapprove("");
421     	// we should stay at the AdHoc node following the disapprove
422     	TestUtilities.assertAtNodeNew(doc, "AdHoc");
423     	assertTrue(doc.isDisapproved());
424     	//adhoc an ack and fyi
425 
426     	doc.adHocToPrincipal(ActionRequestType.FYI, "", getPrincipalIdForName("rkirkend"), "", true);
427     	doc.adHocToPrincipal(ActionRequestType.ACKNOWLEDGE, "", getPrincipalIdForName("user2"), "", true);
428 
429     	doc.adHocToGroup(ActionRequestType.ACKNOWLEDGE, "", getGroupIdForName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "NonSIT"), "", true);
430 
431     	// rkirkend should have an FYI ad hoc request
432     	doc = getDocument("rkirkend");
433     	assertTrue(doc.isFYIRequested());
434     	doc.fyi();
435 
436 		// user3 should have an ACK ad hoc request because user3 is in the
437 		// NonSIT workgroup
438     	doc = getDocument("user3");
439     	assertTrue(doc.isAcknowledgeRequested());
440 
441 		// user2 should have an ACK ad hoc request (because of their individual
442 		// ack and because they are in the NonSIT workgroup)
443     	doc = getDocument("user2");
444     	assertTrue(doc.isAcknowledgeRequested());
445     	doc.acknowledge("");
446 
447 		// user2's ACK should have counted for the NonSIT workgroup request,
448 		// user 3 should no longer have an ACK
449     	doc = getDocument("user3");
450     	assertFalse(doc.isAcknowledgeRequested());
451 
452 		// finally user1 should have an acknowledge as a result of the
453 		// disapprove since user1 was the initiator
454     	doc = getDocument("user1");
455     	assertTrue(doc.isAcknowledgeRequested());
456     	doc.acknowledge("");
457 
458     	// there should now be no remaining pending requests on the document
459     	TestUtilities.assertNumberOfPendingRequests(doc.getDocumentId(), 0);
460 
461 		// make sure we cant ad hoc approves or completes on a "terminal"
462 		// document
463     	try {
464     		doc.adHocToPrincipal(ActionRequestType.APPROVE, "", getPrincipalIdForName("rkirkend"), "", true);
465     		fail("should have thrown exception cant adhoc approvals on dissaproved documents");
466 		} catch (Exception e) {
467 		}
468     	// try to ad hoc a complete request
469     	try {
470     		doc.adHocToPrincipal(ActionRequestType.COMPLETE, "", getPrincipalIdForName("rkirkend"), "", true);
471     		fail("should have thrown exception cant ad hoc completes on dissaproved documents");
472 		} catch (Exception e) {
473 		}
474 
475     }
476 	
477 	@Test
478 	public void testAdHocWithRequestLabel_ToPrincipal() throws Exception {
479 		WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), ADHOC_DOC);
480 		String label = "MY PRINCIPAL LABEL";
481 		doc.adHocToPrincipal(ActionRequestType.APPROVE, null, "", getPrincipalIdForName("ewestfal"), "", true, label);
482 		
483 		List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(doc.getDocumentId());
484 		assertEquals("Shoudl have 1 request.", 1, actionRequests.size());
485 		ActionRequestValue actionRequest = actionRequests.get(0);
486 		assertEquals("Should be an approve request", ActionRequestType.APPROVE.getCode(), actionRequest.getActionRequested());
487 		assertEquals("Invalid request label", label, actionRequest.getRequestLabel());
488 		assertEquals("Request should be initialized", ActionRequestStatus.INITIALIZED.getCode(), actionRequest.getStatus());
489 		
490 		// now route the document, it should activate the request and create an action item
491 		doc.route("");
492 		
493 		Collection<ActionItem> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(doc.getDocumentId());
494 		assertEquals("Should have 1 action item.", 1, actionItems.size());
495 		ActionItem actionItem = actionItems.iterator().next();
496 		assertEquals("ActionItem should be constructed from request.", actionRequest.getActionRequestId(), actionItem.getActionRequestId());
497 		assertEquals("ActionItem should have same label", label, actionItem.getRequestLabel());
498 	}
499 
500 	@Test
501 	public void testAdHocWithRequestLabel_ToGroup() throws Exception {
502 		WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), ADHOC_DOC);
503 		String label = "MY GROUP LABEL";
504 		Group workflowAdmin = KEWServiceLocator.getIdentityHelperService().getGroupByName("KR-WKFLW", "WorkflowAdmin");
505 		doc.adHocToGroup(ActionRequestType.APPROVE, null, "", workflowAdmin.getId(), "", true, label);
506 		
507 		List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(doc.getDocumentId());
508 		assertEquals("Should have 1 request.", 1, actionRequests.size());
509 		ActionRequestValue actionRequest = actionRequests.get(0);
510 		assertEquals("Should be an approve request", ActionRequestType.APPROVE.getCode(), actionRequest.getActionRequested());
511 		assertEquals("Invalid request label", label, actionRequest.getRequestLabel());
512 		assertEquals("Request should be initialized", ActionRequestStatus.INITIALIZED.getCode(), actionRequest.getStatus());
513 		
514 		// now route the document, it should activate the request and create action items
515 		doc.route("");
516 		
517 		Collection<ActionItem> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(doc.getDocumentId());
518 		assertTrue("Should have more than 1 action item.", actionItems.size() > 1);
519 		for (ActionItem actionItem : actionItems) {
520 			assertEquals("ActionItem should be constructed from request.", actionRequest.getActionRequestId(), actionItem.getActionRequestId());
521 			assertEquals("ActionItem should have same label", label, actionItem.getRequestLabel());
522 		}
523 	}
524 
525 	   @Test
526 	    public void testAdHocWithNoNodes() throws Exception {
527 	        WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), ADHOC_NO_NODE_DOC);
528 	        String label = "MY PRINCIPAL LABEL";
529 	        //DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(document);
530 	        
531 	        doc.adHocToPrincipal(ActionRequestType.FYI, null, "", getPrincipalIdForName("ewestfal"), "", true, label);
532 	        
533 	        List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(doc.getDocumentId());
534 	        assertEquals("Should have 1 request.", 1, actionRequests.size());
535 	        ActionRequestValue actionRequest = actionRequests.get(0);
536 	        assertEquals("Should be a FYI request", ActionRequestType.FYI.getCode(), actionRequest.getActionRequested());
537 	        assertEquals("Invalid request label", label, actionRequest.getRequestLabel());
538 	        assertEquals("Request should be initialized", ActionRequestStatus.INITIALIZED.getCode(), actionRequest.getStatus());
539 	        
540 	        // now route the document, it should activate the request and create an action item
541 	        doc.route("");
542 	        
543 	        Collection<ActionItem> actionItems = KEWServiceLocator.getActionListService().getActionListForSingleDocument(doc.getDocumentId());
544 	        assertEquals("Should have 0 action item.", 0, actionItems.size());
545 	        //ActionItem actionItem = actionItems.iterator().next();
546 	        //assertEquals("ActionItem should be constructed from request.", actionRequest.getActionRequestId(), actionItem.getActionRequestId());
547 	        //assertEquals("ActionItem should have same label", label, actionItem.getRequestLabel());
548 	    }
549 
550     /**
551      * This test allows us to ensure that Priority-Parallel activation is working as expected.
552      */
553     @Test
554     public void testAdHocWithPriorityParallelActivation() throws Exception {
555         String user1 = getPrincipalIdForName("user1");
556         String user2 = getPrincipalIdForName("user2");
557         String user3 = getPrincipalIdForName("user3");
558         String testuser1 = getPrincipalIdForName("testuser1");
559         String testuser2 = getPrincipalIdForName("testuser2");
560         String testuser3 = getPrincipalIdForName("testuser3");
561 
562         WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1, ADHOC_PRIORITY_PARALLEL_DOC);
563 
564         // create adhoc requests as follows:
565         // @Priority 1:
566         //      * ACK to user2
567         // @Priority 2:
568         //      * APPROVE to user3
569         //      * APPROVE to testuser1
570         //      * ACK to testuser2
571         // @Priority 3:
572         //      * FYI to testuser3
573         AdHocToPrincipal.Builder adHocToPrincipal = AdHocToPrincipal.Builder.create(ActionRequestType.ACKNOWLEDGE, null, user2);
574         adHocToPrincipal.setPriority(1);
575         document.adHocToPrincipal(adHocToPrincipal.build(), "");
576 
577         adHocToPrincipal.setPriority(2);
578         adHocToPrincipal.setActionRequested(ActionRequestType.APPROVE);
579         adHocToPrincipal.setTargetPrincipalId(user3);
580         document.adHocToPrincipal(adHocToPrincipal.build(), "");
581 
582         adHocToPrincipal.setTargetPrincipalId(testuser1);
583         document.adHocToPrincipal(adHocToPrincipal.build(), "");
584 
585         adHocToPrincipal.setActionRequested(ActionRequestType.ACKNOWLEDGE);
586         adHocToPrincipal.setTargetPrincipalId(testuser2);
587         document.adHocToPrincipal(adHocToPrincipal.build(), "");
588 
589         adHocToPrincipal.setPriority(3);
590         adHocToPrincipal.setActionRequested(ActionRequestType.FYI);
591         adHocToPrincipal.setTargetPrincipalId(testuser3);
592         document.adHocToPrincipal(adHocToPrincipal.build(), "");
593 
594         // now let's route the document
595         document.route("");
596         assertTrue(document.isEnroute());
597 
598         // check that our action requests are correct
599         List<ActionRequest> rootActionRequests = document.getRootActionRequests();
600         assertEquals("Should have 5 root action requests", 5, rootActionRequests.size());
601         ActionRequest user2Request = null;
602         ActionRequest user3Request = null;
603         ActionRequest testuser1Request = null;
604         ActionRequest testuser2Request = null;
605         ActionRequest testuser3Request = null;
606         for (ActionRequest actionRequest : rootActionRequests) {
607             if (user2.equals(actionRequest.getPrincipalId())) {
608                 user2Request = actionRequest;
609             } else if (user3.equals(actionRequest.getPrincipalId())) {
610                 user3Request = actionRequest;
611             } else if (testuser1.equals(actionRequest.getPrincipalId())) {
612                 testuser1Request = actionRequest;
613             } else if (testuser2.equals(actionRequest.getPrincipalId())) {
614                 testuser2Request = actionRequest;
615             } else if (testuser3.equals(actionRequest.getPrincipalId())) {
616                 testuser3Request = actionRequest;
617             }
618         }
619         // now let's ensure we got the requests we wanted
620         assertNotNull(user2Request);
621         assertEquals(ActionRequestStatus.ACTIVATED, user2Request.getStatus());
622         assertNotNull(user3Request);
623         assertEquals(ActionRequestStatus.ACTIVATED, user3Request.getStatus());
624         assertNotNull(testuser1Request);
625         assertEquals(ActionRequestStatus.ACTIVATED, testuser1Request.getStatus());
626         assertNotNull(testuser2Request);
627         assertEquals(ActionRequestStatus.ACTIVATED, testuser2Request.getStatus());
628         assertNotNull(testuser3Request);
629         assertEquals(ActionRequestStatus.INITIALIZED, testuser3Request.getStatus());
630 
631         // now let's approve for user3
632         document.switchPrincipal(user3);
633         assertTrue(document.isApprovalRequested());
634         document.approve("");
635         assertTrue(document.isEnroute());
636 
637         // make sure the fyi to testuser3 still hasn't been activated yet
638         document.switchPrincipal(testuser3);
639         assertFalse(document.isFYIRequested());
640 
641         // approve for testuser1
642         document.switchPrincipal(testuser1);
643         assertTrue(document.isApprovalRequested());
644         document.approve("");
645 
646         // document should now be in processed status
647         assertTrue(document.isProcessed());
648 
649         // ...and fyi to testuser3 should be activated now, go ahead and clear that fyi
650         document.switchPrincipal(testuser3);
651         assertTrue(document.isFYIRequested());
652         document.fyi();
653 
654         // clear out the remaning ack requests
655         assertTrue(document.isProcessed());
656         document.switchPrincipal(user2);
657         assertTrue(document.isAcknowledgeRequested());
658         document.acknowledge("");
659         assertTrue(document.isProcessed());
660         document.switchPrincipal(testuser2);
661         assertTrue(document.isAcknowledgeRequested());
662         document.acknowledge("");
663 
664         // document should now be final
665         assertTrue(document.isFinal());
666     }
667 
668 	
669     private WorkflowDocument getDocument(String netid) throws WorkflowException {
670     	return WorkflowDocumentFactory.loadDocument(getPrincipalIdForName(netid), docId);
671     }
672     private WorkflowDocument getDocument(String principalId, String docId) throws WorkflowException {
673     	return WorkflowDocumentFactory.loadDocument(principalId, docId);
674     }
675 
676 }