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