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;
017
018 import static org.junit.Assert.assertEquals;
019 import static org.junit.Assert.assertFalse;
020 import static org.junit.Assert.assertNotNull;
021 import static org.junit.Assert.assertNull;
022 import static org.junit.Assert.assertTrue;
023 import static org.junit.Assert.fail;
024
025 import java.util.Date;
026 import java.util.Iterator;
027 import java.util.List;
028
029 import org.junit.Test;
030 import org.kuali.rice.kew.rule.RuleBaseValues;
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.kew.test.KEWTestCase;
036 import org.kuali.rice.kew.api.KewApiConstants;
037
038
039 /**
040 * This is a Test class to test the rule template xml parser
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 *
044 */
045 public class RuleTemplateXmlParserTest extends KEWTestCase {
046 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleTemplateXmlParserTest.class);
047
048 private static final String RULE_ATTRIBUTE_ONE = "TemplateTestRuleAttribute1";
049 private static final String RULE_ATTRIBUTE_TWO = "TemplateTestRuleAttribute2";
050 private static final String RULE_ATTRIBUTE_THREE = "TemplateTestRuleAttribute3";
051 private static final String RULE_ATTRIBUTE_FOUR = "TemplateTestRuleAttribute4";
052 private static final String RULE_ATTRIBUTE_FIVE = "TemplateTestRuleAttribute5";
053
054 private enum TemplateParserGeneralFixture {
055 VALID_TEMPLATE_MIN_XML("ValidRuleTemplate", "RuleTemplate_Valid", new String[]{RULE_ATTRIBUTE_ONE}, new String[0], new String[]{RULE_ATTRIBUTE_ONE}, new String[0]),
056 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]),
057 // below has allowOverwrite=true
058 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}),
059 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}),
060 INVALID_TEMPLATE_OVERWRITE("InvalidRuleTemplateOverwrite", "RuleTemplate_Valid", new String[]{RULE_ATTRIBUTE_ONE}, new String[0], new String[]{RULE_ATTRIBUTE_ONE}, new String[0]),
061 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]),
062 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}),
063 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}),
064 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]),
065 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})
066 ;
067
068 public String fileNameParameter;
069 public String ruleTemplateName;
070 public String[] activeAttributeNames;
071 public String[] inactiveAttributeNames;
072 public String[] requiredAttributeNames;
073 public String[] nonRequiredAttributeNames;
074
075 private TemplateParserGeneralFixture(String fileNameParameter, String ruleTemplateName, String[] activeAttributeNames, String[] inactiveAttributeNames, String[] requiredAttributeNames, String[] nonRequiredAttributeNames) {
076 this.fileNameParameter = fileNameParameter;
077 this.ruleTemplateName = ruleTemplateName;
078 this.activeAttributeNames = activeAttributeNames;
079 this.inactiveAttributeNames = inactiveAttributeNames;
080 this.requiredAttributeNames = requiredAttributeNames;
081 this.nonRequiredAttributeNames = nonRequiredAttributeNames;
082 }
083 }
084
085 protected void loadTestData() throws Exception {
086 loadXmlFile("RuleTemplateConfig.xml");
087 }
088
089 private void testTemplate(String docName, Class expectedException) throws Exception {
090 RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
091 String filename = "RT_" + docName + ".xml";
092 try {
093 parser.parseRuleTemplates(getClass().getResourceAsStream(filename));
094 if (expectedException != null) {
095 fail(filename + " successfully loaded. Expected exception of class '" + expectedException + "'");
096 }
097 } catch (Exception e) {
098 if (expectedException == null || !(expectedException.isAssignableFrom(e.getClass()))) {
099 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 }