001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.xml.export;
018:
019: import java.io.BufferedInputStream;
020: import java.io.ByteArrayInputStream;
021: import java.io.FileInputStream;
022: import java.util.Calendar;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Set;
027:
028: import org.apache.commons.lang.time.DateUtils;
029: import org.junit.Test;
030:
031: import edu.iu.uis.eden.KEWServiceLocator;
032: import edu.iu.uis.eden.export.ExportDataSet;
033: import edu.iu.uis.eden.export.ExportFormat;
034: import edu.iu.uis.eden.routetemplate.RuleBaseValues;
035: import edu.iu.uis.eden.routetemplate.RuleDelegation;
036: import edu.iu.uis.eden.routetemplate.RuleExtension;
037: import edu.iu.uis.eden.routetemplate.RuleExtensionValue;
038: import edu.iu.uis.eden.routetemplate.RuleResponsibility;
039: import edu.iu.uis.eden.test.OldClearDatabaseLifecycle;
040:
041: /**
042: * Tests the RuleXmlExporter by importing XML, exporting it, and then re-importing the xml.<br><br>
043: *
044: * NOTE: It's important to note that the success of this test depends on all of the Rules in any
045: * XML having unique descriptions as this is the only way for the test to identify
046: * the rules from the original imported XML and the XML imported from the export.
047: *
048: * @author Eric Westfall
049: */
050: public class RuleXmlExporterTest extends XmlExporterTestCase {
051:
052: @Test
053: public void testExport() throws Exception {
054: loadXmlFile("edu/iu/uis/eden/actions/ActionsConfig.xml");
055: loadXmlStream(new FileInputStream(
056: "conf/load_from_xml/RuleAttributeContent.xml"));
057: loadXmlStream(new FileInputStream(
058: "conf/load_from_xml/RuleTemplateContent.xml"));
059: loadXmlStream(new FileInputStream(
060: "conf/load_from_xml/DocumentTypeContent.xml"));
061: loadXmlStream(new FileInputStream(
062: "conf/load_from_xml/RuleContent.xml"));
063: assertExport();
064: }
065:
066: /**
067: * Note that the assertion here will fail if you have multiple rules with the same description.
068: */
069: protected void assertExport() throws Exception {
070: // export all existing rules and their dependencies (document types, rule templates, rule attributes)
071: List oldRules = KEWServiceLocator.getRuleService()
072: .fetchAllRules(true);
073: assertAllRulesHaveUniqueNames(oldRules);
074:
075: ExportDataSet dataSet = new ExportDataSet(ExportFormat.XML);
076: dataSet.getRules().addAll(oldRules);
077: dataSet.getDocumentTypes().addAll(
078: KEWServiceLocator.getDocumentTypeService()
079: .findAllCurrent());
080: dataSet.getRuleTemplates().addAll(
081: KEWServiceLocator.getRuleTemplateService().findAll());
082: dataSet.getRuleAttributes().addAll(
083: KEWServiceLocator.getRuleAttributeService().findAll());
084: byte[] xmlBytes = KEWServiceLocator.getXmlExporterService()
085: .export(ExportFormat.XML, dataSet);
086: assertTrue("XML should be non empty.", xmlBytes != null
087: && xmlBytes.length > 0);
088:
089: // now clear the tables
090: new OldClearDatabaseLifecycle().start();
091:
092: // import the exported xml
093: loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(
094: xmlBytes)));
095:
096: List newRules = KEWServiceLocator.getRuleService()
097: .fetchAllRules(true);
098: assertEquals("Should have same number of old and new Rules.",
099: oldRules.size(), newRules.size());
100: for (Iterator iterator = oldRules.iterator(); iterator
101: .hasNext();) {
102: RuleBaseValues oldRule = (RuleBaseValues) iterator.next();
103: boolean foundRule = false;
104: for (Iterator iterator2 = newRules.iterator(); iterator2
105: .hasNext();) {
106: RuleBaseValues newRule = (RuleBaseValues) iterator2
107: .next();
108: if (oldRule.getDescription().equals(
109: newRule.getDescription())) {
110: assertRuleExport(oldRule, newRule);
111: foundRule = true;
112: }
113: }
114: assertTrue("Could not locate the new rule for description "
115: + oldRule.getDescription(), foundRule);
116: }
117: }
118:
119: private void assertRuleExport(RuleBaseValues oldRule,
120: RuleBaseValues newRule) {
121: assertFalse("Ids should be different.", oldRule
122: .getRuleBaseValuesId().equals(
123: newRule.getRuleBaseValuesId()));
124: assertEquals(oldRule.getActiveInd(), newRule.getActiveInd());
125: assertEquals(DateUtils.round(oldRule.getActivationDate(),
126: Calendar.DATE), DateUtils.round(newRule
127: .getActivationDate(), Calendar.DATE));
128: assertEquals(oldRule.getCurrentInd(), newRule.getCurrentInd());
129: assertEquals(oldRule.getDeactivationDate(), newRule
130: .getDeactivationDate());
131: assertEquals(oldRule.getDelegateRule(), newRule
132: .getDelegateRule());
133: assertEquals(oldRule.getDescription(), newRule.getDescription());
134: assertEquals(oldRule.getDocTypeName(), newRule.getDocTypeName());
135: assertEquals(DateUtils.round(oldRule.getFromDate(),
136: Calendar.DATE), DateUtils.round(newRule.getFromDate(),
137: Calendar.DATE));
138: assertEquals(oldRule.getIgnorePrevious(), newRule
139: .getIgnorePrevious());
140: assertEquals(oldRule.getPreviousVersionId(), newRule
141: .getPreviousVersionId());
142: assertEquals(oldRule.getRouteHeaderId(), newRule
143: .getRouteHeaderId());
144: assertEquals(oldRule.getRuleTemplate().getName(), newRule
145: .getRuleTemplate().getName());
146: assertEquals(DateUtils
147: .round(oldRule.getToDate(), Calendar.DATE), DateUtils
148: .round(newRule.getToDate(), Calendar.DATE));
149: assertEquals(oldRule.getVersionNbr(), newRule.getVersionNbr());
150:
151: assertRuleExtensions(oldRule.getRuleExtensions(), newRule
152: .getRuleExtensions());
153: assertResponsibilities(oldRule.getResponsibilities(), newRule
154: .getResponsibilities());
155:
156: }
157:
158: private void assertRuleExtensions(List oldRuleExtensions,
159: List newRuleExtensions) {
160: assertEquals(oldRuleExtensions.size(), newRuleExtensions.size());
161: for (Iterator iterator = oldRuleExtensions.iterator(); iterator
162: .hasNext();) {
163: RuleExtension oldExtension = (RuleExtension) iterator
164: .next();
165: boolean foundExtension = false;
166: for (Iterator iterator2 = newRuleExtensions.iterator(); iterator2
167: .hasNext();) {
168: RuleExtension newExtension = (RuleExtension) iterator2
169: .next();
170: if (oldExtension.getRuleTemplateAttribute()
171: .getRuleAttribute().getName().equals(
172: newExtension.getRuleTemplateAttribute()
173: .getRuleAttribute().getName())
174: && oldExtension
175: .getRuleTemplateAttribute()
176: .getRuleTemplate()
177: .getName()
178: .equals(
179: newExtension
180: .getRuleTemplateAttribute()
181: .getRuleTemplate()
182: .getName())) {
183: assertExtensionValues(oldExtension
184: .getExtensionValues(), newExtension
185: .getExtensionValues());
186: foundExtension = true;
187: break;
188: }
189: }
190: assertTrue("Could not locate rule extension.",
191: foundExtension);
192: }
193: }
194:
195: private void assertExtensionValues(List oldExtensionValues,
196: List newExtensionValues) {
197: assertEquals(oldExtensionValues.size(), newExtensionValues
198: .size());
199: for (Iterator iterator = oldExtensionValues.iterator(); iterator
200: .hasNext();) {
201: RuleExtensionValue oldValue = (RuleExtensionValue) iterator
202: .next();
203: boolean foundValue = false;
204: for (Iterator iterator2 = oldExtensionValues.iterator(); iterator2
205: .hasNext();) {
206: RuleExtensionValue newValue = (RuleExtensionValue) iterator2
207: .next();
208: if (oldValue.getKey().equals(newValue.getKey())) {
209: assertEquals(oldValue.getValue(), newValue
210: .getValue());
211: foundValue = true;
212: break;
213: }
214: }
215: assertTrue("Could not locate extension value.", foundValue);
216: }
217: }
218:
219: private void assertResponsibilities(List oldResps, List newResps) {
220: assertEquals(oldResps.size(), newResps.size());
221: for (Iterator iterator = oldResps.iterator(); iterator
222: .hasNext();) {
223: RuleResponsibility oldResp = (RuleResponsibility) iterator
224: .next();
225: boolean foundResp = false;
226: for (Iterator iterator2 = newResps.iterator(); iterator2
227: .hasNext();) {
228: RuleResponsibility newResp = (RuleResponsibility) iterator2
229: .next();
230: if (oldResp.getRuleResponsibilityName().equals(
231: newResp.getRuleResponsibilityName())) {
232: assertEquals(oldResp.getActionRequestedCd(),
233: newResp.getActionRequestedCd());
234: assertEquals(oldResp.getApprovePolicy(), newResp
235: .getApprovePolicy());
236: assertEquals(oldResp.getResolvedRoleName(), newResp
237: .getResolvedRoleName());
238: assertEquals(oldResp.getRole(), newResp.getRole());
239: assertEquals(oldResp.getRuleResponsibilityType(),
240: newResp.getRuleResponsibilityType());
241: assertEquals(oldResp.getPriority(), newResp
242: .getPriority());
243: assertDelegations(oldResp.getDelegationRules(),
244: newResp.getDelegationRules());
245: foundResp = true;
246: break;
247: }
248: }
249: assertTrue("Could not locate responsibility "
250: + oldResp.getRuleResponsibilityName() + " on rule "
251: + oldResp.getRuleBaseValues().getDescription(),
252: foundResp);
253: }
254: }
255:
256: private void assertDelegations(List oldDelegations,
257: List newDelegations) {
258: assertEquals(oldDelegations.size(), newDelegations.size());
259: for (Iterator iterator = oldDelegations.iterator(); iterator
260: .hasNext();) {
261: RuleDelegation oldDelegation = (RuleDelegation) iterator
262: .next();
263: boolean foundDelegation = false;
264: for (Iterator iterator2 = newDelegations.iterator(); iterator2
265: .hasNext();) {
266: RuleDelegation newDelegation = (RuleDelegation) iterator2
267: .next();
268: if (oldDelegation.getDelegationRuleBaseValues()
269: .getDescription().equals(
270: newDelegation
271: .getDelegationRuleBaseValues()
272: .getDescription())) {
273: assertEquals(oldDelegation.getDelegationType(),
274: newDelegation.getDelegationType());
275: assertRuleExport(oldDelegation
276: .getDelegationRuleBaseValues(),
277: newDelegation.getDelegationRuleBaseValues());
278: foundDelegation = true;
279: break;
280: }
281: }
282: assertTrue("Could not locate delegation.", foundDelegation);
283: }
284: }
285:
286: private void assertAllRulesHaveUniqueNames(List rules)
287: throws Exception {
288: Set<String> ruleDescriptions = new HashSet<String>();
289: for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
290: RuleBaseValues rule = (RuleBaseValues) iterator.next();
291: assertFalse(
292: "Found 2 rules with the same description '"
293: + rule.getDescription()
294: + "'. "
295: + "In order for this test to work, all rules in the configuration files must have unique descriptions.",
296: ruleDescriptions.contains(rule.getDescription()));
297: ruleDescriptions.add(rule.getDescription());
298: }
299: }
300:
301: }
|