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