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.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          System.out.println(drhv.getInitialRouteNodeInstances().size());
55  
56      	assertTrue("Document should be initiatied", document.isInitiated());
57      	Set<String> nodeNames = document.getNodeNames();
58      	assertEquals("Wrong number of node names.", 1, nodeNames.size());
59      	assertEquals("Wrong node name.", ADHOC_NODE, nodeNames.iterator().next());
60      	document.route("Routing sequentially.");
61          
62          // should have generated a request to "bmcgough"
63      	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
64          assertTrue("Document should be enroute", document.isEnroute());
65      	nodeNames = document.getNodeNames();
66      	assertEquals("Wrong number of node names.", 1, nodeNames.size());
67      	assertEquals("Wrong node name.", WORKFLOW_DOCUMENT_NODE, nodeNames.iterator().next());
68          List<ActionRequest> requests = document.getRootActionRequests();
69          assertEquals(1, requests.size());
70          ActionRequest request = requests.get(0);
71          assertEquals(getPrincipalIdForName("bmcgough"), request.getPrincipalId());
72          assertEquals(ActionRequestType.APPROVE, request.getActionRequested());
73          TestUtilities.assertAtNode(document, WORKFLOW_DOCUMENT_NODE);
74          assertTrue(document.isApprovalRequested());
75          document.approve("Test approve by bmcgough");
76          
77          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("temay"), document.getDocumentId());
78          assertTrue("Document should be processed.", document.isProcessed());
79          requests = document.getRootActionRequests();
80          assertEquals(3, requests.size());
81          boolean toTemay = false;
82          boolean toJhopf = false;
83          for (int i = 0; i < requests.size(); i++) {
84              ActionRequest requestVO = requests.get(i);
85              if (requestVO.getPrincipalId().equals(getPrincipalIdForName("temay"))) {
86                  toTemay = true;
87                  assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
88                  assertEquals(ACKNOWLEDGE_1_NODE, requestVO.getNodeName());
89                  assertEquals(ActionRequestStatus.ACTIVATED, requestVO.getStatus());
90              } else if (requestVO.getPrincipalId().equals(getPrincipalIdForName("jhopf"))) {
91                  toJhopf = true;
92                  assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
93                  assertEquals(ACKNOWLEDGE_2_NODE, requestVO.getNodeName());
94                  assertEquals(ActionRequestStatus.ACTIVATED, requestVO.getStatus());
95              }
96          }
97          assertTrue("Should be an acknowledge to temay", toTemay);
98          assertTrue("Should be an acknowledge to jhopf", toJhopf);
99  //        assertEquals(ACKNOWLEDGE_2_NODE, document.getRouteHeader().getNodeNames()[0]);
100         // have temay take her acknowledge
101         document.acknowledge("Temay taking acknowledge");
102         
103         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
104         assertTrue("Document should be processed.", document.isProcessed());
105         requests = document.getRootActionRequests();
106         toTemay = false;
107         toJhopf = false;
108         for (int i = 0; i < requests.size(); i++) {
109             ActionRequest requestVO = requests.get(i);
110             if (requestVO.getPrincipalId().equals(getPrincipalIdForName("temay"))) {
111                 toTemay = true;
112                 assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
113                 assertEquals(ActionRequestStatus.DONE, requestVO.getStatus());
114             } else if (requestVO.getPrincipalId().equals(getPrincipalIdForName("jhopf"))) {
115                 toJhopf = true;
116                 assertEquals(ActionRequestType.ACKNOWLEDGE, requestVO.getActionRequested());
117                 assertEquals(ACKNOWLEDGE_2_NODE, requestVO.getNodeName());
118                 assertEquals(ActionRequestStatus.ACTIVATED, requestVO.getStatus());
119             }
120         }
121         assertTrue("Should be a DONE acknowledge to temay", toTemay);
122         assertTrue("Should be an acknowledge to jhopf", toJhopf);
123         // have jhopf take his acknowledge, this should cause the document to go final
124         document.acknowledge("Jhopf taking acknowledge");
125         
126     	// TODO when we are able to, we should also verify the RouteNodeInstances are correct
127         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
128     	assertTrue("Document should be final.", document.isFinal());
129         
130         verifyRoutingPath(document.getDocumentId());
131     }        
132 
133     private void verifyRoutingPath(String documentId) {
134         DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
135         List initial = document.getInitialRouteNodeInstances();
136         assertEquals(1, initial.size());
137         RouteNodeInstance adhoc = (RouteNodeInstance)initial.get(0);
138         assertEquals(ADHOC_NODE, adhoc.getRouteNode().getRouteNodeName());
139         assertEquals(0, adhoc.getPreviousNodeInstances().size());
140         
141         List next = adhoc.getNextNodeInstances();
142         assertEquals(1, next.size());
143         RouteNodeInstance wd = (RouteNodeInstance)next.get(0);
144         assertEquals(WORKFLOW_DOCUMENT_NODE, wd.getRouteNode().getRouteNodeName());
145         assertEquals(1, wd.getPreviousNodeInstances().size());
146         
147         next = wd.getNextNodeInstances();
148         assertEquals(1, next.size());
149         RouteNodeInstance ack1 = (RouteNodeInstance)next.get(0);
150         assertEquals(ACKNOWLEDGE_1_NODE, ack1.getRouteNode().getRouteNodeName());
151         assertEquals(1, ack1.getPreviousNodeInstances().size());
152         
153         next = ack1.getNextNodeInstances();
154         assertEquals(1, next.size());
155         RouteNodeInstance ack2 = (RouteNodeInstance)next.get(0);
156         assertEquals(ACKNOWLEDGE_2_NODE, ack2.getRouteNode().getRouteNodeName());
157         assertEquals(1, ack2.getPreviousNodeInstances().size());
158         
159         next = ack2.getNextNodeInstances();
160         assertEquals(0, next.size());
161     }
162 
163 }