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