001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.actionlist;
018:
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.Timestamp;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Date;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.ojb.broker.PersistenceBroker;
030: import org.junit.Test;
031: import org.kuali.workflow.test.WorkflowTestCase;
032: import org.springframework.transaction.TransactionStatus;
033: import org.springframework.transaction.support.TransactionCallback;
034: import org.springframework.transaction.support.TransactionTemplate;
035: import org.springmodules.orm.ojb.PersistenceBrokerCallback;
036:
037: import edu.iu.uis.eden.EdenConstants;
038: import edu.iu.uis.eden.KEWServiceLocator;
039: import edu.iu.uis.eden.actionitem.ActionItem;
040: import edu.iu.uis.eden.clientapp.WorkflowDocument;
041: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
042: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
043: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
044: import edu.iu.uis.eden.routeheader.RouteHeaderService;
045: import edu.iu.uis.eden.test.TestUtilities;
046: import edu.iu.uis.eden.user.AuthenticationUserId;
047: import edu.iu.uis.eden.user.UserService;
048: import edu.iu.uis.eden.user.WorkflowUser;
049: import edu.iu.uis.eden.user.WorkflowUserId;
050: import edu.iu.uis.eden.workgroup.GroupNameId;
051: import edu.iu.uis.eden.workgroup.Workgroup;
052:
053: /**
054: * @author rkirkend
055: */
056: public class ActionListTest extends WorkflowTestCase {
057:
058: private static final String[] AUTHENTICATION_IDS = { "ewestfal",
059: "rkirkend", "jhopf", "bmcgough" };
060: private static final Long[] WORKGROUP_IDS = { new Long(1),
061: new Long(2), new Long(3), new Long(4) };
062:
063: private DocumentRouteHeaderValue routeHeader1;
064: private DocumentRouteHeaderValue routeHeader2;
065: private DocumentRouteHeaderValue routeHeader3;
066: private List actionItems = new ArrayList();
067:
068: protected void loadTestData() throws Exception {
069: loadXmlFile("ActionListConfig.xml");
070: }
071:
072: private void setUpOldSchool() throws Exception {
073: super .setUpTransaction();
074: routeHeader1 = generateDocRouteHeader();
075: routeHeader1.setActionItems(new ArrayList());
076: routeHeader2 = generateDocRouteHeader();
077: routeHeader2.setActionItems(new ArrayList());
078: for (int i = 0; i < AUTHENTICATION_IDS.length; i++) {
079: routeHeader1.getActionItems().add(
080: generateActionItem(routeHeader1, "K",
081: AUTHENTICATION_IDS[i], null));
082: routeHeader2.getActionItems().add(
083: generateActionItem(routeHeader2, "A",
084: AUTHENTICATION_IDS[i], null));
085: }
086: routeHeader3 = generateDocRouteHeader();
087: routeHeader3.setActionItems(new ArrayList());
088: for (int i = 0; i < WORKGROUP_IDS.length; i++) {
089: routeHeader3.getActionItems().add(
090: generateActionItem(routeHeader3, "A",
091: AUTHENTICATION_IDS[i], WORKGROUP_IDS[i]));
092: }
093:
094: getRouteHeaderService().saveRouteHeader(routeHeader1);
095: getRouteHeaderService().saveRouteHeader(routeHeader2);
096: getRouteHeaderService().saveRouteHeader(routeHeader3);
097:
098: actionItems.addAll(routeHeader1.getActionItems());
099: actionItems.addAll(routeHeader2.getActionItems());
100: actionItems.addAll(routeHeader3.getActionItems());
101: for (Iterator iterator = actionItems.iterator(); iterator
102: .hasNext();) {
103: ActionItem actionItem = (ActionItem) iterator.next();
104: getActionListService().saveActionItem(actionItem);
105: }
106:
107: }
108:
109: @Test
110: public void testRouteHeaderDelete() throws Exception {
111: setUpOldSchool();
112: Collection actionItems = getActionListService()
113: .findByRouteHeaderId(routeHeader1.getRouteHeaderId());
114: assertEquals("Route header " + routeHeader1.getRouteHeaderId()
115: + " should have action items.",
116: AUTHENTICATION_IDS.length, actionItems.size());
117: getActionListService().deleteByRouteHeaderId(
118: routeHeader1.getRouteHeaderId());
119: actionItems = getActionListService().findByRouteHeaderId(
120: routeHeader1.getRouteHeaderId());
121: assertEquals(
122: "There should be no remaining action items for route header "
123: + routeHeader1.getRouteHeaderId(), 0,
124: actionItems.size());
125: actionItems = getActionListService().findByRouteHeaderId(
126: routeHeader2.getRouteHeaderId());
127: assertEquals("Route header " + routeHeader2.getRouteHeaderId()
128: + " should have action items.",
129: AUTHENTICATION_IDS.length, actionItems.size());
130: }
131:
132: @Test
133: public void testActionListCount() throws Exception {
134: setUpOldSchool();
135: TransactionTemplate transactionTemplate = getTransactionTemplate();
136: transactionTemplate.execute(new TransactionCallback() {
137: public Object doInTransaction(TransactionStatus status) {
138: return TestUtilities.getPersistenceBrokerTemplate()
139: .execute(new PersistenceBrokerCallback() {
140: public Object doInPersistenceBroker(
141: PersistenceBroker pb) {
142: try {
143: Connection conn = pb
144: .serviceConnectionManager()
145: .getConnection();
146: PreparedStatement ps = conn
147: .prepareStatement("select distinct ACTN_ITM_PRSN_EN_ID from en_actn_itm_t");
148: ResultSet rs = ps.executeQuery();
149: int emplIdCnt = 0;
150: int loopCnt = 0;
151: UserService userService = (UserService) KEWServiceLocator
152: .getService(KEWServiceLocator.USER_SERVICE);
153: //do first 5 for time sake
154: while (rs.next() && ++loopCnt < 6) {
155: String workflowId = rs
156: .getString(1);
157: PreparedStatement ps1 = conn
158: .prepareStatement("select count (*) from en_actn_itm_t where ACTN_ITM_PRSN_EN_ID = '"
159: + workflowId
160: + "'");
161: ResultSet rsWorkflowIdCnt = ps1
162: .executeQuery();
163: if (rsWorkflowIdCnt.next()) {
164: emplIdCnt = rsWorkflowIdCnt
165: .getInt(1);
166: } else {
167: throw new Exception(
168: "WorkflowId "
169: + workflowId
170: + " didn't return a count. Test SQL invalid.");
171: }
172: Collection actionList = getActionListService()
173: .findByWorkflowUser(
174: userService
175: .getWorkflowUser(new WorkflowUserId(
176: workflowId)));
177: assertEquals(
178: "ActionItemService returned incorrect number of ActionItems for user "
179: + workflowId
180: + " ActionList",
181: emplIdCnt, actionList
182: .size());
183: ps1.close();
184: rsWorkflowIdCnt.close();
185: }
186: rs.close();
187: ps.close();
188: } catch (Exception e) {
189: throw new RuntimeException(e);
190: }
191: return null;
192: }
193: });
194: }
195: });
196: }
197:
198: /**
199: * Tests that the user's secondary action list works appropriately. Also checks that if a user
200: * is their own secondary delegate, their request shows up in their main action list rather than
201: * their secondary list.
202: */
203: @Test
204: public void testSecondaryActionList() throws Exception {
205: WorkflowDocument document = new WorkflowDocument(
206: new NetworkIdVO("jhopf"), "ActionListDocumentType");
207: document.routeDocument("");
208:
209: // at this point the document should be routed to the following people
210: // 1) approve to bmcgough with primary delegate of rkirkend and secondary delegates of ewestfal and bmcgough (himself)
211: // 2) approve to jitrue with a secondary delegate of jitrue (himself)
212: // 3) acknowledge to user1
213: // 4) approve to NonSIT workgroup (which should include user1)
214:
215: // now lets verify that everyone's action lists look correct
216:
217: WorkflowUser bmcgough = KEWServiceLocator.getUserService()
218: .getWorkflowUser(new AuthenticationUserId("bmcgough"));
219: WorkflowUser rkirkend = KEWServiceLocator.getUserService()
220: .getWorkflowUser(new AuthenticationUserId("rkirkend"));
221: WorkflowUser ewestfal = KEWServiceLocator.getUserService()
222: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
223: WorkflowUser jitrue = KEWServiceLocator.getUserService()
224: .getWorkflowUser(new AuthenticationUserId("jitrue"));
225: WorkflowUser user1 = KEWServiceLocator.getUserService()
226: .getWorkflowUser(new AuthenticationUserId("user1"));
227: Workgroup NonSIT = KEWServiceLocator.getWorkgroupService()
228: .getWorkgroup(new GroupNameId("NonSIT"));
229:
230: ActionListFilter noFilter = new ActionListFilter();
231: ActionListFilter primaryFilter = new ActionListFilter();
232: primaryFilter
233: .setDelegationType(EdenConstants.DELEGATION_SECONDARY);
234: primaryFilter.setExcludeDelegationType(true);
235: ActionListFilter secondaryFilter = new ActionListFilter();
236: secondaryFilter
237: .setDelegationType(EdenConstants.DELEGATION_SECONDARY);
238: Collection actionItems = null;
239: ActionItem actionItem = null;
240:
241: actionItems = getActionListService().getActionList(bmcgough,
242: primaryFilter);
243: assertEquals(
244: "bmcgough should have 0 items in his primary action list.",
245: 0, actionItems.size());
246: actionItems = getActionListService().getActionList(bmcgough,
247: secondaryFilter);
248: assertEquals(
249: "bmcgough should have 1 item in his secondary action list.",
250: 1, actionItems.size());
251: actionItem = (ActionItem) actionItems.iterator().next();
252: assertEquals("Should be an approve request.",
253: EdenConstants.ACTION_REQUEST_APPROVE_REQ, actionItem
254: .getActionRequestCd());
255: assertEquals("Should be a secondary delegation request.",
256: EdenConstants.DELEGATION_SECONDARY, actionItem
257: .getDelegationType());
258: actionItems = getActionListService().getActionList(bmcgough,
259: noFilter);
260: assertEquals(
261: "bmcgough should have 1 item in his entire action list.",
262: 1, actionItems.size());
263:
264: actionItems = getActionListService().getActionList(rkirkend,
265: primaryFilter);
266: assertEquals(
267: "bmcgough should have 1 item in his primary action list.",
268: 1, actionItems.size());
269:
270: actionItems = getActionListService().getActionList(jitrue,
271: primaryFilter);
272: assertEquals(
273: "jitrue should have 1 item in his primary action list.",
274: 1, actionItems.size());
275:
276: actionItems = getActionListService().getActionList(ewestfal,
277: secondaryFilter);
278: assertEquals(
279: "ewestfal should have 1 item in his secondary action list.",
280: 1, actionItems.size());
281:
282: // check that user1's approve comes out as their action item from the action list
283: actionItems = getActionListService().getActionList(user1,
284: noFilter);
285: assertEquals(
286: "user1 should have 1 item in his primary action list.",
287: 1, actionItems.size());
288: actionItem = (ActionItem) actionItems.iterator().next();
289: assertEquals("Should be an approve request.",
290: EdenConstants.ACTION_REQUEST_APPROVE_REQ, actionItem
291: .getActionRequestCd());
292: assertEquals("Should be to a workgroup.", NonSIT
293: .getWorkflowGroupId().getGroupId(), actionItem
294: .getWorkgroupId());
295: // check that user1 acknowledge shows up when filtering
296: ActionListFilter ackFilter = new ActionListFilter();
297: ackFilter
298: .setActionRequestCd(EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
299: actionItems = getActionListService().getActionList(user1,
300: ackFilter);
301: assertEquals(
302: "user1 should have 1 item in his primary action list.",
303: 1, actionItems.size());
304: actionItem = (ActionItem) actionItems.iterator().next();
305: assertEquals("Should be an acknowledge request.",
306: EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ,
307: actionItem.getActionRequestCd());
308: assertNull("Should not be to a workgroup.", actionItem
309: .getWorkgroupId());
310:
311: // all members of NonSIT should have a single primary Approve Request
312: for (Iterator iterator = NonSIT.getUsers().iterator(); iterator
313: .hasNext();) {
314: WorkflowUser user = (WorkflowUser) iterator.next();
315: actionItems = getActionListService().getActionList(user,
316: primaryFilter);
317: assertEquals("Workgroup Member " + user.getDisplayName()
318: + " should have 1 action item.", 1, actionItems
319: .size());
320: actionItem = (ActionItem) actionItems.iterator().next();
321: assertEquals("Should be an approve request.",
322: EdenConstants.ACTION_REQUEST_APPROVE_REQ,
323: actionItem.getActionRequestCd());
324: assertEquals("Should be to a workgroup.", NonSIT
325: .getWorkflowGroupId().getGroupId(), actionItem
326: .getWorkgroupId());
327: }
328: }
329:
330: private DocumentRouteHeaderValue generateDocRouteHeader() {
331: DocumentRouteHeaderValue routeHeader = new DocumentRouteHeaderValue();
332: routeHeader.setAppDocId("Test");
333: routeHeader.setApprovedDate(null);
334: routeHeader.setCreateDate(new Timestamp(new Date().getTime()));
335: routeHeader.setDocContent("test");
336: routeHeader.setDocRouteLevel(new Integer(1));
337: routeHeader
338: .setDocRouteStatus(EdenConstants.ROUTE_HEADER_ENROUTE_CD);
339: routeHeader.setDocTitle("Test");
340: routeHeader.setDocumentTypeId(new Long(1));
341: routeHeader.setDocVersion(new Integer(
342: EdenConstants.CURRENT_DOCUMENT_VERSION));
343: routeHeader.setRouteStatusDate(new Timestamp(new Date()
344: .getTime()));
345: routeHeader
346: .setStatusModDate(new Timestamp(new Date().getTime()));
347: routeHeader.setInitiatorWorkflowId("someone");
348: return routeHeader;
349: }
350:
351: private ActionItem generateActionItem(
352: DocumentRouteHeaderValue routeHeader,
353: String actionRequested, String authenticationId,
354: Long workgroupId) throws EdenUserNotFoundException {
355: ActionItem actionItem = new ActionItem();
356: actionItem.setActionRequestCd(actionRequested);
357: actionItem.setActionRequestId(new Long(1));
358: actionItem.setWorkflowId(KEWServiceLocator.getUserService()
359: .getWorkflowUser(
360: new AuthenticationUserId(authenticationId))
361: .getWorkflowUserId().getWorkflowId());
362: actionItem.setRouteHeaderId(routeHeader.getRouteHeaderId());
363: actionItem.setRouteHeader(routeHeader);
364: actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
365: actionItem.setDocHandlerURL("Unit testing");
366: actionItem.setDocLabel("unit testing");
367: actionItem.setDocTitle(routeHeader.getDocTitle());
368: actionItem.setDocName("docname");
369: actionItem.setWorkgroupId(workgroupId);
370: // actionItem.setResponsibilityId(new Long(-1));
371: return actionItem;
372: }
373:
374: private ActionListService getActionListService() {
375: return (ActionListService) KEWServiceLocator
376: .getActionListService();
377: }
378:
379: private RouteHeaderService getRouteHeaderService() {
380: return KEWServiceLocator.getRouteHeaderService();
381: }
382: }
|