View Javadoc

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