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