1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
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
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
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
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);
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
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);
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
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);
209 testTemplate(TemplateParserGeneralFixture.INVALID_TEMPLATE_OVERWRITE.fileNameParameter, RuntimeException.class);
210 }
211
212
213
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
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
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
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
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
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
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
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
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
300
301 options = template.getRuleTemplateOptions();
302 assertEquals(1, options.size());
303 assertOptionValue(template, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "true");
304
305
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
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
321
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
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
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
351
352 assertNoDefaultsSpecified(template);
353 }
354
355
356
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
364 List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
365 assertEquals(0, options.size());
366
367
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
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
383
384
385 protected void assertNoDefaultsSpecified(RuleTemplateBo template) {
386
387 assertDefaultTemplateOptions(template);
388
389 assertDefaultRuleDefaults(template);
390 }
391
392
393
394
395
396 protected void assertDefaultTemplateOptions(RuleTemplateBo template) {
397 List<RuleTemplateOptionBo> options = template.getRuleTemplateOptions();
398 assertEquals(0, options.size());
399 }
400
401
402
403
404
405 protected void assertDefaultRuleDefaults(RuleTemplateBo template) {
406 RuleBaseValues ruleDefaults = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(template.getId());
407
408 assertNull(ruleDefaults);
409 }
410
411
412
413
414
415
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
425
426
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
436
437
438
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
451
452
453
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
496
497
498
499
500
501
502
503 }