View Javadoc

1   /*
2    * Copyright 2005-2007 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.actions;
18  
19  
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.junit.Test;
24  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
25  import org.kuali.rice.kew.dto.NetworkIdDTO;
26  import org.kuali.rice.kew.exception.WorkflowException;
27  import org.kuali.rice.kew.service.KEWServiceLocator;
28  import org.kuali.rice.kew.service.WorkflowDocument;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  
31  
32  public class SaveActionEventTest extends KEWTestCase {
33      
34      public static final String DOCUMENT_TYPE_NAME = "SaveActionEventTest";
35      public static final String DOCUMENT_TYPE_NAME_NON_INITIATOR = "SaveActionEventTestNonInitiator";
36      public static final String ADHOC_NODE = "AdHoc";
37      public static final String WORKFLOW_DOCUMENT_NODE = "WorkflowDocument";
38      
39      protected void loadTestData() throws Exception {
40          loadXmlFile("ActionsConfig.xml");
41      }
42      
43      @Test public void testSaveActionEvent() throws Exception {
44          WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), DOCUMENT_TYPE_NAME);
45          document.saveRoutingData();
46          assertTrue("Document should be initiated.", document.stateIsInitiated());
47          List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
48          assertEquals("There should be no action requests.", 0, actionRequests.size());
49          assertTrue("Document should be initiated.", document.stateIsInitiated());
50          document.saveDocument("");
51          
52          // document should be SAVED now at the AdHoc node
53          document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), document.getRouteHeaderId());
54          assertEquals("Document should be at AdHoc node.", ADHOC_NODE, document.getNodeNames()[0]);
55          assertTrue("Document should be SAVED.", document.stateIsSaved());
56          // there should now be one COMPLETE request to the initiator
57          actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
58          assertEquals("There should be one COMPLETE request to the initiator.", 1, actionRequests.size());
59          assertTrue("Initiator should have complete request.", document.isCompletionRequested());
60          ActionRequestValue savedRequest = (ActionRequestValue)actionRequests.get(0);
61          assertNotNull(savedRequest);
62          assertTrue("Saved request should be a complete request.", savedRequest.isCompleteRequst());
63          assertEquals("Request should be at the AdHoc node.", ADHOC_NODE, savedRequest.getNodeInstance().getName());
64          
65          // if we try and call route document as rkirkend, it should throw an InvalidActionTakenException
66          document = new WorkflowDocument(new NetworkIdDTO("rkirkend"), document.getRouteHeaderId());
67          try {
68              document.routeDocument("");
69              fail("RouteDocument should have thrown an exception because we aren't the initiator");
70          } catch (WorkflowException e) {}
71          
72          
73          // now, route document as the initiator
74          document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), document.getRouteHeaderId());
75          document.routeDocument("Routing Rowdy Roddy Pipper");
76          
77          // document should be marked as ENROUTE, move to the WorkflowDocument node, 
78          // and approve requests to rkirkend and bmcgough should be generated
79          actionRequests = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getRouteHeaderId());
80          assertEquals("Should be 2 pending requests.", 2, actionRequests.size());
81          // rkirkend should have request
82          document = new WorkflowDocument(new NetworkIdDTO("rkirkend"), document.getRouteHeaderId());
83          assertTrue("Document should be in routing.", document.stateIsEnroute());
84          assertTrue("rkirkend should have approve request.", document.isApprovalRequested());
85          // bmcgough should have request
86          document = new WorkflowDocument(new NetworkIdDTO("bmcgough"), document.getRouteHeaderId());
87          assertTrue("Document should be in routing.", document.stateIsEnroute());
88          assertTrue("bmcgough should have approve request.", document.isApprovalRequested());
89          
90          // check node and requests
91          assertEquals("Document should be at WorkflowDocument node.", WORKFLOW_DOCUMENT_NODE, document.getNodeNames()[0]);
92          for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
93              ActionRequestValue request = (ActionRequestValue) iterator.next();
94              assertNotNull(request.getNodeInstance());
95              assertEquals("Request should be at WorkflowDocument node.", WORKFLOW_DOCUMENT_NODE, request.getNodeInstance().getName());
96          }
97          
98          // now, since saveDocument effectively sets the status to saved and generates a complete request, let make sure that we can
99          // take a complete or approve action against the doc to make it transition
100         document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), DOCUMENT_TYPE_NAME);
101         document.saveDocument("");
102         assertTrue("Document should be saved.", document.stateIsSaved());
103         assertTrue("Should have complete request.", document.isCompletionRequested());
104         assertEquals("Document should be at AdHoc node.", ADHOC_NODE, document.getNodeNames()[0]);
105         // take the complete action
106         document.complete("");
107         document = new WorkflowDocument(new NetworkIdDTO("rkirkend"), document.getRouteHeaderId());
108         assertTrue("Document should be enroute.", document.stateIsEnroute());
109         assertTrue("Should have approve request.", document.isApprovalRequested());
110         assertEquals("Document should be at WorkflowDocument node.", WORKFLOW_DOCUMENT_NODE, document.getNodeNames()[0]);
111 
112         // try above scenario with approve because approve should count for completion
113         document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), DOCUMENT_TYPE_NAME);
114         document.saveDocument("");
115         assertTrue("Document should be saved.", document.stateIsSaved());
116         assertTrue("Should have complete request.", document.isCompletionRequested());
117         assertTrue("Should also indicate approval is valid.", document.isApprovalRequested());
118         assertEquals("Document should be at AdHoc node.", ADHOC_NODE, document.getNodeNames()[0]);
119         // take the approve action
120         document.approve("");
121         document = new WorkflowDocument(new NetworkIdDTO("rkirkend"), document.getRouteHeaderId());
122         assertTrue("Document should be enroute.", document.stateIsEnroute());
123         assertTrue("Should have approve request.", document.isApprovalRequested());
124         assertEquals("Document should be at WorkflowDocument node.", WORKFLOW_DOCUMENT_NODE, document.getNodeNames()[0]);
125     }
126     
127     
128     /**
129      * Tests for when INITIATOR_MUST_SAVE policy is equal to true (default value).  In this case if non-initiator user
130      * attempts a save of a document with this policy an exception should be thrown
131      */
132     @Test public void testDefaultInitiatorMustSavePolicy() throws Exception {
133     	WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), DOCUMENT_TYPE_NAME);
134     	document.saveRoutingData();
135     	
136     	// verify that there are no requests that have been generated
137     	List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
138     	assertEquals("There should be no action requests.", 0, actionRequests.size());
139     	
140     	// try saving as a user who's not ewestfal
141     	document = new WorkflowDocument(new NetworkIdDTO("rkirkend"), document.getRouteHeaderId());
142     	assertTrue(document.stateIsInitiated());
143     	try {
144     		document.saveDocument("");
145     		fail("A WorkflowException should have been thrown.");
146     	} catch (WorkflowException e) {
147     		e.printStackTrace();
148     	}
149     	
150     	// ensure that the request did not get generated
151     	actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
152     	assertEquals("There should be no action requests.", 0, actionRequests.size());
153     	
154     	// now save it as the intiator and it should be successful and generate a request
155     	document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), document.getRouteHeaderId());
156     	document.saveDocument("");
157     	assertTrue(document.stateIsSaved());
158     	actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
159     	assertEquals("There should be one action request.", 1, actionRequests.size());
160     }
161 
162     /**
163      * Tests for when INITIATOR_MUST_SAVE policy is equal to false.  In this case if non-initiator user
164      * attempts a save of a document with this policy an exception should NOT be thrown
165      */
166     @Test public void testFalseInitiatorMustSavePolicy() throws Exception {
167         WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), DOCUMENT_TYPE_NAME_NON_INITIATOR);
168         document.saveRoutingData();
169         
170         // verify that there are no requests that have been generated
171         List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
172         assertEquals("There should be no action requests.", 0, actionRequests.size());
173         
174         // try saving as a user who's not ewestfal
175         document = new WorkflowDocument(new NetworkIdDTO("rkirkend"), document.getRouteHeaderId());
176         assertTrue(document.stateIsInitiated());
177         try {
178             document.saveDocument("");
179         } catch (WorkflowException e) {
180             e.printStackTrace();
181             fail("A WorkflowException should not have been thrown.");
182         }
183         
184         // ensure that the document was saved and the request was generated
185         assertTrue(document.stateIsSaved());
186         actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
187         assertEquals("There should be one action request.", 1, actionRequests.size());
188     }
189 }