View Javadoc

1   /**
2    * Copyright 2005-2013 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.actions;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.util.Collection;
24  
25  import org.junit.Test;
26  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
27  import org.kuali.rice.kew.api.WorkflowDocument;
28  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
29  import org.kuali.rice.kew.api.action.InvalidActionTakenException;
30  import org.kuali.rice.kew.api.document.DocumentStatus;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.test.KEWTestCase;
33  
34  public class RouteDocumentTest extends KEWTestCase {
35  
36  	public static final String DOCUMENT_TYPE_NAME = "BlanketApproveSequentialTest";
37      public static final String DOCUMENT_TYPE_POLICY_TEST_NAME = "BlanketApprovePolicyTest";
38      
39      protected void loadTestData() throws Exception {
40          loadXmlFile("ActionsConfig.xml");
41      }
42          
43  	/**
44       * Tests that an exception is thrown if you try to execute a "route" command on an already routed document.
45       */
46      @Test public void testRouteAlreadyRoutedDocument() throws Exception {
47      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
48      	document.route("");
49      	
50      	assertTrue("Document should be ENROUTE.", document.isEnroute());
51      	assertFalse("There should not be a request to ewestfal.", document.isApprovalRequested());
52      	
53      	// verify that only 1 action taken has been performed
54      	Collection<ActionTakenValue> actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
55      	assertEquals("There should be only 1 action taken.", 1, actionTakens.size());
56      	
57      	// now try and route the document again, an exception should be thrown
58      	try {
59      		document.route("");
60      		fail("A WorkflowException should have been thrown.");
61      	} catch (InvalidActionTakenException e) {
62      		e.printStackTrace();
63      	}
64      	
65      	// verify that there is still only 1 action taken (the transaction above should have rolled back)
66      	actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
67      	assertEquals("There should still be only 1 action taken.", 1, actionTakens.size());
68      }
69  	
70      /**
71       * Tests that an exception is not thrown if you try to execute a "route" command on a document you did not initiate.
72       */
73      @Test public void testRouteDocumentAsNonInitiatorUser() throws Exception {
74          WorkflowDocument firstDocument = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_POLICY_TEST_NAME);
75          WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), firstDocument.getDocumentId());
76          try {
77              document.route("");
78          } catch (Exception e) {
79              e.printStackTrace();
80              fail("Exception thrown but should not have have been... Exception was of type " + e.getClass().getName() + " and message was " + e.getMessage());
81          }
82          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), firstDocument.getDocumentId());
83          assertEquals("Document should be in Enroute status.", DocumentStatus.ENROUTE, document.getStatus());
84  
85          // verify that there is 1 action taken
86          Collection<ActionTakenValue> actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
87          assertEquals("There should be 1 action taken.", 1, actionTakens.size());
88      }
89      
90      /**
91       * Tests that an exception is not thrown if you try to execute a "route" command on a document you did not initiate.
92       */
93      @Test public void testRouteDefaultDocumentAsNonInitiatorUser() throws Exception {
94          WorkflowDocument firstDocument = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
95          WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), firstDocument.getDocumentId());
96          try {
97              document.route("");
98              fail("Exception should have been thrown.");
99          } 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 }