001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kew.rule;
017
018 import mocks.MockDocumentRefreshQueueImpl;
019 import org.junit.Test;
020 import org.kuali.rice.core.api.delegation.DelegationType;
021 import org.kuali.rice.kew.api.WorkflowDocument;
022 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
023 import org.kuali.rice.kew.service.KEWServiceLocator;
024 import org.kuali.rice.kew.test.KEWTestCase;
025 import org.kuali.rice.kew.api.KewApiConstants;
026 import org.kuali.rice.kim.api.identity.principal.Principal;
027 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028 import org.kuali.rice.test.BaselineTestCase;
029
030 import java.util.List;
031
032 import static org.junit.Assert.*;
033
034 /**
035 * Tests adding a delegation rule
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
039 public class AddRuleDelegationTest extends KEWTestCase {
040
041 private static final String DELEGATE_USER = "user2";
042 private static final String DELEGATE_USER2 = "pmckown";
043
044 private static final String DOCTYPE = "AddDelegationTest_DocType";
045 private static final String RULE_TEMPLATE = "AddDelegationTest_RuleTemplate";
046 private static final String DELEGATION_TEMPLATE = "AddDelegationTest_DelegationTemplate";
047
048 protected void loadTestData() throws Exception {
049 loadXmlFile("AddRuleDelegationTestData.xml");
050 }
051
052 /**
053 *
054 * Tests that adding a delegation for a rule for which a document has a pending action request causes
055 * the document to be requeued. See KULRICE-3575
056 *
057 * @throws Exception
058 */
059 @Test public void testNewDelegationTriggersRequeue() throws Exception {
060 String docType = "RiceDocument.testNewDelegationTriggersRequeue";
061
062 // route a document of this type
063 WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
064 wd.route("");
065
066 // clear the current set of requeued document ids
067 MockDocumentRefreshQueueImpl.clearRequeuedDocumentIds();
068
069 // create and save a rule delegation
070 RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
071
072 assertTrue("our document should have been requeued!",
073 MockDocumentRefreshQueueImpl.getRequeuedDocumentIds().contains(wd.getDocumentId()));
074 }
075
076
077 /**
078 * Tests adding a delegation rule. The implementation is mostly a cut-and-paste copy of
079 * createDelegateRule and routeRule methods from DelegatRule2Action Struts action.
080 */
081 @Test
082 public void testAddRuleDelegation() throws Exception {
083
084 RuleBaseValues originalRule = RuleTestUtils.getRule(DOCTYPE, RULE_TEMPLATE);
085
086 List<RuleResponsibilityBo> originalResps = originalRule.getRuleResponsibilities();
087 assertTrue("assuming there is 1 responsibility", originalResps != null && originalResps.size() == 1);
088
089 RuleResponsibilityBo originalResp = originalResps.get(0);
090
091 RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
092
093 Principal principal2 = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER);
094
095 // 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)
096 originalRule = KEWServiceLocator.getRuleService().findRuleBaseValuesById(originalRule.getId());
097 assertTrue("Original rule should be current.", originalRule.getCurrentInd());
098 List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities();
099 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 }