1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
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
58
59
60
61
62 @Test public void testNewDelegationTriggersRequeue() throws Exception {
63 String docType = "RiceDocument.testNewDelegationTriggersRequeue";
64
65
66 WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
67 wd.route("");
68
69
70 MockDocumentRefreshQueueImpl.clearRequeuedDocumentIds();
71
72
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
82
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
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
121
122
123 Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER2);
124
125
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
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
150
151
152 String newRuleDelegationId = newRuleDelegation.getRuleDelegationId();
153
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
168 newRuleDelegation = KEWServiceLocator.getRuleDelegationService().findByRuleDelegationId(newRuleDelegationId);
169 assertNotNull(newRuleDelegation);
170 assertFalse("Rule delegation should no longer be current.", newRuleDelegation.getDelegationRule().getCurrentInd());
171
172
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
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
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 }