View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.rule;
18  
19  import mocks.MockDocumentRequeuerImpl;
20  import org.junit.Test;
21  import org.kuali.rice.kew.api.WorkflowDocument;
22  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
23  import org.kuali.rice.kew.api.action.DelegationType;
24  import org.kuali.rice.kew.service.KEWServiceLocator;
25  import org.kuali.rice.kew.test.KEWTestCase;
26  import org.kuali.rice.kew.util.KEWConstants;
27  import org.kuali.rice.kim.api.identity.principal.Principal;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  
30  import java.util.List;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   * Tests adding a delegation rule
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class AddRuleDelegationTest extends KEWTestCase {
39  	
40  	private static final String DELEGATE_USER = "user2";
41  	private static final String DELEGATE_USER2 = "pmckown";
42  
43  	private static final String DOCTYPE = "AddDelegationTest_DocType";
44  	private static final String RULE_TEMPLATE = "AddDelegationTest_RuleTemplate";
45  	private static final String DELEGATION_TEMPLATE = "AddDelegationTest_DelegationTemplate";
46  
47  	protected void loadTestData() throws Exception {
48  		loadXmlFile("AddRuleDelegationTestData.xml");
49  	}
50  	
51  	/**
52  	 * 
53  	 * Tests that adding a delegation for a rule for which a document has a pending action request causes
54  	 * the document to be requeued. See KULRICE-3575
55  	 * 
56  	 * @throws Exception
57  	 */
58      @Test public void testNewDelegationTriggersRequeue() throws Exception {
59      	String docType = "RiceDocument.testNewDelegationTriggersRequeue";
60      	
61      	// route a document of this type
62      	WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
63      	wd.route("");
64      	
65      	// clear the current set of requeued document ids
66  		MockDocumentRequeuerImpl.clearRequeuedDocumentIds();
67      	
68      	// create and save a rule delegation 
69  		RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
70      	
71  		assertTrue("our document should have been requeued!", 
72  				MockDocumentRequeuerImpl.getRequeuedDocumentIds().contains(wd.getDocumentId()));
73      }
74  
75  
76  	/**
77  	 * Tests adding a delegation rule.  The implementation is mostly a cut-and-paste copy of
78  	 * createDelegateRule and routeRule methods from DelegatRule2Action Struts action.
79  	 */
80  	@Test
81  	public void testAddRuleDelegation() throws Exception {
82  
83  		RuleBaseValues originalRule = RuleTestUtils.getRule(DOCTYPE, RULE_TEMPLATE);
84  		
85      	List<RuleResponsibility> originalResps = originalRule.getResponsibilities();
86      	assertTrue("assuming there is 1 responsibility", originalResps != null && originalResps.size() == 1);
87      	
88      	RuleResponsibility originalResp = originalResps.get(0);
89      	
90  		RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
91  
92  		Principal principal2 = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER);
93  
94  		// check the original rule, it should be the same (i.e. not be re-versioned as KEW used to do pre 1.0 when a delegate was added)
95  		originalRule = KEWServiceLocator.getRuleService().findRuleBaseValuesById(originalRule.getRuleBaseValuesId());
96  		assertTrue("Original rule should be current.", originalRule.getCurrentInd());
97  		List<RuleResponsibility> responsibilities = originalRule.getResponsibilities();
98  		originalResp = responsibilities.get(0);
99  		assertEquals("Original rule should have 1 delegation now.", 1, originalResp.getDelegationRules().size());
100 
101 		List<RuleDelegation> newRuleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
102 		assertEquals("Should be 1 delegation", 1, newRuleDelegations.size());
103 
104 		RuleDelegation newRuleDelegation = newRuleDelegations.get(0);
105 		assertEquals("Incorrect responsibility id", originalResp.getResponsibilityId(), newRuleDelegation.getResponsibilityId());
106 		assertNotNull("Name should not be null", newRuleDelegation.getDelegationRuleBaseValues().getName());
107 		assertTrue("delegate rule should be current", newRuleDelegation.getDelegationRuleBaseValues().getCurrentInd());
108 		assertTrue("delegate rule should be flagged as a delegate", newRuleDelegation.getDelegationRuleBaseValues().getDelegateRule());
109 		assertEquals("Should have 1 responsibility", 1, newRuleDelegation.getDelegationRuleBaseValues().getResponsibilities().size());
110 		assertEquals("Incorrect responsibility name", principal2.getPrincipalId(), newRuleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityName());
111 		assertEquals("Incorrect responsibility type", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID, newRuleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityType());
112 		assertEquals("Incorrect delegation type", DelegationType.PRIMARY.getCode(), newRuleDelegation.getDelegationType());
113 
114 
115 		/**
116 		 * Let's add another delegate rule.
117 		 */
118 
119 		Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER2);
120 
121 		// let's save the new rule delegation
122 		RuleTestUtils.createRuleDelegationToUser(originalRule, originalResp, delegatePrincipal);
123 
124 		List<RuleDelegation> ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
125 		assertEquals("There should be 2 delegation rules", 2, ruleDelegations.size());
126 		boolean foundFirstDelegateRule = false;
127 		for (RuleDelegation ruleDelegation : ruleDelegations) {
128 			if (ruleDelegation.getRuleDelegationId().equals(newRuleDelegation.getRuleDelegationId())) {
129 				foundFirstDelegateRule = true;
130 				assertEquals("Rule Version should not have changed.", ruleDelegation.getVersionNumber(), newRuleDelegation.getVersionNumber());
131 			} else {
132 				// this should be our new rule delegation
133 				assertEquals("Incorrect responsibility id", originalResp.getResponsibilityId(), ruleDelegation.getResponsibilityId());
134 				assertNotNull("Name should not be null", ruleDelegation.getDelegationRuleBaseValues().getName());
135 				assertTrue("delegate rule should be current", ruleDelegation.getDelegationRuleBaseValues().getCurrentInd());
136 				assertTrue("delegate rule should be flagged as a delegate", ruleDelegation.getDelegationRuleBaseValues().getDelegateRule());
137 				assertEquals("Should have 1 responsibility", 1, ruleDelegation.getDelegationRuleBaseValues().getResponsibilities().size());
138 				assertEquals("Incorrect responsibility name", delegatePrincipal.getPrincipalId(), ruleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityName());
139 				assertEquals("Incorrect responsibility type", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID, ruleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityType());
140 			}
141 		}
142 		assertTrue("Failed to find the first delegate rule", foundFirstDelegateRule);
143 
144 		/**
145 		 *  now let's try editing our first delegate rule
146 		 */
147 
148 		String newRuleDelegationId = newRuleDelegation.getRuleDelegationId();
149 		// change the delegation type to secondary
150 		newRuleDelegation.setDelegationType(DelegationType.SECONDARY.getCode());
151 		saveNewVersion(newRuleDelegation);
152 		String newRuleDelegationId2 = newRuleDelegation.getRuleDelegationId();
153 
154 		// let's check the original and verify that its been re-versioned
155 		newRuleDelegation = KEWServiceLocator.getRuleDelegationService().findByRuleDelegationId(newRuleDelegationId);
156 		assertNotNull(newRuleDelegation);
157 		assertFalse("Rule delegation should no longer be current.", newRuleDelegation.getDelegationRuleBaseValues().getCurrentInd());
158 
159 		// there should still be 2 rule delegations, however one of them has been reversioned
160 		ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
161 		assertEquals("There should be 2 delegation rules", 2, ruleDelegations.size());
162 		boolean foundReversionedDelegateRule = false;
163 		for (RuleDelegation ruleDelegation : ruleDelegations) {
164 			if (ruleDelegation.getRuleDelegationId().equals(newRuleDelegationId2)) {
165 				// this is our reversioned rule
166 				foundReversionedDelegateRule = true;
167 				assertEquals("Previous version relationship should be set up now", newRuleDelegation.getDelegationRuleBaseValues().getRuleBaseValuesId(), ruleDelegation.getDelegationRuleBaseValues().getPreviousVersionId());
168 				assertEquals("Rule Version should have been incremented.", 
169 						Long.valueOf(newRuleDelegation.getVersionNumber().longValue() + 1), 
170 						ruleDelegation.getVersionNumber());
171 			}
172 		}
173 		assertTrue("Failed to find the reversioned delegate rule", foundReversionedDelegateRule);
174 	}
175 
176 	private void saveNewVersion(RuleDelegation ruleDelegation) {
177 		// clear out the keys
178 		ruleDelegation.setRuleDelegationId(null);
179 		ruleDelegation.setDelegateRuleId(null);
180 		for (RuleResponsibility ruleResponsibility : ruleDelegation.getDelegationRuleBaseValues().getResponsibilities()) {
181 			ruleResponsibility.setRuleBaseValuesId(null);
182 			//ruleResponsibility.setRuleBaseValues(null);
183 			ruleResponsibility.setResponsibilityId(null);
184 			ruleResponsibility.setRuleResponsibilityKey(null);
185 		}
186 		KEWServiceLocator.getRuleService().saveRuleDelegation(ruleDelegation, true);
187 	}
188 
189 }