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;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.util.Date;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import org.junit.Test;
30  import org.kuali.rice.kew.rule.RuleBaseValues;
31  import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
32  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
33  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kew.test.KEWTestCase;
36  import org.kuali.rice.kew.api.KewApiConstants;
37  
38  
39  /**
40   * This is a Test class to test the rule template xml parser
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class RuleTemplateXmlParserTest extends KEWTestCase {
46      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleTemplateXmlParserTest.class);
47  
48      private static final String RULE_ATTRIBUTE_ONE = "TemplateTestRuleAttribute1";
49      private static final String RULE_ATTRIBUTE_TWO = "TemplateTestRuleAttribute2";
50      private static final String RULE_ATTRIBUTE_THREE = "TemplateTestRuleAttribute3";
51      private static final String RULE_ATTRIBUTE_FOUR = "TemplateTestRuleAttribute4";
52      private static final String RULE_ATTRIBUTE_FIVE = "TemplateTestRuleAttribute5";
53  
54      private enum TemplateParserGeneralFixture {
55          VALID_TEMPLATE_MIN_XML("ValidRuleTemplate", "RuleTemplate_Valid", new String[]{RULE_ATTRIBUTE_ONE}, new String[0], new String[]{RULE_ATTRIBUTE_ONE}, new String[0]),
56          VALID_TEMPLATE_FULL_XML("ValidRuleTemplate_Full", "RuleTemplate_Valid_Full", new String[]{RULE_ATTRIBUTE_ONE}, new String[0], new String[]{RULE_ATTRIBUTE_ONE}, new String[0]),
57          // below has allowOverwrite=true
58          VALID_TEMPLATE_OVERWRITE("ValidRuleTemplateOverwrite", "RuleTemplate_Valid", new String[]{RULE_ATTRIBUTE_ONE}, new String[]{RULE_ATTRIBUTE_FOUR}, new String[]{RULE_ATTRIBUTE_FOUR}, new String[]{RULE_ATTRIBUTE_ONE}),
59          VALID_TEMPLATE_FULL_OVERWRITE("ValidRuleTemplateFullOverwrite", "RuleTemplate_Valid_Full", new String[]{RULE_ATTRIBUTE_ONE}, new String[]{RULE_ATTRIBUTE_FOUR}, new String[]{RULE_ATTRIBUTE_FOUR}, new String[]{RULE_ATTRIBUTE_ONE}),
60          INVALID_TEMPLATE_OVERWRITE("InvalidRuleTemplateOverwrite", "RuleTemplate_Valid", new String[]{RULE_ATTRIBUTE_ONE}, new String[0], new String[]{RULE_ATTRIBUTE_ONE}, new String[0]),
61          VALID_TEMPLATE_WITH_FULL_DEFAULTS("ValidRuleTemplateWithFullDefaults", "RuleTemplate_Valid_Defaults", new String[]{RULE_ATTRIBUTE_TWO}, new String[0], new String[]{RULE_ATTRIBUTE_TWO}, new String[0]),
62          VALID_TEMPLATE_WITH_LIMITED_DEFAULTS("ValidRuleTemplateWithSomeDefaults", "RuleTemplate_Valid_Some_Defaults", new String[]{RULE_ATTRIBUTE_THREE}, new String[0], new String[0], new String[]{RULE_ATTRIBUTE_THREE}),
63          VALID_TEMPLATE_WITH_LIMITED_DEFAULTS_OVERWRITE("ValidRuleTemplateWithSomeDefaultsOverwrite", "RuleTemplate_Valid_Some_Defaults", new String[]{RULE_ATTRIBUTE_THREE}, new String[0], new String[0], new String[]{RULE_ATTRIBUTE_THREE}),
64          VALID_TEMPLATE_WITH_DESCRIPTION_DEFAULT("ValidRuleTemplateWithDefaultsDescriptionOnly", "RuleTemplate_Valid_Description_Default", new String[]{RULE_ATTRIBUTE_TWO}, new String[0], new String[]{RULE_ATTRIBUTE_TWO}, new String[0]),
65          VALID_TEMPLATE_WITH_LIMITED_DEFAULTS_REMOVED("ValidRuleTemplateWithSomeRemovedDefaultsOverwrite", "RuleTemplate_Valid_Some_Defaults", new String[]{RULE_ATTRIBUTE_THREE}, new String[0], new String[0], new String[]{RULE_ATTRIBUTE_THREE})
66          ;
67  
68          public String fileNameParameter;
69          public String ruleTemplateName;
70          public String[] activeAttributeNames;
71          public String[] inactiveAttributeNames;
72          public String[] requiredAttributeNames;
73          public String[] nonRequiredAttributeNames;
74  
75          private TemplateParserGeneralFixture(String fileNameParameter, String ruleTemplateName, String[] activeAttributeNames, String[] inactiveAttributeNames, String[] requiredAttributeNames, String[] nonRequiredAttributeNames) {
76              this.fileNameParameter = fileNameParameter;
77              this.ruleTemplateName = ruleTemplateName;
78              this.activeAttributeNames = activeAttributeNames;
79              this.inactiveAttributeNames = inactiveAttributeNames;
80              this.requiredAttributeNames = requiredAttributeNames;
81              this.nonRequiredAttributeNames = nonRequiredAttributeNames;
82          }
83      }
84  
85      protected void loadTestData() throws Exception {
86          loadXmlFile("RuleTemplateConfig.xml");
87      }
88  
89      private void testTemplate(String docName, Class expectedException) throws Exception {
90          RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
91          String filename = "RT_" +  docName + ".xml";
92          try {
93              parser.parseRuleTemplates(getClass().getResourceAsStream(filename));
94              if (expectedException != null) {
95                  fail(filename + " successfully loaded.  Expected exception of class '" + expectedException + "'");
96              }
97          } catch (Exception e) {
98              if (expectedException == null || !(expectedException.isAssignableFrom(e.getClass()))) {
99                  throw e;
100             } else {
101                 log.error(filename + " exception: " + e);
102             }
103         }
104     }
105 
106     private void testListOfTemplateAttributes(List ruleTemplateAttributes, String[] activeRuleTemplateAttributeNames, String[] requiredRuleTemplateAttributeNames) {
107         for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator.hasNext();) {
108             RuleTemplateAttributeBo templateAttribute = (RuleTemplateAttributeBo) iterator.next();
109             String ruleAttributeName = templateAttribute.getRuleAttribute().getName();
110             
111             LOG.info("Attribute name '" + ruleAttributeName +"' active indicator is " + templateAttribute.isActive());
112             if (activeRuleTemplateAttributeNames == null) {
113                 assertEquals("Active indicator should be false for all attributes but is not for attribute '" + ruleAttributeName + "'",Boolean.FALSE, templateAttribute.getActive());
114             } else {
115                 runTestsOnTemplateAttributeField(ruleAttributeName, templateAttribute.isActive(), activeRuleTemplateAttributeNames, "active");
116             }
117 
118             LOG.info("Attribute name '" + ruleAttributeName +"' required indicator is " + templateAttribute.isRequired());
119             if (requiredRuleTemplateAttributeNames == null) {
120                 assertEquals("Required indicator should be false for all attributes but is not for attribute '" + ruleAttributeName + "'",Boolean.FALSE, templateAttribute.getRequired());
121             } else {
122                 runTestsOnTemplateAttributeField(ruleAttributeName, templateAttribute.isRequired(), requiredRuleTemplateAttributeNames, "required");
123             }
124         }
125     }
126     
127     private void testAllAttributesActive(List activeRuleTemplateAttributes, String[] activeRuleTemplateAttributeNames) {
128         for (Iterator iterator = activeRuleTemplateAttributes.iterator(); iterator.hasNext();) {
129             RuleTemplateAttributeBo activeTemplateAttribute = (RuleTemplateAttributeBo) iterator.next();
130             String ruleAttributeName = activeTemplateAttribute.getRuleAttribute().getName();
131             assertEquals("Template Attribute with name '" + ruleAttributeName + "' has invalid active value", Boolean.TRUE, activeTemplateAttribute.getActive());
132             boolean foundAttribute = false;
133             for (int i = 0; i < activeRuleTemplateAttributeNames.length; i++) {
134                 String shouldBeActiveAttributeName = activeRuleTemplateAttributeNames[i];
135                 if (shouldBeActiveAttributeName.equals(ruleAttributeName)) {
136                     foundAttribute = true;
137                     break;
138                 }
139             }
140             if (!foundAttribute) {
141                 fail("Template Attribute with name '" + ruleAttributeName + "' should have been in active template name list but was not found");
142             }
143         }
144     }
145     
146     private void runTestsOnTemplateAttributeField(String ruleAttributeName, boolean valueToConfirm, String[] attributeNamesShouldBeTrue, String errorMessageIdentifier) {
147         boolean foundAttribute = false;
148         for (String attributeNameThatShouldPass : attributeNamesShouldBeTrue) {
149             if (ruleAttributeName.equals(attributeNameThatShouldPass)) {
150                 foundAttribute = true;
151                 if (!valueToConfirm) {
152                     fail("Attribute with name '" + ruleAttributeName + "' should have been " + errorMessageIdentifier + " but is not");
153                 }
154             }
155         }
156         if ( (!foundAttribute) && (valueToConfirm) ) {
157             fail("Attribute with name '" + ruleAttributeName + "' should not be " + errorMessageIdentifier + " but is");
158         }
159     }
160 
161     /**
162      * Loads valid template definitions
163      */
164     @Test public void testLoadValidTemplate() throws Exception {
165         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_XML.fileNameParameter, null);
166 
167         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.fileNameParameter, null);
168         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.ruleTemplateName);
169         testListOfTemplateAttributes(template.getRuleTemplateAttributes(), TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.activeAttributeNames, TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.requiredAttributeNames);
170         testAllAttributesActive(template.getActiveRuleTemplateAttributes(), TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.activeAttributeNames);
171 
172         assertNoDefaultsSpecified(template);
173     }
174 
175     /**
176      * Loads a "minimal" template definition and then updates/overwrites it
177      */
178     @Test public void testLoadValidTemplateWithOverwrite() throws Exception {
179         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.fileNameParameter, null);
180         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_OVERWRITE.fileNameParameter, null); // allowOverwrite=true
181         
182         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_OVERWRITE.ruleTemplateName);
183         testListOfTemplateAttributes(template.getRuleTemplateAttributes(), TemplateParserGeneralFixture.VALID_TEMPLATE_OVERWRITE.activeAttributeNames, TemplateParserGeneralFixture.VALID_TEMPLATE_OVERWRITE.requiredAttributeNames);
184         testAllAttributesActive(template.getActiveRuleTemplateAttributes(), TemplateParserGeneralFixture.VALID_TEMPLATE_OVERWRITE.activeAttributeNames);
185         
186         assertNoDefaultsSpecified(template);
187     }
188     
189     /**
190      * Loads a "full" template definition and then updates/overwrites it
191      */
192     @Test public void testLoadValidTemplateFullWithOverwrite() throws Exception {
193         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_XML.fileNameParameter, null);
194         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_OVERWRITE.fileNameParameter, null); // allowOverwrite=true
195         
196         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_OVERWRITE.ruleTemplateName);
197         testListOfTemplateAttributes(template.getRuleTemplateAttributes(), TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_OVERWRITE.activeAttributeNames, TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_OVERWRITE.requiredAttributeNames);
198         testAllAttributesActive(template.getActiveRuleTemplateAttributes(), TemplateParserGeneralFixture.VALID_TEMPLATE_FULL_OVERWRITE.activeAttributeNames);
199 
200         assertNoDefaultsSpecified(template);
201     }
202 
203     /**
204      * Tests attempting to overwrite a template without the allowOverwrite flag set to true
205      */
206     @Test public void testLoadInvalidTemplateWithOverwrite() throws Exception {
207         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.fileNameParameter, null);
208         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_MIN_XML.fileNameParameter, RuntimeException.class);  // no allowOverwrite specified
209         testTemplate(TemplateParserGeneralFixture.INVALID_TEMPLATE_OVERWRITE.fileNameParameter, RuntimeException.class);  // allowOverwrite=false
210     }
211 
212     /**
213      * Tests loading a template with a full ruleDefaults section
214      */
215     @Test public void testLoadValidTemplateWithFullDefaults() throws Exception {
216         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_FULL_DEFAULTS.fileNameParameter, null);
217 
218         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_FULL_DEFAULTS.ruleTemplateName);
219 
220         // test the rule template options
221         List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
222         assertEquals(5, options.size());
223         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
224         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_APPROVE_REQ, "true");
225         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ, "false");
226         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_FYI_REQ, "false");
227         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_DEFAULT_CD, "A");
228 
229         // test those set in the default/template rule
230         RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
231         assertTrue(ruleDefaults.getTemplateRuleInd());
232         assertEquals("Testy Me A Template", ruleDefaults.getDescription());
233         assertEquals("01/11/2006", ruleDefaults.getFromDateString());
234         assertEquals("01/01/2100", ruleDefaults.getToDateString());
235         assertTrue(ruleDefaults.isForceAction());
236         assertFalse(ruleDefaults.isActive());
237     }
238 
239     /**
240      * Tests loading a template with a partial ruleDefaults section
241      */
242     @Test public void testLoadValidTemplateWithSomeDefaults() throws Exception {
243         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS.fileNameParameter, null);
244         
245         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS.ruleTemplateName);
246 
247         // test the rule template options
248         List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
249         assertEquals(5, options.size());
250         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
251         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_APPROVE_REQ, "true");
252         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ, "false");
253         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_FYI_REQ, "false");
254         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_DEFAULT_CD, "A");
255 
256         // test those set in the default/template rule
257         RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
258         assertTrue(ruleDefaults.getTemplateRuleInd());
259         assertEquals("a rule based on RuleTemplate_Valid_Some_Defaults", ruleDefaults.getDescription());
260         assertFalse(ruleDefaults.isForceAction());
261         assertFalse(ruleDefaults.isActive());
262         assertEquals("01/11/2006", ruleDefaults.getFromDateString());
263         assertEquals("01/01/2100", ruleDefaults.getToDateString());
264         assertNotNull(ruleDefaults.getActivationDate());
265         assertTrue(new Date(System.currentTimeMillis() - 10000).before(ruleDefaults.getActivationDate()) && new Date(System.currentTimeMillis() + 100).after(ruleDefaults.getActivationDate()));
266         assertNull(ruleDefaults.getDeactivationDate());
267     }
268         
269     /**
270      * Tests loading a template with a partial ruleDefaults section and then updating/overwriting it
271      */
272     @Test public void testLoadValidTemplateWithSomeDefaultsOverwrite() throws Exception {
273         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS.fileNameParameter, null);
274         
275         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS.ruleTemplateName);
276         
277         // test the rule template options
278         List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
279         assertEquals(5, options.size());
280         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
281         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_APPROVE_REQ, "true");
282         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ, "false");
283         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_FYI_REQ, "false");
284         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_DEFAULT_CD, "A");
285 
286         // test those set in the default/template rule
287         RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
288         assertTrue(ruleDefaults.getTemplateRuleInd());
289         assertEquals("a rule based on RuleTemplate_Valid_Some_Defaults", ruleDefaults.getDescription());
290         assertFalse(ruleDefaults.isForceAction());
291         assertFalse(ruleDefaults.isActive());
292         assertEquals("01/11/2006", ruleDefaults.getFromDateString());
293         assertEquals("01/01/2100", ruleDefaults.getToDateString());
294         
295         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS_OVERWRITE.fileNameParameter, null);
296 
297         template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS_OVERWRITE.ruleTemplateName);
298 
299         // we overwrite the template and specify a new subset of defaults...any setting omitted should be removed, i.e. reset to a default
300         // value
301         options = template.getRuleTemplateOptions();
302         assertEquals(1, options.size());
303         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "true");
304 
305         // test those set in the default/template rule
306         ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
307         assertTrue(ruleDefaults.getTemplateRuleInd());
308         assertEquals("a rule based on (updated) RuleTemplate_Valid_Some_Defaults", ruleDefaults.getDescription());
309         assertFalse(ruleDefaults.isForceAction());
310         assertFalse(ruleDefaults.isActive());
311         // activation date defaults to current time
312         assertNull(ruleDefaults.getFromDateValue());
313         assertNotNull(ruleDefaults.getActivationDate());
314         assertTrue(new Date(System.currentTimeMillis() - 10000).before(ruleDefaults.getActivationDate()) && new Date(System.currentTimeMillis() + 100).after(ruleDefaults.getActivationDate()));
315         assertNull(ruleDefaults.getToDateString());
316         assertNull(ruleDefaults.getDeactivationDate());
317     }
318 
319     /**
320      * Tests loading a template with a partial ruleDefaults section, and then updating/overwriting it with one with no ruleDefaults
321      * section defined at all 
322      */
323     @Test public void testLoadValidTemplateWithSomeRemovedDefaults() throws Exception {
324         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS.fileNameParameter, null);
325         
326         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS.ruleTemplateName);
327         
328         // test the rule template options
329         List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
330         assertEquals(5, options.size());
331         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
332         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_APPROVE_REQ, "true");
333         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ, "false");
334         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_FYI_REQ, "false");
335         assertOptionValue(template, KewApiConstants.ACTION_REQUEST_DEFAULT_CD, "A");
336 
337         // test those set in the default/template rule
338         RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
339         assertTrue(ruleDefaults.getTemplateRuleInd());
340         assertEquals("a rule based on RuleTemplate_Valid_Some_Defaults", ruleDefaults.getDescription());
341         assertFalse(ruleDefaults.isForceAction());
342         assertFalse(ruleDefaults.isActive());
343         assertEquals("01/11/2006", ruleDefaults.getFromDateString());
344         assertEquals("01/01/2100", ruleDefaults.getToDateString());
345         
346         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS_REMOVED.fileNameParameter, null);
347 
348         template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_LIMITED_DEFAULTS_REMOVED.ruleTemplateName);
349 
350         // we have removed all defaults, make sure they are gone...
351         // specifically that the default/template rule is GONE
352         assertNoDefaultsSpecified(template);
353     }
354 
355     /**
356      * Tests loading a template with the minimal required ruleDefaults
357      */
358     @Test public void testLoadValidTemplateWithDescriptionDefault() throws Exception {
359         testTemplate(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_DESCRIPTION_DEFAULT.fileNameParameter, null);
360         
361         RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserGeneralFixture.VALID_TEMPLATE_WITH_DESCRIPTION_DEFAULT.ruleTemplateName);
362 
363         // test the rule template options; this one just has the instructions, nothing else
364         List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
365         assertEquals(0, options.size());
366 
367         // test those set in the default/template rule; everything default exception description which is specified
368         RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
369         assertTrue(ruleDefaults.getTemplateRuleInd());
370         assertEquals("a description", ruleDefaults.getDescription());
371         assertFalse(ruleDefaults.isForceAction());
372         assertFalse(ruleDefaults.isActive());
373         // activation date defaults to current time
374         assertNull(ruleDefaults.getFromDateValue());
375         assertNotNull(ruleDefaults.getActivationDate());
376         assertTrue(new Date(System.currentTimeMillis() - 10000).before(ruleDefaults.getActivationDate()) && new Date(System.currentTimeMillis() + 100).after(ruleDefaults.getActivationDate()));
377         assertNull(ruleDefaults.getToDateString());
378         assertNull(ruleDefaults.getDeactivationDate());
379     }
380 
381     /**
382      * Asserts that if the defaults element is omitted, the correct defaults are set
383      * @param template the RuleTemplate to check
384      */
385     protected void assertNoDefaultsSpecified(RuleTemplateBo template) {
386         // test the rule template options
387         assertDefaultTemplateOptions(template);
388         // test those set in the default/template rule
389         assertDefaultRuleDefaults(template);
390     }
391 
392     /**
393      * Asserts that if the defaults element is omitted, the correct RuleTemplateOptions are set (or not set)
394      * @param template the RuleTemplate to check
395      */
396     protected void assertDefaultTemplateOptions(RuleTemplateBo template) {
397         List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
398         assertEquals(0, options.size());
399     }
400 
401     /**
402      * Asserts that if the defaults element is omitted, the correct template rule defaults are set
403      * @param template the RuleTemplate to check
404      */
405     protected void assertDefaultRuleDefaults(RuleTemplateBo template) {
406         RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
407         // if ruleDefaults were not specified, then the defaults/template rule should not have been created, or should have been deleted
408         assertNull(ruleDefaults);
409     }
410 
411     /**
412      * Tests that the rule default template options are present.  Must be kept in sync with defaults
413      * defined in RuleTemplate object
414      * @param ruleTemplate the ruleTemplate to check
415      * @param present whether the option should be present or not
416      */
417     protected void assertRuleDefaultsArePresent(RuleTemplateBo template, boolean present) {
418         for (String key: RuleTemplateBo.DEFAULT_OPTION_KEYS) {
419             assertOptionPresence(template, key, present);
420         }
421     }
422 
423     /**
424      * Tests that the rule default template options are present and set to null.  Must be kept in sync with defaults
425      * defined in RuleTemplate object
426      * @param ruleTemplate the ruleTemplate to check
427      */
428     protected void assertRuleDefaultsAreNull(RuleTemplateBo template) {
429         for (String key: RuleTemplateBo.DEFAULT_OPTION_KEYS) {
430             assertOptionValue(template, key, null);
431         }
432     }
433 
434     /**
435      * Asserts that the rule template has a specific rule template option defined (with any value)
436      * @param template the rule template
437      * @param key the RuleTemplateOption key
438      * @param present whether the option should be present
439      */
440     protected void assertOptionPresence(RuleTemplateBo template, String key, boolean present) {
441         RuleTemplateOptionBo option = template.getRuleTemplateOption(key);
442         if (present) {
443             if (option == null) fail("Rule template option '" + key + "' is not defined on template: " + template);
444         } else {
445             if (option != null) fail("Rule template option '" + key + "' is defined on template: " + template);
446         }
447     }
448 
449     /**
450      * Asserts that the rule template has a specific rule template option defined with a specific value
451      * @param template the rule template
452      * @param key the RuleTemplateOption key
453      * @param value the RuleTemplateOption value
454      */
455     protected void assertOptionValue(RuleTemplateBo template, String key, String value) {
456         RuleTemplateOptionBo option = template.getRuleTemplateOption(key);
457         if (option == null) fail("Rule template option '" + key + "' not defined on template: " + template);
458         assertEquals("Incorrect rule template option value for key '" + key + "'.  Expected '" + value + "' but found '" + option.getValue() + "'", value, option.getValue());
459     }
460 
461     private enum TemplateParserAttributeActivationFixture {
462         ATTRIBUTE_1(new String[]{RULE_ATTRIBUTE_ONE}, new String[]{RULE_ATTRIBUTE_TWO}),
463         ATTRIBUTE_2(new String[]{}, new String[]{RULE_ATTRIBUTE_ONE,RULE_ATTRIBUTE_TWO}),
464         ATTRIBUTE_3(new String[]{RULE_ATTRIBUTE_ONE,RULE_ATTRIBUTE_THREE}, new String[]{RULE_ATTRIBUTE_TWO}),
465         ATTRIBUTE_4(new String[]{RULE_ATTRIBUTE_TWO,RULE_ATTRIBUTE_FIVE}, new String[]{RULE_ATTRIBUTE_ONE,RULE_ATTRIBUTE_THREE,RULE_ATTRIBUTE_FOUR,}),
466         ;
467 
468         public static final String RULE_TEMPLATE_XML_FILENAME_PARM = "ActivationAttributesTest_";
469         public static final String RULE_TEMPLATE_NAME = "RuleTemplate_Activation_Test";
470 
471         public String[] activeAttributeNames;
472         public String[] inactiveAttributeNames;
473 
474         private TemplateParserAttributeActivationFixture(String[] activeAttributeNames, String[] inactiveAttributeNames) {
475             this.activeAttributeNames = activeAttributeNames;
476             this.inactiveAttributeNames = inactiveAttributeNames;
477         }
478     }
479 
480     @Test public void testAttributeActivationAndRemoval() throws Exception {
481         RuleTemplateBo template = null;
482         int totalAttributes = -1;
483         for (TemplateParserAttributeActivationFixture currentEnum : TemplateParserAttributeActivationFixture.values()) {
484             String fileNameParameter = TemplateParserAttributeActivationFixture.RULE_TEMPLATE_XML_FILENAME_PARM + (currentEnum.ordinal() + 1);
485             testTemplate(fileNameParameter, null);
486             template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(TemplateParserAttributeActivationFixture.RULE_TEMPLATE_NAME);
487             assertEquals("Total Number of Active Attributes from Rule Template is wrong",currentEnum.activeAttributeNames.length,template.getActiveRuleTemplateAttributes().size());
488             totalAttributes = currentEnum.activeAttributeNames.length + currentEnum.inactiveAttributeNames.length; 
489             assertEquals("Total Number of Attributes from Rule Template is wrong",totalAttributes,template.getRuleTemplateAttributes().size());
490             testListOfTemplateAttributes(template.getRuleTemplateAttributes(), currentEnum.activeAttributeNames, null);
491             testAllAttributesActive(template.getActiveRuleTemplateAttributes(), currentEnum.activeAttributeNames);
492         }
493     }
494 
495     // test for ingesting active attribute
496 
497     // test for ingesting inactive attribute
498 
499     // test for ingesting active attribute and then reingest to test manual inactivation
500 
501     // test for ingesting active attribute and then reingest to test automatic inactivation
502 
503 }