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.actions;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
20  import org.kuali.rice.kew.actions.BlanketApproveTest.NotifySetup;
21  import org.kuali.rice.kew.api.KewApiServiceLocator;
22  import org.kuali.rice.kew.api.WorkflowDocument;
23  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
24  import org.kuali.rice.kew.api.action.ActionRequest;
25  import org.kuali.rice.kew.api.action.ActionType;
26  import org.kuali.rice.kew.api.document.DocumentStatus;
27  import org.kuali.rice.kew.service.KEWServiceLocator;
28  import org.kuali.rice.kew.test.KEWTestCase;
29  import org.kuali.rice.kew.test.TestUtilities;
30  import org.kuali.rice.test.BaselineTestCase;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import static org.junit.Assert.*;
36  
37  /**
38   * Tests the super user actions available on the API.
39   */
40  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
41  public class SuperUserActionTest extends KEWTestCase {
42      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserActionTest.class);
43  
44      protected void loadTestData() throws Exception {
45          loadXmlFile("ActionsConfig.xml");
46      }
47  	
48      @Test public void testSuperUserApprove() throws Exception {
49          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
50          document.route("");
51          
52          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
53          assertTrue("WorkflowDocument should indicate jhopf as SuperUser", document.isValidAction(ActionType.SU_BLANKET_APPROVE));
54          document.superUserBlanketApprove("");
55          assertTrue("Document should be 'processed' after Super User Approve", document.isProcessed());
56          List requests = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getDocumentId());
57          assertTrue("Should be active requests still", requests.size() == 2);//number of acks and fyi's configured through rules
58          for (Iterator iter = requests.iterator(); iter.hasNext();) {
59  			ActionRequestValue request = (ActionRequestValue) iter.next();
60  			if (request.isApproveOrCompleteRequest()) {
61  				fail("There should be no approve or complete requests after su approve");
62  			}
63  		}
64  	}
65  	
66      @Test public void testSuperUserApproveExceptionCases() throws Exception {
67      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "SUApproveDocument");
68          document.route("");
69          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
70          try {
71          	document.approve("");
72          } catch (Exception e) {
73          }
74          TestUtilities.getExceptionThreader().join();
75          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
76          assertTrue("Document should be in exception routing", document.isException());
77          document.superUserBlanketApprove("");
78          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
79          assertTrue("Document should be final", document.isFinal());
80          
81          List actionRequests = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getDocumentId());
82          assertTrue("Should be no active requests for SU Approved document", actionRequests.isEmpty());
83      }
84      
85      @Test public void testSuperUserApproveExceptionCasesWithNotifications() throws Exception {
86      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "SUApproveDocumentNotifications");
87          document.route("");
88          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
89          try {
90          	document.approve("");
91          } catch (Exception e) {
92          }
93          TestUtilities.getExceptionThreader().join();
94          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
95          assertTrue("Document should be in exception routing", document.isException());
96          document.superUserBlanketApprove("");
97          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
98          assertTrue("Document should be 'processed'", document.isProcessed());
99          
100         List actionRequests = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getDocumentId());
101         assertFalse("Should be active requests for SU Approved document", actionRequests.isEmpty());
102         for (Iterator iter = actionRequests.iterator(); iter.hasNext();) {
103 			ActionRequestValue request = (ActionRequestValue) iter.next();
104 			assertTrue("Should be an ack notification request", request.isAcknowledgeRequest());
105 		}
106     }
107     
108     @Test public void testSuperUserInitiatorApprove() throws Exception {
109 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
110         assertTrue("WorkflowDocument should indicate ewestfal as SuperUser", document.isValidAction(ActionType.SU_BLANKET_APPROVE));
111         document.superUserBlanketApprove("");
112         assertTrue("Document should be 'processed' after Super User Approve", document.isProcessed());
113         List requests = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getDocumentId());
114         assertTrue("Should be active requests still", requests.size() == 2);//number of acks and fyi's configured through rules
115         for (Iterator iter = requests.iterator(); iter.hasNext();) {
116 			ActionRequestValue request = (ActionRequestValue) iter.next();
117 			if (request.isApproveOrCompleteRequest()) {
118 				fail("There should be no approve or complete requests after su approve");
119 			}
120 		}
121 	}
122 	
123 	@Test public void testSuperUserApproveWithNotifications() throws Exception {
124 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "NotificationTestChild");
125         assertTrue("WorkflowDocument should indicate ewestfal as SuperUser", document.isValidAction(ActionType.SU_BLANKET_APPROVE));
126         document.superUserBlanketApprove("");
127         assertTrue("Document should be 'processed' after Super User Approve", document.isProcessed());
128         List requests = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getDocumentId());
129         assertTrue("Should be active requests still", requests.size() > 2);//number of acks and fyi's configured through rules - we need these for approvals too
130         for (Iterator iter = requests.iterator(); iter.hasNext();) {
131 			ActionRequestValue request = (ActionRequestValue) iter.next();
132 			if (request.isApproveOrCompleteRequest()) {
133 				fail("There should be no approve or complete requests after su approve");
134 			}
135 		} 
136 	}
137 	
138 	@Test public void testSuperUserApproveInvalidUser() throws Exception {
139 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
140         document.route("");
141         
142         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("quickstart"), document.getDocumentId());
143         try {
144         	assertFalse("WorkflowDocument should not indicate quickstart as SuperUser", document.isValidAction(ActionType.SU_BLANKET_APPROVE));
145         	document.superUserBlanketApprove("");
146         	fail("invalid user attempted to SuperUserApprove");
147         } catch (Exception e) {
148         }
149         
150 	}
151 	
152 	@Test public void testSuperUserActionDisregardPostProcessing() throws Exception {
153 		
154 		String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
155 		
156 	    // verify that the post processor class still throws exceptions when post processing document
157         WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
158         try {
159             document.superUserBlanketApprove("");
160             fail("Document should throw exception from post processor");
161         } catch (Exception e) {
162         }
163         
164         // test that ignoring the post processor works correctly
165         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
166         try {
167             KEWServiceLocator.getWorkflowDocumentService().superUserCancelAction(bmcgoughPrincipalId, KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId()), "", false);
168         } catch (Exception e) {
169             LOG.error("Exception Found:", e);
170             fail("Document should not throw an exception when ignoring post processor during superUserCancelAction");
171         }
172 
173         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
174         try {
175             KEWServiceLocator.getWorkflowDocumentService().superUserDisapproveAction(bmcgoughPrincipalId, KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId()), "", false);
176         } catch (Exception e) {
177             LOG.error("Exception Found:", e);
178             fail("Document should not throw an exception when ignoring post processor during superUserDisapproveAction");
179         }
180 
181         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
182         try {
183             KEWServiceLocator.getWorkflowDocumentService().superUserApprove(bmcgoughPrincipalId, KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId()), "", false);
184         } catch (Exception e) {
185             LOG.error("Exception Found:", e);
186             fail("Document should not throw an exception when ignoring post processor during superUserApprove");
187         }
188 
189         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
190         try {
191             KEWServiceLocator.getWorkflowDocumentService().superUserNodeApproveAction(bmcgoughPrincipalId, document.getDocumentId(), "Acknowledge1", "", false);
192         } catch (Exception e) {
193             LOG.error("Exception Found:", e);
194             fail("Document should not throw an exception when ignoring post processor during superUserNodeApprove");
195         }
196 
197         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
198         try {
199             KEWServiceLocator.getWorkflowDocumentService().superUserReturnDocumentToPreviousNode(bmcgoughPrincipalId, document.getDocumentId(), "WorkflowDocumentTemplate", "", false);
200         } catch (Exception e) {
201             LOG.error("Exception Found:", e);
202             fail("Document should not throw an exception when ignoring post processor during superUserReturnDocumentToPreviousNode");
203         }
204 
205         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), generateDummyEnrouteDocument("ewestfal").getDocumentId());
206         try {
207         	String actionRequestId = null;
208             // get actionRequestId to use... there should only be one active action request
209             List<ActionRequest> actionRequests = KewApiServiceLocator.getWorkflowDocumentService().getRootActionRequests(document.getDocumentId());
210             for (ActionRequest actionRequest : actionRequests) {
211                 if (actionRequest.isActivated()) {
212                     // if we already found an active action request fail the test
213                     if (actionRequestId != null) {
214                         fail("Found two active action requests for document.  Ids: " + actionRequestId + "  &  " + actionRequest.getId());
215                     }
216                     actionRequestId = actionRequest.getId();
217                 }
218             }
219             
220             KEWServiceLocator.getWorkflowDocumentService().superUserActionRequestApproveAction(bmcgoughPrincipalId, document.getDocumentId(), actionRequestId, "", false);
221         } catch (Exception e) {
222             LOG.error("Exception Found:", e);
223             fail("Document should not throw an exception when ignoring post processor during superUserActionRequestApproveAction");
224         }
225 
226 	}
227 	
228 	private WorkflowDocument generateDummyEnrouteDocument(String initiatorNetworkId) throws Exception {
229         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(initiatorNetworkId), "SuperUserActionInvalidPostProcessor");
230         assertEquals("Document should be at start node","AdHoc", document.getNodeNames().iterator().next());
231         document.route("");
232         assertEquals("Document should be at WorkflowDocument2 node","WorkflowDocument2", document.getNodeNames().iterator().next());
233         assertEquals("Document should be enroute", DocumentStatus.ENROUTE, document.getStatus());
234         return document;
235 	}
236 	
237 	
238 }