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.routeheader;
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.WorkflowRuntimeException;
23  import org.kuali.rice.kew.api.action.ActionRequest;
24  import org.kuali.rice.kew.api.action.ActionRequestType;
25  import org.kuali.rice.kew.api.document.Document;
26  import org.kuali.rice.kew.test.KEWTestCase;
27  
28  import java.util.List;
29  import java.util.Set;
30  
31  import static org.junit.Assert.*;
32  
33  public class AppDocStatusTest extends KEWTestCase {
34      	    
35      protected void loadTestData() throws Exception {
36      	super.loadTestData();
37          loadXmlFile("AppDocStatusTestConfig.xml");
38      }
39          
40      /**
41       * 
42       * This method performs several positive tests related to Application Document Status
43       * For these tests the doctype definition defines a valid set of statuses.
44       * It also defines two status transitions in the route path
45       * It tests:
46       * 	- That the AppDocStatus is properly set by the workflow engine during
47       *    appropriate transitions.
48       *  - That the AppDocStatus may be retrieved by the client API
49       *  - That the AppDocStatus may be set by the client API
50       *  - That a history of AppDocStatus transitions is created.
51       * 
52       */
53      @Test public void testValidAppDocStatus() throws Exception {
54      	// Create document
55      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestAppDocStatusDoc2");
56      	document.saveDocumentData();
57      	assertNotNull(document.getDocumentId());
58      	assertTrue("Document should be initiatied", document.isInitiated());
59      	assertTrue("Invalid route level.", document.getNodeNames().contains("Initiated"));
60      	
61      	// route document to first stop and check status, etc.
62      	document.route("Test Routing.");    	
63      	String appDocStatus = document.getDocument().getApplicationDocumentStatus();
64      	assertTrue("Application Document Status:" + appDocStatus +" is invalid", "Approval in Progress".equalsIgnoreCase(appDocStatus));
65          
66          // should have generated a request to "bmcgough"
67      	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
68          assertTrue("Document should be enroute", document.isEnroute());
69      	Set<String> nodeNames = document.getNodeNames();
70      	assertEquals("Wrong number of node names.", 1, nodeNames.size());
71      	assertTrue("Wrong node name.", document.getNodeNames().contains("DestinationApproval"));
72  
73      	// check action request
74          List<ActionRequest> requests = document.getRootActionRequests();
75          assertEquals(1, requests.size());
76          ActionRequest request = requests.get(0);
77          assertEquals(getPrincipalIdForName("bmcgough"), request.getPrincipalId());
78          assertEquals(ActionRequestType.APPROVE, request.getActionRequested());
79          assertEquals("DestinationApproval", request.getNodeName());
80          assertTrue(document.isApprovalRequested());
81          
82          // approve the document to send it to its next route node
83          document.approve("Test approve by bmcgough");
84          
85          // check status 
86          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("temay"), document.getDocumentId());
87          Document rh = document.getDocument();
88      	appDocStatus = rh.getApplicationDocumentStatus();
89      	assertTrue("Application Document Status:" + appDocStatus +" is invalid", "Submitted".equalsIgnoreCase(appDocStatus));
90          
91          // should have generated a request to "temay"
92      	assertTrue("Document should be enroute", document.isEnroute());
93      	nodeNames = document.getNodeNames();
94      	assertEquals("Wrong number of node names.", 1, nodeNames.size());
95      	assertTrue("Wrong node name.", nodeNames.contains("TravelerApproval"));
96      	document.approve("Test approve by temay");
97      	
98      	// update the AppDocStatus via client API
99          document.setApplicationDocumentStatus("Completed");
100         document.saveDocumentData();
101 
102         // get a refreshed document and check it out
103         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("temay"), document.getDocumentId());
104 //        assertTrue("Document should be processed.", document.isProcessed());        
105         rh = document.getDocument();
106     	appDocStatus = rh.getApplicationDocumentStatus();
107     	assertTrue("Application Document Status:" + appDocStatus +" is invalid", "Completed".equalsIgnoreCase(appDocStatus));
108     	
109         // check app doc status transition history
110         List<org.kuali.rice.kew.api.document.DocumentStatusTransition> history = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatusTransitionHistory(
111                 document.getDocumentId());
112         
113         assertEquals(3, history.size());
114     	assertTrue("First History record has incorrect status", "Approval In Progress".equalsIgnoreCase(history.get(0).getNewStatus()));
115     	assertTrue("Second History record has incorrect old status", "Approval In Progress".equalsIgnoreCase(
116                 history.get(1).getOldStatus()));
117     	assertTrue("Second History record has incorrect new status", "Submitted".equalsIgnoreCase(history.get(1).getNewStatus()));
118     	assertTrue("Third History record has incorrect old status", "Submitted".equalsIgnoreCase(history.get(2).getOldStatus()));
119     	assertTrue("Third History record has incorrect new status", "Completed".equalsIgnoreCase(history.get(2).getNewStatus()));
120                
121     	// TODO when we are able to, we should also verify the RouteNodeInstances are correct
122         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
123     	assertTrue("Document should be final.", document.isFinal());
124     }        
125 
126     /**
127      * 
128      * This method is similar to the above test, except that the doctype definition
129      * does NOT specify a valid set of values.  This means that the value can be any valid string.
130      * 
131      * @throws Exception
132      */
133     @Test public void testAppDocStatusValuesNotDefined() throws Exception {
134     	// Create document
135     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestAppDocStatusDoc1");
136     	document.saveDocumentData();
137     	assertNotNull(document.getDocumentId());
138     	assertTrue("Document should be initiatied", document.isInitiated());
139     	assertTrue("Invalid route level.", document.getNodeNames().contains("Initiated"));
140     	
141     	// route document to first stop and check status, etc.
142     	document.route("Test Routing.");    	
143     	Document rh = document.getDocument();
144     	String appDocStatus = rh.getApplicationDocumentStatus();
145     	assertTrue("Application Document Status:" + appDocStatus +" is invalid", "Approval in Progress".equalsIgnoreCase(appDocStatus));
146         
147         // should have generated a request to "bmcgough"
148     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
149         assertTrue("Document should be enroute", document.isEnroute());
150     	Set<String> nodeNames = document.getNodeNames();
151     	assertEquals("Wrong number of node names.", 1, nodeNames.size());
152     	assertTrue("Wrong node name.", nodeNames.contains("step1"));
153 
154     	// check action request
155         List<ActionRequest> requests = document.getRootActionRequests();
156         assertEquals(1, requests.size());
157         ActionRequest request = requests.get(0);
158         assertEquals(getPrincipalIdForName("bmcgough"), request.getPrincipalId());
159         assertEquals(ActionRequestType.APPROVE, request.getActionRequested());
160         assertEquals("step1", request.getNodeName());
161         assertTrue(document.isApprovalRequested());
162         
163         // approve the document to send it to its next route node
164         document.approve("Test approve by bmcgough");
165         
166         // check status 
167         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("temay"), document.getDocumentId());
168         rh = document.getDocument();
169     	appDocStatus = rh.getApplicationDocumentStatus();
170     	assertTrue("Application Document Status:" + appDocStatus +" is invalid", "Submitted".equalsIgnoreCase(appDocStatus));
171         
172         // should have generated a request to "temay"
173     	assertTrue("Document should be enroute", document.isEnroute());
174     	nodeNames = document.getNodeNames();
175     	assertEquals("Wrong number of node names.", 1, nodeNames.size());
176     	assertTrue("Wrong node name.", nodeNames.contains("step2"));
177     	document.approve("Test approve by temay");
178     	
179     	// update the AppDocStatus via client API
180         document.setApplicationDocumentStatus("Some Random Value");
181         document.saveDocumentData();
182 
183         // get a refreshed document and check it out
184         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("temay"), document.getDocumentId());
185 //        assertTrue("Document should be processed.", document.isProcessed());        
186         rh = document.getDocument();
187     	appDocStatus = rh.getApplicationDocumentStatus();
188     	assertTrue("Application Document Status:" + appDocStatus +" is invalid", "Some Random Value".equalsIgnoreCase(appDocStatus));
189     	
190         // check app doc status transition history
191         List<org.kuali.rice.kew.api.document.DocumentStatusTransition> history = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatusTransitionHistory(
192                 document.getDocumentId());
193         
194         assertEquals(3, history.size());
195     	assertTrue("First History record has incorrect status", "Approval In Progress".equalsIgnoreCase(history.get(0)
196                 .getNewStatus()));
197     	assertTrue("Second History record has incorrect old status", "Approval In Progress".equalsIgnoreCase(
198                 history.get(1).getOldStatus()));
199     	assertTrue("Second History record has incorrect new status", "Submitted".equalsIgnoreCase(history.get(1)
200                 .getNewStatus()));
201     	assertTrue("Third History record has incorrect old status", "Submitted".equalsIgnoreCase(history.get(2).getOldStatus()));
202     	assertTrue("Third History record has incorrect new status", "Some Random Value".equalsIgnoreCase(history.get(2)
203                 .getNewStatus()));
204                
205     	// TODO when we are able to, we should also verify the RouteNodeInstances are correct
206         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
207     	assertTrue("Document should be final.", document.isFinal());
208     }        
209 
210     /**
211      * 
212      * This test attempts to set an invalid status value for a document that has a valid set
213      * of statuses defined.
214      * It expects to throw a WorkflowRuntimeException when attempting to set the invalid status value.
215      * 
216      * @throws Exception
217      */
218     @Test public void testInvalidAppDocStatusValue() throws Exception {
219     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestAppDocStatusDoc2");
220     	document.saveDocumentData();
221     	assertNotNull(document.getDocumentId());
222     	assertTrue("Document should be initiatied", document.isInitiated());
223     	assertTrue("Invalid route level.", document.getNodeNames().contains("Initiated"));
224     	    	
225     	// update the AppDocStatus via client API
226     	boolean gotException = false;
227     	try {
228     		document.setApplicationDocumentStatus("BAD STATUS");
229     		document.saveDocumentData();
230     	} catch (Throwable t){
231     		gotException = true;
232     		WorkflowRuntimeException ex = new WorkflowRuntimeException();
233     		assertEquals("WrongExceptionType", t.getClass(), ex.getClass());
234     	} finally {
235     		assertTrue("Expected WorkflowRuntimeException not thrown.", gotException);
236     		
237     	}
238     }
239 }