001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.kew.actions;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertFalse;
020    import static org.junit.Assert.assertTrue;
021    import static org.junit.Assert.fail;
022    
023    import java.util.Collection;
024    
025    import org.junit.Test;
026    import org.kuali.rice.kew.actiontaken.ActionTakenValue;
027    import org.kuali.rice.kew.api.WorkflowDocument;
028    import org.kuali.rice.kew.api.WorkflowDocumentFactory;
029    import org.kuali.rice.kew.api.action.InvalidActionTakenException;
030    import org.kuali.rice.kew.api.document.DocumentStatus;
031    import org.kuali.rice.kew.service.KEWServiceLocator;
032    import org.kuali.rice.kew.test.KEWTestCase;
033    
034    public class RouteDocumentTest extends KEWTestCase {
035    
036            public static final String DOCUMENT_TYPE_NAME = "BlanketApproveSequentialTest";
037        public static final String DOCUMENT_TYPE_POLICY_TEST_NAME = "BlanketApprovePolicyTest";
038        
039        protected void loadTestData() throws Exception {
040            loadXmlFile("ActionsConfig.xml");
041        }
042            
043            /**
044         * Tests that an exception is thrown if you try to execute a "route" command on an already routed document.
045         */
046        @Test public void testRouteAlreadyRoutedDocument() throws Exception {
047            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
048            document.route("");
049            
050            assertTrue("Document should be ENROUTE.", document.isEnroute());
051            assertFalse("There should not be a request to ewestfal.", document.isApprovalRequested());
052            
053            // verify that only 1 action taken has been performed
054            Collection<ActionTakenValue> actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
055            assertEquals("There should be only 1 action taken.", 1, actionTakens.size());
056            
057            // now try and route the document again, an exception should be thrown
058            try {
059                    document.route("");
060                    fail("A WorkflowException should have been thrown.");
061            } catch (InvalidActionTakenException e) {
062                    e.printStackTrace();
063            }
064            
065            // verify that there is still only 1 action taken (the transaction above should have rolled back)
066            actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
067            assertEquals("There should still be only 1 action taken.", 1, actionTakens.size());
068        }
069            
070        /**
071         * Tests that an exception is not thrown if you try to execute a "route" command on a document you did not initiate.
072         */
073        @Test public void testRouteDocumentAsNonInitiatorUser() throws Exception {
074            WorkflowDocument firstDocument = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_POLICY_TEST_NAME);
075            WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), firstDocument.getDocumentId());
076            try {
077                document.route("");
078            } catch (Exception e) {
079                e.printStackTrace();
080                fail("Exception thrown but should not have have been... Exception was of type " + e.getClass().getName() + " and message was " + e.getMessage());
081            }
082            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), firstDocument.getDocumentId());
083            assertEquals("Document should be in Enroute status.", DocumentStatus.ENROUTE, document.getStatus());
084    
085            // verify that there is 1 action taken
086            Collection<ActionTakenValue> actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
087            assertEquals("There should be 1 action taken.", 1, actionTakens.size());
088        }
089        
090        /**
091         * Tests that an exception is not thrown if you try to execute a "route" command on a document you did not initiate.
092         */
093        @Test public void testRouteDefaultDocumentAsNonInitiatorUser() throws Exception {
094            WorkflowDocument firstDocument = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
095            WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), firstDocument.getDocumentId());
096            try {
097                document.route("");
098                fail("Exception should have been thrown.");
099            } catch (Exception e) {
100                e.printStackTrace();
101            }
102            assertFalse("Document should not be ENROUTE.", document.isEnroute());
103    //        assertFalse("There should not be a request to user2.", document.isApprovalRequested());
104    //        
105    //        // verify that there are no actions taken
106    //        Collection actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
107    //        assertEquals("There should be 0 actions taken.", 0, actionTakens.size());
108        }
109        
110    }