1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.rule;
18
19 import mocks.MockDocumentRequeuerImpl;
20 import org.junit.Test;
21 import org.kuali.rice.kew.api.WorkflowDocument;
22 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
23 import org.kuali.rice.kew.api.action.DelegationType;
24 import org.kuali.rice.kew.service.KEWServiceLocator;
25 import org.kuali.rice.kew.test.KEWTestCase;
26 import org.kuali.rice.kew.util.KEWConstants;
27 import org.kuali.rice.kim.api.identity.principal.Principal;
28 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29
30 import java.util.List;
31
32 import static org.junit.Assert.*;
33
34
35
36
37
38 public class AddRuleDelegationTest extends KEWTestCase {
39
40 private static final String DELEGATE_USER = "user2";
41 private static final String DELEGATE_USER2 = "pmckown";
42
43 private static final String DOCTYPE = "AddDelegationTest_DocType";
44 private static final String RULE_TEMPLATE = "AddDelegationTest_RuleTemplate";
45 private static final String DELEGATION_TEMPLATE = "AddDelegationTest_DelegationTemplate";
46
47 protected void loadTestData() throws Exception {
48 loadXmlFile("AddRuleDelegationTestData.xml");
49 }
50
51
52
53
54
55
56
57
58 @Test public void testNewDelegationTriggersRequeue() throws Exception {
59 String docType = "RiceDocument.testNewDelegationTriggersRequeue";
60
61
62 WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
63 wd.route("");
64
65
66 MockDocumentRequeuerImpl.clearRequeuedDocumentIds();
67
68
69 RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
70
71 assertTrue("our document should have been requeued!",
72 MockDocumentRequeuerImpl.getRequeuedDocumentIds().contains(wd.getDocumentId()));
73 }
74
75
76
77
78
79
80 @Test
81 public void testAddRuleDelegation() throws Exception {
82
83 RuleBaseValues originalRule = RuleTestUtils.getRule(DOCTYPE, RULE_TEMPLATE);
84
85 List<RuleResponsibility> originalResps = originalRule.getResponsibilities();
86 assertTrue("assuming there is 1 responsibility", originalResps != null && originalResps.size() == 1);
87
88 RuleResponsibility originalResp = originalResps.get(0);
89
90 RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
91
92 Principal principal2 = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER);
93
94
95 originalRule = KEWServiceLocator.getRuleService().findRuleBaseValuesById(originalRule.getRuleBaseValuesId());
96 assertTrue("Original rule should be current.", originalRule.getCurrentInd());
97 List<RuleResponsibility> responsibilities = originalRule.getResponsibilities();
98 originalResp = responsibilities.get(0);
99 assertEquals("Original rule should have 1 delegation now.", 1, originalResp.getDelegationRules().size());
100
101 List<RuleDelegation> newRuleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
102 assertEquals("Should be 1 delegation", 1, newRuleDelegations.size());
103
104 RuleDelegation newRuleDelegation = newRuleDelegations.get(0);
105 assertEquals("Incorrect responsibility id", originalResp.getResponsibilityId(), newRuleDelegation.getResponsibilityId());
106 assertNotNull("Name should not be null", newRuleDelegation.getDelegationRuleBaseValues().getName());
107 assertTrue("delegate rule should be current", newRuleDelegation.getDelegationRuleBaseValues().getCurrentInd());
108 assertTrue("delegate rule should be flagged as a delegate", newRuleDelegation.getDelegationRuleBaseValues().getDelegateRule());
109 assertEquals("Should have 1 responsibility", 1, newRuleDelegation.getDelegationRuleBaseValues().getResponsibilities().size());
110 assertEquals("Incorrect responsibility name", principal2.getPrincipalId(), newRuleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityName());
111 assertEquals("Incorrect responsibility type", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID, newRuleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityType());
112 assertEquals("Incorrect delegation type", DelegationType.PRIMARY.getCode(), newRuleDelegation.getDelegationType());
113
114
115
116
117
118
119 Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER2);
120
121
122 RuleTestUtils.createRuleDelegationToUser(originalRule, originalResp, delegatePrincipal);
123
124 List<RuleDelegation> ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
125 assertEquals("There should be 2 delegation rules", 2, ruleDelegations.size());
126 boolean foundFirstDelegateRule = false;
127 for (RuleDelegation ruleDelegation : ruleDelegations) {
128 if (ruleDelegation.getRuleDelegationId().equals(newRuleDelegation.getRuleDelegationId())) {
129 foundFirstDelegateRule = true;
130 assertEquals("Rule Version should not have changed.", ruleDelegation.getVersionNumber(), newRuleDelegation.getVersionNumber());
131 } else {
132
133 assertEquals("Incorrect responsibility id", originalResp.getResponsibilityId(), ruleDelegation.getResponsibilityId());
134 assertNotNull("Name should not be null", ruleDelegation.getDelegationRuleBaseValues().getName());
135 assertTrue("delegate rule should be current", ruleDelegation.getDelegationRuleBaseValues().getCurrentInd());
136 assertTrue("delegate rule should be flagged as a delegate", ruleDelegation.getDelegationRuleBaseValues().getDelegateRule());
137 assertEquals("Should have 1 responsibility", 1, ruleDelegation.getDelegationRuleBaseValues().getResponsibilities().size());
138 assertEquals("Incorrect responsibility name", delegatePrincipal.getPrincipalId(), ruleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityName());
139 assertEquals("Incorrect responsibility type", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID, ruleDelegation.getDelegationRuleBaseValues().getResponsibilities().get(0).getRuleResponsibilityType());
140 }
141 }
142 assertTrue("Failed to find the first delegate rule", foundFirstDelegateRule);
143
144
145
146
147
148 String newRuleDelegationId = newRuleDelegation.getRuleDelegationId();
149
150 newRuleDelegation.setDelegationType(DelegationType.SECONDARY.getCode());
151 saveNewVersion(newRuleDelegation);
152 String newRuleDelegationId2 = newRuleDelegation.getRuleDelegationId();
153
154
155 newRuleDelegation = KEWServiceLocator.getRuleDelegationService().findByRuleDelegationId(newRuleDelegationId);
156 assertNotNull(newRuleDelegation);
157 assertFalse("Rule delegation should no longer be current.", newRuleDelegation.getDelegationRuleBaseValues().getCurrentInd());
158
159
160 ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(originalResp.getResponsibilityId());
161 assertEquals("There should be 2 delegation rules", 2, ruleDelegations.size());
162 boolean foundReversionedDelegateRule = false;
163 for (RuleDelegation ruleDelegation : ruleDelegations) {
164 if (ruleDelegation.getRuleDelegationId().equals(newRuleDelegationId2)) {
165
166 foundReversionedDelegateRule = true;
167 assertEquals("Previous version relationship should be set up now", newRuleDelegation.getDelegationRuleBaseValues().getRuleBaseValuesId(), ruleDelegation.getDelegationRuleBaseValues().getPreviousVersionId());
168 assertEquals("Rule Version should have been incremented.",
169 Long.valueOf(newRuleDelegation.getVersionNumber().longValue() + 1),
170 ruleDelegation.getVersionNumber());
171 }
172 }
173 assertTrue("Failed to find the reversioned delegate rule", foundReversionedDelegateRule);
174 }
175
176 private void saveNewVersion(RuleDelegation ruleDelegation) {
177
178 ruleDelegation.setRuleDelegationId(null);
179 ruleDelegation.setDelegateRuleId(null);
180 for (RuleResponsibility ruleResponsibility : ruleDelegation.getDelegationRuleBaseValues().getResponsibilities()) {
181 ruleResponsibility.setRuleBaseValuesId(null);
182
183 ruleResponsibility.setResponsibilityId(null);
184 ruleResponsibility.setRuleResponsibilityKey(null);
185 }
186 KEWServiceLocator.getRuleService().saveRuleDelegation(ruleDelegation, true);
187 }
188
189 }