View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.actions;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.Collection;
25  
26  import org.junit.Test;
27  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
28  import org.kuali.rice.kew.api.WorkflowDocument;
29  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
30  import org.kuali.rice.kew.api.action.InvalidActionTakenException;
31  import org.kuali.rice.kew.api.document.DocumentStatus;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.kew.test.KEWTestCase;
34  
35  public class RouteDocumentTest extends KEWTestCase {
36  
37  	public static final String DOCUMENT_TYPE_NAME = "BlanketApproveSequentialTest";
38      public static final String DOCUMENT_TYPE_POLICY_TEST_NAME = "BlanketApprovePolicyTest";
39      
40      protected void loadTestData() throws Exception {
41          loadXmlFile("ActionsConfig.xml");
42      }
43          
44  	/**
45       * Tests that an exception is thrown if you try to execute a "route" command on an already routed document.
46       */
47      @Test public void testRouteAlreadyRoutedDocument() throws Exception {
48      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
49      	document.route("");
50      	
51      	assertTrue("Document should be ENROUTE.", document.isEnroute());
52      	assertFalse("There should not be a request to ewestfal.", document.isApprovalRequested());
53      	
54      	// verify that only 1 action taken has been performed
55      	Collection<ActionTakenValue> actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
56      	assertEquals("There should be only 1 action taken.", 1, actionTakens.size());
57      	
58      	// now try and route the document again, an exception should be thrown
59      	try {
60      		document.route("");
61      		fail("A WorkflowException should have been thrown.");
62      	} catch (InvalidActionTakenException e) {
63      		e.printStackTrace();
64      	}
65      	
66      	// verify that there is still only 1 action taken (the transaction above should have rolled back)
67      	actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
68      	assertEquals("There should still be only 1 action taken.", 1, actionTakens.size());
69      }
70  	
71      /**
72       * Tests that an exception is not thrown if you try to execute a "route" command on a document you did not initiate.
73       */
74      @Test public void testRouteDocumentAsNonInitiatorUser() throws Exception {
75          WorkflowDocument firstDocument = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_POLICY_TEST_NAME);
76          WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), firstDocument.getDocumentId());
77          try {
78              document.route("");
79          } catch (Exception e) {
80              e.printStackTrace();
81              fail("Exception thrown but should not have have been... Exception was of type " + e.getClass().getName() + " and message was " + e.getMessage());
82          }
83          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), firstDocument.getDocumentId());
84          assertEquals("Document should be in Enroute status.", DocumentStatus.ENROUTE, document.getStatus());
85  
86          // verify that there is 1 action taken
87          Collection<ActionTakenValue> actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
88          assertEquals("There should be 1 action taken.", 1, actionTakens.size());
89      }
90      
91      /**
92       * Tests that an exception is not thrown if you try to execute a "route" command on a document you did not initiate.
93       */
94      @Test public void testRouteDefaultDocumentAsNonInitiatorUser() throws Exception {
95          WorkflowDocument firstDocument = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
96          WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), firstDocument.getDocumentId());
97          try {
98              document.route("");
99              fail("Exception should have been thrown.");
100         } catch (Exception e) {
101             e.printStackTrace();
102         }
103         assertFalse("Document should not be ENROUTE.", document.isEnroute());
104 //        assertFalse("There should not be a request to user2.", document.isApprovalRequested());
105 //        
106 //        // verify that there are no actions taken
107 //        Collection actionTakens = KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
108 //        assertEquals("There should be 0 actions taken.", 0, actionTakens.size());
109     }
110     
111 }