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