View Javadoc

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