1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.actionlist;
18
19
20 import org.apache.ojb.broker.PersistenceBroker;
21 import org.junit.Test;
22 import org.kuali.rice.kew.actionitem.ActionItem;
23 import org.kuali.rice.kew.actionlist.service.ActionListService;
24 import org.kuali.rice.kew.actionrequest.Recipient;
25 import org.kuali.rice.kew.dto.NetworkIdDTO;
26 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
27 import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
28 import org.kuali.rice.kew.service.KEWServiceLocator;
29 import org.kuali.rice.kew.service.WorkflowDocument;
30 import org.kuali.rice.kew.test.KEWTestCase;
31 import org.kuali.rice.kew.test.TestUtilities;
32 import org.kuali.rice.kew.util.KEWConstants;
33 import org.kuali.rice.kew.util.WebFriendlyRecipient;
34 import org.kuali.rice.kim.bo.Group;
35 import org.kuali.rice.kim.bo.Person;
36 import org.kuali.rice.kim.service.KIMServiceLocator;
37 import org.kuali.rice.kim.util.KimConstants;
38 import org.springframework.transaction.TransactionStatus;
39 import org.springframework.transaction.support.TransactionCallback;
40 import org.springframework.transaction.support.TransactionTemplate;
41 import org.springmodules.orm.ojb.PersistenceBrokerCallback;
42
43 import java.sql.Connection;
44 import java.sql.PreparedStatement;
45 import java.sql.ResultSet;
46 import java.sql.Timestamp;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Date;
50 import java.util.List;
51
52
53
54
55
56 public class ActionListTest extends KEWTestCase {
57
58 private static final String[] AUTHENTICATION_IDS = { "ewestfal", "rkirkend", "jhopf", "bmcgough" };
59 private static final String[] WORKGROUP_IDS = { "1", "2", "3", "4" };
60
61 private DocumentRouteHeaderValue routeHeader1;
62 private DocumentRouteHeaderValue routeHeader2;
63 private List<ActionItem> actionItems = new ArrayList<ActionItem>();
64
65 protected void loadTestData() throws Exception {
66 loadXmlFile("ActionListConfig.xml");
67 }
68
69 private void setUpOldSchool() throws Exception {
70 super.setUpAfterDataLoad();
71 routeHeader1 = generateDocRouteHeader();
72 routeHeader1.setActionItems(new ArrayList<ActionItem>());
73 routeHeader2 = generateDocRouteHeader();
74 routeHeader2.setActionItems(new ArrayList<ActionItem>());
75 for (String AUTHENTICATION_ID : AUTHENTICATION_IDS)
76 {
77 routeHeader1.getActionItems().add(generateActionItem(routeHeader1, "K", AUTHENTICATION_ID, null));
78 routeHeader2.getActionItems().add(generateActionItem(routeHeader2, "A", AUTHENTICATION_ID, null));
79 }
80 DocumentRouteHeaderValue routeHeader3 = generateDocRouteHeader();
81 routeHeader3.setActionItems(new ArrayList<ActionItem>());
82 for (int i = 0; i < WORKGROUP_IDS.length; i++) {
83 routeHeader3.getActionItems().add(generateActionItem(routeHeader3, "A", AUTHENTICATION_IDS[i], WORKGROUP_IDS[i]));
84 }
85
86 getRouteHeaderService().saveRouteHeader(routeHeader1);
87 getRouteHeaderService().saveRouteHeader(routeHeader2);
88 getRouteHeaderService().saveRouteHeader(routeHeader3);
89
90 actionItems.addAll(routeHeader1.getActionItems());
91 actionItems.addAll(routeHeader2.getActionItems());
92 actionItems.addAll(routeHeader3.getActionItems());
93 for (ActionItem actionItem : actionItems)
94 {
95 getActionListService().saveActionItem(actionItem);
96 }
97
98
99 }
100
101 @Test public void testRouteHeaderDelete() throws Exception {
102 setUpOldSchool();
103 Collection<ActionItem> actionItems = getActionListService().findByRouteHeaderId(routeHeader1.getRouteHeaderId());
104 assertEquals("Route header " + routeHeader1.getRouteHeaderId() + " should have action items.", AUTHENTICATION_IDS.length, actionItems.size());
105 getActionListService().deleteByRouteHeaderId(routeHeader1.getRouteHeaderId());
106 actionItems = getActionListService().findByRouteHeaderId(routeHeader1.getRouteHeaderId());
107 assertEquals("There should be no remaining action items for route header " + routeHeader1.getRouteHeaderId(), 0, actionItems.size());
108 actionItems = getActionListService().findByRouteHeaderId(routeHeader2.getRouteHeaderId());
109 assertEquals("Route header " + routeHeader2.getRouteHeaderId() + " should have action items.", AUTHENTICATION_IDS.length, actionItems.size());
110 }
111
112 @Test public void testActionListCount() throws Exception {
113 setUpOldSchool();
114 TransactionTemplate transactionTemplate = getTransactionTemplate();
115 transactionTemplate.execute(new TransactionCallback() {
116 public Object doInTransaction(TransactionStatus status) {
117 return TestUtilities.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
118 public Object doInPersistenceBroker(PersistenceBroker pb) {
119 try {
120 Connection conn = pb.serviceConnectionManager().getConnection();
121 PreparedStatement ps = conn.prepareStatement("select distinct PRNCPL_ID from krew_actn_itm_t");
122 ResultSet rs = ps.executeQuery();
123 int emplIdCnt = 0;
124 int loopCnt = 0;
125
126 while (rs.next() && ++loopCnt < 6) {
127 String workflowId = rs.getString(1);
128 PreparedStatement ps1 = conn.prepareStatement("select count(*) from krew_actn_itm_t where PRNCPL_ID = ?");
129 ps1.setString(1, workflowId);
130 ResultSet rsWorkflowIdCnt = ps1.executeQuery();
131 if (rsWorkflowIdCnt.next()) {
132 emplIdCnt = rsWorkflowIdCnt.getInt(1);
133 } else {
134 throw new Exception("WorkflowId " + workflowId + " didn't return a count. Test SQL invalid.");
135 }
136 Collection<ActionItem> actionList = getActionListService().findByPrincipalId(workflowId);
137 assertEquals("ActionItemService returned incorrect number of ActionItems for user " + workflowId + " ActionList", emplIdCnt, actionList.size());
138 ps1.close();
139 rsWorkflowIdCnt.close();
140 }
141 rs.close();
142 ps.close();
143 } catch (Exception e) {
144 throw new RuntimeException(e);
145 }
146 return null;
147 }
148 });
149 }
150 });
151 }
152
153
154
155
156
157
158 @Test public void testSecondaryActionList() throws Exception {
159 WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType");
160 document.routeDocument("");
161
162
163
164
165
166
167
168
169
170 String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
171 String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
172 String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
173 String jitruePrincipalId = getPrincipalIdForName("jitrue");
174 String user1PrincipalId = getPrincipalIdForName("user1");
175
176 Group NonSIT = KIMServiceLocator.getIdentityManagementService().getGroupByName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "NonSIT");
177
178 ActionListFilter noFilter = new ActionListFilter();
179 ActionListFilter excludeSecondaryFilter = new ActionListFilter();
180 excludeSecondaryFilter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
181 excludeSecondaryFilter.setExcludeDelegationType(true);
182 ActionListFilter secondaryFilter = new ActionListFilter();
183 secondaryFilter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
184 Collection<ActionItem> actionItems = null;
185 ActionItem actionItem = null;
186
187 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, excludeSecondaryFilter);
188 assertEquals("bmcgough should have 0 items in his primary action list.", 0, actionItems.size());
189 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, secondaryFilter);
190 assertEquals("bmcgough should have 1 item in his secondary action list.", 1, actionItems.size());
191 actionItem = actionItems.iterator().next();
192 assertEquals("Should be an approve request.", KEWConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
193 assertEquals("Should be a secondary delegation request.", KEWConstants.DELEGATION_SECONDARY, actionItem.getDelegationType());
194 actionItem = actionItems.iterator().next();
195 assertEquals("Should be an approve request.", KEWConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
196 assertEquals("Should be a secondary delegation request.", KEWConstants.DELEGATION_SECONDARY, actionItem.getDelegationType());
197 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, noFilter);
198 assertEquals("bmcgough should have 1 item in his entire action list.", 1, actionItems.size());
199
200 actionItems = getActionListService().getActionList(rkirkendPrincipalId, excludeSecondaryFilter);
201 assertEquals("bmcgough should have 1 item in his primary action list.", 1, actionItems.size());
202
203 actionItems = getActionListService().getActionList(jitruePrincipalId, excludeSecondaryFilter);
204 assertEquals("jitrue should have 1 item in his primary action list.", 1, actionItems.size());
205
206 actionItems = getActionListService().getActionList(ewestfalPrincipalId, secondaryFilter);
207 assertEquals("ewestfal should have 1 item in his secondary action list.", 1, actionItems.size());
208
209
210 actionItems = getActionListService().getActionList(user1PrincipalId, noFilter);
211 assertEquals("user1 should have 1 item in his primary action list.", 1, actionItems.size());
212 actionItem = actionItems.iterator().next();
213 assertEquals("Should be an approve request.", KEWConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
214 assertEquals("Should be to a workgroup.", NonSIT.getGroupId(), actionItem.getGroupId());
215
216 ActionListFilter ackFilter = new ActionListFilter();
217 ackFilter.setActionRequestCd(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
218 actionItems = getActionListService().getActionList(user1PrincipalId, ackFilter);
219 assertEquals("user1 should have 1 item in his primary action list.", 1, actionItems.size());
220 actionItem = (ActionItem)actionItems.iterator().next();
221 assertEquals("Should be an acknowledge request.", KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, actionItem.getActionRequestCd());
222 assertNull("Should not be to a workgroup.", actionItem.getGroupId());
223
224
225 List<String> memberPrincipalIds = KIMServiceLocator.getIdentityManagementService().getGroupMemberPrincipalIds(NonSIT.getGroupId());
226 for (String memberPrincipalId : memberPrincipalIds)
227 {
228
229 actionItems = getActionListService().getActionList(memberPrincipalId, excludeSecondaryFilter);
230 assertEquals("Workgroup Member " + memberPrincipalId + " should have 1 action item.", 1, actionItems.size());
231 actionItem = (ActionItem) actionItems.iterator().next();
232 assertEquals("Should be an approve request.", KEWConstants.ACTION_REQUEST_APPROVE_REQ, actionItem.getActionRequestCd());
233 assertEquals("Should be to a workgroup.", NonSIT.getGroupId(), actionItem.getGroupId());
234 }
235
236 document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType_PrimaryDelegate");
237 document.routeDocument("");
238 document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType_PrimaryDelegate2");
239 document.routeDocument("");
240
241 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, excludeSecondaryFilter);
242 assertEquals("bmcgough should have 0 items in his primary action list.", 0, actionItems.size());
243 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, secondaryFilter);
244 assertEquals("bmcgough should have 1 item in his secondary action list.", 3, actionItems.size());
245 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, new ActionListFilter());
246 assertEquals("bmcgough should have 1 item in his entire action list.", 3, actionItems.size());
247
248 ActionListFilter filter = null;
249
250 filter = new ActionListFilter();
251 filter.setDelegatorId(KEWConstants.DELEGATION_DEFAULT);
252 filter.setPrimaryDelegateId(KEWConstants.PRIMARY_DELEGATION_DEFAULT);
253 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
254 assertEquals("bmcgough should have 0 items in his entire action list.", 0, actionItems.size());
255
256
257 filter = new ActionListFilter();
258 filter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
259 filter.setDelegatorId(KEWConstants.ALL_CODE);
260 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
261 assertEquals("bmcgough has incorrect action list item count.", 3, actionItems.size());
262
263
264 filter = new ActionListFilter();
265 filter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
266 filter.setDelegatorId(KEWConstants.DELEGATION_DEFAULT);
267 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
268 assertEquals("bmcgough has incorrect action list item count.", 0, actionItems.size());
269
270
271 filter = new ActionListFilter();
272 filter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
273 filter.setDelegatorId(bmcgoughPrincipalId);
274 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
275 assertEquals("bmcgough has incorrect action list item count.", 3, actionItems.size());
276 filter = new ActionListFilter();
277 filter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
278 filter.setDelegatorId(bmcgoughPrincipalId);
279 actionItems = getActionListService().getActionList(ewestfalPrincipalId, filter);
280 assertEquals("ewestfal has incorrect action list item count.", 3, actionItems.size());
281 filter = new ActionListFilter();
282 filter.setDelegationType(KEWConstants.DELEGATION_SECONDARY);
283 filter.setDelegatorId(jitruePrincipalId);
284 actionItems = getActionListService().getActionList(jitruePrincipalId, filter);
285 assertEquals("jitrue has incorrect action list item count.", 3, actionItems.size());
286 }
287
288
289
290
291
292
293 @Test public void testPrimaryDelegationActionList() throws Exception {
294 WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType");
295 document.routeDocument("");
296
297
298
299
300
301
302
303
304
305 String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
306 String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
307 String delyeaPrincipalId = getPrincipalIdForName("delyea");
308 String temayPrincipalId = getPrincipalIdForName("temay");
309 String jhopfPrincipalId = getPrincipalIdForName("jhopf");
310
311 ActionListFilter showPrimaryFilter = new ActionListFilter();
312 showPrimaryFilter.setDelegationType(KEWConstants.DELEGATION_PRIMARY);
313 Collection<ActionItem> actionItems = null;
314 ActionItem actionItem = null;
315
316
317 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, showPrimaryFilter);
318 assertEquals("bmcgough should have 1 item in his primary delegation action list.", 1, actionItems.size());
319 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, new ActionListFilter());
320 assertEquals("bmcgough should have 1 item in his entire action list.", 1, actionItems.size());
321
322 document = new WorkflowDocument(jhopfPrincipalId, "ActionListDocumentType_PrimaryDelegate");
323 document.routeDocument("");
324 document = new WorkflowDocument(jhopfPrincipalId, "ActionListDocumentType_PrimaryDelegate2");
325 document.routeDocument("");
326
327 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, showPrimaryFilter);
328
329 assertEquals("bmcgough should have 1 item in his primary delegation action list.", 3, actionItems.size());
330
331 ActionListFilter filter = null;
332
333
334 filter = new ActionListFilter();
335 filter.setDelegatorId(KEWConstants.DELEGATION_DEFAULT);
336 filter.setPrimaryDelegateId(KEWConstants.PRIMARY_DELEGATION_DEFAULT);
337 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
338 assertEquals("bmcgough should have 0 items in his entire action list.", 0, actionItems.size());
339
340
341 filter = new ActionListFilter();
342 filter.setDelegationType(KEWConstants.DELEGATION_PRIMARY);
343 filter.setPrimaryDelegateId(KEWConstants.ALL_CODE);
344 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
345 assertEquals("bmcgough should have 1 item in his entire action list.", 3, actionItems.size());
346
347
348 filter = new ActionListFilter();
349 filter.setDelegationType(KEWConstants.DELEGATION_PRIMARY);
350 filter.setPrimaryDelegateId(KEWConstants.PRIMARY_DELEGATION_DEFAULT);
351 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
352 assertEquals("bmcgough should have 1 item in his entire action list.", 0, actionItems.size());
353
354
355 filter = new ActionListFilter();
356 filter.setDelegationType(KEWConstants.DELEGATION_PRIMARY);
357 filter.setPrimaryDelegateId(rkirkendPrincipalId);
358 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
359 assertEquals("bmcgough should have 3 items in his entire action list.", 3, actionItems.size());
360 filter = new ActionListFilter();
361 filter.setDelegationType(KEWConstants.DELEGATION_PRIMARY);
362 filter.setPrimaryDelegateId(delyeaPrincipalId);
363 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
364 assertEquals("bmcgough should have 2 items in his entire action list.", 2, actionItems.size());
365 filter = new ActionListFilter();
366 filter.setDelegationType(KEWConstants.DELEGATION_PRIMARY);
367 filter.setPrimaryDelegateId(temayPrincipalId);
368 actionItems = getActionListService().getActionList(bmcgoughPrincipalId, filter);
369 assertEquals("bmcgough should have 1 item in his entire action list.", 1, actionItems.size());
370
371 }
372
373
374
375
376 @Test public void testGettingDelegationUsers() throws Exception {
377
378 Person jhopf = KIMServiceLocator.getPersonService().getPersonByPrincipalName("jhopf");
379 Person bmcgough = KIMServiceLocator.getPersonService().getPersonByPrincipalName("bmcgough");
380 WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType");
381 document.routeDocument("");
382 document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType_PrimaryDelegate");
383 document.routeDocument("");
384 document = new WorkflowDocument(new NetworkIdDTO("jhopf"), "ActionListDocumentType_PrimaryDelegate2");
385 document.routeDocument("");
386
387 Collection<Recipient> recipients = getActionListService().findUserPrimaryDelegations(jhopf.getPrincipalId());
388 assertEquals("Wrong size of users who were delegated to via Primary Delegation", 0, recipients.size());
389 recipients = getActionListService().findUserPrimaryDelegations(bmcgough.getPrincipalId());
390 assertEquals("Wrong size of users who were delegated to via Primary Delegation", 3, recipients.size());
391 String user1 = KIMServiceLocator.getPersonService().getPersonByPrincipalName("rkirkend").getPrincipalId();
392 String user2 = KIMServiceLocator.getPersonService().getPersonByPrincipalName("temay").getPrincipalId();
393 String user3 = KIMServiceLocator.getPersonService().getPersonByPrincipalName("delyea").getPrincipalId();
394
395 boolean foundUser1 = false;
396 boolean foundUser2 = false;
397 boolean foundUser3 = false;
398 for (Recipient recipient : recipients) {
399 if (user1.equals(((WebFriendlyRecipient)recipient).getRecipientId())) {
400 foundUser1 = true;
401 } else if (user2.equals(((WebFriendlyRecipient)recipient).getRecipientId())) {
402 foundUser2 = true;
403 } else if (user3.equals(((WebFriendlyRecipient)recipient).getRecipientId())) {
404 foundUser3 = true;
405 } else {
406 fail("Found invalid recipient in list with display name '" + ((WebFriendlyRecipient)recipient).getDisplayName() + "'");
407 }
408 }
409 assertTrue("Should have found user " + user1, foundUser1);
410 assertTrue("Should have found user " + user2, foundUser2);
411 assertTrue("Should have found user " + user3, foundUser3);
412
413 recipients = getActionListService().findUserSecondaryDelegators(bmcgough.getPrincipalId());
414 assertEquals("Wrong size of users who were have delegated to given user via Secondary Delegation", 1, recipients.size());
415 WebFriendlyRecipient recipient = (WebFriendlyRecipient)recipients.iterator().next();
416 assertEquals("Wrong employee id of primary delegate", "bmcgough", getPrincipalNameForId(recipient.getRecipientId()));
417 }
418
419 private DocumentRouteHeaderValue generateDocRouteHeader() {
420 DocumentRouteHeaderValue routeHeader = new DocumentRouteHeaderValue();
421 routeHeader.setAppDocId("Test");
422 routeHeader.setApprovedDate(null);
423 routeHeader.setCreateDate(new Timestamp(new Date().getTime()));
424 routeHeader.setDocContent("test");
425 routeHeader.setDocRouteLevel(1);
426 routeHeader.setDocRouteStatus(KEWConstants.ROUTE_HEADER_ENROUTE_CD);
427 routeHeader.setDocTitle("Test");
428 routeHeader.setDocumentTypeId((long) 1);
429 routeHeader.setDocVersion(KEWConstants.CURRENT_DOCUMENT_VERSION);
430 routeHeader.setRouteStatusDate(new Timestamp(new Date().getTime()));
431 routeHeader.setStatusModDate(new Timestamp(new Date().getTime()));
432 routeHeader.setInitiatorWorkflowId("someone");
433 return routeHeader;
434 }
435
436 private ActionItem generateActionItem(DocumentRouteHeaderValue routeHeader, String actionRequested, String authenticationId, String groupId) {
437 ActionItem actionItem = new ActionItem();
438 actionItem.setActionRequestCd(actionRequested);
439 actionItem.setActionRequestId((long) 1);
440 actionItem.setPrincipalId(getPrincipalIdForName(authenticationId));
441 actionItem.setRouteHeaderId(routeHeader.getRouteHeaderId());
442 actionItem.setRouteHeader(routeHeader);
443 actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
444 actionItem.setDocHandlerURL("Unit testing");
445 actionItem.setDocLabel("unit testing");
446 actionItem.setDocTitle(routeHeader.getDocTitle());
447 actionItem.setDocName("docname");
448 actionItem.setGroupId(groupId);
449
450 return actionItem;
451 }
452
453 private ActionListService getActionListService() {
454 return KEWServiceLocator.getActionListService();
455 }
456
457 private RouteHeaderService getRouteHeaderService() {
458 return KEWServiceLocator.getRouteHeaderService();
459 }
460 }