001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.xml.export;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertFalse;
020    import static org.junit.Assert.assertNull;
021    import static org.junit.Assert.assertTrue;
022    
023    import java.io.BufferedInputStream;
024    import java.io.ByteArrayInputStream;
025    import java.util.Iterator;
026    import java.util.List;
027    
028    import org.junit.Test;
029    import org.kuali.rice.core.api.CoreApiServiceLocator;
030    import org.kuali.rice.kew.export.KewExportDataSet;
031    import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
032    import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
033    import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
034    import org.kuali.rice.kew.service.KEWServiceLocator;
035    import org.kuali.rice.test.BaselineTestCase;
036    import org.kuali.rice.test.ClearDatabaseLifecycle;
037    
038    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
039    public class RuleTemplateXmlExporterTest extends XmlExporterTestCase {
040    
041        @Test public void testExport() throws Exception {
042            loadXmlFile("RuleTemplateExportConfig.xml");
043            assertExport();
044        }
045    
046        protected void assertExport() throws Exception {
047            // export all existing rule templates and their dependencies (rule attributes)
048            List oldRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
049            KewExportDataSet dataSet = new KewExportDataSet();
050            dataSet.getRuleTemplates().addAll(oldRuleTemplates);
051            dataSet.getRuleAttributes().addAll(KEWServiceLocator.getRuleAttributeService().findAll());
052            byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet());
053            assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
054    
055            // now clear the tables
056            new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()).start();
057    
058            // import the exported xml
059            loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
060    
061            List newRuleTemplates = KEWServiceLocator.getRuleTemplateService().findAll();
062            assertEquals("Should have same number of old and new RuleTemplates.", oldRuleTemplates.size(), newRuleTemplates.size());
063            for (Iterator iterator = oldRuleTemplates.iterator(); iterator.hasNext();) {
064                RuleTemplateBo oldRuleTemplate = (RuleTemplateBo) iterator.next();
065                boolean foundTemplate = false;
066                for (Iterator iterator2 = newRuleTemplates.iterator(); iterator2.hasNext();) {
067                    RuleTemplateBo newRuleTemplate = (RuleTemplateBo) iterator2.next();
068                    if (oldRuleTemplate.getName().equals(newRuleTemplate.getName())) {
069                        assertRuleTemplateExport(oldRuleTemplate, newRuleTemplate);
070                        foundTemplate = true;
071                    }
072                }
073                assertTrue("Could not locate the new rule template for name " + oldRuleTemplate.getName(), foundTemplate);
074            }
075        }
076    
077        private void assertRuleTemplateExport(RuleTemplateBo oldRuleTemplate, RuleTemplateBo newRuleTemplate) {
078            assertFalse("Ids should be different.", oldRuleTemplate.getId().equals(newRuleTemplate.getId()));
079            assertEquals(oldRuleTemplate.getDescription(), newRuleTemplate.getDescription());
080            assertEquals(oldRuleTemplate.getName(), newRuleTemplate.getName());
081            if (oldRuleTemplate.getDelegationTemplate() != null) {
082                assertRuleTemplateExport(oldRuleTemplate.getDelegationTemplate(), newRuleTemplate.getDelegationTemplate());
083            } else {
084                assertNull(newRuleTemplate.getDelegationTemplate());
085            }
086            assertAttributes(oldRuleTemplate.getRuleTemplateAttributes(), newRuleTemplate.getRuleTemplateAttributes(), "attribute");
087            assertAttributes(oldRuleTemplate.getActiveRuleTemplateAttributes(), newRuleTemplate.getActiveRuleTemplateAttributes(), "active attribute");
088            assertOptions(oldRuleTemplate.getRuleTemplateOptions(), newRuleTemplate.getRuleTemplateOptions());
089        }
090    
091        private void assertAttributes(List oldAttributes, List newAttributes, String errorMessageAttributeLabel) {
092            assertEquals(oldAttributes.size(), newAttributes.size());
093            for (Iterator iterator = oldAttributes.iterator(); iterator.hasNext();) {
094                RuleTemplateAttributeBo oldAttribute = (RuleTemplateAttributeBo) iterator.next();
095                boolean foundAttribute = false;
096                for (Iterator iterator2 = newAttributes.iterator(); iterator2.hasNext();) {
097                    RuleTemplateAttributeBo newAttribute = (RuleTemplateAttributeBo) iterator2.next();
098                    if (oldAttribute.getRuleAttribute().getName().equals(newAttribute.getRuleAttribute().getName())) {
099                        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    }