View Javadoc

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