View Javadoc
1   /**
2    * Copyright 2005-2014 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.actionrequest;
17  
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.junit.Test;
28  import org.kuali.rice.kew.api.KewApiServiceLocator;
29  import org.kuali.rice.kew.api.document.DocumentRefreshQueue;
30  import org.kuali.rice.kew.api.WorkflowDocument;
31  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
32  import org.kuali.rice.kew.api.action.ActionRequest;
33  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kew.test.KEWTestCase;
36  
37  /**
38   * Tests the reference implementation of the {@link DocumentRefreshQueue}.
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class DocumentRefreshQueueTest extends KEWTestCase {
43  
44      protected void loadTestData() throws Exception {
45          loadXmlFile("ActionRequestsConfig.xml");
46      }
47  
48      private static final String SEQ_DOCUMENT_TYPE_NAME = "DRSeqDocType";
49      private static final String PAR_DOCUMENT_TYPE_NAME = "DRParDocType";
50  
51      /**
52       * Tests document requeueing at a single node.
53       *
54       * @throws Exception encountered during testing
55       */
56      @Test
57      public void testDocumentRequeueSingleNode() throws Exception {
58          String initiatorPrincipalId = getPrincipalIdForName("ewestfal");
59          String firstApproverPrincipalId = getPrincipalIdForName("bmcgough");
60          String secondApproverPrincipalId = getPrincipalIdForName("rkirkend");
61  
62          // Create and route a document
63          WorkflowDocument document = WorkflowDocumentFactory.createDocument(initiatorPrincipalId, SEQ_DOCUMENT_TYPE_NAME);
64          String documentNumber = document.getDocumentId();
65          document.route("");
66          document = WorkflowDocumentFactory.loadDocument(initiatorPrincipalId, documentNumber);
67          assertTrue(document.isEnroute());
68          assertEquals("Wrong number of requests", 2, document.getRootActionRequests().size());
69  
70          // Get all of the request ids from the initial action requests
71          Set<String> initialRequestIds = new HashSet<String>();
72  
73          for (ActionRequest request : document.getRootActionRequests()) {
74              initialRequestIds.add(request.getId());
75          }
76  
77          // Requeue the document
78          DocumentRouteHeaderValue documentRouteHeader
79                  = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
80          DocumentRefreshQueue documentRequeuer = KewApiServiceLocator.getDocumentRequeuerService(
81                  documentRouteHeader.getDocumentType().getApplicationId(), documentNumber, 0);
82          documentRequeuer.refreshDocument(documentNumber);
83  
84          // Load the document for the first approver, check its state, and approve
85          document = WorkflowDocumentFactory.loadDocument(firstApproverPrincipalId, documentNumber);
86          assertTrue(document.isEnroute());
87          assertEquals("Wrong number of requests", 2, document.getRootActionRequests().size());
88  
89          for (ActionRequest request : document.getRootActionRequests()) {
90              assertTrue("Request ids should be different", !initialRequestIds.contains(request.getId()));
91          }
92  
93          assertTrue(document.isApprovalRequested());
94          document.approve("");
95  
96          // Requeue the document again
97          documentRequeuer.refreshDocument(document.getDocumentId());
98  
99          // Load the document for the second approver and make sure that there is only one request to the second approver
100         // and that all other requests are complete
101         document = WorkflowDocumentFactory.loadDocument(secondApproverPrincipalId, documentNumber);
102         assertTrue(document.isEnroute());
103         assertEquals("Wrong number of requests", 2, document.getRootActionRequests().size());
104 
105         boolean pendingToSecondApprover = false;
106 
107         for (ActionRequest request : document.getRootActionRequests()) {
108             if (request.getPrincipalId().equals(secondApproverPrincipalId) && request.isActivated()) {
109                 assertFalse("Second approver has too many requests", pendingToSecondApprover);
110                 pendingToSecondApprover = true;
111             } else {
112                 assertTrue("Previous requests to all others should be done", request.isDone());
113             }
114         }
115 
116         assertTrue(document.isApprovalRequested());
117     }
118 
119     /**
120      * Tests document requeueing at multiple nodes.
121      *
122      * @throws Exception encountered during testing
123      */
124     @Test
125     public void testDocumentRequeueMultipleNodes() throws Exception {
126         String initiatorPrincipalId = getPrincipalIdForName("ewestfal");
127         String firstApproverPrincipalId = getPrincipalIdForName("bmcgough");
128         String secondApproverPrincipalId = getPrincipalIdForName("rkirkend");
129         String thirdApproverPrincipalId = getPrincipalIdForName("pmckown");
130 
131         // Create and route a document
132         WorkflowDocument document = WorkflowDocumentFactory.createDocument(initiatorPrincipalId, PAR_DOCUMENT_TYPE_NAME);
133         String documentNumber = document.getDocumentId();
134         document.route("");
135         document = WorkflowDocumentFactory.loadDocument(initiatorPrincipalId, documentNumber);
136         assertTrue(document.isEnroute());
137         assertEquals("Wrong number of requests", 3, document.getRootActionRequests().size());
138 
139         // Get all of the request ids from the initial action requests
140         Set<String> initialRequestIds = new HashSet<String>();
141 
142         for (ActionRequest request : document.getRootActionRequests()) {
143             initialRequestIds.add(request.getId());
144         }
145 
146         // Requeue the document
147         DocumentRouteHeaderValue documentRouteHeader
148                 = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
149         DocumentRefreshQueue documentRequeuer = KewApiServiceLocator.getDocumentRequeuerService(
150                 documentRouteHeader.getDocumentType().getApplicationId(), documentNumber, 0);
151         documentRequeuer.refreshDocument(documentNumber);
152 
153         // Load the document for the first approver, check its state, and approve
154         document = WorkflowDocumentFactory.loadDocument(firstApproverPrincipalId, documentNumber);
155         assertTrue(document.isEnroute());
156         assertEquals("Wrong number of requests", 3, document.getRootActionRequests().size());
157 
158         for (ActionRequest request : document.getRootActionRequests()) {
159             assertTrue("Request ids should be different", !initialRequestIds.contains(request.getId()));
160         }
161 
162         assertTrue(document.isApprovalRequested());
163         document.approve("");
164 
165         // Requeue the document again
166         documentRequeuer.refreshDocument(document.getDocumentId());
167 
168         // Load the document for the second approver and make sure that there is only one request to the second approver
169         document = WorkflowDocumentFactory.loadDocument(secondApproverPrincipalId, documentNumber);
170         assertTrue(document.isEnroute());
171         assertEquals("Wrong number of requests", 3, document.getRootActionRequests().size());
172 
173         boolean pendingToSecondApprover = false;
174 
175         for (ActionRequest request : document.getRootActionRequests()) {
176             if (request.getPrincipalId().equals(secondApproverPrincipalId) && request.isActivated()) {
177                 assertFalse("Second approver has too many requests.", pendingToSecondApprover);
178                 pendingToSecondApprover = true;
179             }
180         }
181 
182         assertTrue(document.isApprovalRequested());
183         document.approve("");
184 
185         // Requeue the document again
186         documentRequeuer.refreshDocument(document.getDocumentId());
187 
188         // Load the document for the third approver and make sure that there is only one request to the third approver
189         // and that all other requests are complete
190         document = WorkflowDocumentFactory.loadDocument(thirdApproverPrincipalId, documentNumber);
191         assertTrue(document.isEnroute());
192         assertEquals("Wrong number of requests", 3, document.getRootActionRequests().size());
193 
194         boolean pendingToThirdApprover = false;
195 
196         for (ActionRequest request : document.getRootActionRequests()) {
197             if (request.getPrincipalId().equals(thirdApproverPrincipalId) && request.isActivated()) {
198                 assertFalse("Third approver has too many requests.", pendingToThirdApprover);
199                 pendingToThirdApprover = true;
200             } else {
201                 assertTrue("Previous requests to all others should be done.", request.isDone());
202             }
203         }
204 
205         assertTrue(document.isApprovalRequested());
206         document.approve("");
207     }
208 
209 }