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.messaging.exceptionhandling;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  import org.junit.Test;
23  import org.kuali.rice.kew.api.WorkflowDocument;
24  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
25  import org.kuali.rice.kew.api.document.DocumentStatus;
26  import org.kuali.rice.kew.test.KEWTestCase;
27  import org.kuali.rice.kew.test.TestUtilities;
28  import org.kuali.rice.test.BaselineTestCase;
29  
30  /**
31   * This is a unit test for testing the functionality of the ExceptionRoutingService. 
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
36  public class ExceptionRoutingServiceTest extends KEWTestCase {
37  
38  	/**
39  	 * Checks to make sure that the KIM routing is working.
40  	 * Based upon the test method org.kuali.rice.kew.doctype.DocumentTypeTest.testFinalApproverRouting()
41  	 */
42  	@Test public void testKimExceptionRouting() throws Exception {
43  		loadXmlFile("RouteExceptionTestDoc.xml");
44  		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("admin"), "TestFinalApproverDocumentType");
45          document.setTitle("");
46          document.route("");
47          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
48          try {
49              document.approve("");
50              fail("document should have thrown routing exception");
51          } catch (Exception e) {
52              //deal with single transaction issue in test.
53          	TestUtilities.getExceptionThreader().join();
54          	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
55              assertTrue("Document should be in exception routing", document.isException());
56          }
57  	}
58  
59  	/**
60  	 * Checks to make sure that the KIM routing is working with hierarchical documents.
61  	 * Based upon the test method org.kuali.rice.kew.doctype.DocumentTypeTest.testFinalApproverRouting()
62  	 */
63  	@Test public void testKimExceptionRoutingWithDocHierarchy() throws Exception {
64  		loadXmlFile("RouteExceptionTestDoc.xml");
65  		String[] docNames = {"TestFinalApproverDocumentType_Child", "TestFinalApproverDocumentType_GrandChild"};
66  		// Test the child doc and then the grandchild doc.
67  		for (int i = 0; i < docNames.length; i++) {
68  			// Perform the same steps as in the previous unit test.
69  			WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("admin"), docNames[i]);
70  	        document.setTitle("");
71  	        document.route("");
72  	        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
73  	        try {
74  	            document.approve("");
75  	            fail("document should have thrown routing exception");
76  	        } catch (Exception e) {
77  	            //deal with single transaction issue in test.
78  	        	TestUtilities.getExceptionThreader().join();
79  	        	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
80  	            assertTrue("Document should be in exception routing", document.isException());
81  	        }
82  		}
83  	}
84  
85  	/**
86  	 * Checks to make sure that the KIM routing is working for a RiceDocument child.
87  	 * Based upon the test method org.kuali.rice.kew.doctype.DocumentTypeTest.testFinalApproverRouting()
88  	 */
89  	@Test public void testKimExceptionRoutingWithRiceDocumentChild() throws Exception {
90  		loadXmlFile("RouteExceptionTestDoc.xml");
91  		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("admin"), "DocumentTypeDocument_New");
92          document.setTitle("");
93          document.route("");
94          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
95          try {
96              document.approve("");
97              fail("document should have thrown routing exception");
98          } catch (Exception e) {
99              //deal with single transaction issue in test.
100         	TestUtilities.getExceptionThreader().join();
101         	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
102             assertTrue("Document should be in exception routing", document.isException());
103         }
104 	}
105 
106     /**
107      * Makes sure the {@link org.kuali.rice.kew.routeheader.service.WorkflowDocumentService#placeInExceptionRouting(String, org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue, String)}
108      * method exposed through {@link org.kuali.rice.kew.api.action.WorkflowDocumentActionsService#placeInExceptionRouting(org.kuali.rice.kew.api.action.DocumentActionParameters)} and
109      * {@link WorkflowDocument#placeInExceptionRouting(String)} at the time of this writing works when not called in the context of an exisiting message.
110      */
111     @Test public void testExplicitlyPlacingDocumentInException() {
112         loadXmlFile("org/kuali/rice/kew/routeheader/AppDocStatusTestConfig.xml");
113         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "TestAppDocStatusDoc1");
114         document.setTitle("");
115         document.route("");
116         // no message will be associated with this invocation inside ExceptionRoutingServiceImpl
117         document.placeInExceptionRouting("explicitly placing in exception routing");
118         assertEquals(DocumentStatus.EXCEPTION, document.getStatus());
119     }
120 }