View Javadoc

1   /*
2    * Copyright 2007 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 java.util.ArrayList;
19  import java.util.List;
20  
21  import org.junit.Test;
22  import org.kuali.rice.kew.actionlist.ActionListFilter;
23  import org.kuali.rice.kew.dto.DocumentDetailDTO;
24  import org.kuali.rice.kew.dto.KeyValueDTO;
25  import org.kuali.rice.kew.dto.NetworkIdDTO;
26  import org.kuali.rice.kew.dto.ReportActionToTakeDTO;
27  import org.kuali.rice.kew.dto.ReportCriteriaDTO;
28  import org.kuali.rice.kew.engine.node.BranchState;
29  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30  import org.kuali.rice.kew.rule.TestRuleAttribute;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.service.WorkflowDocument;
33  import org.kuali.rice.kew.service.WorkflowInfo;
34  import org.kuali.rice.kew.test.KEWTestCase;
35  import org.kuali.rice.kew.util.FutureRequestDocumentStateManager;
36  import org.kuali.rice.kew.util.KEWConstants;
37  
38  
39  /**
40   * Tests users requesting to see all future requests, not seeing any future requests on documents and the clearing of those
41   * statuses on documents.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class FutureRequestsTest extends KEWTestCase {
46  
47      /**
48       * Verify future requests status are being preserved on {@link DocumentRouteHeaderValue} objects when set from the
49       * {@link WorkflowDocument}
50       *
51       * @throws Exception
52       */
53      @Test
54      public void testSavingFutureRequestsStatuses() throws Exception {
55          List<String> ids = new ArrayList<String>();
56          ids.add(getPrincipalIdForName("user1"));
57          TestRuleAttribute.setRecipientPrincipalIds("TestRole", "TestRole-user1", ids);
58  
59          // Test receiving future requests
60  
61          String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
62          WorkflowDocument document = new WorkflowDocument(rkirkendPrincipalId, "TestDocumentType");
63          document.setReceiveFutureRequests();
64          document.routeDocument("");
65  
66          DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getRouteHeaderId());
67          
68          FutureRequestDocumentStateManager futRequestStateMan = new FutureRequestDocumentStateManager(routeHeader, rkirkendPrincipalId);
69          assertTrue(futRequestStateMan.isReceiveFutureRequests());
70          assertFalse(futRequestStateMan.isClearFutureRequestState());
71          assertFalse(futRequestStateMan.isDoNotReceiveFutureRequests());
72  
73          // Test not receiving future requests
74  
75          document = new WorkflowDocument(rkirkendPrincipalId, "TestDocumentType");
76          document.setDoNotReceiveFutureRequests();
77          document.routeDocument("");
78  
79          routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getRouteHeaderId());
80          
81          futRequestStateMan = new FutureRequestDocumentStateManager(routeHeader, rkirkendPrincipalId);
82          assertFalse(futRequestStateMan.isReceiveFutureRequests());
83          assertFalse(futRequestStateMan.isClearFutureRequestState());
84          assertTrue(futRequestStateMan.isDoNotReceiveFutureRequests());
85  
86          // test clearing state from existing document
87          document = new WorkflowDocument(rkirkendPrincipalId, document.getRouteHeaderId());
88          document.setClearFutureRequests();
89          document.approve("");
90  
91          routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getRouteHeaderId());
92          futRequestStateMan = new FutureRequestDocumentStateManager(routeHeader, rkirkendPrincipalId);
93          assertFalse(futRequestStateMan.isReceiveFutureRequests());
94          assertTrue(futRequestStateMan.isClearFutureRequestState());
95          assertFalse(futRequestStateMan.isDoNotReceiveFutureRequests());
96  
97          // reload the route header
98          routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getRouteHeaderId());
99          int deactivatedCount = 0;
100         for (BranchState state : routeHeader.getRootBranchState()) {
101             if (state.getKey().contains(FutureRequestDocumentStateManager.FUTURE_REQUESTS_VAR_KEY)) {
102                 fail("state clearing should have removed all future request vars");
103             } else if (state.getKey().contains(FutureRequestDocumentStateManager.DEACTIVATED_REQUESTS_VARY_KEY)) {
104                 deactivatedCount++;
105             }
106         }
107         assertEquals(2, deactivatedCount);
108         // test standard scenario of not setting a future request status on the document
109         document = new WorkflowDocument(rkirkendPrincipalId, "TestDocumentType");
110         document.routeDocument("");
111         routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getRouteHeaderId());
112         futRequestStateMan = new FutureRequestDocumentStateManager(routeHeader, rkirkendPrincipalId);
113         assertFalse(futRequestStateMan.isReceiveFutureRequests());
114         assertFalse(futRequestStateMan.isClearFutureRequestState());
115         assertFalse(futRequestStateMan.isDoNotReceiveFutureRequests());
116     }
117 
118     /**
119      * Tests future requests work with routing and force action rules
120      *
121      * @throws Exception
122      */
123     @Test
124     public void testFutureRequestsWithRouting() throws Exception {
125         this.loadXmlFile(this.getClass(), "FutureRequestsConfig.xml");
126 
127         String user1PrincipalId = getPrincipalIdForName("user1");
128         String user2PrincipalId = getPrincipalIdForName("user2");
129 
130         // Node 1 - user1 approval (forceAction true)
131         //          user2 approval (forceAction false)
132         // Node 2 - NonSIT approval (forceAction false)
133         //          user1 approval (forceAction true)
134         // Node 3 - user2 approval (forceAction false)
135         WorkflowDocument document = new WorkflowDocument(user1PrincipalId, "FutureRequestsDoc");
136         document.setDoNotReceiveFutureRequests();
137         document.routeDocument("");
138 
139         document = new WorkflowDocument(user1PrincipalId, document.getRouteHeaderId());
140         assertFalse(document.isApprovalRequested());
141 
142         document = new WorkflowDocument(user2PrincipalId, document.getRouteHeaderId());
143         assertTrue(document.isApprovalRequested());
144         document.setReceiveFutureRequests();
145         document.approve("");
146 
147         // should have another request from second rule that is not force action because
148         // of policy
149         document = new WorkflowDocument(user2PrincipalId, document.getRouteHeaderId());
150         assertTrue(document.isApprovalRequested());
151 
152         // user2 should have action items. user1 should not
153         assertEquals(1, KEWServiceLocator.getActionListService().getActionList(user2PrincipalId, new ActionListFilter()).size());
154         assertEquals(1, KEWServiceLocator.getActionListService().getActionList(user1PrincipalId, new ActionListFilter()).size());
155 
156         document.approve("");
157 
158         // test for request to user2 and not a workgroup
159         document = new WorkflowDocument(user2PrincipalId, document.getRouteHeaderId());
160         assertTrue(document.isApprovalRequested());
161     }
162 
163     /**
164      * Tests future requests operation in conjunction with the {@link WorkflowInfo#documentWillHaveAtLeastOneActionRequest(ReportCriteriaDTO, String[])} method
165      *
166      * @throws Exception
167      */
168     @Test
169     public void testFutureRequestsWithRoutingAndWorkflowInfoActionRequestCheck() throws Exception {
170         this.loadXmlFile(this.getClass(), "FutureRequestsConfig.xml");
171 
172         String user1PrincipalId = getPrincipalIdForName("user1");
173         String user2PrincipalId = getPrincipalIdForName("user2");
174 
175         WorkflowDocument document = new WorkflowDocument(user1PrincipalId, "FutureRequestsDoc");
176         document.routeDocument("");
177 
178         // Node1
179         //user1 should have approval requested
180         document = new WorkflowDocument(user1PrincipalId, document.getRouteHeaderId());
181         WorkflowInfo info = new WorkflowInfo();
182         ReportCriteriaDTO reportCriteriaDTO = new ReportCriteriaDTO(document.getRouteHeaderId());
183         reportCriteriaDTO.setTargetPrincipalIds(new String[]{user1PrincipalId});
184         String actionToTakeNode = "Node1";
185         reportCriteriaDTO.setActionsToTake(new ReportActionToTakeDTO[]{new ReportActionToTakeDTO(KEWConstants.ACTION_TAKEN_APPROVED_CD, user1PrincipalId, actionToTakeNode)});
186         assertTrue("User " + user1PrincipalId + " should have approval requests on the document", info.documentWillHaveAtLeastOneActionRequest(reportCriteriaDTO, new String[]{KEWConstants.ACTION_REQUEST_APPROVE_REQ}, false));
187 
188         info = new WorkflowInfo();
189         reportCriteriaDTO = new ReportCriteriaDTO(document.getRouteHeaderId());
190         reportCriteriaDTO.setTargetPrincipalIds(new String[]{user1PrincipalId});
191         actionToTakeNode = "Node1";
192         reportCriteriaDTO.setActionsToTake(new ReportActionToTakeDTO[]{new ReportActionToTakeDTO(KEWConstants.ACTION_TAKEN_APPROVED_CD, user1PrincipalId, actionToTakeNode)});
193         DocumentDetailDTO documentVO = info.routingReport(reportCriteriaDTO);
194         assertTrue("User " + user1PrincipalId + " should have one or more approval requests on the document", documentVO.getActionRequests().length > 0);
195 
196         info = new WorkflowInfo();
197         reportCriteriaDTO = new ReportCriteriaDTO(document.getRouteHeaderId());
198         String delyeaPrincipalId = getPrincipalIdForName("delyea");
199         NetworkIdDTO delyeaIdDTO = new NetworkIdDTO("delyea");
200         reportCriteriaDTO.setTargetPrincipalIds(new String[]{user1PrincipalId});
201         actionToTakeNode = "Node1";
202         reportCriteriaDTO.setActionsToTake(new ReportActionToTakeDTO[]{new ReportActionToTakeDTO(KEWConstants.ACTION_TAKEN_APPROVED_CD, user1PrincipalId, actionToTakeNode)});
203         documentVO = info.routingReport(reportCriteriaDTO);
204         assertTrue("User " + delyeaPrincipalId + " should not have any requests on the document but routingReport() method should return all action requests anyway", documentVO.getActionRequests().length > 0);
205 
206         document = new WorkflowDocument(user1PrincipalId, "FutureRequestsDoc");
207         document.setDoNotReceiveFutureRequests();
208         document.routeDocument("");
209 
210         document = new WorkflowDocument(user1PrincipalId, document.getRouteHeaderId());
211         assertFalse(document.isApprovalRequested());
212 
213         // user1 should not have approval requested
214         info = new WorkflowInfo();
215         reportCriteriaDTO = new ReportCriteriaDTO(document.getRouteHeaderId());
216         reportCriteriaDTO.setTargetPrincipalIds(new String[]{user1PrincipalId});
217         assertFalse("User " + user1PrincipalId + " should not have any approval request on the document", info.documentWillHaveAtLeastOneActionRequest(reportCriteriaDTO, new String[]{KEWConstants.ACTION_REQUEST_APPROVE_REQ}, false));
218 
219         // user2 should have approval requested
220         info = new WorkflowInfo();
221         reportCriteriaDTO = new ReportCriteriaDTO(document.getRouteHeaderId());
222         reportCriteriaDTO.setTargetPrincipalIds(new String[]{user2PrincipalId});
223         assertTrue("User " + user2PrincipalId + " should have any approval request on the document", info.documentWillHaveAtLeastOneActionRequest(reportCriteriaDTO, new String[]{KEWConstants.ACTION_REQUEST_APPROVE_REQ}, false));
224 
225     }
226     
227     /**
228      * 
229      * This method tests future action request with duplicate nodes. Both on a straight path
230      * and a split path.  
231      * 
232      * This test was written because of KULRICE-4074.  Branch data was not being save when we
233      * call saveRoutingData.
234      * 
235      * @throws Exception
236      */
237     @Test
238     public void testFutureRequestsWithDuplicateNodesSplit() throws Exception {
239     	// This file has the split node that mimmics KC
240     	testFutureRequestsWithDuplicateNodesImpl("FutureRequestsConfig2.xml");    	    	    
241     }
242     
243     @Test
244     public void testFutureRequestsWithDuplicateNodesStraight() throws Exception {
245     	// This file has no split node
246     	testFutureRequestsWithDuplicateNodesImpl("FutureRequestsConfig3.xml");
247     }
248     
249     private void testFutureRequestsWithDuplicateNodesImpl(String fileName) throws Exception{    	
250     	this.loadXmlFile(this.getClass(), fileName);
251     	
252 
253         String user1PrincipalId = getPrincipalIdForName("user1");
254         String user2PrincipalId = getPrincipalIdForName("user2");
255         String user3PrincipalId = getPrincipalIdForName("earl");
256 
257         WorkflowDocument document = new WorkflowDocument(user1PrincipalId, "FutureRequestsDoc");
258         document.routeDocument("");
259        
260 
261         // Node1
262         //user1 should have approval requested
263         document = new WorkflowDocument(user3PrincipalId, document.getRouteHeaderId());
264         assertTrue("should have approval status", document.isApprovalRequested());
265 
266         document.setReceiveFutureRequests();
267         
268         /*
269          *  Uncomment the following line to duplicate the error in KC
270          */        
271         document.saveRoutingData();
272         
273         document.approve("route node 1");
274 
275         document = new WorkflowDocument(user1PrincipalId, document.getRouteHeaderId());
276         List<KeyValueDTO> l =document.getRouteHeader().getVariables();
277         assertFalse("should not have approval status 1", document.isApprovalRequested());
278 
279         document = new WorkflowDocument(user2PrincipalId, document.getRouteHeaderId());
280         assertTrue("should have approval status 2", document.isApprovalRequested());
281         document.approve("routing node 2");
282         
283         // Node3
284         //user1 should have approval requested bc of future action requests
285         document = new WorkflowDocument(user3PrincipalId, document.getRouteHeaderId());
286         System.out.println("Doc status prior to 3: " + document.getStatusDisplayValue());
287         assertTrue("should have approval status 3", document.isApprovalRequested());
288         document.approve("routing node 3");
289         System.out.println("Doc status after 3: " + document.getStatusDisplayValue());
290     }
291 }