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.routemanager;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.api.KewApiServiceLocator;
20  import org.kuali.rice.kew.api.WorkflowDocument;
21  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
22  import org.kuali.rice.kew.api.action.ActionRequest;
23  import org.kuali.rice.kew.api.action.InvalidActionTakenException;
24  import org.kuali.rice.kew.api.document.DocumentProcessingQueue;
25  import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
26  import org.kuali.rice.kew.messaging.MessageServiceNames;
27  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  import org.kuali.rice.kew.test.TestUtilities;
31  import org.kuali.rice.kim.api.group.Group;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.kuali.rice.ksb.messaging.service.KSBXMLService;
34  import org.kuali.rice.test.BaselineTestCase;
35  
36  import javax.xml.namespace.QName;
37  import java.util.Collection;
38  import java.util.List;
39  
40  import static org.junit.Assert.*;
41  
42  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
43  public class ExceptionRoutingTest extends KEWTestCase {
44  
45      protected void loadTestData() throws Exception {
46          loadXmlFile("RouteManagerConfig.xml");
47      }
48  
49      protected void setUpAfterDataLoad() throws Exception {
50  		super.setUpAfterDataLoad();
51  		// reset these static constants, otherwise they will cause problems between test runs
52  		ExceptionRoutingTestPostProcessor.THROW_DO_ACTION_TAKEN_EXCEPTION = false;
53  		ExceptionRoutingTestPostProcessor.THROW_ROUTE_DELETE_ROUTE_HEADER_EXCEPTION = false;
54  		ExceptionRoutingTestPostProcessor.THROW_ROUTE_STATUS_CHANGE_EXCEPTION = false;
55  		ExceptionRoutingTestPostProcessor.THROW_ROUTE_STATUS_LEVEL_EXCEPTION = false;
56  		ExceptionRoutingTestPostProcessor.TRANSITIONED_OUT_OF_EXCEPTION_ROUTING = false;
57  		ExceptionRoutingTestPostProcessor.BLOW_UP_ON_TRANSITION_INTO_EXCEPTION = false;
58  	}
59  
60      @Test public void testSequentialExceptionRouting() throws Exception {
61          WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "ExceptionRoutingSequentialDoc");
62          try {
63              doc.route("");
64              fail("should have thrown routing exception");
65          } catch (Exception e) {
66          }
67  
68          TestUtilities.getExceptionThreader().join();//this is necessary to ensure that the exception request will be generated.
69  
70          doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), doc.getDocumentId());
71          assertTrue("Document should be in exception status", doc.isException());
72  
73          List<ActionRequest> actionRequests = KewApiServiceLocator.getWorkflowDocumentService().getRootActionRequests(doc.getDocumentId());
74  
75          assertEquals("Should be a single exception request", 1, actionRequests.size());
76          for (ActionRequest actionRequest : actionRequests) {
77              Group group = KimApiServiceLocator.getGroupService().getGroup(actionRequest.getGroupId());
78              assertTrue("Request should be an exception request.", actionRequest.isExceptionRequest());
79              assertTrue("Complete should be requested", actionRequest.isCompleteRequest());
80              assertTrue("Request should be a workgroup request", actionRequest.isGroupRequest());
81              assertEquals("Request should be to 'ExceptionRoutingGroup'", "ExceptionRoutingGroup", group.getName());
82              assertNotNull("annotation cannot be null", actionRequest.getAnnotation());
83              assertFalse("annotation cannot be empty", "".equals(actionRequest.getAnnotation()));
84          }
85  
86      }
87      
88      /**
89       * This tests the solution for KULRICE-4493.  Essentially, the problem was that when the workflow engine
90       * would transition the document to exception status it would invoke the post processor.  If invoking
91       * the post processor raised an exception, that would cause the transaction to get rolled back and the
92       * document would get "stuck" in the ENROUTE state with no pending requests.
93       */
94  //    @Test public void testExceptionRouting_BlowUpOnStatusChangeToException() throws Exception {
95  //    	
96  //    	// first, configure the post processor so that it throws an exception when we call doRouteStatusChange on transition into exception status
97  //    	ExceptionRoutingTestPostProcessor.BLOW_UP_ON_TRANSITION_INTO_EXCEPTION = true;
98  //    	    	
99  //    	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "AlwaysExplodeTestDocument");
100 //    	try {
101 //    		doc.route("");
102 //    		fail("We should be in exception routing");
103 //    	} catch (Exception e) {
104 //    	}
105 //
106 //    	TestUtilities.getExceptionThreader().join();//this is necessary to ensure that the exception request will be generated.
107 //    	
108 //    	doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), doc.getDocumentId());
109 //    	assertTrue("document should be in exception routing", doc.isException());
110 //
111 //    }
112 
113 	@Test public void testInvalidActionsInExceptionRouting() throws Exception {
114         WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "ExceptionRoutingSequentialDoc");
115         try {
116             doc.route("");
117             fail("should have thrown routing exception");
118         } catch (Exception e) {
119             log.info("Expected exception occurred: " + e);
120         }
121 
122         TestUtilities.getExceptionThreader().join();//this is necessary to ensure that the exception request will be generated.
123 
124         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), doc.getDocumentId());
125         assertTrue("Document should be in exception status", doc.isException());
126 
127         try {
128             doc.route("routing a document that is in exception routing");
129             fail("Succeeded in routing document that is in exception routing");
130         } catch (InvalidActionTakenException iate) {
131             log.info("Expected exception occurred: " + iate);
132         }
133     }
134 
135 	@Test public void testParallelExceptionRouting() throws Exception {
136         WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "ExceptionRoutingParallelDoc");
137         doc.route("");
138         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), doc.getDocumentId());
139         assertTrue("User should have an approve request", doc.isApprovalRequested());
140         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), doc.getDocumentId());
141         assertTrue("User should have an approve request", doc.isApprovalRequested());
142         List<RouteNodeInstance> nodes = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(
143                 doc.getDocumentId());
144 
145         // at this point we should be at RouteNode1 and RouteNode3
146         assertEquals("There should be two active nodes", 2, nodes.size());
147         TestUtilities.assertAtNode(doc, "RouteNode1");
148         TestUtilities.assertAtNode(doc, "RouteNode3");
149 
150         try {
151             doc.approve("");
152             fail("should have generated routing exception");
153         } catch (Exception e) {
154         }
155 
156         TestUtilities.getExceptionThreader().join();//this is necessary to ensure that the exception request will be generated.
157         List<ActionRequest> actionRequests = KewApiServiceLocator.getWorkflowDocumentService().getRootActionRequests(doc.getDocumentId());
158         RouteNodeInstance routeNode1 = null;
159         for (RouteNodeInstance nodeInstanceVO : nodes) {
160         	if (nodeInstanceVO.getName().equals("RouteNode1")) {
161         		routeNode1 = nodeInstanceVO;
162         	}
163         }
164         assertNotNull("Could not locate the routeNode1 node instance.", routeNode1);
165 
166         boolean hasCompleteRequest = false;
167         for (ActionRequest actionRequest : actionRequests) {
168             if (actionRequest.isCompleteRequest()) {
169             	Group group = KimApiServiceLocator.getGroupService().getGroup(actionRequest.getGroupId());
170                 assertTrue("Complete should be requested", actionRequest.isCompleteRequest());
171                 assertTrue("Request should be a workgroup request", actionRequest.isGroupRequest());
172                 assertNull("For exception routing, node instance should have a null id.", actionRequest.getRouteNodeInstanceId());
173                 //assertEquals("Node instance id should be id of routeNode1", routeNode1.getRouteNodeInstanceId(), actionRequest.getNodeInstanceId());
174                 assertEquals("Request should be to 'ExceptionRoutingGroup'", "ExceptionRoutingGroup", group.getName());
175                 hasCompleteRequest = true;
176             }
177         }
178         assertTrue("Document should have had a complete request", hasCompleteRequest);
179         ExplodingRuleAttribute.dontExplode=true;
180 
181         //there should be a single action item to our member of the exception workgroup
182         Collection actionItems = KEWServiceLocator.getActionListService().findByDocumentId(doc.getDocumentId());
183         assertEquals("There should only be action items for the member of our exception workgroup", 1, actionItems.size());
184 
185         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user3"), doc.getDocumentId());
186         assertTrue("Document should be routing for completion to member of exception workgroup", doc.isCompletionRequested());
187         assertTrue("Document should be in exception status", doc.isException());
188         doc.complete("");
189 
190         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), doc.getDocumentId());
191         doc.approve("");
192 
193         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), doc.getDocumentId());
194         doc.approve("");
195 
196         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), doc.getDocumentId());
197         doc.approve("");
198 
199         doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), doc.getDocumentId());
200         doc.approve("");
201 
202         assertTrue("Document should be final", doc.isFinal());
203     }
204 
205     /**
206      * this tests that the document appropriately gets to exception routing if there is a
207      * problem when transitioning out of first node
208      *
209      * @throws Exception
210      */
211     @Test public void testExceptionInTransitionFromStart() throws Exception {
212 
213     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "AdhocTransitionTestDocument");
214     	//blow chunks transitioning out of adhoc to the first route node
215     	ExceptionRoutingTestPostProcessor.THROW_ROUTE_STATUS_LEVEL_EXCEPTION = true;
216 
217     	try {
218     		doc.route("");
219     		fail("We should be in exception routing");
220     	} catch (Exception e) {
221     	}
222 
223     	TestUtilities.getExceptionThreader().join();//this is necessary to ensure that the exception request will be generated.
224     	doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), doc.getDocumentId());
225     	assertTrue("document should be in exception routing", doc.isException());
226     }
227 
228     /**
229      * Test to verify the fix for KULWF-669.
230      *
231      * This tests that if we requeue an exception document (through the RouteQueueService) that it doesn't transition
232      * out of exception routing.  Then check that, if we complete it, it properly transitions out of exception routing.
233      */
234     @Test public void testRequeueOfExceptionDocument() throws Exception {
235     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "AdhocTransitionTestDocument");
236     	document.route("");
237         assertFalse("Document should not be in exception routing.", document.isException());
238 
239         // in fact, at this point it should be routed to jhopf
240         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
241         assertTrue("Jhopf should have an approve.", document.isApprovalRequested());
242 
243         // let's tell it to blow up on level change
244         ExceptionRoutingTestPostProcessor.THROW_ROUTE_STATUS_CHANGE_EXCEPTION = true;
245         try {
246         	document.approve("");
247         	fail("We should be in exception routing");
248     	} catch (Exception e) {
249     	}
250 
251     	TestUtilities.waitForExceptionRouting();
252     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
253     	assertTrue("document should be in exception routing", document.isException());
254 
255     	// now requeue the document it should stay at exception routing and the status change callback should not
256     	// indicate a transition out of exception routing (this is to make sure it's not going out of exception
257     	// routing and then right back in)
258     	ExceptionRoutingTestPostProcessor.THROW_ROUTE_STATUS_CHANGE_EXCEPTION = false;
259     	assertFalse("Should not have transitioned out of exception routing yet.", ExceptionRoutingTestPostProcessor.TRANSITIONED_OUT_OF_EXCEPTION_ROUTING);
260     	// the requeue here should happen synchronously because we are using the SynchronousRouteQueue
261     	DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
262         DocumentProcessingQueue documentProcessingQueue = MessageServiceNames.getDocumentProcessingQueue(routeHeaderValue);
263     	documentProcessingQueue.process(String.valueOf(document.getDocumentId()));
264 
265     	// the document should still be in exception routing
266     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
267     	assertTrue("document should be in exception routing", document.isException());
268         assertFalse("document shouldn't have transitioned out of exception routing.", ExceptionRoutingTestPostProcessor.TRANSITIONED_OUT_OF_EXCEPTION_ROUTING);
269 
270         // now turn status change exceptions off and complete the exception request
271         ExceptionRoutingTestPostProcessor.THROW_ROUTE_STATUS_CHANGE_EXCEPTION = false;
272         assertTrue("rkirkend should be in the exception workgroup.", document.isCompletionRequested());
273         document.complete("Completing out of exception routing.");
274 
275         // Note: The behavior here will be a bit different then in a real setting because in these tests the route queue is synchronous so jhopf's original
276         // Approve never actually took place because the transaction was rolled back (because of the exception in the post process).  Therefore, we still
277         // need to take action as him again to push the document to FINAL
278         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
279         assertTrue(document.isApprovalRequested());
280         document.approve("");
281 
282         // document should now be FINAL
283         assertTrue("Document should be FINAL.", document.isFinal());
284 
285         // the status change out of exception routing should have happened
286         assertTrue("Document should have transitioned out of exception routing.", ExceptionRoutingTestPostProcessor.TRANSITIONED_OUT_OF_EXCEPTION_ROUTING);
287     }
288 
289 }