View Javadoc

1   /*
2    * Copyright 2007-2009 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.routelog.web;
17  
18  import java.util.Collection;
19  import java.util.List;
20  
21  import org.junit.Test;
22  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
23  import org.kuali.rice.kew.actions.AcknowledgeAction;
24  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
25  import org.kuali.rice.kew.dto.DTOConverter;
26  import org.kuali.rice.kew.exception.WorkflowException;
27  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.service.WorkflowDocument;
30  import org.kuali.rice.kew.test.KEWTestCase;
31  
32  
33  /**
34   * Unit tests for RouteLogAction -- very incomplete, only tests populateRouteLogFutureRequests
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class RouteLogActionTest extends KEWTestCase
40  {
41      RouteLogAction routeLogAction = new RouteLogAction();
42  
43      protected void loadTestData() throws Exception 
44      {
45          loadXmlFile(AcknowledgeAction.class, "ActionsConfig.xml");
46      }
47      
48  	/**
49       * Test that existing action requests don't show up in future list (KULRICE-2641)
50       */
51      @SuppressWarnings("unchecked")
52  	@Test public void testPopulateRouteLogFutureRequests_HasNoExistingRequests() throws Exception {
53      	
54      	WorkflowDocument document = new WorkflowDocument(getPrincipalIdForName("user1"), getClass().getSimpleName());
55      	document.routeDocument("1 - user1 route");
56      	verifyFutureRequestState(document);
57  
58      	document = new WorkflowDocument(getPrincipalIdForName("ewestfal"), document.getRouteHeaderId());
59      	document.approve("2 - ewestfal approve");
60      	verifyFutureRequestState(document);
61  
62      	document = new WorkflowDocument(getPrincipalIdForName("user2"), document.getRouteHeaderId());
63      	document.approve("3 - user2 approve");
64      	verifyFutureRequestState(document);
65  
66      	document = new WorkflowDocument(getPrincipalIdForName("user3"), document.getRouteHeaderId());
67      	document.acknowledge("4 - user3 acknowledge");
68      	verifyFutureRequestState(document);
69      }
70  
71  	/**
72  	 * This method runs RouteLogAction.populateRouteLogFutureRequests then checks that the future actions 
73  	 * aren't "actions taken" or "actions pending".  It also checks that the total number of action requests
74  	 * add up (taken + pending + future == total)
75  	 * 
76  	 * @param document
77  	 * @throws WorkflowException
78  	 * @throws Exception
79  	 */
80  	private void verifyFutureRequestState(WorkflowDocument document)
81  			throws WorkflowException, Exception {
82  		Collection<ActionTakenValue> actionsTaken = KEWServiceLocator.getActionTakenService().findByRouteHeaderId(document.getRouteHeaderId());
83      	Collection<ActionRequestValue> actionsPending = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getRouteHeaderId());
84      	
85      	DocumentRouteHeaderValue docRouteHeaderValue = DTOConverter.convertRouteHeaderVO(document.getRouteHeader());
86      	RouteLogForm routeLogForm = new RouteLogForm();
87      	routeLogAction.populateRouteLogFutureRequests(routeLogForm, docRouteHeaderValue);
88          List<ActionRequestValue> futureRootRequests = routeLogForm.getFutureRootRequests();
89          
90          int takenRequestCount = 0;
91          // check that the actions taken aren't in the future list
92          for (ActionTakenValue actionTaken : actionsTaken) {
93          	if (actionTaken.getActionRequests() != null) {
94          		for (ActionRequestValue actionRequestTaken : actionTaken.getActionRequests()) {
95          			++takenRequestCount;
96          			for (ActionRequestValue futureRequest : futureRootRequests) {
97          				assertFalse("action taken is in futureRootRequests",
98          						futureRequest.getActionRequestId().equals(actionRequestTaken.getActionRequestId()));
99          			}
100         		}
101         	}
102         }
103 
104         int pendingRequestsCount = 0;
105         // check that the pending requests aren't in the future list
106         for (ActionRequestValue pendingAction : actionsPending) {
107         	++pendingRequestsCount;
108         	for (ActionRequestValue futureRequest : futureRootRequests) {
109         		assertFalse("action taken is in futureRootRequests",
110         				futureRequest.getActionRequestId().equals(pendingAction.getActionRequestId()));
111         	}
112         }
113         
114         // there are 3 route nodes for this document, not counting AdHoc
115         assertTrue("taken + pending + future == 3", takenRequestCount + pendingRequestsCount + futureRootRequests.size() == 3);
116         
117         recursiveValidate(routeLogForm.getFutureRootRequests());
118 	}
119 
120 	/**
121      * Test that a document that will have no future action requests is handled without exception (KULRICE-2838)
122      */
123     @Test public void testPopulateRouteLogFutureRequests_ZeroFutureRequests() throws Exception {
124     	String user1PrincipalId = getPrincipalIdForName("ewestfal");
125 
126     	RouteLogForm routeLogForm = new RouteLogForm();
127 
128     	WorkflowDocument document = new WorkflowDocument(user1PrincipalId, "RouteLogActionTestTrivial");
129     	// for this simple doc type, no future requests
130     	document.routeDocument("");
131     	
132     	DocumentRouteHeaderValue docRouteHeaderValue = DTOConverter.convertRouteHeaderVO(document.getRouteHeader());
133 
134     	try {
135     		routeLogAction.populateRouteLogFutureRequests(routeLogForm, docRouteHeaderValue);
136     	} catch (Exception e) {
137     		fail("calculating future requests where there will be none should not be a problem");
138     	}
139         
140         assertTrue("We're expecting 0 future action requests",routeLogForm.getFutureActionRequestCount()==0);
141         assertTrue("We're expecting 0 future action requests",
142         		routeLogForm.getFutureRootRequests() == null || routeLogForm.getFutureRootRequests().size() == 0);
143     }
144     
145 	/**
146 	 * This method recurses through the action requests and checks that values are set
147 	 * appropriately for display in the Future Action Requests section of the Route Log.
148 	 * 
149 	 * @param actionRequestValues
150 	 */
151 	private void recursiveValidate(List<ActionRequestValue> actionRequestValues) {
152 		if (actionRequestValues != null) for (ActionRequestValue actionRequestValue : actionRequestValues) {
153 			assertNotNull(actionRequestValue.getActionRequested());
154 			assertNotNull(actionRequestValue.getActionRequestedLabel());
155 			assertNotNull(actionRequestValue.getNodeInstance());
156 			assertNotNull(actionRequestValue.getNodeInstance().getName());
157 			assertNotNull(actionRequestValue.getNodeInstance().getRouteNode());
158 			assertNotNull(actionRequestValue.getNodeInstance().getRouteNode().getNodeType());
159 
160 			recursiveValidate(actionRequestValue.getChildrenRequests());
161 		}
162 	}
163 
164 }