View Javadoc

1   /**
2    * Copyright 2005-2013 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.apache.commons.lang.StringUtils;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.kew.export.KewExportDataSet;
22  import org.kuali.rice.kew.rule.bo.RuleAttribute;
23  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
24  import org.kuali.rice.kew.service.KEWServiceLocator;
25  import org.kuali.rice.test.BaselineTestCase;
26  
27  import java.io.BufferedInputStream;
28  import java.io.ByteArrayInputStream;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import static org.junit.Assert.*;
33  
34  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
35  public class RuleAttributeXmlExporterTest extends XmlExporterTestCase {
36      
37      /**
38       * This will test some rule attributes with routing and searching config.
39       */
40  	@Test public void testExport() throws Exception {
41          loadXmlFile("RuleAttributeExportConfig.xml");
42          assertExport();
43      }
44          
45      protected void assertExport() throws Exception {
46          // export all existing rule attributes
47          List oldRuleAttributes = KEWServiceLocator.getRuleAttributeService().findAll();
48          KewExportDataSet dataSet = new KewExportDataSet();
49          dataSet.getRuleAttributes().addAll(oldRuleAttributes);
50          byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
51          assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
52          
53          // import the exported xml
54          loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
55          
56          List newRuleAttributes = KEWServiceLocator.getRuleAttributeService().findAll();
57          assertEquals("Should have same number of old and new RuleAttributes.", oldRuleAttributes.size(), newRuleAttributes.size());
58          for (Iterator iterator = oldRuleAttributes.iterator(); iterator.hasNext();) {
59              RuleAttribute oldRuleAttribute = (RuleAttribute) iterator.next();
60              boolean foundAttribute = false;
61              for (Iterator iterator2 = newRuleAttributes.iterator(); iterator2.hasNext();) {
62                  RuleAttribute newRuleAttribute = (RuleAttribute) iterator2.next();
63                  if (oldRuleAttribute.getName().equals(newRuleAttribute.getName())) {
64                      assertRuleAttributeExport(oldRuleAttribute, newRuleAttribute);
65                      foundAttribute = true;
66                  }
67              }
68              assertTrue("Could not locate the new rule attribute for name " + oldRuleAttribute.getName(), foundAttribute);
69          }
70      }
71      
72      private void assertRuleAttributeExport(RuleAttribute oldRuleAttribute, RuleAttribute newRuleAttribute) {
73          // ids should be the same because we don't version rule attributes, but thier version number should be different
74          assertEquals("Ids should be the same.", oldRuleAttribute.getId(), newRuleAttribute.getId());
75          assertFalse("Version numbers should be different.", oldRuleAttribute.getVersionNumber().equals(newRuleAttribute.getVersionNumber()));
76          assertEquals(oldRuleAttribute.getDescription(), newRuleAttribute.getDescription());
77          assertEquals(oldRuleAttribute.getName(), newRuleAttribute.getName());
78          assertEquals(oldRuleAttribute.getLabel(), newRuleAttribute.getLabel());
79          assertEquals(oldRuleAttribute.getType(), newRuleAttribute.getType());
80          assertEquals(StringUtils.deleteWhitespace(oldRuleAttribute.getXmlConfigData()), StringUtils.deleteWhitespace(newRuleAttribute.getXmlConfigData()));
81          assertRuleTemplateAttributes(oldRuleAttribute.getRuleTemplateAttributes(), newRuleAttribute.getRuleTemplateAttributes());
82      }
83      
84      private void assertRuleTemplateAttributes(List oldRuleTemplateAttributes, List newRuleTemplateAttributes) {
85          assertEquals(oldRuleTemplateAttributes.size(), newRuleTemplateAttributes.size());
86          for (Iterator iterator = oldRuleTemplateAttributes.iterator(); iterator.hasNext();) {
87              RuleTemplateAttributeBo oldAttribute = (RuleTemplateAttributeBo) iterator.next();
88              boolean foundAttribute = false;
89              for (Iterator iterator2 = oldRuleTemplateAttributes.iterator(); iterator2.hasNext();) {
90                  RuleTemplateAttributeBo newAttribute = (RuleTemplateAttributeBo) iterator2.next();
91                  if (oldAttribute.getRuleAttribute().getName().equals(newAttribute.getRuleAttribute().getName())) {
92                      assertEquals(oldAttribute.getRequired(), newAttribute.getRequired());
93                      foundAttribute = true;
94                  }
95              }
96              assertTrue("Could not locate new attribute.", foundAttribute);
97          }
98      }
99          
100 }