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.junit.Test;
20 import org.kuali.rice.core.api.delegation.DelegationType;
21 import org.kuali.rice.kew.api.WorkflowDocument;
22 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
23 import org.kuali.rice.kew.service.KEWServiceLocator;
24 import org.kuali.rice.kew.test.KEWTestCase;
25 import org.kuali.rice.kew.api.KewApiConstants;
26 import org.kuali.rice.kim.api.identity.principal.Principal;
27 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28 import org.kuali.rice.test.BaselineTestCase;
29
30 import java.util.List;
31
32 import static org.junit.Assert.*;
33
34
35
36
37
38 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
39 public class AddRuleDelegationTest extends KEWTestCase {
40
41 private static final String DELEGATE_USER = "user2";
42 private static final String DELEGATE_USER2 = "pmckown";
43
44 private static final String DOCTYPE = "AddDelegationTest_DocType";
45 private static final String RULE_TEMPLATE = "AddDelegationTest_RuleTemplate";
46 private static final String DELEGATION_TEMPLATE = "AddDelegationTest_DelegationTemplate";
47
48 protected void loadTestData() throws Exception {
49 loadXmlFile("AddRuleDelegationTestData.xml");
50 }
51
52
53
54
55
56
57
58
59 @Test public void testNewDelegationTriggersRequeue() throws Exception {
60 String docType = "RiceDocument.testNewDelegationTriggersRequeue";
61
62
63 WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
64 wd.route("");
65
66
67 MockDocumentRefreshQueueImpl.clearRequeuedDocumentIds();
68
69
70 RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
71
72 assertTrue("our document should have been requeued!",
73 MockDocumentRefreshQueueImpl.getRequeuedDocumentIds().contains(wd.getDocumentId()));
74 }
75
76
77
78
79
80
81 @Test
82 public void testAddRuleDelegation() throws Exception {
83
84 RuleBaseValues originalRule = RuleTestUtils.getRule(DOCTYPE, RULE_TEMPLATE);
85
86 List<RuleResponsibilityBo> originalResps = originalRule.getRuleResponsibilities();
87 assertTrue("assuming there is 1 responsibility", originalResps != null && originalResps.size() == 1);
88
89 RuleResponsibilityBo originalResp = originalResps.get(0);
90
91 RuleTestUtils.createDelegationToUser(DOCTYPE, RULE_TEMPLATE, DELEGATE_USER);
92
93 Principal principal2 = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER);
94
95
96 originalRule = KEWServiceLocator.getRuleService().findRuleBaseValuesById(originalRule.getId());
97 assertTrue("Original rule should be current.", originalRule.getCurrentInd());
98 List<RuleResponsibilityBo> responsibilities = originalRule.getRuleResponsibilities();
99 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
118
119
120 Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER2);
121
122
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
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
147
148
149 String newRuleDelegationId = newRuleDelegation.getRuleDelegationId();
150
151 newRuleDelegation.setDelegationType(DelegationType.SECONDARY);
152 saveNewVersion(newRuleDelegation);
153 String newRuleDelegationId2 = newRuleDelegation.getRuleDelegationId();
154
155
156 newRuleDelegation = KEWServiceLocator.getRuleDelegationService().findByRuleDelegationId(newRuleDelegationId);
157 assertNotNull(newRuleDelegation);
158 assertFalse("Rule delegation should no longer be current.", newRuleDelegation.getDelegationRule().getCurrentInd());
159
160
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
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
179 ruleDelegation.setRuleDelegationId(null);
180 ruleDelegation.setDelegateRuleId(null);
181 ruleDelegation.setObjectId(null);
182 for (RuleResponsibilityBo ruleResponsibility : ruleDelegation.getDelegationRule().getRuleResponsibilities()) {
183 ruleResponsibility.setRuleBaseValuesId(null);
184
185 ruleResponsibility.setResponsibilityId(null);
186 ruleResponsibility.setId(null);
187 ruleResponsibility.setObjectId(null);
188 }
189 KEWServiceLocator.getRuleService().saveRuleDelegation(ruleDelegation, true);
190 }
191
192 }