1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.xml.export;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.BufferedInputStream;
24 import java.io.ByteArrayInputStream;
25 import java.io.FileInputStream;
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Set;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.commons.lang.time.DateUtils;
35 import org.junit.Test;
36 import org.kuali.rice.core.api.CoreApiServiceLocator;
37 import org.kuali.rice.kew.export.KewExportDataSet;
38 import org.kuali.rice.kew.rule.RuleBaseValues;
39 import org.kuali.rice.kew.rule.RuleDelegation;
40 import org.kuali.rice.kew.rule.RuleExtension;
41 import org.kuali.rice.kew.rule.RuleExtensionValue;
42 import org.kuali.rice.kew.rule.RuleResponsibility;
43 import org.kuali.rice.kew.rule.web.WebRuleUtils;
44 import org.kuali.rice.kew.service.KEWServiceLocator;
45 import org.kuali.rice.test.BaselineTestCase;
46 import org.kuali.rice.test.ClearDatabaseLifecycle;
47
48
49
50
51
52
53
54
55
56
57
58 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
59 public class RuleXmlExporterTest extends XmlExporterTestCase {
60
61 @Test public void testExport() throws Exception {
62 loadXmlFile("org/kuali/rice/kew/actions/ActionsConfig.xml");
63 loadXmlStream(new FileInputStream(getBaseDir() + "/src/test/resources/org/kuali/rice/kew/batch/data/RuleAttributeContent.xml"));
64 loadXmlStream(new FileInputStream(getBaseDir() + "/src/test/resources/org/kuali/rice/kew/batch/data/RuleTemplateContent.xml"));
65 loadXmlStream(new FileInputStream(getBaseDir() + "/src/test/resources/org/kuali/rice/kew/batch/data/DocumentTypeContent.xml"));
66 loadXmlStream(new FileInputStream(getBaseDir() + "/src/test/resources/org/kuali/rice/kew/batch/data/RuleContent.xml"));
67 assertRuleBaseValuesStateIndependence();
68 assertExport();
69 }
70
71
72
73
74 protected void assertExport() throws Exception {
75
76 List oldRules = KEWServiceLocator.getRuleService().fetchAllRules(true);
77 assertAllRulesHaveUniqueNames(oldRules);
78 List oldRuleDelegations = KEWServiceLocator.getRuleDelegationService().findAllCurrentRuleDelegations();
79 assertAllRuleDelegationsHaveUniqueNames(oldRuleDelegations);
80
81 KewExportDataSet dataSet = new KewExportDataSet();
82 dataSet.getRules().addAll(oldRules);
83 dataSet.getRuleDelegations().addAll(oldRuleDelegations);
84 dataSet.getDocumentTypes().addAll(KEWServiceLocator.getDocumentTypeService().findAllCurrent());
85 dataSet.getRuleTemplates().addAll(KEWServiceLocator.getRuleTemplateService().findAll());
86 dataSet.getRuleAttributes().addAll(KEWServiceLocator.getRuleAttributeService().findAll());
87 byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
88 assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
89
90
91 ClearDatabaseLifecycle clearLifeCycle = new ClearDatabaseLifecycle();
92 clearLifeCycle.getTablesToClear().add("KREW_RULE_T");
93 clearLifeCycle.getTablesToClear().add("KREW_RULE_RSP_T");
94 clearLifeCycle.getTablesToClear().add("KREW_DLGN_RSP_T");
95 clearLifeCycle.getTablesToClear().add("KREW_RULE_ATTR_T");
96 clearLifeCycle.getTablesToClear().add("KREW_RULE_TMPL_T");
97 clearLifeCycle.getTablesToClear().add("KREW_DOC_TYP_T");
98 clearLifeCycle.start();
99 new ClearCacheLifecycle().stop();
100
101
102 loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
103
104 List newRules = KEWServiceLocator.getRuleService().fetchAllRules(true);
105 assertEquals("Should have same number of old and new Rules.", oldRules.size(), newRules.size());
106 for (Iterator iterator = oldRules.iterator(); iterator.hasNext();) {
107 RuleBaseValues oldRule = (RuleBaseValues) iterator.next();
108 boolean foundRule = false;
109 for (Iterator iterator2 = newRules.iterator(); iterator2.hasNext();) {
110 RuleBaseValues newRule = (RuleBaseValues) iterator2.next();
111 if (oldRule.getDescription().equals(newRule.getDescription())) {
112 assertRuleExport(oldRule, newRule);
113 foundRule = true;
114 }
115 }
116 assertTrue("Could not locate the new rule for description " + oldRule.getDescription(), foundRule);
117 }
118
119 List newRuleDelegations = KEWServiceLocator.getRuleDelegationService().findAllCurrentRuleDelegations();
120 assertDelegations(oldRuleDelegations, newRuleDelegations);
121 }
122
123
124
125
126
127 protected void assertRuleBaseValuesStateIndependence() throws Exception {
128 for (Object o : KEWServiceLocator.getRuleService().fetchAllRules(true)) {
129 RuleBaseValues rule = (RuleBaseValues)o;
130 KewExportDataSet dataSet = new KewExportDataSet();
131 dataSet.getRules().add(rule);
132
133
134 byte[] saveXmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
135 String saveStr = new String(saveXmlBytes);
136
137
138 WebRuleUtils.populateRuleMaintenanceFields(rule);
139
140
141 byte[] loadXmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
142 String loadStr = new String(loadXmlBytes);
143
144
145 assertTrue("The load/render state of the RuleBaseValues shouldn't effect the export: \n" +
146 saveStr + "\n\n != \n\n" + loadStr,
147 StringUtils.equals(saveStr, loadStr));
148 }
149 }
150
151 private void assertRuleExport(RuleBaseValues oldRule, RuleBaseValues newRule) {
152 assertFalse("Ids should be different.", oldRule.getRuleBaseValuesId().equals(newRule.getRuleBaseValuesId()));
153 assertEquals(oldRule.getActiveInd(), newRule.getActiveInd());
154 assertEquals(DateUtils.round(oldRule.getActivationDate(), Calendar.DATE), DateUtils.round(newRule.getActivationDate(), Calendar.DATE));
155 assertEquals(oldRule.getName(), newRule.getName());
156 assertEquals(oldRule.getCurrentInd(), newRule.getCurrentInd());
157 assertEquals(oldRule.getDeactivationDate(), newRule.getDeactivationDate());
158 assertEquals(oldRule.getDelegateRule(), newRule.getDelegateRule());
159 assertEquals(oldRule.getDescription(), newRule.getDescription());
160 assertEquals(oldRule.getDocTypeName(), newRule.getDocTypeName());
161
162 if (oldRule.getFromDate() == null) {
163 assertNull(newRule.getFromDate());
164 } else {
165 assertEquals(DateUtils.round(oldRule.getFromDate(), Calendar.DATE), DateUtils.round(newRule.getFromDate(), Calendar.DATE));
166 }
167 if (oldRule.getToDate() == null) {
168 assertNull(newRule.getToDate());
169 } else {
170 assertEquals(DateUtils.round(oldRule.getToDate(), Calendar.DATE), DateUtils.round(newRule.getToDate(), Calendar.DATE));
171 }
172 assertEquals(oldRule.getFromDateString(),newRule.getFromDateString() );
173 assertEquals(oldRule.getToDateString(),newRule.getToDateString() );
174
175 assertEquals(oldRule.getForceAction(), newRule.getForceAction());
176
177 if(!oldRule.getDelegateRule().booleanValue())
178 assertEquals(oldRule.getPreviousVersionId(), newRule.getPreviousVersionId());
179
180 assertEquals(oldRule.getDocumentId(), newRule.getDocumentId());
181
182 if (oldRule.getRuleTemplate() == null) {
183 assertNull(newRule.getRuleTemplate());
184 } else {
185 assertEquals(oldRule.getRuleTemplate().getName(), newRule.getRuleTemplate().getName());
186 }
187 if (oldRule.getRuleExpressionDef() == null) {
188 assertNull(newRule.getRuleExpressionDef());
189 } else {
190 assertEquals(oldRule.getRuleExpressionDef().getExpression(), newRule.getRuleExpressionDef().getExpression());
191 assertEquals(oldRule.getRuleExpressionDef().getType(), newRule.getRuleExpressionDef().getType());
192 }
193 if(!oldRule.getDelegateRule().booleanValue())
194 assertEquals(oldRule.getVersionNbr(), newRule.getVersionNbr());
195
196 assertRuleExtensions(oldRule.getRuleExtensions(), newRule.getRuleExtensions());
197 assertResponsibilities(oldRule.getResponsibilities(), newRule.getResponsibilities());
198
199
200 }
201
202 private void assertRuleExtensions(List oldRuleExtensions, List newRuleExtensions) {
203 assertEquals(oldRuleExtensions.size(), newRuleExtensions.size());
204 for (Iterator iterator = oldRuleExtensions.iterator(); iterator.hasNext();) {
205 RuleExtension oldExtension = (RuleExtension) iterator.next();
206 boolean foundExtension = false;
207 for (Iterator iterator2 = newRuleExtensions.iterator(); iterator2.hasNext();) {
208 RuleExtension newExtension = (RuleExtension) iterator2.next();
209 if (oldExtension.getRuleTemplateAttribute().getRuleAttribute().getName().equals(newExtension.getRuleTemplateAttribute().getRuleAttribute().getName()) &&
210 oldExtension.getRuleTemplateAttribute().getRuleTemplate().getName().equals(newExtension.getRuleTemplateAttribute().getRuleTemplate().getName())) {
211 assertExtensionValues(oldExtension.getExtensionValues(), newExtension.getExtensionValues());
212 foundExtension = true;
213 break;
214 }
215 }
216 assertTrue("Could not locate rule extension.", foundExtension);
217 }
218 }
219
220 private void assertExtensionValues(List oldExtensionValues, List newExtensionValues) {
221 assertEquals(oldExtensionValues.size(), newExtensionValues.size());
222 for (Iterator iterator = oldExtensionValues.iterator(); iterator.hasNext();) {
223 RuleExtensionValue oldValue = (RuleExtensionValue) iterator.next();
224 boolean foundValue = false;
225 for (Iterator iterator2 = oldExtensionValues.iterator(); iterator2.hasNext();) {
226 RuleExtensionValue newValue = (RuleExtensionValue) iterator2.next();
227 if (oldValue.getKey().equals(newValue.getKey())) {
228 assertEquals(oldValue.getValue(), newValue.getValue());
229 foundValue = true;
230 break;
231 }
232 }
233 assertTrue("Could not locate extension value.", foundValue);
234 }
235 }
236
237 private void assertResponsibilities(List oldResps, List newResps) {
238 assertEquals(oldResps.size(), newResps.size());
239 for (Iterator iterator = oldResps.iterator(); iterator.hasNext();) {
240 RuleResponsibility oldResp = (RuleResponsibility) iterator.next();
241 boolean foundResp = false;
242 for (Iterator iterator2 = newResps.iterator(); iterator2.hasNext();) {
243 RuleResponsibility newResp = (RuleResponsibility) iterator2.next();
244 if (oldResp.getRuleResponsibilityName().equals(newResp.getRuleResponsibilityName())) {
245 assertEquals(oldResp.getActionRequestedCd(), newResp.getActionRequestedCd());
246 assertEquals(oldResp.getApprovePolicy(), newResp.getApprovePolicy());
247 assertEquals(oldResp.getResolvedRoleName(), newResp.getResolvedRoleName());
248 assertEquals(oldResp.getRole(), newResp.getRole());
249 assertEquals(oldResp.getRuleResponsibilityType(), newResp.getRuleResponsibilityType());
250 assertEquals(oldResp.getPriority(), newResp.getPriority());
251 foundResp = true;
252 break;
253 }
254 }
255 assertTrue("Could not locate responsibility "+oldResp.getRuleResponsibilityName()+" on rule "+oldResp.getRuleBaseValues().getDescription(), foundResp);
256 }
257 }
258
259 private void assertDelegations(List oldDelegations, List newDelegations) {
260 assertEquals(oldDelegations.size(), newDelegations.size());
261 for (Iterator iterator = oldDelegations.iterator(); iterator.hasNext();) {
262 RuleDelegation oldDelegation = (RuleDelegation) iterator.next();
263 boolean foundDelegation = false;
264 for (Iterator iterator2 = newDelegations.iterator(); iterator2.hasNext();) {
265 RuleDelegation newDelegation = (RuleDelegation) iterator2.next();
266 if (oldDelegation.getDelegationRuleBaseValues().getName().equals(newDelegation.getDelegationRuleBaseValues().getName())) {
267 assertEquals(oldDelegation.getDelegationType(), newDelegation.getDelegationType());
268 assertFalse(oldDelegation.getResponsibilityId().equals(newDelegation.getResponsibilityId()));
269 assertRuleExport(oldDelegation.getDelegationRuleBaseValues(), newDelegation.getDelegationRuleBaseValues());
270 foundDelegation = true;
271 break;
272 }
273 }
274 assertTrue("Could not locate delegation.", foundDelegation);
275 }
276 }
277
278 private void assertAllRulesHaveUniqueNames(List rules) throws Exception {
279 Set<String> ruleDescriptions = new HashSet<String>();
280 for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
281 RuleBaseValues rule = (RuleBaseValues) iterator.next();
282 assertFalse("Found 2 rules with the same description '" + rule.getDescription() + "'. " +
283 "In order for this test to work, all rules in the configuration files must have unique descriptions.",
284 ruleDescriptions.contains(rule.getDescription()));
285 ruleDescriptions.add(rule.getDescription());
286 }
287 }
288
289 private void assertAllRuleDelegationsHaveUniqueNames(List<RuleDelegation> ruleDelegations) throws Exception {
290 List<RuleBaseValues> rules = new ArrayList<RuleBaseValues>();
291 for (RuleDelegation ruleDelegation : ruleDelegations) {
292 rules.add(ruleDelegation.getDelegationRuleBaseValues());
293 }
294 assertAllRulesHaveUniqueNames(rules);
295 }
296
297 }