View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.actionrequest;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.junit.Test;
23  import org.kuali.rice.kew.service.KEWServiceLocator;
24  import org.kuali.rice.kew.service.WorkflowDocument;
25  import org.kuali.rice.kew.test.KEWTestCase;
26  import org.kuali.rice.kew.test.TestUtilities;
27  import org.kuali.rice.kew.util.KEWConstants;
28  import org.kuali.rice.kim.service.KIMServiceLocator;
29  
30  
31  /**
32   * This test exercises various Action Request graph scenarios and tests them for correctness.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  /**
37   * This is a description of what this class does - jjhanso don't forget to fill this in. 
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class ActionRequestScenariosTest extends KEWTestCase {
43  
44  	protected void loadTestData() throws Exception {
45  		loadXmlFile("ActionRequestsConfig.xml");
46  	}
47  
48      /**
49       * Tests InlineRequestsRouteModule routing.
50       *
51       * @throws Exception
52       */
53      @Test public void testInlineRequestsRouteModule() throws Exception {
54  
55          WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("arh14"), "InlineRequestsDocumentType");
56          document.setApplicationContent("<blah><step>step1</step></blah>");
57          document.routeDocument("");
58  
59          TestUtilities.assertAtNode(document, "step1");
60          List requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
61          assertEquals("Should be 1 request.", 1, requests.size());
62          ActionRequestValue user1Request = (ActionRequestValue) requests.get(0);
63          assertEquals(getPrincipalIdForName("user1"), user1Request.getPrincipalId());
64  
65          // open doc as user1 and route it
66          document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), document.getRouteHeaderId());
67          document.setApplicationContent("<blah><step>step2</step></blah>");
68          document.approve("");
69  
70          TestUtilities.assertAtNode(document, "step2");
71          requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
72          assertEquals("Should be 1 request.", 1, requests.size());
73          ActionRequestValue workgroupRequest = (ActionRequestValue) requests.get(0);
74          assertEquals(getGroupIdFromGroupName("KR-WKFLW", "TestWorkgroup"), workgroupRequest.getGroupId());
75  
76          // open doc as user in TestWorkgroup and route it
77          document = new WorkflowDocument(getPrincipalIdFromPrincipalName("temay"), document.getRouteHeaderId());
78          document.setApplicationContent("<blah><step>step3</step></blah>");
79          document.approve("");
80  
81          TestUtilities.assertAtNode(document, "step3");
82          requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
83          assertEquals("Should be 1 request.", 1, requests.size());
84          ActionRequestValue initiatorRequest = (ActionRequestValue) requests.get(0);
85          assertEquals("INITIATOR", initiatorRequest.getRoleName());
86  
87          //assertEquals(document.getRouteHeader().getInitiator().getDisplayName(), initiatorRequest.getRecipient().getDisplayName());
88  
89          assertFalse("Document should not be FINAL", document.stateIsFinal());
90  
91          // open doc as initiator and route it
92          document = new WorkflowDocument(getPrincipalIdFromPrincipalName("arh14"), document.getRouteHeaderId());
93          document.approve("");
94  
95          assertTrue("Document should be FINAL", document.stateIsFinal());
96      }
97  
98      @Test public void testInlineRequestsRouteModule_UsingAttributes() throws Exception {
99          /*WorkflowDocument document = new WorkflowDocument(new NetworkIdVO("arh14"), "InlineRequestsDocumentType_UsingAttributes");
100         try {
101             document.routeDocument("");
102             fail("Bad route succeeded");
103         } catch (WorkflowException we) {
104             // should throw exception as no approvals were generated
105         }*/
106 
107         WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("arh14"), "InlineRequestsDocumentType_UsingAttributes");
108         document.setApplicationContent("<blah><step>step1</step></blah>");
109         document.routeDocument("");
110 
111         TestUtilities.assertAtNode(document, "step1");
112         List requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
113         assertEquals("Should be 1 request.", 1, requests.size());
114         ActionRequestValue user1Request = (ActionRequestValue) requests.get(0);
115         assertEquals(getPrincipalIdForName("user1"), user1Request.getPrincipalId());
116 
117         // open doc as user1 and route it
118         document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), document.getRouteHeaderId());
119         document.setApplicationContent("<blah><step>step2</step></blah>");
120         document.approve("");
121 
122         TestUtilities.assertAtNode(document, "step2");
123         requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
124         assertEquals("Should be 1 request.", 1, requests.size());
125         ActionRequestValue workgroupRequest = (ActionRequestValue) requests.get(0);
126         assertEquals(getGroupIdFromGroupName("KR-WKFLW", "TestWorkgroup"), workgroupRequest.getGroupId());
127 
128         // open doc as user in TestWorkgroup and route it
129         document = new WorkflowDocument(getPrincipalIdFromPrincipalName("temay"), document.getRouteHeaderId());
130         document.setApplicationContent("<blah><step>step3</step></blah>");
131         document.approve("");
132 
133         TestUtilities.assertAtNode(document, "step3");
134         requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
135         assertEquals("Should be 1 request.", 1, requests.size());
136         ActionRequestValue initiatorRequest = (ActionRequestValue) requests.get(0);
137         assertEquals("INITIATOR", initiatorRequest.getRoleName());
138         //assertEquals(getPrincipalIdForName("INITIATOR"), initiatorRequest.getPrincipalId());
139         //assertEquals(document.getRouteHeader().getInitiator().getDisplayName(), initiatorRequest.getRecipient().getDisplayName());
140 
141         assertFalse("Document should not be FINAL", document.stateIsFinal());
142 
143         // open doc as initiator and route it
144         document = new WorkflowDocument(getPrincipalIdFromPrincipalName("arh14"), document.getRouteHeaderId());
145         document.approve("");
146 
147         assertTrue("Document should be FINAL", document.stateIsFinal());
148     }
149 
150     /**
151 	 * Test that force action works properly in the face of delegations.
152 	 * Tests the resolution of KULWF-642.
153 	 *
154 	 * @throws Exception
155 	 */
156 	@Test public void testForceActionWithDelegation() throws Exception {
157 		// at first, we'll route the document so that the bug is not exposed and verify the action request graph
158 		WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), "testForceActionWithDelegation");
159 		document.routeDocument("");
160 		TestUtilities.assertAtNode(document, "Node1");
161 		List rootRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocId(document.getRouteHeaderId());
162 		assertEquals("Should be 1 root request.", 1, rootRequests.size());
163 		ActionRequestValue ewestfalRequest = (ActionRequestValue)rootRequests.get(0);
164 		assertTrue("Request to ewestfal should be force action of true", ewestfalRequest.getForceAction());
165 		assertEquals("Should have 1 child request.", 1, ewestfalRequest.getChildrenRequests().size());
166 		ActionRequestValue rkirkendRequest = (ActionRequestValue)ewestfalRequest.getChildrenRequests().get(0);
167 		assertFalse("Request to rkirkend should be force action of false", rkirkendRequest.getForceAction());
168 
169 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), "testForceActionWithDelegation");
170 
171 		// After we route the document it should be at the first node in the document where "ewestfal"
172 		// is the primary approver with force action = true and "rkirkend" is the primary
173 		// delegate with force action = false.  In the KULWF-642 bug, the document would have
174 		// progressed past the first node in an auto-approve scenario even though ewestfal's rule
175 		// is force action = true;
176 		document.routeDocument("");
177 
178 		// we should be at the first node in the document
179 		TestUtilities.assertAtNode(document, "Node1");
180 
181 		document.approve("");
182 		assertTrue("Document should be FINAL", document.stateIsFinal());
183 
184 
185 	}
186 
187 	/**
188 	 * Test that Role to Role Delegation works properly.
189 	 * Implemented to expose the bug and test the fix for KULWF-655.
190      * @throws Exception
191      */
192 	@Test public void testRoleToRoleDelegation() throws Exception {
193 		WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), "testRoleToRoleDelegation");
194 		document.routeDocument("");
195 
196 		// after routing the document we should have an approve request to ewestfal, this request should have
197 		// one primary delegate and three secondary delegates
198 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), document.getRouteHeaderId());
199 		assertTrue("ewestfal should have an approve request.", document.isApprovalRequested());
200 		// now check all of ewestfal's delegates
201 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
202 		assertTrue("Should have an approve request.", document.isApprovalRequested());
203 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("xqi"), document.getRouteHeaderId());
204 		assertTrue("Should have an approve request.", document.isApprovalRequested());
205 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jitrue"), document.getRouteHeaderId());
206 		assertTrue("Should have an approve request.", document.isApprovalRequested());
207 
208 		// now approve as the primary delegator, this is where we were seeing the problem in KULWF-655, the
209 		// action request graph was not getting properly deactivated and it was not getting associated with the
210 		// "ActionTaken" properly
211 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
212 		document.approve("Approving as primary delegate.");
213 
214 		// after the primary delegate approves, verify that the entire action request graph was
215 		// deactivated in grand fashion
216 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), document.getRouteHeaderId());
217 		assertFalse("the primary approver should no longer have an approve request.", document.isApprovalRequested());
218 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
219 		assertFalse("Should not have an approve request.", document.isApprovalRequested());
220 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("xqi"), document.getRouteHeaderId());
221 		assertFalse("Should not have an approve request.", document.isApprovalRequested());
222 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jitrue"), document.getRouteHeaderId());
223 		assertFalse("Should not have an approve request.", document.isApprovalRequested());
224 
225 		List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
226 		assertEquals("Wrong number of action requests.", 7, actionRequests.size());
227 		for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
228 			ActionRequestValue request = (ActionRequestValue) iterator.next();
229 			assertTrue("Request should be deactivated.", request.isDeactivated());
230 			if (request.isRoleRequest()) {
231 				assertEquals("Should be all approve request", KEWConstants.APPROVE_POLICY_ALL_APPROVE, request.getApprovePolicy());
232 			} else {
233 				assertEquals("Should not have first approve policy set", KEWConstants.APPROVE_POLICY_FIRST_APPROVE, request.getApprovePolicy());
234 			}
235 		}
236 
237 	}
238 
239 	//testMixedbagRoleToRoleDelegation
240 
241 	@Test public void testRoleToRoleMixedApprovePoliciesDelegation() throws Exception {
242 		WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), "testMixedbagRoleToRoleDelegation");
243 		document.routeDocument("");
244 
245 		// after routing the document we should have an approve request to ewestfal, this request should have
246 		// one primary delegate and three secondary delegates
247 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), document.getRouteHeaderId());
248 		assertTrue("ewestfal should have an approve request.", document.isApprovalRequested());
249 		// now check all of ewestfal's delegates
250 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
251 		assertTrue("Should have an approve request.", document.isApprovalRequested());
252 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("xqi"), document.getRouteHeaderId());
253 		assertTrue("Should have an approve request.", document.isApprovalRequested());
254 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jitrue"), document.getRouteHeaderId());
255 		assertTrue("Should have an approve request.", document.isApprovalRequested());
256 
257 		// now approve as the primary delegator, this is where we were seeing the problem in KULWF-655, the
258 		// action request graph was not getting properly deactivated and it was not getting associated with the
259 		// "ActionTaken" properly
260 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
261 		document.approve("Approving as primary delegate.");
262 
263 		// after the primary delegate approves, verify that the entire action request graph was
264 		// deactivated in grand fashion
265 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), document.getRouteHeaderId());
266 		assertFalse("the primary approver should no longer have an approve request.", document.isApprovalRequested());
267 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
268 		assertFalse("Should not have an approve request.", document.isApprovalRequested());
269 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("xqi"), document.getRouteHeaderId());
270 		assertFalse("Should not have an approve request.", document.isApprovalRequested());
271 		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jitrue"), document.getRouteHeaderId());
272 		assertFalse("Should not have an approve request.", document.isApprovalRequested());
273 
274 		List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
275 		assertEquals("Wrong number of action requests.", 7, actionRequests.size());
276 		for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
277 			ActionRequestValue request = (ActionRequestValue) iterator.next();
278 			assertTrue("Request should be deactivated.", request.isDeactivated());
279 			if (request.isRoleRequest() && request.getRoleName().equals(RoleToRoleDelegationRole.MAIN_ROLE)) {
280 				assertEquals("Should be all approve request", KEWConstants.APPROVE_POLICY_ALL_APPROVE, request.getApprovePolicy());
281 			} else if (request.isRoleRequest() && request.getRoleName().equals(RoleToRoleDelegationRole.PRIMARY_DELEGATE_ROLE)) {
282 				assertEquals("Should be first approve request", KEWConstants.APPROVE_POLICY_FIRST_APPROVE, request.getApprovePolicy());
283 			} else if (request.isRoleRequest() && request.getRoleName().equals(RoleToRoleDelegationRole.SECONDARY_DELEGATE_ROLE)) {
284 				assertEquals("Should be first approve request", KEWConstants.APPROVE_POLICY_FIRST_APPROVE, request.getApprovePolicy());
285 			} else if (request.isRoleRequest()) {
286 				fail("the roles have been messed up");
287 			} else {
288 				assertEquals("Should not have first approve policy set", KEWConstants.APPROVE_POLICY_FIRST_APPROVE, request.getApprovePolicy());
289 			}
290 		}
291 
292 	}
293 
294 	// see: https://test.kuali.org/jira/browse/KULRICE-2001
295 	@Test public void testUnresolvableRoleAttributeRecipients() throws Exception {
296         WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), "UnresolvableRoleRecipsDocType");
297         try {
298         	document.routeDocument("");
299         } catch (Exception e) {
300             // this doc has a rule with a role that produces an invalid recipient id
301             // should receive an error when it attempts to route to the invalid recipient and trigger exception routing on the document
302         	TestUtilities.getExceptionThreader().join();
303         	document = new WorkflowDocument(getPrincipalIdForName("user1"), document.getRouteHeaderId());
304             assertTrue("Document should be in exception routing", document.stateIsException());
305         }
306 	}
307 	
308 	/*
309 	 * The test was created to test Groups with with the All approve policy
310 	 * This is commented out because that is currently not supported in rice.
311 	 */
312 //	@Test public void testGroupRecipientsWithAllApprovePolicy() throws Exception {
313 //        WorkflowDocument document = new WorkflowDocument(getPrincipalIdFromPrincipalName("user1"), "testGroupAllApprovePolicy");
314 //        document.routeDocument("");
315 //        
316 //        assertTrue("Should have approval policy of All", document.getActionRequests()[0].getApprovePolicy().equals(KEWConstants.APPROVE_POLICY_ALL_APPROVE));
317 //        
318 //    	document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), document.getRouteHeaderId());
319 //		assertTrue("ewestfal should have an approve request.", document.isApprovalRequested());
320 //		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
321 //		assertTrue("Should have an approve request.", document.isApprovalRequested());
322 //		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("xqi"), document.getRouteHeaderId());
323 //		assertTrue("Should have an approve request.", document.isApprovalRequested());
324 //		document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jitrue"), document.getRouteHeaderId());
325 //		assertTrue("Should have an approve request.", document.isApprovalRequested());
326 		
327 		//approve document as jitrue
328 		//document.approve("Approving as primary jitrue.");
329 		
330 		//make sure other group members still have approve requests.
331 		//document = new WorkflowDocument(getPrincipalIdFromPrincipalName("ewestfal"), document.getRouteHeaderId());
332 		//assertTrue("ewestfal should have an approve request.", document.isApprovalRequested());
333 		//document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jhopf"), document.getRouteHeaderId());
334 		//assertTrue("Should have an approve request.", document.isApprovalRequested());
335 		//document = new WorkflowDocument(getPrincipalIdFromPrincipalName("xqi"), document.getRouteHeaderId());
336 		//assertTrue("Should have an approve request.", document.isApprovalRequested());
337 		
338 		//document = new WorkflowDocument(getPrincipalIdFromPrincipalName("jitrue"), document.getRouteHeaderId());
339 		//assertTrue("Should NOT have an approve request.", document.isApprovalRequested());
340 //	}
341 
342 
343 	private String getPrincipalIdFromPrincipalName(String principalName) {
344 	    return KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName).getPrincipalId();
345 	}
346 
347     private String getGroupIdFromGroupName(String namespace, String groupName) {
348         return KIMServiceLocator.getIdentityManagementService().getGroupByName(namespace, groupName).getGroupId();
349     }
350 
351     private String getRoleIdFromRoleName(String namespaceCode, String roleName) {
352         return KIMServiceLocator.getRoleService().getRoleIdByName(namespaceCode, roleName);
353     }
354 }