View Javadoc
1   /**
2    * Copyright 2005-2014 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          // Previous tests may not have forced a full clear of the tables,
40          new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()).start();
41  
42          loadXmlFile("RuleTemplateExportConfig.xml");
43          assertExport();
44      }
45  
46      protected void assertExport() throws Exception {
47          // export all existing rule templates and their dependencies (rule attributes)
48          List oldRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
49          KewExportDataSet dataSet = new KewExportDataSet();
50          dataSet.getRuleTemplates().addAll(oldRuleTemplates);
51          dataSet.getRuleAttributes().addAll(KEWServiceLocator.getRuleAttributeService().findAll());
52          byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
53          assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
54  
55          // now clear the tables
56          new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()).start();
57  
58          // import the exported xml
59          loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
60  
61          List newRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
62          assertEquals("Should have same number of old and new RuleTemplates.", oldRuleTemplates.size(), newRuleTemplates.size());
63          for (Iterator iterator = oldRuleTemplates.iterator(); iterator.hasNext();) {
64              RuleTemplateBo oldRuleTemplate = (RuleTemplateBo) iterator.next();
65              boolean foundTemplate = false;
66              for (Iterator iterator2 = newRuleTemplates.iterator(); iterator2.hasNext();) {
67                  RuleTemplateBo newRuleTemplate = (RuleTemplateBo) iterator2.next();
68                  if (oldRuleTemplate.getName().equals(newRuleTemplate.getName())) {
69                      assertRuleTemplateExport(oldRuleTemplate, newRuleTemplate);
70                      foundTemplate = true;
71                  }
72              }
73              assertTrue("Could not locate the new rule template for name " + oldRuleTemplate.getName(), foundTemplate);
74          }
75      }
76  
77      private void assertRuleTemplateExport(RuleTemplateBo oldRuleTemplate, RuleTemplateBo newRuleTemplate) {
78          assertFalse("Ids should be different.", oldRuleTemplate.getId().equals(newRuleTemplate.getId()));
79          assertEquals(oldRuleTemplate.getDescription(), newRuleTemplate.getDescription());
80          assertEquals(oldRuleTemplate.getName(), newRuleTemplate.getName());
81          if (oldRuleTemplate.getDelegationTemplate() != null) {
82              assertRuleTemplateExport(oldRuleTemplate.getDelegationTemplate(), newRuleTemplate.getDelegationTemplate());
83          } else {
84              assertNull(newRuleTemplate.getDelegationTemplate());
85          }
86          assertAttributes(oldRuleTemplate.getRuleTemplateAttributes(), newRuleTemplate.getRuleTemplateAttributes(), "attribute");
87          assertAttributes(oldRuleTemplate.getActiveRuleTemplateAttributes(), newRuleTemplate.getActiveRuleTemplateAttributes(), "active attribute");
88          assertOptions(oldRuleTemplate.getRuleTemplateOptions(), newRuleTemplate.getRuleTemplateOptions());
89      }
90  
91      private void assertAttributes(List oldAttributes, List newAttributes, String errorMessageAttributeLabel) {
92          assertEquals(oldAttributes.size(), newAttributes.size());
93          for (Iterator iterator = oldAttributes.iterator(); iterator.hasNext();) {
94              RuleTemplateAttributeBo oldAttribute = (RuleTemplateAttributeBo) iterator.next();
95              boolean foundAttribute = false;
96              for (Iterator iterator2 = newAttributes.iterator(); iterator2.hasNext();) {
97                  RuleTemplateAttributeBo newAttribute = (RuleTemplateAttributeBo) iterator2.next();
98                  if (oldAttribute.getRuleAttribute().getName().equals(newAttribute.getRuleAttribute().getName())) {
99                      assertEquals(oldAttribute.getRequired(), newAttribute.getRequired());
100                     foundAttribute = true;
101                 }
102             }
103             assertTrue("Could not locate " + errorMessageAttributeLabel + " with name '" + oldAttribute.getRuleAttribute().getName() + "' in new attributes list.", foundAttribute);
104         }
105     }
106 
107     private void assertOptions(List oldTemplateOptions, List newTemplateOptions) {
108         assertEquals(oldTemplateOptions.size(), newTemplateOptions.size());
109         for (Iterator iterator = oldTemplateOptions.iterator(); iterator.hasNext();) {
110             RuleTemplateOptionBo oldOption = (RuleTemplateOptionBo) iterator.next();
111             boolean foundOption = false;
112             for (Iterator iterator2 = newTemplateOptions.iterator(); iterator2.hasNext();) {
113                 RuleTemplateOptionBo newOption = (RuleTemplateOptionBo) iterator2.next();
114                 if (oldOption.getCode().equals(newOption.getCode())) {
115                     assertEquals(oldOption.getValue(), newOption.getValue());
116                     foundOption = true;
117                 }
118             }
119             assertTrue("Could not locate rule template option.", foundOption);
120         }
121     }
122 
123 }