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.engine;
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.action.ActionRequest;
22  import org.kuali.rice.kew.api.action.ActionRequestStatus;
23  import org.kuali.rice.kew.api.action.ActionRequestType;
24  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
25  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kew.test.KEWTestCase;
28  import org.kuali.rice.kew.test.TestUtilities;
29  
30  import java.util.List;
31  import java.util.Set;
32  
33  import static org.junit.Assert.*;
34  
35  public class SequentialRoutingTest extends KEWTestCase {
36      
37      
38      private static final String DOCUMENT_TYPE_NAME = "SeqDocType";
39  	private static final String ADHOC_NODE = "AdHoc";
40  	private static final String WORKFLOW_DOCUMENT_NODE = "WorkflowDocument";
41      private static final String ACKNOWLEDGE_1_NODE = "Acknowledge1";
42      private static final String ACKNOWLEDGE_2_NODE = "Acknowledge2";
43  	    
44      protected void loadTestData() throws Exception {
45          loadXmlFile("EngineConfig.xml");
46      }
47  
48      @Test public void testSequentialRoute() throws Exception {
49      	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCUMENT_TYPE_NAME);
50      	document.saveDocumentData();
51      	assertNotNull(document.getDocumentId());
52  
53          DocumentRouteHeaderValue drhv = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
54  
55      	assertTrue("Document should be initiatied", document.isInitiated());
56      	Set<String> nodeNames = document.getNodeNames();
57      	assertEquals("Wrong number of node names.", 1, nodeNames.size());
58      	assertEquals("Wrong node name.", ADHOC_NODE, nodeNames.iterator().next());
59      	document.route("Routing sequentially.");
60          
61          // should have generated a request to "bmcgough"
62      	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
63          assertTrue("Document should be enroute", document.isEnroute());
64      	nodeNames = document.getNodeNames();
65      	assertEquals("Wrong number of node names.", 1, nodeNames.size());
66      	assertEquals("Wrong node name.", WORKFLOW_DOCUMENT_NODE, nodeNames.iterator().next());
67          List<ActionRequest> requests = document.getRootActionRequests();
68          assertEquals(1, requests.size());
69          ActionRequest request = requests.get(0);
70          assertEquals(getPrincipalIdForName("bmcgough"), request.getPrincipalId());
71          assertEquals(ActionRequestType.APPROVE, request.getActionRequested());
72          TestUtilities.assertAtNode(document, WORKFLOW_DOCUMENT_NODE);
73          assertTrue(document.isApprovalRequested());
74          document.approve("Test approve by bmcgough");
75          
76          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("temay"), document.getDocumentId());
77          assertTrue("Document should be processed.", document.isProcessed());
78          requests = document.getRootActionRequests();
79          assertEquals(3, requests.size());
80          boolean toTemay = false;
81          boolean toJhopf = false;
82          for (int i = 0; i < requests.size(); i++) {
83              ActionRequest requestVO = requests.get(i);
84              if (requestVO.getPrincipalId().equals(getPrincipalIdForName("temay"))) {
85                  toTemay = true;
86                  assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
87                  assertEquals(ACKNOWLEDGE_1_NODE, requestVO.getNodeName());
88                  assertEquals(ActionRequestStatus.ACTIVATED, requestVO.getStatus());
89              } else if (requestVO.getPrincipalId().equals(getPrincipalIdForName("jhopf"))) {
90                  toJhopf = true;
91                  assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
92                  assertEquals(ACKNOWLEDGE_2_NODE, requestVO.getNodeName());
93                  assertEquals(ActionRequestStatus.ACTIVATED, requestVO.getStatus());
94              }
95          }
96          assertTrue("Should be an acknowledge to temay", toTemay);
97          assertTrue("Should be an acknowledge to jhopf", toJhopf);
98  //        assertEquals(ACKNOWLEDGE_2_NODE, document.getRouteHeader().getNodeNames()[0]);
99          // have temay take her acknowledge
100         document.acknowledge("Temay taking acknowledge");
101         
102         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
103         assertTrue("Document should be processed.", document.isProcessed());
104         requests = document.getRootActionRequests();
105         toTemay = false;
106         toJhopf = false;
107         for (int i = 0; i < requests.size(); i++) {
108             ActionRequest requestVO = requests.get(i);
109             if (requestVO.getPrincipalId().equals(getPrincipalIdForName("temay"))) {
110                 toTemay = true;
111                 assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
112                 assertEquals(ActionRequestStatus.DONE, requestVO.getStatus());
113             } else if (requestVO.getPrincipalId().equals(getPrincipalIdForName("jhopf"))) {
114                 toJhopf = true;
115                 assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
116                 assertEquals(ACKNOWLEDGE_2_NODE, requestVO.getNodeName());
117                 assertEquals(ActionRequestStatus.ACTIVATED, requestVO.getStatus());
118             }
119         }
120         assertTrue("Should be a DONE acknowledge to temay", toTemay);
121         assertTrue("Should be an acknowledge to jhopf", toJhopf);
122         // have jhopf take his acknowledge, this should cause the document to go final
123         document.acknowledge("Jhopf taking acknowledge");
124         
125     	// TODO when we are able to, we should also verify the RouteNodeInstances are correct
126         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
127     	assertTrue("Document should be final.", document.isFinal());
128         
129         verifyRoutingPath(document.getDocumentId());
130     }        
131 
132     private void verifyRoutingPath(String documentId) {
133         DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
134         List initial = document.getInitialRouteNodeInstances();
135         assertEquals(1, initial.size());
136         RouteNodeInstance adhoc = (RouteNodeInstance)initial.get(0);
137         assertEquals(ADHOC_NODE, adhoc.getRouteNode().getRouteNodeName());
138         assertEquals(0, adhoc.getPreviousNodeInstances().size());
139         
140         List next = adhoc.getNextNodeInstances();
141         assertEquals(1, next.size());
142         RouteNodeInstance wd = (RouteNodeInstance)next.get(0);
143         assertEquals(WORKFLOW_DOCUMENT_NODE, wd.getRouteNode().getRouteNodeName());
144         assertEquals(1, wd.getPreviousNodeInstances().size());
145         
146         next = wd.getNextNodeInstances();
147         assertEquals(1, next.size());
148         RouteNodeInstance ack1 = (RouteNodeInstance)next.get(0);
149         assertEquals(ACKNOWLEDGE_1_NODE, ack1.getRouteNode().getRouteNodeName());
150         assertEquals(1, ack1.getPreviousNodeInstances().size());
151         
152         next = ack1.getNextNodeInstances();
153         assertEquals(1, next.size());
154         RouteNodeInstance ack2 = (RouteNodeInstance)next.get(0);
155         assertEquals(ACKNOWLEDGE_2_NODE, ack2.getRouteNode().getRouteNodeName());
156         assertEquals(1, ack2.getPreviousNodeInstances().size());
157         
158         next = ack2.getNextNodeInstances();
159         assertEquals(0, next.size());
160     }
161 
162 }