View Javadoc

1   /**
2    * Copyright 2005-2011 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.actionlist;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.delegation.DelegationType;
20  import org.kuali.rice.kew.actionitem.ActionItem;
21  import org.kuali.rice.kew.actionlist.service.ActionListService;
22  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
23  import org.kuali.rice.kew.actionrequest.Recipient;
24  import org.kuali.rice.kew.api.KewApiConstants;
25  import org.kuali.rice.kew.api.WorkflowDocument;
26  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
27  import org.kuali.rice.kew.api.action.ActionRequest;
28  import org.kuali.rice.kew.api.document.Document;
29  import org.kuali.rice.kew.api.document.DocumentUpdate;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.kew.test.KEWTestCase;
34  import org.kuali.rice.kew.test.TestUtilities;
35  import org.kuali.rice.kew.util.WebFriendlyRecipient;
36  import org.kuali.rice.kim.api.KimConstants;
37  import org.kuali.rice.kim.api.group.Group;
38  import org.kuali.rice.kim.api.identity.Person;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  import org.springframework.jdbc.core.StatementCallback;
41  import org.springframework.transaction.TransactionStatus;
42  import org.springframework.transaction.support.TransactionCallback;
43  import org.springframework.transaction.support.TransactionTemplate;
44  
45  import java.sql.Connection;
46  import java.sql.PreparedStatement;
47  import java.sql.ResultSet;
48  import java.sql.Statement;
49  import java.sql.Timestamp;
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.Date;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  import static org.junit.Assert.*;
57  
58  /**
59   * @author Kuali Rice Team (rice.collab@kuali.org)
60   */
61  public class ActionListTest extends KEWTestCase {
62  
63      private static final String[] AUTHENTICATION_IDS = { "ewestfal", "rkirkend", "jhopf", "bmcgough" };
64      private static final String[] WORKGROUP_IDS = { "1", "2", "3", "4" };
65  
66      private DocumentRouteHeaderValue routeHeader1;
67      private DocumentRouteHeaderValue routeHeader2;
68      private DocumentRouteHeaderValue routeHeader3;
69      private List<ActionItem> actionItems = new ArrayList<ActionItem>();
70  
71      protected void loadTestData() throws Exception {
72          loadXmlFile("ActionListConfig.xml");
73      }
74  
75      private void setUpOldSchool() throws Exception {
76          super.setUpAfterDataLoad();
77          List<ActionItem> actionItems1 = new ArrayList<ActionItem>();
78          List<ActionItem> actionItems2 = new ArrayList<ActionItem>();
79          List<ActionItem> actionItems3 = new ArrayList<ActionItem>();
80          routeHeader1 = generateDocRouteHeader();
81          routeHeader2 = generateDocRouteHeader();
82          routeHeader3 = generateDocRouteHeader();
83          
84          getRouteHeaderService().saveRouteHeader(routeHeader1);
85          getRouteHeaderService().saveRouteHeader(routeHeader2);
86          getRouteHeaderService().saveRouteHeader(routeHeader3);
87  
88          for (int i = 0; i < AUTHENTICATION_IDS.length; i++) {
89              actionItems1.add(generateActionItem(routeHeader1, "K", AUTHENTICATION_IDS[i], null));
90              actionItems2.add(generateActionItem(routeHeader2, "A", AUTHENTICATION_IDS[i], null));
91          }
92          for (int i = 0; i < WORKGROUP_IDS.length; i++) {
93              actionItems3.add(generateActionItem(routeHeader3, "A", AUTHENTICATION_IDS[i], WORKGROUP_IDS[i]));
94          }
95          
96          actionItems.addAll(actionItems1);
97          actionItems.addAll(actionItems2);
98          actionItems.addAll(actionItems3);
99          for (Iterator<ActionItem> iterator = actionItems.iterator(); iterator.hasNext();) {
100             ActionItem actionItem = iterator.next();
101             getActionListService().saveActionItem(actionItem);
102         }
103     }
104 
105     @Test public void testRouteHeaderDelete() throws Exception {
106     	setUpOldSchool();
107         Collection<ActionItem> actionItems = getActionListService().findByDocumentId(routeHeader1.getDocumentId());
108         assertEquals("Route header " + routeHeader1.getDocumentId() + " should have action items.", AUTHENTICATION_IDS.length, actionItems.size());
109         getActionListService().deleteByDocumentId(routeHeader1.getDocumentId());
110         actionItems = getActionListService().findByDocumentId(routeHeader1.getDocumentId());
111         assertEquals("There should be no remaining action items for route header " + routeHeader1.getDocumentId(), 0, actionItems.size());
112         actionItems = getActionListService().findByDocumentId(routeHeader2.getDocumentId());
113         assertEquals("Route header " + routeHeader2.getDocumentId() + " should have action items.", AUTHENTICATION_IDS.length, actionItems.size());
114     }
115 
116     @Test public void testActionListCount() throws Exception {
117     	setUpOldSchool();
118         TransactionTemplate transactionTemplate = getTransactionTemplate();
119         transactionTemplate.execute(new TransactionCallback() {
120             public Object doInTransaction(TransactionStatus status) {
121             	return TestUtilities.getJdbcTemplate().execute(new StatementCallback() {
122             		public Object doInStatement(Statement stmt) {
123                         try {
124                             Connection conn = stmt.getConnection();
125                             PreparedStatement ps = conn.prepareStatement("select distinct PRNCPL_ID from krew_actn_itm_t");
126                             ResultSet rs = ps.executeQuery();
127                             int emplIdCnt = 0;
128                             int loopCnt = 0;
129                             //do first 5 for time sake
130                             while (rs.next() && ++loopCnt < 6) {
131                                 String workflowId = rs.getString(1);
132                                 PreparedStatement ps1 = conn.prepareStatement("select count(*) from krew_actn_itm_t where PRNCPL_ID = ?");
133                                 ps1.setString(1, workflowId);
134                                 ResultSet rsWorkflowIdCnt = ps1.executeQuery();
135                                 if (rsWorkflowIdCnt.next()) {
136                                     emplIdCnt = rsWorkflowIdCnt.getInt(1);
137                                 } else {
138                                     throw new Exception("WorkflowId " + workflowId + " didn't return a count.  Test SQL invalid.");
139                                 }
140                                 Collection<ActionItem> actionList = getActionListService().findByPrincipalId(workflowId);
141                                 assertEquals("ActionItemService returned incorrect number of ActionItems for user " + workflowId + " ActionList", emplIdCnt, actionList.size());
142                                 ps1.close();
143                                 rsWorkflowIdCnt.close();
144                             }
145                             rs.close();
146                             ps.close();
147                         } catch (Exception e) {
148                             throw new RuntimeException(e);
149                         }
150                         return null;
151                     }
152                 });
153             }
154         });
155     }
156 
157     @Test
158     public void testActionListMaxActionItemAndCount() throws Exception {
159         setUpOldSchool();
160         TransactionTemplate transactionTemplate = getTransactionTemplate();
161         transactionTemplate.execute(new TransactionCallback() {
162             public Object doInTransaction(TransactionStatus status) {
163                 return TestUtilities.getJdbcTemplate().execute(new StatementCallback() {
164                     public Object doInStatement(Statement stmt) {
165                         try {
166                             Connection conn = stmt.getConnection();
167                             PreparedStatement ps = conn.prepareStatement(
168                                     "select distinct PRNCPL_ID from krew_actn_itm_t");
169                             ResultSet rs = ps.executeQuery();
170                             int cnt = 0;
171                             int maxVal = 0;
172                             int loopCnt = 0;
173                             //do first 5 for time sake
174                             while (rs.next() && ++loopCnt < 6) {
175                                 String workflowId = rs.getString(1);
176                                 PreparedStatement ps1 = conn.prepareStatement(
177                                         "select cast((max(actn_itm_id))as decimal) as max_id, count(distinct(doc_hdr_id)) as total_records"
178                                                 + "  from ("
179                                                 + "  select actn_itm_id,doc_hdr_id "
180                                                 + "  from KREW_ACTN_ITM_T   where    prncpl_id=? "
181                                                 + "  group by  actn_itm_id,doc_hdr_id "
182                                                 + "  ) T");
183                                 ps1.setString(1, workflowId);
184                                 ResultSet rsWorkflowIdCnt = ps1.executeQuery();
185                                 if (rsWorkflowIdCnt.next()) {
186                                     maxVal = rsWorkflowIdCnt.getInt(1);
187                                     cnt = rsWorkflowIdCnt.getInt(2);
188                                 } else {
189                                     throw new Exception(
190                                             "WorkflowId " + workflowId + " didn't return a result set.  Test SQL invalid.");
191                                 }
192                                 List<Integer> ls = getActionListService().getMaxActionItemIdAndCountForUser(workflowId);
193                                 assertEquals((Integer) cnt, ls.get(1));
194                                 assertEquals((Integer) maxVal, ls.get(0));
195                                 ps1.close();
196                                 rsWorkflowIdCnt.close();
197                             }
198                             rs.close();
199                             ps.close();
200                         } catch (Exception e) {
201                             throw new RuntimeException(e);
202                         }
203                         return null;
204                     }
205                 });
206             }
207         });
208     }
209 
210     /**
211      * Tests that the user's secondary action list works appropriately.  Also checks that if a user
212      * is their own secondary delegate, their request shows up in their main action list rather than
213      * their secondary list.
214      */
215     @Test public void testSecondaryActionList() throws Exception {
216     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType");
217     	document.route("");
218 
219     	// at this point the document should be routed to the following people
220     	// 1) approve to bmcgough with primary delegate of rkirkend and secondary delegates of ewestfal and bmcgough (himself)
221     	// 2) approve to jitrue with a secondary delegate of jitrue (himself)
222     	// 3) acknowledge to user1
223     	// 4) approve to NonSIT workgroup (which should include user1)
224 
225     	// now lets verify that everyone's action lists look correct
226 
227     	String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
228     	String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
229     	String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
230     	String jitruePrincipalId = getPrincipalIdForName("jitrue");
231     	String user1PrincipalId = getPrincipalIdForName("user1");
232 
233     	Group NonSIT = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
234                 KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "NonSIT");
235 
236     	ActionListFilter noFilter = new ActionListFilter();
237     	ActionListFilter excludeSecondaryFilter = new ActionListFilter();
238     	excludeSecondaryFilter.setDelegationType(DelegationType.SECONDARY.getCode());
239     	excludeSecondaryFilter.setExcludeDelegationType(true);
240     	ActionListFilter secondaryFilter = new ActionListFilter();
241     	secondaryFilter.setDelegationType(DelegationType.SECONDARY.getCode());
242     	Collection<ActionItem> actionItems = null;
243     	ActionItem actionItem = null;
244 
245     	actionItems = getActionListService().getActionList(bmcgoughPrincipalId, excludeSecondaryFilter);
246     	assertEquals("bmcgough should have 0 items in his primary action list.", 0, actionItems.size());
247     	actionItems = getActionListService().getActionList(bmcgoughPrincipalId, secondaryFilter);
248     	assertEquals("bmcgough should have 1 item in his secondary action list.", 1, actionItems.size());
249         actionItem = actionItems.iterator().next();
250         assertEquals("Should be an approve request.", KewApiConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
251         assertEquals("Should be a secondary delegation request.", DelegationType.SECONDARY, actionItem.getDelegationType());
252     	actionItem = actionItems.iterator().next();
253     	assertEquals("Should be an approve request.", KewApiConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
254     	assertEquals("Should be a secondary delegation request.", DelegationType.SECONDARY, actionItem.getDelegationType());
255     	actionItems = getActionListService().getActionList(bmcgoughPrincipalId, noFilter);
256     	assertEquals("bmcgough should have 1 item in his entire action list.", 1, actionItems.size());
257 
258     	actionItems = getActionListService().getActionList(rkirkendPrincipalId, excludeSecondaryFilter);
259     	assertEquals("bmcgough should have 1 item in his primary action list.", 1, actionItems.size());
260 
261     	actionItems = getActionListService().getActionList(jitruePrincipalId, excludeSecondaryFilter);
262     	assertEquals("jitrue should have 1 item in his primary action list.", 1, actionItems.size());
263 
264     	actionItems = getActionListService().getActionList(ewestfalPrincipalId, secondaryFilter);
265     	assertEquals("ewestfal should have 1 item in his secondary action list.", 1, actionItems.size());
266 
267     	// check that user1's approve comes out as their action item from the action list
268     	actionItems = getActionListService().getActionList(user1PrincipalId, noFilter);
269     	assertEquals("user1 should have 1 item in his primary action list.", 1, actionItems.size());
270     	actionItem = actionItems.iterator().next();
271     	assertEquals("Should be an approve request.", KewApiConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
272     	assertEquals("Should be to a workgroup.", NonSIT.getId(), actionItem.getGroupId());
273     	// check that user1 acknowledge shows up when filtering
274     	ActionListFilter ackFilter = new ActionListFilter();
275     	ackFilter.setActionRequestCd(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
276     	actionItems = getActionListService().getActionList(user1PrincipalId, ackFilter);
277     	assertEquals("user1 should have 1 item in his primary action list.", 1, actionItems.size());
278     	actionItem = (ActionItem)actionItems.iterator().next();
279     	assertEquals("Should be an acknowledge request.", KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, actionItem.getActionRequestCd());
280     	assertNull("Should not be to a workgroup.", actionItem.getGroupId());
281 
282     	// all members of NonSIT should have a single primary Approve Request
283         List<String> memberPrincipalIds = KimApiServiceLocator.getGroupService().getMemberPrincipalIds(NonSIT.getId());
284         for (String memberPrincipalId : memberPrincipalIds)
285         {
286             //will want to convert to Kim Principal
287             actionItems = getActionListService().getActionList(memberPrincipalId, excludeSecondaryFilter);
288             assertEquals("Workgroup Member " + memberPrincipalId + " should have 1 action item.", 1, actionItems.size());
289             actionItem = (ActionItem) actionItems.iterator().next();
290             assertEquals("Should be an approve request.", KewApiConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
291             assertEquals("Should be to a workgroup.", NonSIT.getId(), actionItem.getGroupId());
292         }
293 
294         document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType_PrimaryDelegate");
295         document.route("");
296         document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType_PrimaryDelegate2");
297         document.route("");
298 
299         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, excludeSecondaryFilter);
300         assertEquals("bmcgough should have 0 items in his primary action list.", 0, actionItems.size());
301         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, secondaryFilter);
302         assertEquals("bmcgough should have 1 item in his secondary action list.", 3, actionItems.size());
303         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, new ActionListFilter());
304         assertEquals("bmcgough should have 1 item in his entire action list.", 3, actionItems.size());
305 
306         ActionListFilter filter = null;
307         // test a standard filter with no delegations
308         filter = new ActionListFilter();
309         filter.setDelegatorId(KewApiConstants.DELEGATION_DEFAULT);
310         filter.setPrimaryDelegateId(KewApiConstants.PRIMARY_DELEGATION_DEFAULT);
311         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
312         assertEquals("bmcgough should have 0 items in his entire action list.", 0, actionItems.size());
313 
314         // test secondary delegation with all selected returns all
315         filter = new ActionListFilter();
316         filter.setDelegationType(DelegationType.SECONDARY.getCode());
317         filter.setDelegatorId(KewApiConstants.ALL_CODE);
318         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
319         assertEquals("bmcgough has incorrect action list item count.", 3, actionItems.size());
320 
321         // test that primary delegation with none selected returns none
322         filter = new ActionListFilter();
323         filter.setDelegationType(DelegationType.SECONDARY.getCode());
324         filter.setDelegatorId(KewApiConstants.DELEGATION_DEFAULT);
325         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
326         assertEquals("bmcgough has incorrect action list item count.", 0, actionItems.size());
327 
328         // test that primary delegation with single user ids works corectly
329         filter = new ActionListFilter();
330         filter.setDelegationType(DelegationType.SECONDARY.getCode());
331         filter.setDelegatorId(bmcgoughPrincipalId);
332         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
333         assertEquals("bmcgough has incorrect action list item count.", 3, actionItems.size());
334         filter = new ActionListFilter();
335         filter.setDelegationType(DelegationType.SECONDARY.getCode());
336         filter.setDelegatorId(bmcgoughPrincipalId);
337         actionItems = getActionListService().getActionList(ewestfalPrincipalId, filter);
338         assertEquals("ewestfal has incorrect action list item count.", 3, actionItems.size());
339         filter = new ActionListFilter();
340         filter.setDelegationType(DelegationType.SECONDARY.getCode());
341         filter.setDelegatorId(jitruePrincipalId);
342         actionItems = getActionListService().getActionList(jitruePrincipalId, filter);
343         assertEquals("jitrue has incorrect action list item count.", 3, actionItems.size());
344     }
345 
346     /**
347      * Tests that the user's secondary action list works appropriately.  Also checks that if a user
348      * is their own secondary delegate, their request shows up in their main action list rather than
349      * their secondary list.
350      */
351     @Test public void testPrimaryDelegationActionList() throws Exception {
352     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType");
353     	document.route("");
354 
355     	// at this point the document should be routed to the following people
356     	// 1) approve to bmcgough with primary delegate of rkirkend and secondary delegates of ewestfal and bmcgough (himself)
357     	// 2) approve to jitrue with a secondary delegate of jitrue (himself)
358     	// 3) acknowledge to user1
359     	// 4) approve to NonSIT workgroup (which should include user1)
360 
361     	// now lets verify that everyone's action lists look correct
362 
363     	String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
364     	String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
365     	String delyeaPrincipalId = getPrincipalIdForName("delyea");
366     	String temayPrincipalId = getPrincipalIdForName("temay");
367     	String jhopfPrincipalId = getPrincipalIdForName("jhopf");
368 
369     	ActionListFilter showPrimaryFilter = new ActionListFilter();
370     	showPrimaryFilter.setDelegationType(DelegationType.PRIMARY.getCode());
371     	Collection<ActionItem> actionItems = null;
372     	ActionItem actionItem = null;
373 
374     	// make sure showing primary delegations show primary delegated action items
375     	actionItems = getActionListService().getActionList(bmcgoughPrincipalId, showPrimaryFilter);
376     	assertEquals("bmcgough should have 1 item in his primary delegation action list.", 1, actionItems.size());
377     	actionItems = getActionListService().getActionList(bmcgoughPrincipalId, new ActionListFilter());
378     	assertEquals("bmcgough should have 1 item in his entire action list.", 1, actionItems.size());
379 
380     	document = WorkflowDocumentFactory.createDocument(jhopfPrincipalId, "ActionListDocumentType_PrimaryDelegate");
381     	document.route("");
382     	document = WorkflowDocumentFactory.createDocument(jhopfPrincipalId, "ActionListDocumentType_PrimaryDelegate2");
383         document.route("");
384 
385         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, showPrimaryFilter);
386         // should be 6 total action items but 3 distinct doc ids
387         assertEquals("bmcgough should have 1 item in his primary delegation action list.", 3, actionItems.size());
388 
389         ActionListFilter filter = null;
390 
391         // test a standard filter with no delegations
392         filter = new ActionListFilter();
393         filter.setDelegatorId(KewApiConstants.DELEGATION_DEFAULT);
394         filter.setPrimaryDelegateId(KewApiConstants.PRIMARY_DELEGATION_DEFAULT);
395         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
396         assertEquals("bmcgough should have 0 items in his entire action list.", 0, actionItems.size());
397 
398         // test primary delegation with all selected returns all
399         filter = new ActionListFilter();
400         filter.setDelegationType(DelegationType.PRIMARY.getCode());
401         filter.setPrimaryDelegateId(KewApiConstants.ALL_CODE);
402         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
403         assertEquals("bmcgough should have 1 item in his entire action list.", 3, actionItems.size());
404 
405         // test that primary delegation with none selected returns none
406         filter = new ActionListFilter();
407         filter.setDelegationType(DelegationType.PRIMARY.getCode());
408         filter.setPrimaryDelegateId(KewApiConstants.PRIMARY_DELEGATION_DEFAULT);
409         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
410         assertEquals("bmcgough should have 1 item in his entire action list.", 0, actionItems.size());
411 
412         // test that primary delegation with single user ids works corectly
413         filter = new ActionListFilter();
414         filter.setDelegationType(DelegationType.PRIMARY.getCode());
415         filter.setPrimaryDelegateId(rkirkendPrincipalId);
416         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
417         assertEquals("bmcgough should have 3 items in his entire action list.", 3, actionItems.size());
418         filter = new ActionListFilter();
419         filter.setDelegationType(DelegationType.PRIMARY.getCode());
420         filter.setPrimaryDelegateId(delyeaPrincipalId);
421         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
422         assertEquals("bmcgough should have 2 items in his entire action list.", 2, actionItems.size());
423         filter = new ActionListFilter();
424         filter.setDelegationType(DelegationType.PRIMARY.getCode());
425         filter.setPrimaryDelegateId(temayPrincipalId);
426         actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
427         assertEquals("bmcgough should have 1 item in his entire action list.", 1, actionItems.size());
428 
429     }
430 
431     /**
432      * Tests that the retrieval of primary and secondary delegation users is working correctly
433      */
434     @Test public void testGettingDelegationUsers() throws Exception {
435 
436         Person jhopf = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("jhopf");
437         Person bmcgough = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("bmcgough");
438     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType");
439     	document.route("");
440         document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType_PrimaryDelegate");
441         document.route("");
442         document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"), "ActionListDocumentType_PrimaryDelegate2");
443         document.route("");
444 
445         Collection<Recipient> recipients = getActionListService().findUserPrimaryDelegations(jhopf.getPrincipalId());
446         assertEquals("Wrong size of users who were delegated to via Primary Delegation", 0, recipients.size());
447     	recipients = getActionListService().findUserPrimaryDelegations(bmcgough.getPrincipalId());
448     	assertEquals("Wrong size of users who were delegated to via Primary Delegation", 3, recipients.size());
449     	String user1 = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("rkirkend").getPrincipalId();
450     	String user2 = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("temay").getPrincipalId();
451     	String user3 = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("delyea").getPrincipalId();
452 
453     	boolean foundUser1 = false;
454         boolean foundUser2 = false;
455         boolean foundUser3 = false;
456     	for (Recipient recipient : recipients) {
457             if (user1.equals(((WebFriendlyRecipient)recipient).getRecipientId())) {
458                 foundUser1 = true;
459             } else if (user2.equals(((WebFriendlyRecipient)recipient).getRecipientId())) {
460                 foundUser2 = true;
461             } else if (user3.equals(((WebFriendlyRecipient)recipient).getRecipientId())) {
462                 foundUser3 = true;
463             } else {
464                 fail("Found invalid recipient in list with display name '" + ((WebFriendlyRecipient)recipient).getDisplayName() + "'");
465             }
466         }
467     	assertTrue("Should have found user " + user1, foundUser1);
468         assertTrue("Should have found user " + user2, foundUser2);
469         assertTrue("Should have found user " + user3, foundUser3);
470 
471     	recipients = getActionListService().findUserSecondaryDelegators(bmcgough.getPrincipalId());
472     	assertEquals("Wrong size of users who were have delegated to given user via Secondary Delegation", 1, recipients.size());
473     	WebFriendlyRecipient recipient = (WebFriendlyRecipient)recipients.iterator().next();
474     	assertEquals("Wrong employee id of primary delegate", "bmcgough", getPrincipalNameForId(recipient.getRecipientId()));
475     }
476 
477     @Test
478     public void testCreateActionItemForActionRequest() throws Exception {
479         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"),
480                 "ActionListDocumentType");
481         document.route("");
482         List<ActionRequest> requests = document.getRootActionRequests();
483         assertTrue("there must be ActionRequestDTOs to test!", requests != null && requests.size() > 0);
484 
485         for (ActionRequest reqDTO : requests) {
486             if (reqDTO.getParentActionRequestId() == null) {
487                 ActionRequestValue reqVal = ActionRequestValue.from(reqDTO);
488 
489                 List<ActionItem> actionItems = new ArrayList<ActionItem>();
490                 actionItems.add(getActionListService().createActionItemForActionRequest(reqVal));
491                 assertTrue(actionItems.size() > 0);
492             }
493         }
494 
495     }
496 
497     @Test
498     public void testDeleteActionItem() throws Exception {
499         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"),
500                 "ActionListDocumentType");
501         document.route("");
502         Collection<ActionItem> actionItems = getActionListService().getActionList(getPrincipalIdForName("bmcgough"),
503                 new ActionListFilter());
504         assertEquals("bmcgough should have 1 item in his action list.", 1, actionItems.size());
505         //delete one of the action items
506         ActionItem itm = null;
507         for (Iterator<ActionItem> iterator = actionItems.iterator(); iterator.hasNext(); ) {
508             ActionItem actionItem = iterator.next();
509             itm = getActionListService().findByActionItemId(actionItem.getId());
510             getActionListService().deleteActionItem(itm);
511             break;
512         }
513         actionItems = getActionListService().getActionList(getPrincipalIdForName("bmcgough"), new ActionListFilter());
514         assertEquals("bmcgough should have 0 item in his action list.", 0, actionItems.size());
515     }
516 
517     @Test
518     public void testFindByDocumentTypeName() throws Exception {
519         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"),
520                 "ActionListDocumentType");
521         document.route("");
522         Collection<ActionItem> actionItems = getActionListService().findByDocumentTypeName("ActionListDocumentType");
523         assertEquals("There should be 10 action items", 10, actionItems.size());
524     }
525 
526     @Test
527     public void testFindByActionRequestId() throws Exception {
528         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("jhopf"),
529                 "ActionListDocumentType");
530         document.route("");
531         Collection<ActionRequest> actionRequests = document.getRootActionRequests();
532         for (Iterator<ActionRequest> iterator = actionRequests.iterator(); iterator.hasNext(); ) {
533             ActionRequest actionRequest = iterator.next();
534             if (actionRequest.getActionRequested().getCode().equals(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ)) {
535                 Collection<ActionItem> actionItems = getActionListService().findByActionRequestId(
536                         actionRequest.getId());
537                 assertTrue(actionItems.size() == 1);
538             }
539         }
540     }
541     @Test
542     public void testUpdateActionItemForTitleChange() throws Exception {
543           setUpOldSchool();
544         Collection<ActionItem> actionItems;
545         actionItems = getActionListService().findByDocumentId(routeHeader1.getDocumentId());
546          for (Iterator<ActionItem> iterator = actionItems.iterator(); iterator.hasNext(); ) {
547             ActionItem actionItem = iterator.next();
548             assertEquals("Test",actionItem.getDocTitle());
549          }
550         DocumentUpdate.Builder builder = DocumentUpdate.Builder.create();
551         builder.setTitle("NewTitle");
552         DocumentUpdate documentUpdate = builder.build();
553         routeHeader1.applyDocumentUpdate(documentUpdate);
554         actionItems = getActionListService().findByDocumentId(routeHeader1.getDocumentId());
555         for (Iterator<ActionItem> iterator = actionItems.iterator(); iterator.hasNext(); ) {
556             ActionItem actionItem = iterator.next();
557             assertEquals("NewTitle",actionItem.getDocTitle());
558          }
559     }
560 
561     private DocumentRouteHeaderValue generateDocRouteHeader() {
562         DocumentRouteHeaderValue routeHeader = new DocumentRouteHeaderValue();
563         routeHeader.setAppDocId("Test");
564         routeHeader.setApprovedDate(null);
565         routeHeader.setCreateDate(new Timestamp(new Date().getTime()));
566         routeHeader.setDocContent("test");
567         routeHeader.setDocRouteLevel(1);
568         routeHeader.setDocRouteStatus(KewApiConstants.ROUTE_HEADER_ENROUTE_CD);
569         routeHeader.setDocTitle("Test");
570         routeHeader.setDocumentTypeId("1");
571         routeHeader.setDocVersion(KewApiConstants.DocumentContentVersions.CURRENT);
572         routeHeader.setRouteStatusDate(new Timestamp(new Date().getTime()));
573         routeHeader.setDateModified(new Timestamp(new Date().getTime()));
574         routeHeader.setInitiatorWorkflowId("someone");
575         return routeHeader;
576     }
577 
578     private ActionItem generateActionItem(DocumentRouteHeaderValue routeHeader, String actionRequested, String authenticationId, String groupId) {
579         ActionItem actionItem = new ActionItem();
580         actionItem.setActionRequestCd(actionRequested);
581         actionItem.setActionRequestId("1");
582         actionItem.setPrincipalId(getPrincipalIdForName(authenticationId));
583         actionItem.setDocumentId(routeHeader.getDocumentId());
584         actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
585         actionItem.setDocHandlerURL("Unit testing");
586         actionItem.setDocLabel("unit testing");
587         actionItem.setDocTitle(routeHeader.getDocTitle());
588         actionItem.setDocName("docname");
589         actionItem.setGroupId(groupId);
590 //        actionItem.setResponsibilityId(new Long(-1));
591         return actionItem;
592     }
593 
594     private ActionListService getActionListService() {
595         return KEWServiceLocator.getActionListService();
596     }
597 
598     private RouteHeaderService getRouteHeaderService() {
599         return KEWServiceLocator.getRouteHeaderService();
600     }
601 }