View Javadoc
1   /**
2    * Copyright 2005-2014 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 testDirtyContent() {
40          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
41          document.setApplicationContent("application content");
42          document.setAttributeContent("attribute content");
43          document.setSearchableContent("searchable content");
44          assertEquals("application content", document.getApplicationContent());
45          assertEquals("application content", document.getDocumentContent().getApplicationContent());
46          assertEquals("attribute content", document.getAttributeContent());
47          assertEquals("attribute content", document.getDocumentContent().getAttributeContent());
48          assertEquals("searchable content", document.getDocumentContent().getSearchableContent());
49      }
50  
51      @Test public void testLoadNonExistentDocument() throws Exception {
52      	try {
53      		WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), "123456789");
54      		fail("load of non-existent document should have thrown IllegalArgumentException");
55      	} catch (IllegalArgumentException e) {}
56      }
57  
58      @Test public void testWorkflowDocument() throws Exception {
59          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
60          document.route("");
61  
62          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
63          document.approve("");
64  
65          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
66          document.approve("");
67  
68          boolean containsInitiated = false;
69          boolean containsTemplate1 = false;
70          boolean containsTemplate2 = false;
71          for (RouteNodeInstance routeNodeInstance : document.getRouteNodeInstances()) {
72              if (routeNodeInstance.getName().equals("Initiated")) {
73                  containsInitiated = true;
74              } else if (routeNodeInstance.getName().equals("Template1")) {
75                  containsTemplate1 = true;
76              } else if (routeNodeInstance.getName().equals("Template2")) {
77                  containsTemplate2 = true;
78              }
79          }
80  
81          assertTrue("Should have gone through initiated node", containsInitiated);
82          assertTrue("Should have gone through template1 node", containsTemplate1);
83          assertTrue("Should have gone through template2 node", containsTemplate2);
84      }
85  
86      /**
87       * Test that the document is being updated appropriately after a return to previous call
88       *
89       * @throws Exception
90       */
91      @Test public void testReturnToPreviousCorrectlyUpdatingDocumentStatus() throws Exception {
92  
93          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
94          document.route("");
95  
96          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
97          document.returnToPreviousNode("", "Initiated");
98  
99          assertFalse("ewestfal should no longer have approval status", document.isApprovalRequested());
100         assertFalse("ewestfal should no long have blanket approve status", document.isBlanketApproveCapable());
101 
102         //just for good measure
103         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
104         assertTrue("rkirkend should now have an approve request", document.isApprovalRequested());
105     }
106 
107     @Test public void testGetPreviousRouteNodeNames() throws Exception {
108 
109     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
110         document.route("");
111 
112         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
113         document.approve("");
114 
115         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
116         List<String> previousNodeNames = document.getPreviousNodeNames();
117         assertEquals("Should have 2 previous Node Names", 2, previousNodeNames.size());
118         assertEquals("Last node name should be the first visisted", "Initiated", previousNodeNames.get(0));
119         assertEquals("First node name should be last node visited", "Template1", previousNodeNames.get(1));
120         Set<String> currentNodes = document.getNodeNames();
121         assertEquals("Should have 1 current node name", 1, currentNodes.size());
122         assertEquals("Current node name incorrect", "Template2", currentNodes.iterator().next());
123         document.returnToPreviousNode("", "Template1");
124         previousNodeNames = document.getPreviousNodeNames();
125         assertEquals("Should have 1 previous Node Name", 1, previousNodeNames.size());
126         assertEquals("Previous Node name incorrect", "Initiated", previousNodeNames.get(0));
127 
128     }
129 
130     @Test public void testIsRouteCapable() throws Exception {
131 
132     	WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
133 
134     	verifyIsRouteCapable(false, getPrincipalIdForName("ewestfal"), doc.getDocumentId());
135     	verifyIsRouteCapable(false, "2001", doc.getDocumentId());
136 
137     	verifyIsRouteCapable(true, getPrincipalIdForName("rkirkend"), doc.getDocumentId());
138     	verifyIsRouteCapable(true, "2002", doc.getDocumentId());
139 
140         doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "NonInitiatorCanRouteDocument");
141 
142         verifyIsRouteCapable(true, getPrincipalIdForName("ewestfal"), doc.getDocumentId());
143         verifyIsRouteCapable(true, "2001", doc.getDocumentId());
144 
145         verifyIsRouteCapable(true, getPrincipalIdForName("rkirkend"), doc.getDocumentId());
146         verifyIsRouteCapable(true, "2002", doc.getDocumentId());
147     }
148 
149     private void verifyIsRouteCapable(boolean routeCapable, String userId, String docId) throws Exception {
150     	WorkflowDocument doc = WorkflowDocumentFactory.loadDocument(userId, docId);
151     	assertEquals(routeCapable, doc.isRouteCapable());
152     }
153 
154 }