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