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.util.Iterator;
020: import java.util.List;
021:
022: import org.junit.Test;
023: import org.kuali.workflow.test.WorkflowTestCase;
024:
025: import edu.iu.uis.eden.KEWServiceLocator;
026: import edu.iu.uis.eden.clientapp.WorkflowDocument;
027: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
028: import edu.iu.uis.eden.doctype.DocumentType;
029:
030: public class RuleServiceTest extends WorkflowTestCase {
031:
032: protected void loadTestData() throws Exception {
033: loadXmlFile("RouteTemplateConfig.xml");
034: }
035:
036: @Test
037: public void testClearCacheWithDocumentTypeUpdate() throws Exception {
038: //put the rules in the cache by routing documents
039: WorkflowDocument document = new WorkflowDocument(
040: new NetworkIdVO("rkirkend"), "EDENSERVICE-DOCS.child1");
041: document.routeDocument("");
042: document = new WorkflowDocument(new NetworkIdVO("rkirkend"),
043: "EDENSERVICE-DOCS.child1child");
044: document.routeDocument("");
045: document = new WorkflowDocument(new NetworkIdVO("rkirkend"),
046: "EDENSERVICE-DOCS.child2");
047: document.routeDocument("");
048: document = new WorkflowDocument(new NetworkIdVO("rkirkend"),
049: "EDENSERVICE-DOCS.child3");
050: document.routeDocument("");
051: document = new WorkflowDocument(new NetworkIdVO("rkirkend"),
052: "NotRelated");
053: document.routeDocument("");
054:
055: //verify the cache's contents are correct
056: List<RuleBaseValues> rulesCached = getListFromCache(
057: "DocumentTypeRouting", "EDENSERVICE-DOCS.child1");
058: assertEquals("Wrong number of rules cached", 1, rulesCached
059: .size());
060: RuleBaseValues child1RuleFirstCached = (RuleBaseValues) rulesCached
061: .get(0);
062:
063: rulesCached = getListFromCache("DocumentTypeRouting",
064: "EDENSERVICE-DOCS.child1child");
065: assertEquals("Wrong number of rules cached", 2, rulesCached
066: .size());
067: RuleBaseValues child1childRuleFirstCached = null;
068: // make sure this has the right rules which is the rule to the document type above and it's parent
069: for (Iterator iter = rulesCached.iterator(); iter.hasNext();) {
070: RuleBaseValues rule = (RuleBaseValues) iter.next();
071: if (!(rule.getDocTypeName().equals(
072: "EDENSERVICE-DOCS.child1") || rule.getDocTypeName()
073: .equals("EDENSERVICE-DOCS.child1child"))) {
074: fail("Wrong rule in the cache");
075: }
076: if (rule.getDocTypeName().equals(
077: "EDENSERVICE-DOCS.child1child")) {
078: child1childRuleFirstCached = rule;
079: }
080: }
081: rulesCached = getListFromCache("DocumentTypeRouting",
082: "EDENSERVICE-DOCS.child2");
083: assertEquals("Wrong number of rules cached", 1, rulesCached
084: .size());
085: RuleBaseValues child2RuleFirstCached = (RuleBaseValues) rulesCached
086: .get(0);
087:
088: rulesCached = getListFromCache("DocumentTypeRouting",
089: "EDENSERVICE-DOCS.child3");
090: assertEquals("Wrong number of rules cached", 1, rulesCached
091: .size());
092: RuleBaseValues child3RuleFirstCached = (RuleBaseValues) rulesCached
093: .get(0);
094:
095: rulesCached = getListFromCache("DocumentTypeRouting",
096: "NotRelated");
097: assertEquals("Wrong number of rules cached", 1, rulesCached
098: .size());
099: RuleBaseValues notRelatedRuleFirstCached = (RuleBaseValues) rulesCached
100: .get(0);
101:
102: /**
103: * Import a replacement document type from xml. This should clear out all rules within the
104: * DocumentType hierarchy of EDENSERVICE-DOCS.
105: *
106: * This will upload a new child document type to child1child and then update the child3 and child1child document types.
107: */
108:
109: DocumentType root = KEWServiceLocator.getDocumentTypeService()
110: .findByName("EDENSERVICE-DOCS");
111:
112: loadXmlFile("DocumentTypeToImportForCacheTest.xml");
113:
114: root = KEWServiceLocator.getDocumentTypeService().findByName(
115: "EDENSERVICE-DOCS");
116:
117: /**
118: * Check the EDENSERVICE-DOCS.child1 rules.
119: */
120:
121: rulesCached = getListFromCache("DocumentTypeRouting",
122: "EDENSERVICE-DOCS.child1");
123: assertNull("The rules should not be cached.", rulesCached);
124:
125: // now fetch the rules to put them in the cache
126: List rulesFetched = KEWServiceLocator.getRuleService()
127: .fetchAllCurrentRulesForTemplateDocCombination(
128: "DocumentTypeRouting",
129: "EDENSERVICE-DOCS.child1");
130: assertNotNull(
131: "List of rules should be fetched from the service.",
132: rulesFetched);
133: assertEquals("Wrong number of rules fetched", 1, rulesFetched
134: .size());
135:
136: // now fetch from cache again
137: rulesCached = getListFromCache("DocumentTypeRouting",
138: "EDENSERVICE-DOCS.child1");
139: assertNotNull("The rules should be cached.", rulesCached);
140: assertEquals("Wrong number of rules cached", 1, rulesCached
141: .size());
142: RuleBaseValues child1RuleSecondCached = (RuleBaseValues) rulesCached
143: .get(0);
144: assertFalse(
145: "These rules should be different because the cache was updated",
146: child1RuleFirstCached.equals(child1RuleSecondCached));
147: assertEquals("The rule ids should be the same.",
148: child1RuleFirstCached.getRuleBaseValuesId(),
149: child1RuleSecondCached.getRuleBaseValuesId());
150:
151: /**
152: * Check the EDENSERVICE-DOCS.child2 rules.
153: */
154:
155: rulesCached = getListFromCache("DocumentTypeRouting",
156: "EDENSERVICE-DOCS.child2");
157: assertNull("The rules should not be cached.", rulesCached);
158:
159: // now fetch the rules to put them in the cache
160: rulesFetched = KEWServiceLocator.getRuleService()
161: .fetchAllCurrentRulesForTemplateDocCombination(
162: "DocumentTypeRouting",
163: "EDENSERVICE-DOCS.child2");
164: assertNotNull(
165: "List of rules should be fetched from the service.",
166: rulesFetched);
167: assertEquals("Wrong number of rules fetched", 1, rulesFetched
168: .size());
169:
170: // now fetch from cache again
171: rulesCached = getListFromCache("DocumentTypeRouting",
172: "EDENSERVICE-DOCS.child2");
173: assertNotNull("The rules should be cached.", rulesCached);
174: assertEquals("Wrong number of rules cached", 1, rulesCached
175: .size());
176: RuleBaseValues child2RuleSecondCached = (RuleBaseValues) rulesCached
177: .get(0);
178: assertFalse(
179: "These rules should be different because the cache was updated",
180: child2RuleFirstCached.equals(child2RuleSecondCached));
181: assertEquals("The rule ids should be the same.",
182: child2RuleFirstCached.getRuleBaseValuesId(),
183: child2RuleSecondCached.getRuleBaseValuesId());
184:
185: /**
186: * Check EDENSERVICE-DOCS.child3 rules.
187: */
188:
189: rulesCached = getListFromCache("DocumentTypeRouting",
190: "EDENSERVICE-DOCS.child3");
191: assertNull("The rules should not be cached.", rulesCached);
192:
193: // now fetch the rules to put them in the cache
194: rulesFetched = KEWServiceLocator.getRuleService()
195: .fetchAllCurrentRulesForTemplateDocCombination(
196: "DocumentTypeRouting",
197: "EDENSERVICE-DOCS.child3");
198: assertNotNull(
199: "List of rules should be fetched from the service.",
200: rulesFetched);
201: assertEquals("Wrong number of rules fetched", 1, rulesFetched
202: .size());
203:
204: // now fetch from cache again
205: rulesCached = getListFromCache("DocumentTypeRouting",
206: "EDENSERVICE-DOCS.child3");
207: assertEquals("Wrong number of rules cached", 1, rulesCached
208: .size());
209: RuleBaseValues child3RuleSecondCached = (RuleBaseValues) rulesCached
210: .get(0);
211: assertFalse(
212: "These rules should be different because the cache was updated.",
213: child3RuleFirstCached.equals(child3RuleSecondCached));
214: assertEquals("The rule ids should be the same.",
215: child3RuleFirstCached.getRuleBaseValuesId(),
216: child3RuleSecondCached.getRuleBaseValuesId());
217:
218: /**
219: * Check the EDENSERVICE-DOCS.child1child rules.
220: */
221:
222: rulesCached = getListFromCache("DocumentTypeRouting",
223: "EDENSERVICE-DOCS.child1child");
224: assertNull("The rules should not be cached.", rulesCached);
225:
226: // now fetch the rules to put them in the cache
227: rulesFetched = KEWServiceLocator.getRuleService()
228: .fetchAllCurrentRulesForTemplateDocCombination(
229: "DocumentTypeRouting",
230: "EDENSERVICE-DOCS.child1child");
231: assertNotNull(
232: "List of rules should be fetched from the service.",
233: rulesFetched);
234: assertEquals("Wrong number of rules fetched", 2, rulesFetched
235: .size());
236:
237: // fetch from cache again
238: rulesCached = getListFromCache("DocumentTypeRouting",
239: "EDENSERVICE-DOCS.child1child");
240: assertEquals("Wrong number of rules cached", 2, rulesCached
241: .size());
242: RuleBaseValues child1childRuleSecondCached = null;
243: //make sure this has the right rules which is the rule to the document type above and it's parent
244: for (Iterator iter = rulesCached.iterator(); iter.hasNext();) {
245: RuleBaseValues rule = (RuleBaseValues) iter.next();
246: if (!(rule.getDocTypeName().equals(
247: "EDENSERVICE-DOCS.child1") || rule.getDocTypeName()
248: .equals("EDENSERVICE-DOCS.child1child"))) {
249: fail("Wrong rule in the cache");
250: }
251: if (rule.getDocTypeName().equals(
252: "EDENSERVICE-DOCS.child1child")) {
253: child1childRuleSecondCached = rule;
254: }
255: }
256: assertFalse(
257: "These rules should be different because the cache was updated.",
258: child1childRuleFirstCached
259: .equals(child1childRuleSecondCached));
260: assertEquals("The rule ids should be the same.",
261: child1childRuleFirstCached.getRuleBaseValuesId(),
262: child1childRuleSecondCached.getRuleBaseValuesId());
263:
264: /**
265: * Check the NotRelated rules these are the only ones which should not have been flushed from the cache.
266: */
267:
268: rulesCached = getListFromCache("DocumentTypeRouting",
269: "NotRelated");
270: assertEquals("Wrong number of rules cached", 1, rulesCached
271: .size());
272: RuleBaseValues notRelatedRuleSecondCached = (RuleBaseValues) rulesCached
273: .get(0);
274: assertTrue(
275: "These rules should be the same because the cache was not updated",
276: notRelatedRuleFirstCached
277: .equals(notRelatedRuleSecondCached));
278:
279: /**
280: * Grab the EDENSERVICE-DOCS.child1child1child rules from the cache, they should initially be null since
281: * this is a brand new document type.
282: *
283: * After fetching them we should see the rules from EDENSERVICE-DOCS.child1, EDENSERVICE-DOCS.child1child,
284: * and EDENSERVICE-DOCS.child1child1child.
285: */
286:
287: rulesCached = getListFromCache("DocumentTypeRouting",
288: "EDENSERVICE-DOCS.child1child1child");
289: assertNull("Rules should not be cached yet.", rulesCached);
290:
291: // routing a doc should put the rules into the cache
292: document = new WorkflowDocument(new NetworkIdVO("rkirkend"),
293: "EDENSERVICE-DOCS.child1child1child");
294: document.routeDocument("");
295:
296: rulesCached = getListFromCache("DocumentTypeRouting",
297: "EDENSERVICE-DOCS.child1child1child");
298: assertNotNull(rulesCached);
299: assertEquals(3, rulesCached.size());
300: boolean hasRule1 = false;
301: boolean hasRule2 = false;
302: boolean hasRule3 = false;
303: for (RuleBaseValues rule : rulesCached) {
304: if (rule.getDocTypeName().equals("EDENSERVICE-DOCS.child1")) {
305: hasRule1 = true;
306: } else if (rule.getDocTypeName().equals(
307: "EDENSERVICE-DOCS.child1child")) {
308: hasRule2 = true;
309: } else if (rule.getDocTypeName().equals(
310: "EDENSERVICE-DOCS.child1child1child")) {
311: hasRule3 = true;
312: }
313: }
314: assertTrue("Should have had EDENSERVICE-DOCS.child1 rule",
315: hasRule1);
316: assertTrue("Should have had EDENSERVICE-DOCS.child1child rule",
317: hasRule2);
318: assertTrue(
319: "Should have had EDENSERVICE-DOCS.child1child1child rule",
320: hasRule3);
321:
322: /**
323: * Import a new rule for EDENSERVICE-DOCS and verify that the entire hierarchy is flushed
324: * from the cache.
325: */
326:
327: // first verify that there are no rules for EDENSERVICE-DOCS
328: rulesFetched = KEWServiceLocator.getRuleService()
329: .fetchAllCurrentRulesForTemplateDocCombination(
330: "DocumentTypeRouting", "EDENSERVICE-DOCS");
331: assertNotNull("The list should not be null.", rulesFetched);
332: assertEquals("The list should be empty.", 0, rulesFetched
333: .size());
334:
335: loadXmlFile("NewEdenserviceDocsRule.xml");
336:
337: // verify that all rules for doc types in the hierarchy have been flushed from the cache
338:
339: rulesCached = getListFromCache("DocumentTypeRouting",
340: "EDENSERVICE-DOCS");
341: assertNull(
342: "There should be no cache entry for EDENSERVICE-DOCS",
343: rulesCached);
344:
345: rulesCached = getListFromCache("DocumentTypeRouting",
346: "EDENSERVICE-DOCS.child1");
347: assertNull(
348: "There should be no cache entry for EDENSERVICE-DOCS.child1",
349: rulesCached);
350:
351: rulesCached = getListFromCache("DocumentTypeRouting",
352: "EDENSERVICE-DOCS.child2");
353: assertNull(
354: "There should be no cache entry for EDENSERVICE-DOCS.child2",
355: rulesCached);
356:
357: rulesCached = getListFromCache("DocumentTypeRouting",
358: "EDENSERVICE-DOCS.child3");
359: assertNull(
360: "There should be no cache entry for EDENSERVICE-DOCS.child3",
361: rulesCached);
362:
363: rulesCached = getListFromCache("DocumentTypeRouting",
364: "EDENSERVICE-DOCS.child1child");
365: assertNull(
366: "There should be no cache entry for EDENSERVICE-DOCS.child1child",
367: rulesCached);
368:
369: rulesCached = getListFromCache("DocumentTypeRouting",
370: "EDENSERVICE-DOCS.child1child1child");
371: assertNull(
372: "There should be no cache entry for EDENSERVICE-DOCS.child1child1child",
373: rulesCached);
374:
375: // now fetch them from service so they are cached
376: rulesFetched = KEWServiceLocator.getRuleService()
377: .fetchAllCurrentRulesForTemplateDocCombination(
378: "DocumentTypeRouting", "EDENSERVICE-DOCS");
379: assertNotNull("The list should not be null.", rulesFetched);
380: assertEquals("The list should contain a single rule.", 1,
381: rulesFetched.size());
382:
383: // fetch the cache now and verify the same
384: rulesCached = getListFromCache("DocumentTypeRouting",
385: "EDENSERVICE-DOCS");
386: assertNotNull("The list should not be null.", rulesCached);
387: assertEquals("The list should contain a single rule.", 1,
388: rulesCached.size());
389:
390: /**
391: * Since we imported a rule at the top of the document hierarchy and the Lists of rules cached
392: * also include all rules from parent document types, each of the rule lists should be increased by one
393: */
394:
395: // EDENSERVICE-DOCS.child1 should now have 2 rules
396: rulesFetched = KEWServiceLocator.getRuleService()
397: .fetchAllCurrentRulesForTemplateDocCombination(
398: "DocumentTypeRouting",
399: "EDENSERVICE-DOCS.child1");
400: assertEquals(2, rulesFetched.size());
401: rulesCached = getListFromCache("DocumentTypeRouting",
402: "EDENSERVICE-DOCS.child1");
403: assertEquals(2, rulesCached.size());
404:
405: // EDENSERVICE-DOCS.child2 should now have 2 rules
406: rulesFetched = KEWServiceLocator.getRuleService()
407: .fetchAllCurrentRulesForTemplateDocCombination(
408: "DocumentTypeRouting",
409: "EDENSERVICE-DOCS.child2");
410: assertEquals(2, rulesFetched.size());
411: rulesCached = getListFromCache("DocumentTypeRouting",
412: "EDENSERVICE-DOCS.child2");
413: assertEquals(2, rulesCached.size());
414:
415: // EDENSERVICE-DOCS.child3 should now have 2 rules
416: rulesFetched = KEWServiceLocator.getRuleService()
417: .fetchAllCurrentRulesForTemplateDocCombination(
418: "DocumentTypeRouting",
419: "EDENSERVICE-DOCS.child3");
420: assertEquals(2, rulesFetched.size());
421: rulesCached = getListFromCache("DocumentTypeRouting",
422: "EDENSERVICE-DOCS.child3");
423: assertEquals(2, rulesCached.size());
424:
425: // EDENSERVICE-DOCS.child1child should now have 3 rules
426: rulesFetched = KEWServiceLocator.getRuleService()
427: .fetchAllCurrentRulesForTemplateDocCombination(
428: "DocumentTypeRouting",
429: "EDENSERVICE-DOCS.child1child");
430: assertEquals(3, rulesFetched.size());
431: rulesCached = getListFromCache("DocumentTypeRouting",
432: "EDENSERVICE-DOCS.child1child");
433: assertEquals(3, rulesCached.size());
434:
435: // EDENSERVICE-DOCS.child1child1child should now have 4 rules
436: rulesFetched = KEWServiceLocator.getRuleService()
437: .fetchAllCurrentRulesForTemplateDocCombination(
438: "DocumentTypeRouting",
439: "EDENSERVICE-DOCS.child1child1child");
440: assertEquals(4, rulesFetched.size());
441: rulesCached = getListFromCache("DocumentTypeRouting",
442: "EDENSERVICE-DOCS.child1child1child");
443: assertEquals(4, rulesCached.size());
444:
445: // NotRelated should still only have 1 rule
446: rulesFetched = KEWServiceLocator.getRuleService()
447: .fetchAllCurrentRulesForTemplateDocCombination(
448: "DocumentTypeRouting", "NotRelated");
449: assertEquals(1, rulesFetched.size());
450: rulesCached = getListFromCache("DocumentTypeRouting",
451: "NotRelated");
452: assertEquals(1, rulesCached.size());
453:
454: }
455:
456: private List<RuleBaseValues> getListFromCache(
457: String ruleTemplateName, String documentTypeName) {
458: return new RuleServiceImpl().getListFromCache(ruleTemplateName,
459: documentTypeName);
460: }
461:
462: }
|