001/**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.routelog.web;
017
018import static org.junit.Assert.assertFalse;
019import static org.junit.Assert.assertNotNull;
020import static org.junit.Assert.assertTrue;
021import static org.junit.Assert.fail;
022
023import java.util.Collection;
024import java.util.List;
025
026import org.junit.Test;
027import org.kuali.rice.kew.actionrequest.ActionRequestValue;
028import org.kuali.rice.kew.actions.AcknowledgeAction;
029import org.kuali.rice.kew.actiontaken.ActionTakenValue;
030import org.kuali.rice.kew.api.WorkflowDocument;
031import org.kuali.rice.kew.api.WorkflowDocumentFactory;
032import org.kuali.rice.kew.api.exception.WorkflowException;
033import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
034import org.kuali.rice.kew.service.KEWServiceLocator;
035import org.kuali.rice.kew.test.KEWTestCase;
036
037/**
038 * Unit tests for RouteLogAction -- very incomplete, only tests populateRouteLogFutureRequests
039 * 
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 *
042 */
043public class RouteLogActionTest extends KEWTestCase
044{
045    RouteLogAction routeLogAction = new RouteLogAction();
046
047    protected void loadTestData() throws Exception 
048    {
049        loadXmlFile(AcknowledgeAction.class, "ActionsConfig.xml");
050    }
051    
052        /**
053     * Test that existing action requests don't show up in future list (KULRICE-2641)
054     */
055    @SuppressWarnings("unchecked")
056        @Test public void testPopulateRouteLogFutureRequests_HasNoExistingRequests() throws Exception {
057        
058        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), getClass().getSimpleName());
059        document.route("1 - user1 route");
060        verifyFutureRequestState(document);
061
062        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
063        document.approve("2 - ewestfal approve");
064        verifyFutureRequestState(document);
065
066        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
067        document.approve("3 - user2 approve");
068        verifyFutureRequestState(document);
069
070        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user3"), document.getDocumentId());
071        document.acknowledge("4 - user3 acknowledge");
072        verifyFutureRequestState(document);
073    }
074
075        /**
076         * This method runs RouteLogAction.populateRouteLogFutureRequests then checks that the future actions 
077         * aren't "actions taken" or "actions pending".  It also checks that the total number of action requests
078         * add up (taken + pending + future == total)
079         * 
080         * @param document
081         * @throws WorkflowException
082         * @throws Exception
083         */
084        private void verifyFutureRequestState(WorkflowDocument document)
085                        throws WorkflowException, Exception {
086                Collection<ActionTakenValue> actionsTaken = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
087        Collection<ActionRequestValue> actionsPending = KEWServiceLocator.getActionRequestService().findPendingByDoc(document.getDocumentId());
088        
089        DocumentRouteHeaderValue docRouteHeaderValue = DocumentRouteHeaderValue.from(document.getDocument());
090        RouteLogForm routeLogForm = new RouteLogForm();
091        routeLogAction.populateRouteLogFutureRequests(routeLogForm, docRouteHeaderValue);
092        List<ActionRequestValue> futureRootRequests = routeLogForm.getFutureRootRequests();
093        
094        int takenRequestCount = 0;
095        // check that the actions taken aren't in the future list
096        for (ActionTakenValue actionTaken : actionsTaken) {
097                if (actionTaken.getActionRequests() != null) {
098                        for (ActionRequestValue actionRequestTaken : actionTaken.getActionRequests()) {
099                                ++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}