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