View Javadoc

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