View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.xml.export;
18  
19  import java.io.BufferedInputStream;
20  import java.io.ByteArrayInputStream;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.junit.Test;
25  import org.kuali.rice.kew.export.ExportDataSet;
26  import org.kuali.rice.kew.rule.RuleTemplateOption;
27  import org.kuali.rice.kew.rule.bo.RuleTemplate;
28  import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
29  import org.kuali.rice.kew.service.KEWServiceLocator;
30  import org.kuali.rice.test.ClearDatabaseLifecycle;
31  
32  
33  public class RuleTemplateXmlExporterTest extends XmlExporterTestCase {
34  
35      @Test public void testExport() throws Exception {
36          loadXmlFile("RuleTemplateExportConfig.xml");
37          assertExport();
38      }
39  
40      protected void assertExport() throws Exception {
41          // export all existing rule templates and their dependencies (rule attributes)
42          List oldRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
43          ExportDataSet dataSet = new ExportDataSet();
44          dataSet.getRuleTemplates().addAll(oldRuleTemplates);
45          dataSet.getRuleAttributes().addAll(KEWServiceLocator.getRuleAttributeService().findAll());
46          byte[] xmlBytes = KEWServiceLocator.getXmlExporterService().export(dataSet);
47          assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
48  
49          // now clear the tables
50          new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()).start();
51  
52          // import the exported xml
53          loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
54  
55          List newRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
56          assertEquals("Should have same number of old and new RuleTemplates.", oldRuleTemplates.size(), newRuleTemplates.size());
57          for (Iterator iterator = oldRuleTemplates.iterator(); iterator.hasNext();) {
58              RuleTemplate oldRuleTemplate = (RuleTemplate) iterator.next();
59              boolean foundTemplate = false;
60              for (Iterator iterator2 = newRuleTemplates.iterator(); iterator2.hasNext();) {
61                  RuleTemplate newRuleTemplate = (RuleTemplate) iterator2.next();
62                  if (oldRuleTemplate.getName().equals(newRuleTemplate.getName())) {
63                      assertRuleTemplateExport(oldRuleTemplate, newRuleTemplate);
64                      foundTemplate = true;
65                  }
66              }
67              assertTrue("Could not locate the new rule template for name " + oldRuleTemplate.getName(), foundTemplate);
68          }
69      }
70  
71      private void assertRuleTemplateExport(RuleTemplate oldRuleTemplate, RuleTemplate newRuleTemplate) {
72          assertFalse("Ids should be different.", oldRuleTemplate.getRuleTemplateId().equals(newRuleTemplate.getRuleTemplateId()));
73          assertEquals(oldRuleTemplate.getDescription(), newRuleTemplate.getDescription());
74          assertEquals(oldRuleTemplate.getName(), newRuleTemplate.getName());
75          if (oldRuleTemplate.getDelegationTemplate() != null) {
76              assertRuleTemplateExport(oldRuleTemplate.getDelegationTemplate(), newRuleTemplate.getDelegationTemplate());
77          } else {
78              assertNull(newRuleTemplate.getDelegationTemplate());
79          }
80          assertAttributes(oldRuleTemplate.getRuleTemplateAttributes(), newRuleTemplate.getRuleTemplateAttributes(), "attribute");
81          assertAttributes(oldRuleTemplate.getActiveRuleTemplateAttributes(), newRuleTemplate.getActiveRuleTemplateAttributes(), "active attribute");
82          assertOptions(oldRuleTemplate.getRuleTemplateOptions(), newRuleTemplate.getRuleTemplateOptions());
83      }
84  
85      private void assertAttributes(List oldAttributes, List newAttributes, String errorMessageAttributeLabel) {
86          assertEquals(oldAttributes.size(), newAttributes.size());
87          for (Iterator iterator = oldAttributes.iterator(); iterator.hasNext();) {
88              RuleTemplateAttribute oldAttribute = (RuleTemplateAttribute) iterator.next();
89              boolean foundAttribute = false;
90              for (Iterator iterator2 = newAttributes.iterator(); iterator2.hasNext();) {
91                  RuleTemplateAttribute newAttribute = (RuleTemplateAttribute) iterator2.next();
92                  if (oldAttribute.getRuleAttribute().getName().equals(newAttribute.getRuleAttribute().getName())) {
93                      assertEquals(oldAttribute.getRequired(), newAttribute.getRequired());
94                      foundAttribute = true;
95                  }
96              }
97              assertTrue("Could not locate " + errorMessageAttributeLabel + " with name '" + oldAttribute.getRuleAttribute().getName() + "' in new attributes list.", foundAttribute);
98          }
99      }
100 
101     private void assertOptions(List oldTemplateOptions, List newTemplateOptions) {
102         assertEquals(oldTemplateOptions.size(), newTemplateOptions.size());
103         for (Iterator iterator = oldTemplateOptions.iterator(); iterator.hasNext();) {
104             RuleTemplateOption oldOption = (RuleTemplateOption) iterator.next();
105             boolean foundOption = false;
106             for (Iterator iterator2 = newTemplateOptions.iterator(); iterator2.hasNext();) {
107                 RuleTemplateOption newOption = (RuleTemplateOption) iterator2.next();
108                 if (oldOption.getKey().equals(newOption.getKey())) {
109                     assertEquals(oldOption.getValue(), newOption.getValue());
110                     foundOption = true;
111                 }
112             }
113             assertTrue("Could not locate rule template option.", foundOption);
114         }
115     }
116 
117 }