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.clientapp;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.api.WorkflowDocument;
20  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
21  import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
22  import org.kuali.rice.kew.test.KEWTestCase;
23  
24  import java.util.List;
25  import java.util.Set;
26  
27  import static org.junit.Assert.*;
28  
29  /**
30   * Place to test WorkflowDocument.
31   *
32   */
33  public class WorkflowDocumentTest extends KEWTestCase {
34  
35      protected void loadTestData() throws Exception {
36          loadXmlFile("ClientAppConfig.xml");
37      }
38  
39      @Test public void testLoadNonExistentDocument() throws Exception {
40      	try {
41      		WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), "123456789");
42      		fail("load of non-existent document should have thrown IllegalArgumentException");
43      	} catch (IllegalArgumentException e) {}
44      }
45  
46      @Test public void testWorkflowDocument() throws Exception {
47          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
48          document.route("");
49  
50          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
51          document.approve("");
52  
53          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
54          document.approve("");
55  
56          boolean containsInitiated = false;
57          boolean containsTemplate1 = false;
58          boolean containsTemplate2 = false;
59          for (RouteNodeInstance routeNodeInstance : document.getRouteNodeInstances()) {
60              if (routeNodeInstance.getName().equals("Initiated")) {
61                  containsInitiated = true;
62              } else if (routeNodeInstance.getName().equals("Template1")) {
63                  containsTemplate1 = true;
64              } else if (routeNodeInstance.getName().equals("Template2")) {
65                  containsTemplate2 = true;
66              }
67          }
68  
69          assertTrue("Should have gone through initiated node", containsInitiated);
70          assertTrue("Should have gone through template1 node", containsTemplate1);
71          assertTrue("Should have gone through template2 node", containsTemplate2);
72      }
73  
74      /**
75       * Test that the document is being updated appropriately after a return to previous call
76       *
77       * @throws Exception
78       */
79      @Test public void testReturnToPreviousCorrectlyUpdatingDocumentStatus() throws Exception {
80  
81          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
82          document.route("");
83  
84          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
85          document.returnToPreviousNode("Initiated", "");
86  
87          assertFalse("ewestfal should no longer have approval status", document.isApprovalRequested());
88          assertFalse("ewestfal should no long have blanket approve status", document.isBlanketApproveCapable());
89  
90          //just for good measure
91          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
92          assertTrue("rkirkend should now have an approve request", document.isApprovalRequested());
93      }
94  
95      @Test public void testGetPreviousRouteNodeNames() throws Exception {
96  
97      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
98          document.route("");
99  
100         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
101         document.approve("");
102 
103         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
104         List<String> previousNodeNames = document.getPreviousNodeNames();
105         assertEquals("Should have 2 previous Node Names", 2, previousNodeNames.size());
106         assertEquals("Last node name should be the first visisted", "Initiated", previousNodeNames.get(0));
107         assertEquals("First node name should be last node visited", "Template1", previousNodeNames.get(1));
108         Set<String> currentNodes = document.getNodeNames();
109         assertEquals("Should have 1 current node name", 1, currentNodes.size());
110         assertEquals("Current node name incorrect", "Template2", currentNodes.iterator().next());
111         document.returnToPreviousNode("Template1", "");
112         previousNodeNames = document.getPreviousNodeNames();
113         assertEquals("Should have 1 previous Node Name", 1, previousNodeNames.size());
114         assertEquals("Previous Node name incorrect", "Initiated", previousNodeNames.get(0));
115 
116     }
117 
118     @Test public void testIsRouteCapable() throws Exception {
119 
120     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
121 
122     	verifyIsRouteCapable(false, getPrincipalIdForName("ewestfal"), doc.getDocumentId());
123     	verifyIsRouteCapable(false, "2001", doc.getDocumentId());
124 
125     	verifyIsRouteCapable(true, getPrincipalIdForName("rkirkend"), doc.getDocumentId());
126     	verifyIsRouteCapable(true, "2002", doc.getDocumentId());
127 
128         doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "NonInitiatorCanRouteDocument");
129 
130         verifyIsRouteCapable(true, getPrincipalIdForName("ewestfal"), doc.getDocumentId());
131         verifyIsRouteCapable(true, "2001", doc.getDocumentId());
132 
133         verifyIsRouteCapable(true, getPrincipalIdForName("rkirkend"), doc.getDocumentId());
134         verifyIsRouteCapable(true, "2002", doc.getDocumentId());
135     }
136 
137     private void verifyIsRouteCapable(boolean routeCapable, String userId, String docId) throws Exception {
138     	WorkflowDocument doc = WorkflowDocumentFactory.loadDocument(userId, docId);
139     	assertEquals(routeCapable, doc.isRouteCapable());
140     }
141 
142 }