View Javadoc

1   /**
2    * Copyright 2005-2013 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.rule;
17  
18  import mocks.MockDocumentRefreshQueueImpl;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.delegation.DelegationType;
21  import org.kuali.rice.kew.api.WorkflowDocument;
22  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
23  import org.kuali.rice.kew.service.KEWServiceLocator;
24  import org.kuali.rice.kew.test.KEWTestCase;
25  import org.kuali.rice.kew.api.KewApiConstants;
26  import org.kuali.rice.kim.api.identity.principal.Principal;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.test.BaselineTestCase;
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  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
39  public class AddRuleDelegationTest extends KEWTestCase {
40  
41  	private static final String DELEGATE_USER = "user2";
42  	private static final String DELEGATE_USER2 = "pmckown";
43  
44  	private static final String DOCTYPE = "AddDelegationTest_DocType";
45  	private static final String RULE_TEMPLATE = "AddDelegationTest_RuleTemplate";
46  	private static final String DELEGATION_TEMPLATE = "AddDelegationTest_DelegationTemplate";
47  
48  	protected void loadTestData() throws Exception {
49  		loadXmlFile("AddRuleDelegationTestData.xml");
50  	}
51  
52  	/**
53  	 *
54  	 * Tests that adding a delegation for a rule for which a document has a pending action request causes
55  	 * the document to be requeued. See KULRICE-3575
56  	 *
57  	 * @throws Exception
58  	 */
59      @Test public void testNewDelegationTriggersRequeue() throws Exception {
60      	String docType = "RiceDocument.testNewDelegationTriggersRequeue";
61  
62      	// route a document of this type
63      	WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
64      	wd.route("");
65  
66      	// clear the current set of requeued document ids
67  		MockDocumentRefreshQueueImpl.clearRequeuedDocumentIds();
68  
69      	// create and save a rule delegation
70  		RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
71  
72  		assertTrue("our document should have been requeued!",
73  				MockDocumentRefreshQueueImpl.getRequeuedDocumentIds().contains(wd.getDocumentId()));
74      }
75  
76  
77  	/**
78  	 * Tests adding a delegation rule.  The implementation is mostly a cut-and-paste copy of
79  	 * createDelegateRule and routeRule methods from DelegatRule2Action Struts action.
80  	 */
81  	@Test
82  	public void testAddRuleDelegation() throws Exception {
83  
84  		RuleBaseValues originalRule = RuleTestUtils.getRule(DOCTYPE, RULE_TEMPLATE);
85  
86      	List<RuleResponsibilityBo> originalResps = originalRule.getRuleResponsibilities();
87      	assertTrue("assuming there is 1 responsibility", originalResps != null && originalResps.size() == 1);
88  
89      	RuleResponsibilityBo originalResp = originalResps.get(0);
90  
91  		RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
92  
93  		Principal principal2 = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER);
94  
95  		// 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)
96  		originalRule = KEWServiceLocator.getRuleService().findRuleBaseValuesById(originalRule.getId());
97  		assertTrue("Original rule should be current.", originalRule.getCurrentInd());
98  		List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities();
99  		originalResp = responsibilities.get(0);
100 		assertEquals("Original rule should have 1 delegation now.", 1, originalResp.getDelegationRules().size());
101 
102 		List<RuleDelegationBo> newRuleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
103 		assertEquals("Should be 1 delegation", 1, newRuleDelegations.size());
104 
105 		RuleDelegationBo newRuleDelegation = newRuleDelegations.get(0);
106 		assertEquals("Incorrect responsibility id", originalResp.getResponsibilityId(), newRuleDelegation.getResponsibilityId());
107 		assertNotNull("Name should not be null", newRuleDelegation.getDelegationRule().getName());
108 		assertTrue("delegate rule should be current", newRuleDelegation.getDelegationRule().getCurrentInd());
109 		assertTrue("delegate rule should be flagged as a delegate", newRuleDelegation.getDelegationRule().getDelegateRule());
110 		assertEquals("Should have 1 responsibility", 1, newRuleDelegation.getDelegationRule().getRuleResponsibilities().size());
111 		assertEquals("Incorrect responsibility name", principal2.getPrincipalId(), newRuleDelegation.getDelegationRule().getRuleResponsibilities().get(0).getRuleResponsibilityName());
112 		assertEquals("Incorrect responsibility type", KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID, newRuleDelegation.getDelegationRule().getRuleResponsibilities().get(0).getRuleResponsibilityType());
113 		assertEquals("Incorrect delegation type", DelegationType.PRIMARY, newRuleDelegation.getDelegationType());
114 
115 
116 		/**
117 		 * Let's add another delegate rule.
118 		 */
119 
120 		Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER2);
121 
122 		// let's save the new rule delegation
123 		RuleTestUtils.createRuleDelegationToUser(originalRule, originalResp, delegatePrincipal);
124 
125 		List<RuleDelegationBo> ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
126 		assertEquals("There should be 2 delegation rules", 2, ruleDelegations.size());
127 		boolean foundFirstDelegateRule = false;
128 		for (RuleDelegationBo ruleDelegation : ruleDelegations) {
129 			if (ruleDelegation.getRuleDelegationId().equals(newRuleDelegation.getRuleDelegationId())) {
130 				foundFirstDelegateRule = true;
131 				assertEquals("Rule Version should not have changed.", ruleDelegation.getVersionNumber(), newRuleDelegation.getVersionNumber());
132 			} else {
133 				// this should be our new rule delegation
134 				assertEquals("Incorrect responsibility id", originalResp.getResponsibilityId(), ruleDelegation.getResponsibilityId());
135 				assertNotNull("Name should not be null", ruleDelegation.getDelegationRule().getName());
136 				assertTrue("delegate rule should be current", ruleDelegation.getDelegationRule().getCurrentInd());
137 				assertTrue("delegate rule should be flagged as a delegate", ruleDelegation.getDelegationRule().getDelegateRule());
138 				assertEquals("Should have 1 responsibility", 1, ruleDelegation.getDelegationRule().getRuleResponsibilities().size());
139 				assertEquals("Incorrect responsibility name", delegatePrincipal.getPrincipalId(), ruleDelegation.getDelegationRule().getRuleResponsibilities().get(0).getRuleResponsibilityName());
140 				assertEquals("Incorrect responsibility type", KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID, ruleDelegation.getDelegationRule().getRuleResponsibilities().get(0).getRuleResponsibilityType());
141 			}
142 		}
143 		assertTrue("Failed to find the first delegate rule", foundFirstDelegateRule);
144 
145 		/**
146 		 *  now let's try editing our first delegate rule
147 		 */
148 
149 		String newRuleDelegationId = newRuleDelegation.getRuleDelegationId();
150 		// change the delegation type to secondary
151 		newRuleDelegation.setDelegationType(DelegationType.SECONDARY);
152 		saveNewVersion(newRuleDelegation);
153 		String newRuleDelegationId2 = newRuleDelegation.getRuleDelegationId();
154 
155 		// let's check the original and verify that its been re-versioned
156 		newRuleDelegation = KEWServiceLocator.getRuleDelegationService().findByRuleDelegationId(newRuleDelegationId);
157 		assertNotNull(newRuleDelegation);
158 		assertFalse("Rule delegation should no longer be current.", newRuleDelegation.getDelegationRule().getCurrentInd());
159 
160 		// there should still be 2 rule delegations, however one of them has been reversioned
161 		ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
162 		assertEquals("There should be 2 delegation rules", 2, ruleDelegations.size());
163 		boolean foundReversionedDelegateRule = false;
164 		for (RuleDelegationBo ruleDelegation : ruleDelegations) {
165 			if (ruleDelegation.getRuleDelegationId().equals(newRuleDelegationId2)) {
166 				// this is our reversioned rule
167 				foundReversionedDelegateRule = true;
168 				assertEquals("Previous version relationship should be set up now", newRuleDelegation.getDelegationRule().getId(), ruleDelegation.getDelegationRule().getPreviousRuleId());
169 				assertEquals("Rule Version should have been incremented.",
170 						Long.valueOf(newRuleDelegation.getVersionNumber().longValue() + 1),
171 						ruleDelegation.getVersionNumber());
172 			}
173 		}
174 		assertTrue("Failed to find the reversioned delegate rule", foundReversionedDelegateRule);
175 	}
176 
177 	private void saveNewVersion(RuleDelegationBo ruleDelegation) {
178 		// clear out the keys
179 		ruleDelegation.setRuleDelegationId(null);
180 		ruleDelegation.setDelegateRuleId(null);
181         ruleDelegation.setObjectId(null);
182 		for (RuleResponsibilityBo ruleResponsibility : ruleDelegation.getDelegationRule().getRuleResponsibilities()) {
183 			ruleResponsibility.setRuleBaseValuesId(null);
184 			//ruleResponsibility.setRuleBaseValues(null);
185 			ruleResponsibility.setResponsibilityId(null);
186 			ruleResponsibility.setId(null);
187             ruleResponsibility.setObjectId(null);
188 		}
189 		KEWServiceLocator.getRuleService().saveRuleDelegation(ruleDelegation, true);
190 	}
191 
192 }