001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.junit.Test;
025: import org.kuali.rice.core.Core;
026: import org.kuali.workflow.test.WorkflowTestCase;
027:
028: import edu.iu.uis.eden.KEWServiceLocator;
029: import edu.iu.uis.eden.WorkflowServiceErrorException;
030: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
031: import edu.iu.uis.eden.exception.InvalidXmlException;
032: import edu.iu.uis.eden.test.TestUtilities;
033: import edu.iu.uis.eden.xml.RuleXmlParser;
034:
035: public class RuleXmlParserTest extends WorkflowTestCase {
036:
037: protected void loadTestData() throws Exception {
038: loadXmlFile("RouteTemplateConfig.xml");
039: loadXmlFile("DuplicateRuleToImport.xml");
040: }
041:
042: @Test
043: public void testRuleXmlParserCacheUpdate() throws Exception {
044: //Lifecycle cache = new CacheLifecycle();
045: //cache.start();
046: RuleService ruleService = KEWServiceLocator.getRuleService();
047: int ruleSize = ruleService
048: .fetchAllCurrentRulesForTemplateDocCombination(
049: "TestRuleTemplate", "TestDocumentType").size();
050:
051: List collections = new ArrayList();
052: //ultimately it is the content of RulesToImport that determines whether or not we're
053: //going to hit the rules xml parser
054: InputStream xmlFile = TestUtilities.loadResource(this
055: .getClass(), "RulesToImport.xml");
056: collections.add(getFileXmlDocCollection(xmlFile,
057: "WorkflowUnitTestTemp"));
058: KEWServiceLocator.getXmlIngesterService().ingest(collections,
059: null);
060:
061: Thread.sleep(5000);//give cache time to reload;
062: int newRuleSize = ruleService
063: .fetchAllCurrentRulesForTemplateDocCombination(
064: "TestRuleTemplate", "TestDocumentType").size();
065: assertEquals(
066: "Three more rules should have been returned from the cached service",
067: ruleSize + 3, newRuleSize);
068: //cache.stop();
069: }
070:
071: @Test
072: public void testDuplicateRule() throws IOException,
073: InvalidXmlException {
074: InputStream stream = getClass().getResourceAsStream(
075: "DuplicateRuleToImport.xml");
076: assertNotNull(stream);
077: log.info("Importing duplicate again");
078: try {
079: KEWServiceLocator.getRuleService().loadXml(stream, null);
080: } catch (WorkflowServiceErrorException wsee) {
081: if (wsee.getCause() == null
082: || !(wsee.getCause() instanceof InvalidXmlException)) {
083: throw wsee;
084: }
085: }
086: }
087:
088: @Test
089: public void testNotDuplicateRule() throws IOException,
090: InvalidXmlException {
091: InputStream stream = getClass().getResourceAsStream(
092: "NotADuplicateRuleToImport.xml");
093: assertNotNull(stream);
094: log.info("Importing a unique rule");
095: // load the unique template first
096: KEWServiceLocator.getRuleTemplateService()
097: .loadXml(stream, null);
098: stream = getClass().getResourceAsStream(
099: "NotADuplicateRuleToImport.xml");
100: // then the rule
101: KEWServiceLocator.getRuleService().loadXml(stream, null);
102: }
103:
104: @Test
105: public void testParameterReplacement() throws IOException,
106: InvalidXmlException, EdenUserNotFoundException {
107: Core.getCurrentContextConfig().overrideProperty(
108: "test.replacement.user", "user3");
109: Core.getCurrentContextConfig().overrideProperty(
110: "test.replacement.workgroup", "WorkflowAdmin");
111: List<RuleBaseValues> rules = new RuleXmlParser()
112: .parseRules(getClass().getResourceAsStream(
113: "ParameterizedRule.xml"));
114: assertEquals(1, rules.size());
115: RuleBaseValues rule = rules.get(0);
116: assertEquals(2, rule.getResponsibilities().size());
117: RuleResponsibility resp = (RuleResponsibility) rule
118: .getResponsibilities().get(0);
119:
120: if (resp.isUsingWorkflowUser()) {
121: assertEquals("user3", resp.getWorkflowUser()
122: .getAuthenticationUserId().getId());
123: } else {
124: assertEquals("WorkflowAdmin", resp.getWorkgroup()
125: .getGroupNameId().getNameId());
126: }
127:
128: Core.getCurrentContextConfig().overrideProperty(
129: "test.replacement.user", "user1");
130: Core.getCurrentContextConfig().overrideProperty(
131: "test.replacement.workgroup", "TestWorkgroup");
132: rules = new RuleXmlParser().parseRules(getClass()
133: .getResourceAsStream("ParameterizedRule.xml"));
134: assertEquals(1, rules.size());
135: rule = rules.get(0);
136: assertEquals(2, rule.getResponsibilities().size());
137: resp = (RuleResponsibility) rule.getResponsibilities().get(0);
138:
139: if (resp.isUsingWorkflowUser()) {
140: assertEquals("user1", resp.getWorkflowUser()
141: .getAuthenticationUserId().getId());
142: } else {
143: assertEquals("TestWorkgroup", resp.getWorkgroup()
144: .getGroupNameId().getNameId());
145: }
146:
147: }
148: }
|