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.actions;
17  
18  import static org.junit.Assert.assertTrue;
19  import static org.junit.Assert.fail;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.junit.Test;
26  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
27  import org.kuali.rice.kew.actions.BlanketApproveTest.NotifySetup;
28  import org.kuali.rice.kew.api.WorkflowDocument;
29  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kew.test.KEWTestCase;
32  
33  public class RoleTest extends KEWTestCase {
34  
35      protected void loadTestData() throws Exception {
36          loadXmlFile("ActionsConfig.xml");
37      }
38  
39      @Test public void testRoleRequestGeneration() throws Exception {
40          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
41          document.route("");
42          
43          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
44          assertTrue("This user should have an approve request", document.isApprovalRequested());
45          document.approve("");
46          
47          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
48          assertTrue("This user should have an approve request", document.isApprovalRequested());
49          document.approve("");//ewestfal had force action rule
50          
51          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
52          assertTrue("This user should have an approve request", document.isApprovalRequested());
53          document.approve("");
54          
55          //this be the role delegate of jitrue
56          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("natjohns"), document.getDocumentId());
57          assertTrue("This user should have an approve request", document.isApprovalRequested());
58          document.approve("");
59          
60          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
61          document.approve("");
62          
63          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("xqi"), document.getDocumentId());
64          document.acknowledge("");
65          
66          assertTrue("Document should be final", document.isFinal());
67  
68          List requests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(document.getDocumentId());
69          List rootRequests = KEWServiceLocator.getActionRequestService().getRootRequests(requests);
70          
71          //verify our requests have been made correctly
72          for (Iterator iter = rootRequests.iterator(); iter.hasNext();) {
73  			ActionRequestValue request = (ActionRequestValue) iter.next();
74  			if (request.isRoleRequest()) {
75  				//direct children should not be role requests
76  				iterateChildrenRequests(request.getChildrenRequests(), new String[] {"U", "W"}, request);
77  			}
78  		}
79      }
80  	
81      private void iterateChildrenRequests(Collection childrenRequests, String[] requestTypes, ActionRequestValue parentRequest) {
82      	for (Iterator iter = childrenRequests.iterator(); iter.hasNext();) {
83  			ActionRequestValue request = (ActionRequestValue) iter.next();
84  			boolean matched = false;
85  			for (int i = 0; i < requestTypes.length; i++) {
86  				if (request.getRecipientTypeCd().equals(requestTypes[i])) {
87  					matched = true;
88  				}
89  			}
90  			if (!matched) {
91  				fail("Didn't find request of types expected Recipient Type: " + parentRequest.getRecipientTypeCd() + " RoleName: " + parentRequest.getRoleName() + " Qualified Role Name:" + parentRequest.getQualifiedRoleName() + " RuleId: " + parentRequest.getRuleBaseValuesId());
92  			}
93  			//if this is a role then it can't have a child role
94  			if (request.isRoleRequest()) {
95  				//direct children should not be role requests
96  				iterateChildrenRequests(request.getChildrenRequests(), new String[] {"U", "W"}, request);
97  			}
98  		}
99      }
100 }