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.routetemplate;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.junit.Test;
024: import org.kuali.workflow.role.RoleService;
025: import org.kuali.workflow.test.WorkflowTestCase;
026:
027: import edu.iu.uis.eden.EdenConstants;
028: import edu.iu.uis.eden.KEWServiceLocator;
029: import edu.iu.uis.eden.actionrequests.ActionRequestValue;
030: import edu.iu.uis.eden.clientapp.WorkflowDocument;
031: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
032: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
033: import edu.iu.uis.eden.routemodule.TestDocContent;
034: import edu.iu.uis.eden.routemodule.TestRecipient;
035: import edu.iu.uis.eden.routemodule.TestResponsibility;
036: import edu.iu.uis.eden.routemodule.TestRouteLevel;
037: import edu.iu.uis.eden.routemodule.TestRouteModuleXMLHelper;
038: import edu.iu.uis.eden.user.AuthenticationUserId;
039: import edu.iu.uis.eden.user.UserId;
040: import edu.iu.uis.eden.user.WorkflowUser;
041:
042: /**
043: * Tests the role re-resolving. This test depends on the route queue being synchronous.
044: */
045: public class RoleServiceTest extends WorkflowTestCase {
046:
047: private static final String TEST_ROLE = "TestRole";
048: private static final String TEST_GROUP_1 = "TestGroup1";
049: private static final String TEST_GROUP_2 = "TestGroup2";
050: private RoleService roleService;
051: private Long documentId;
052: private List group1 = new ArrayList();
053: private List group2 = new ArrayList();
054:
055: protected void setUpTransaction() throws Exception {
056: super .setUpTransaction();
057: roleService = KEWServiceLocator.getRoleService();
058: initializeAttribute();
059: documentId = routeDocument();
060: }
061:
062: private void initializeAttribute() throws Exception {
063: group1.add(new AuthenticationUserId("jhopf"));
064: group1.add(new AuthenticationUserId("pmckown"));
065: group2.add(new AuthenticationUserId("xqi"));
066: group2.add(new AuthenticationUserId("tbazler"));
067: TestRuleAttribute.addRole(TEST_ROLE);
068: TestRuleAttribute.addQualifiedRole(TEST_ROLE, TEST_GROUP_1);
069: TestRuleAttribute.addQualifiedRole(TEST_ROLE, TEST_GROUP_2);
070: TestRuleAttribute
071: .setRecipients(TEST_ROLE, TEST_GROUP_1, group1);
072: TestRuleAttribute
073: .setRecipients(TEST_ROLE, TEST_GROUP_2, group2);
074: }
075:
076: private Long routeDocument() throws Exception {
077: WorkflowDocument document = new WorkflowDocument(
078: new NetworkIdVO("rkirkend"), "TestDocumentType");
079: document.setApplicationContent(TestRouteModuleXMLHelper
080: .toXML(generateDocContent()));
081: document.routeDocument("testing only");
082: return document.getRouteHeaderId();
083: }
084:
085: @Test
086: public void testReResolveQualifiedRole() throws Exception {
087: DocumentRouteHeaderValue loadedDocument = KEWServiceLocator
088: .getRouteHeaderService().getRouteHeader(documentId);
089: assertEquals(EdenConstants.ROUTE_HEADER_ENROUTE_CD,
090: loadedDocument.getDocRouteStatus());
091: List requests = getTestRoleRequests(loadedDocument);
092: assertEquals("Incorrect number of role control requests.", 2,
093: requests.size());
094: assertRequestGraphs(requests);
095:
096: // change the membership in TEST_GROUP_1
097: List newGroup1Recipients = new ArrayList();
098: newGroup1Recipients.add(new AuthenticationUserId("bmcgough"));
099: newGroup1Recipients.add(new AuthenticationUserId("xqi"));
100: newGroup1Recipients.add(new AuthenticationUserId("rkirkend"));
101: TestRuleAttribute.setRecipients(TEST_ROLE, TEST_GROUP_1,
102: newGroup1Recipients);
103: roleService.reResolveQualifiedRole(loadedDocument, TEST_ROLE,
104: TEST_GROUP_1);
105: loadedDocument = KEWServiceLocator.getRouteHeaderService()
106: .getRouteHeader(documentId);
107: assertEquals(EdenConstants.ROUTE_HEADER_ENROUTE_CD,
108: loadedDocument.getDocRouteStatus());
109: requests = getTestRoleRequests(loadedDocument);
110: // rkirkend is the initiator so his action should count for the TEST_GROUP_1 role after re-resolving, leaving only a single role request
111: assertEquals("Incorrect number of role control requests.", 1,
112: requests.size());
113: assertRequestGraphs(requests);
114: assertInitiatorRequestDone(TEST_ROLE, TEST_GROUP_1);
115:
116: // if we attempt to re-resolve with an non-existant qualified role, it _should_ be legal
117: roleService.reResolveQualifiedRole(loadedDocument, TEST_ROLE,
118: "random cool name");
119: requests = getTestRoleRequests(loadedDocument);
120: assertEquals("Incorrect number of role control requests.", 1,
121: requests.size());
122: assertRequestGraphs(requests);
123: }
124:
125: @Test
126: public void testReResolveQualifiedRoleErrors() throws Exception {
127: // attempting to re-resolve with null values should throw exceptions
128: try {
129: roleService.reResolveQualifiedRole(
130: (DocumentRouteHeaderValue) null, null, null);
131: fail("Exception should have been thrown when null values are passed.");
132: } catch (Exception e) {
133: }
134:
135: DocumentRouteHeaderValue loadedDocument = KEWServiceLocator
136: .getRouteHeaderService().getRouteHeader(documentId);
137: try {
138: roleService.reResolveQualifiedRole(loadedDocument, null,
139: null);
140: fail("Exception should have been thrown when null values are passed.");
141: } catch (Exception e) {
142: }
143:
144: // need to have a valid role name
145: try {
146: roleService.reResolveQualifiedRole(loadedDocument,
147: "GimpyRoleName", TEST_GROUP_1);
148: fail("Exception should be thrown when attempting to re-resolve with a bad role name.");
149: } catch (Exception e) {
150: }
151:
152: // now blanket approve a document to make it processed
153: WorkflowDocument document = new WorkflowDocument(
154: new NetworkIdVO("rkirkend"), "TestDocumentType");
155: document.setApplicationContent(TestRouteModuleXMLHelper
156: .toXML(generateDocContent()));
157: document.blanketApprove("");
158: DocumentRouteHeaderValue baDoc = KEWServiceLocator
159: .getRouteHeaderService().getRouteHeader(
160: document.getRouteHeaderId());
161: try {
162: roleService.reResolveQualifiedRole(baDoc, TEST_ROLE,
163: TEST_GROUP_1);
164: fail("Shouldn't be able to resolve on a document with no active nodes.");
165: } catch (Exception e) {
166: }
167:
168: }
169:
170: @Test
171: public void testReResolveRole() throws Exception {
172: DocumentRouteHeaderValue loadedDocument = KEWServiceLocator
173: .getRouteHeaderService().getRouteHeader(documentId);
174: assertEquals(EdenConstants.ROUTE_HEADER_ENROUTE_CD,
175: loadedDocument.getDocRouteStatus());
176: List requests = getTestRoleRequests(loadedDocument);
177: assertEquals("Incorrect number of role control requests.", 2,
178: requests.size());
179: assertRequestGraphs(requests);
180:
181: // change membership in TEST_GROUP_1 and TEST_GROUP_2
182: List newGroup1 = new ArrayList();
183: List newGroup2 = new ArrayList();
184: newGroup2.add(new AuthenticationUserId("ewestfal"));
185: newGroup2.add(new AuthenticationUserId("jthomas"));
186: // TEST_GROUP_1 should now be an empty role, therefore there should not be a request generated for it after re-resolution
187: TestRuleAttribute.setRecipients(TEST_ROLE, TEST_GROUP_1,
188: newGroup1);
189: TestRuleAttribute.setRecipients(TEST_ROLE, TEST_GROUP_2,
190: newGroup2);
191: // re-resolve entire role
192: roleService.reResolveRole(loadedDocument, TEST_ROLE);
193: loadedDocument = KEWServiceLocator.getRouteHeaderService()
194: .getRouteHeader(documentId);
195: assertEquals(EdenConstants.ROUTE_HEADER_ENROUTE_CD,
196: loadedDocument.getDocRouteStatus());
197: requests = getTestRoleRequests(loadedDocument);
198: // should be 1 because group 1 has no members
199: assertEquals("Incorrect number of role control requests.", 1,
200: requests.size());
201: assertRequestGraphs(requests);
202: }
203:
204: @Test
205: public void testReResolveRoleErrors() throws Exception {
206: // attempting to re-resolve with null values should throw exceptions
207: try {
208: roleService.reResolveRole((DocumentRouteHeaderValue) null,
209: null);
210: fail("Exception should have been thrown when null values are passed.");
211: } catch (Exception e) {
212: }
213:
214: DocumentRouteHeaderValue loadedDocument = KEWServiceLocator
215: .getRouteHeaderService().getRouteHeader(documentId);
216: try {
217: roleService.reResolveRole(loadedDocument, null);
218: fail("Exception should have been thrown when null values are passed.");
219: } catch (Exception e) {
220: }
221:
222: // need to have a valid role name
223: try {
224: roleService.reResolveRole(loadedDocument, "GimpyRoleName");
225: fail("Exception should be thrown when attempting to re-resolve with a bad role name.");
226: } catch (Exception e) {
227: }
228:
229: // now blanket approve a document to make it processed
230: WorkflowDocument document = new WorkflowDocument(
231: new NetworkIdVO("rkirkend"), "TestDocumentType");
232: document.setApplicationContent(TestRouteModuleXMLHelper
233: .toXML(generateDocContent()));
234: document.blanketApprove("");
235: DocumentRouteHeaderValue baDoc = KEWServiceLocator
236: .getRouteHeaderService().getRouteHeader(
237: document.getRouteHeaderId());
238: try {
239: roleService.reResolveRole(baDoc, TEST_ROLE);
240: fail("Shouldn't be able to re-resolve on a document with no active nodes.");
241: } catch (Exception e) {
242: }
243: }
244:
245: /**
246: * Extract requests sent to TestRole.
247: */
248: private List getTestRoleRequests(DocumentRouteHeaderValue document) {
249: List testRoleRequests = new ArrayList();
250: List requests = KEWServiceLocator.getActionRequestService()
251: .findPendingRootRequestsByDocIdAtRouteLevel(
252: document.getRouteHeaderId(),
253: document.getDocRouteLevel());
254: for (Iterator iterator = requests.iterator(); iterator
255: .hasNext();) {
256: ActionRequestValue actionRequest = (ActionRequestValue) iterator
257: .next();
258: if (TEST_ROLE.equals(actionRequest.getRoleName())) {
259: testRoleRequests.add(actionRequest);
260: }
261: }
262: return testRoleRequests;
263: }
264:
265: private void assertRequestGraphs(List requests) throws Exception {
266: for (Iterator iterator = requests.iterator(); iterator
267: .hasNext();) {
268: ActionRequestValue request = (ActionRequestValue) iterator
269: .next();
270: if (TEST_GROUP_1.equals(request.getQualifiedRoleName())) {
271: assertQualifiedRoleRequest(request, TEST_ROLE,
272: TEST_GROUP_1);
273: } else if (TEST_GROUP_2.equals(request
274: .getQualifiedRoleName())) {
275: assertQualifiedRoleRequest(request, TEST_ROLE,
276: TEST_GROUP_2);
277: }
278: }
279: }
280:
281: private void assertQualifiedRoleRequest(ActionRequestValue request,
282: String roleName, String qualifiedRoleName) throws Exception {
283: assertActionRequest(request, roleName, qualifiedRoleName);
284: List recipients = TestRuleAttribute.getRecipients(roleName,
285: qualifiedRoleName);
286: assertEquals("Incorrect number of children requests.",
287: recipients.size(), request.getChildrenRequests().size());
288: for (Iterator childIt = request.getChildrenRequests()
289: .iterator(); childIt.hasNext();) {
290: ActionRequestValue childRequest = (ActionRequestValue) childIt
291: .next();
292: assertActionRequest(childRequest, roleName,
293: qualifiedRoleName);
294: assertTrue("Child request to invalid user: "
295: + childRequest.getWorkflowUser()
296: .getAuthenticationUserId()
297: .getAuthenticationId(), containsUser(
298: recipients, childRequest.getWorkflowUser()));
299: assertEquals("Child request should have no children.", 0,
300: childRequest.getChildrenRequests().size());
301: }
302: }
303:
304: private void assertActionRequest(ActionRequestValue request,
305: String roleName, String qualifiedRoleName) {
306: assertEquals("Incorrect role name.", roleName, request
307: .getRoleName());
308: assertEquals("Incorrect qualified role name.",
309: qualifiedRoleName, request.getQualifiedRoleName());
310: assertEquals("Incorrect qualified role name label.",
311: qualifiedRoleName, request.getQualifiedRoleNameLabel());
312: assertTrue("Request should be activated or done.",
313: EdenConstants.ACTION_REQUEST_ACTIVATED.equals(request
314: .getStatus())
315: || EdenConstants.ACTION_REQUEST_DONE_STATE
316: .equals(request.getStatus()));
317: }
318:
319: private boolean containsUser(List users, WorkflowUser user)
320: throws Exception {
321: for (Iterator iterator = users.iterator(); iterator.hasNext();) {
322: UserId userId = (UserId) iterator.next();
323: WorkflowUser itUser = KEWServiceLocator.getUserService()
324: .getWorkflowUser(userId);
325: if (itUser.getWorkflowId().equals(user.getWorkflowId())) {
326: return true;
327: }
328: }
329: return false;
330: }
331:
332: /**
333: * Gets all "DONE" action requests that are to the initiator (rkirkend). It then verifies that the initiator has a
334: * complete request and a re-resolved request.
335: */
336: private void assertInitiatorRequestDone(String roleName,
337: String qualifiedRoleNameLabel) throws Exception {
338: WorkflowUser initiator = KEWServiceLocator.getUserService()
339: .getWorkflowUser(new AuthenticationUserId("rkirkend"));
340: List requests = KEWServiceLocator.getActionRequestService()
341: .findByStatusAndDocId(
342: EdenConstants.ACTION_REQUEST_DONE_STATE,
343: documentId);
344: for (Iterator iterator = requests.iterator(); iterator
345: .hasNext();) {
346: ActionRequestValue request = (ActionRequestValue) iterator
347: .next();
348: if (!initiator.getWorkflowId().equals(
349: request.getWorkflowId())) {
350: iterator.remove();
351: }
352: }
353: assertEquals(
354: "Initiator should have a complete request and their re-resolved request.",
355: 2, requests.size());
356: int roleRequestCount = 0;
357: for (Iterator iterator = requests.iterator(); iterator
358: .hasNext();) {
359: ActionRequestValue actionRequest = (ActionRequestValue) iterator
360: .next();
361: if (TEST_ROLE.equals(actionRequest.getRoleName())) {
362: roleRequestCount++;
363: assertActionRequest(actionRequest, roleName,
364: qualifiedRoleNameLabel);
365: assertTrue("Initiator request should have a parent.",
366: actionRequest.getParentActionRequest() != null);
367: }
368: }
369: assertEquals(
370: "There should be 1 DONE request from the result of the role re-resolve.",
371: 1, roleRequestCount);
372: }
373:
374: private TestDocContent generateDocContent() {
375: TestDocContent docContent = new TestDocContent();
376: List routeLevels = new ArrayList();
377: TestRouteLevel routeLevel1 = new TestRouteLevel();
378: routeLevels.add(routeLevel1);
379: docContent.setRouteLevels(routeLevels);
380: routeLevel1.setPriority(1);
381: List responsibilities = new ArrayList();
382: routeLevel1.setResponsibilities(responsibilities);
383: TestResponsibility responsibility1 = new TestResponsibility();
384: responsibility1
385: .setActionRequested(EdenConstants.ACTION_REQUEST_APPROVE_REQ);
386: responsibility1.setPriority(1);
387: TestRecipient recipient1 = new TestRecipient();
388: recipient1.setId("rkirkend");
389: recipient1
390: .setType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
391: responsibility1.setRecipient(recipient1);
392: responsibilities.add(responsibility1);
393: return docContent;
394: }
395:
396: }
|