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 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  
43  
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  
63  
64  
65  
66  
67      @Test public void testNewDelegationTriggersRequeue() throws Exception {
68      	String docType = "RiceDocument.testNewDelegationTriggersRequeue";
69  
70      	
71      	WorkflowDocument wd = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), DOCTYPE);
72      	wd.route("");
73  
74      	
75  		MockDocumentRefreshQueueImpl.clearRequeuedDocumentIds();
76  
77      	
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  
87  
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 		
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 
126 
127 
128 		Principal delegatePrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(DELEGATE_USER2);
129 
130 		
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 				
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 
155 
156 
157         String newRuleDelegationId = newRuleDelegation.getRuleDelegationId();
158 		
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 		
173 		newRuleDelegation = KEWServiceLocator.getRuleDelegationService().findByRuleDelegationId(newRuleDelegationId);
174 		assertNotNull(newRuleDelegation);
175 		assertFalse("Rule delegation should no longer be current.", newRuleDelegation.getDelegationRule().getCurrentInd());
176 
177 		
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 				
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 }