001: package org.drools.repository;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.IOException;
005: import java.util.ArrayList;
006: import java.util.Calendar;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.zip.ZipEntry;
010: import java.util.zip.ZipInputStream;
011:
012: import javax.jcr.PathNotFoundException;
013: import javax.jcr.RepositoryException;
014: import javax.jcr.Session;
015: import javax.jcr.SimpleCredentials;
016:
017: import junit.framework.TestCase;
018:
019: public class RulesRepositoryTest extends TestCase {
020:
021: public void testDefaultPackage() throws Exception {
022: RulesRepository repo = RepositorySessionUtil.getRepository();
023:
024: Iterator it = repo.listPackages();
025: boolean foundDefault = false;
026: while (it.hasNext()) {
027: PackageItem item = (PackageItem) it.next();
028: if (item.getName().equals(RulesRepository.DEFAULT_PACKAGE)) {
029: foundDefault = true;
030: }
031: }
032: assertTrue(foundDefault);
033:
034: PackageItem def = repo.loadDefaultPackage();
035: assertNotNull(def);
036: assertEquals(RulesRepository.DEFAULT_PACKAGE, def.getName());
037:
038: String userId = repo.getSession().getUserID();
039: assertNotNull(userId);
040: assertFalse(userId.equals(""));
041:
042: }
043:
044: public void testAddVersionARule() throws Exception {
045: RulesRepository repo = RepositorySessionUtil.getRepository();
046: PackageItem pack = repo.createPackage("testAddVersionARule",
047: "description");
048: repo.save();
049:
050: AssetItem rule = pack.addAsset("my rule", "foobar");
051: assertEquals("my rule", rule.getName());
052:
053: rule.updateContent("foo foo");
054: rule.checkin("version0");
055:
056: pack.addAsset("other rule", "description");
057:
058: rule.updateContent("foo bar");
059: rule.checkin("version1");
060:
061: PackageItem pack2 = repo.loadPackage("testAddVersionARule");
062:
063: Iterator it = pack2.getAssets();
064:
065: it.next();
066: it.next();
067:
068: assertFalse(it.hasNext());
069:
070: AssetItem prev = (AssetItem) rule.getPrecedingVersion();
071:
072: assertEquals("foo bar", rule.getContent());
073: assertEquals("foo foo", prev.getContent());
074:
075: }
076:
077: public void testFindRulesByName() throws Exception {
078: RulesRepository repo = RepositorySessionUtil.getRepository();
079:
080: repo.loadDefaultPackage().addAsset("findRulesByNamex1", "X");
081: repo.loadDefaultPackage().addAsset("findRulesByNamex2", "X");
082: repo.save();
083:
084: List list = iteratorToList(repo
085: .findAssetsByName("findRulesByNamex1"));
086: assertEquals(1, list.size());
087:
088: list = iteratorToList(repo
089: .findAssetsByName("findRulesByNamex2"));
090: assertEquals(1, list.size());
091:
092: list = iteratorToList(repo
093: .findAssetsByName("findRulesByNamex%"));
094: assertEquals(2, list.size());
095:
096: repo.createPackageSnapshot(RulesRepository.DEFAULT_PACKAGE,
097: "testFindRulesByName");
098: repo.save();
099:
100: list = iteratorToList(repo
101: .findAssetsByName("findRulesByNamex2"));
102: AssetItem item = (AssetItem) list.get(0);
103: assertEquals("findRulesByNamex2", item.getName());
104: assertEquals("X", item.getDescription());
105: assertEquals(1, list.size());
106:
107: list = iteratorToList(repo
108: .findAssetsByName("findRulesByNamex%"));
109: assertEquals(2, list.size());
110:
111: }
112:
113: public void testLoadRuleByUUIDWithConcurrentSessions()
114: throws Exception {
115: RulesRepository repo = RepositorySessionUtil.getRepository();
116:
117: PackageItem rulePackageItem = repo.loadDefaultPackage();
118: AssetItem rule = rulePackageItem.addAsset("testLoadRuleByUUID",
119: "this is a description");
120:
121: repo.save();
122:
123: String uuid = rule.getNode().getUUID();
124:
125: AssetItem loaded = repo.loadAssetByUUID(uuid);
126: assertNotNull(loaded);
127: assertEquals("testLoadRuleByUUID", loaded.getName());
128: assertEquals("this is a description", loaded.getDescription());
129:
130: long oldVersionNumber = loaded.getVersionNumber();
131:
132: loaded.updateContent("xxx");
133: loaded.checkin("woo");
134:
135: AssetItem reload = repo.loadAssetByUUID(uuid);
136: assertEquals("testLoadRuleByUUID", reload.getName());
137: assertEquals("xxx", reload.getContent());
138: System.out.println(reload.getVersionNumber());
139: System.out.println(loaded.getVersionNumber());
140: assertFalse(reload.getVersionNumber() == oldVersionNumber);
141:
142: // try loading rule package that was not created
143: try {
144: repo
145: .loadAssetByUUID("01010101-0101-0101-0101-010101010101");
146: fail("Exception not thrown loading rule package that was not created.");
147: } catch (RulesRepositoryException e) {
148: // that is OK!
149: assertNotNull(e.getMessage());
150: }
151:
152: //now test concurrent session access...
153:
154: AssetItem asset1 = repo.loadDefaultPackage().addAsset(
155: "testMultiSession", "description");
156: asset1.updateContent("yeah");
157: asset1.checkin("boo");
158: uuid = asset1.getUUID();
159: asset1.updateState("Draft");
160: repo.save();
161:
162: Session s2 = repo.getSession().getRepository().login(
163: new SimpleCredentials("fdd", "password".toCharArray()));
164:
165: RulesRepository repo2 = new RulesRepository(s2);
166:
167: AssetItem asset2 = repo2.loadAssetByUUID(uuid);
168: asset2.updateContent("yeah 42");
169: asset2.checkin("yeah");
170:
171: asset1 = repo.loadAssetByUUID(uuid);
172: assertEquals("yeah 42", asset1.getContent());
173: asset1.updateContent("yeah 43");
174: asset1.checkin("la");
175:
176: asset2 = repo2.loadAssetByUUID(uuid);
177: assertEquals("yeah 43", asset2.getContent());
178: }
179:
180: public void testAddRuleCalendarWithDates() {
181: RulesRepository rulesRepository = RepositorySessionUtil
182: .getRepository();
183:
184: Calendar effectiveDate = Calendar.getInstance();
185: Calendar expiredDate = Calendar.getInstance();
186: expiredDate.setTimeInMillis(effectiveDate.getTimeInMillis()
187: + (1000 * 60 * 60 * 24));
188: AssetItem ruleItem1 = rulesRepository.loadDefaultPackage()
189: .addAsset("testAddRuleCalendarCalendar", "desc");
190: ruleItem1.updateDateEffective(effectiveDate);
191: ruleItem1.updateDateExpired(expiredDate);
192:
193: assertNotNull(ruleItem1);
194: assertNotNull(ruleItem1.getNode());
195: assertEquals(effectiveDate, ruleItem1.getDateEffective());
196: assertEquals(expiredDate, ruleItem1.getDateExpired());
197:
198: ruleItem1.checkin("ho ");
199: }
200:
201: public void testGetState() {
202: RulesRepository rulesRepository = RepositorySessionUtil
203: .getRepository();
204:
205: StateItem state0 = rulesRepository.createState("testGetState");
206: assertNotNull(state0);
207: assertEquals("testGetState", state0.getName());
208: StateItem stateItem1 = rulesRepository.getState("testGetState");
209: assertNotNull(stateItem1);
210: assertEquals("testGetState", stateItem1.getName());
211:
212: StateItem stateItem2 = rulesRepository.getState("testGetState");
213: assertNotNull(stateItem2);
214: assertEquals("testGetState", stateItem2.getName());
215: assertEquals(stateItem1, stateItem2);
216: }
217:
218: public void testGetTag() {
219: RulesRepository rulesRepository = RepositorySessionUtil
220: .getRepository();
221:
222: CategoryItem root = rulesRepository.loadCategory("/");
223: CategoryItem tagItem1 = root.addCategory("testGetTag", "ho");
224: assertNotNull(tagItem1);
225: assertEquals("testGetTag", tagItem1.getName());
226: assertEquals("testGetTag", tagItem1.getFullPath());
227:
228: CategoryItem tagItem2 = rulesRepository
229: .loadCategory("testGetTag");
230: assertNotNull(tagItem2);
231: assertEquals("testGetTag", tagItem2.getName());
232: assertEquals(tagItem1, tagItem2);
233:
234: //now test getting a tag down in the tag hierarchy
235: CategoryItem tagItem3 = tagItem2.addCategory("TestChildTag1",
236: "ka");
237: assertNotNull(tagItem3);
238: assertEquals("TestChildTag1", tagItem3.getName());
239: assertEquals("testGetTag/TestChildTag1", tagItem3.getFullPath());
240: }
241:
242: public void testListPackages() {
243: RulesRepository rulesRepository = RepositorySessionUtil
244: .getRepository();
245: rulesRepository.createPackage("testListPackages", "desc");
246:
247: assertTrue(rulesRepository.containsPackage("testListPackages"));
248: assertFalse(rulesRepository.containsPackage("XXXXXXX"));
249:
250: Iterator it = rulesRepository.listPackages();
251: assertTrue(it.hasNext());
252:
253: boolean found = false;
254: while (it.hasNext()) {
255: PackageItem element = (PackageItem) it.next();
256: if (element.getName().equals("testListPackages")) {
257: found = true;
258: break;
259: }
260: System.out.println(element.getName());
261: }
262: assertTrue(found);
263:
264: }
265:
266: /**
267: * Here we are testing to make sure that category links don't pick up stuff in snapshots area.
268: */
269: public void testCategoriesAndSnapshots() throws Exception {
270: RulesRepository repo = RepositorySessionUtil.getRepository();
271: repo.loadCategory("/").addCategory(
272: "testCategoriesAndSnapshots", "X");
273:
274: PackageItem pkg = repo.createPackage(
275: "testCategoriesAndSnapshots", "");
276: pkg.addAsset("testCat1", "x", "/testCategoriesAndSnapshots",
277: "drl");
278: pkg.addAsset("testCat2", "x", "/testCategoriesAndSnapshots",
279: "drl");
280: repo.save();
281:
282: List items = repo
283: .findAssetsByCategory("/testCategoriesAndSnapshots");
284: assertEquals(2, items.size());
285:
286: repo.createPackageSnapshot("testCategoriesAndSnapshots",
287: "SNAP 1");
288: items = repo.findAssetsByCategory("testCategoriesAndSnapshots");
289: assertEquals(2, items.size());
290:
291: }
292:
293: public void testMoveRulePackage() throws Exception {
294: RulesRepository repo = RepositorySessionUtil.getRepository();
295: PackageItem pkg = repo.createPackage("testMove", "description");
296: AssetItem r = pkg.addAsset("testMove", "description");
297: r.checkin("version0");
298: String uuid = r.getUUID();
299: assertEquals("testMove", r.getPackageName());
300:
301: repo.save();
302:
303: assertEquals(1, iteratorToList(pkg.getAssets()).size());
304:
305: repo.createPackage("testMove2", "description");
306: repo.moveRuleItemPackage("testMove2", r.node.getUUID(),
307: "explanation");
308:
309: pkg = repo.loadPackage("testMove");
310: assertEquals(0, iteratorToList(pkg.getAssets()).size());
311:
312: pkg = repo.loadPackage("testMove2");
313: assertEquals(1, iteratorToList(pkg.getAssets()).size());
314:
315: r = (AssetItem) pkg.getAssets().next();
316: assertEquals("testMove", r.getName());
317: assertEquals("testMove2", r.getPackageName());
318: assertEquals("explanation", r.getCheckinComment());
319:
320: AssetItem p = (AssetItem) r.getPrecedingVersion();
321: assertEquals("testMove", p.getPackageName());
322: assertEquals("version0", p.getCheckinComment());
323: assertEquals(uuid, r.getUUID());
324: }
325:
326: public void testCopyAsset() throws Exception {
327: RulesRepository repo = RepositorySessionUtil.getRepository();
328: repo.createPackage("testCopyAsset", "asset");
329: AssetItem item = repo.loadDefaultPackage().addAsset(
330: "testCopyAssetSource", "desc");
331: item.updateContent("la");
332: item.checkin("");
333: item.updateDescription("mmm");
334: item.checkin("again");
335: assertEquals(2, item.getVersionNumber());
336:
337: String uuid = repo.copyAsset(item.getUUID(), "testCopyAsset",
338: "testCopyAssetDestination");
339: AssetItem dest = repo.loadAssetByUUID(uuid);
340: assertEquals("la", dest.getContent());
341: assertEquals("testCopyAsset", dest.getPackageName());
342: assertFalse(uuid.equals(item.getUUID()));
343: assertEquals(1, dest.getVersionNumber());
344: }
345:
346: public void testRenameAsset() throws Exception {
347: RulesRepository repo = RepositorySessionUtil.getRepository();
348: repo.createPackage("testRenameAsset", "asset");
349: AssetItem item = repo.loadPackage("testRenameAsset").addAsset(
350: "testRenameAssetSource", "desc");
351: item.updateContent("la");
352: item.checkin("");
353:
354: String uuid = repo.renameAsset(item.getUUID(), "testRename2");
355: item = repo.loadAssetByUUID(uuid);
356: assertEquals("testRename2", item.getName());
357: assertEquals("testRename2", item.getTitle());
358:
359: List assets = iteratorToList(repo
360: .loadPackage("testRenameAsset").getAssets());
361: assertEquals(1, assets.size());
362: item = (AssetItem) assets.get(0);
363: assertEquals("testRename2", item.getName());
364: assertEquals("la", item.getContent());
365:
366: }
367:
368: public void testRenamePackage() throws Exception {
369: RulesRepository repo = RepositorySessionUtil.getRepository();
370: PackageItem original = repo.createPackage("testRenamePackage",
371: "asset");
372: List packagesOriginal = iteratorToList(repo.listPackages());
373: AssetItem item = repo.loadPackage("testRenamePackage")
374: .addAsset("testRenameAssetSource", "desc");
375: item.updateContent("la");
376: item.checkin("");
377:
378: String uuid = repo.renamePackage(original.getUUID(),
379: "testRenamePackage2");
380:
381: PackageItem pkg = repo.loadPackageByUUID(uuid);
382: assertEquals("testRenamePackage2", pkg.getName());
383:
384: List assets = iteratorToList(repo.loadPackage(
385: "testRenamePackage2").getAssets());
386: assertEquals(1, assets.size());
387: item = (AssetItem) assets.get(0);
388: assertEquals("testRenameAssetSource", item.getName());
389: assertEquals("la", item.getContent());
390: assertEquals("testRenamePackage2", item.getPackageName());
391:
392: List packageFinal = iteratorToList(repo.listPackages());
393: assertEquals(packagesOriginal.size(), packageFinal.size());
394:
395: }
396:
397: public void testCopyPackage() throws Exception {
398: RulesRepository repo = RepositorySessionUtil.getRepository();
399: PackageItem source = repo.createPackage("testCopyPackage",
400: "asset");
401: AssetItem item = source.addAsset("testCopyPackage", "desc");
402: item.updateContent("la");
403: item.checkin("");
404: repo.save();
405:
406: repo.copyPackage("testCopyPackage", "testCopyPackage2");
407: PackageItem dest = repo.loadPackage("testCopyPackage2");
408: assertNotNull(dest);
409: assertFalse(source.getUUID().equals(dest.getUUID()));
410:
411: assertEquals(1, iteratorToList(dest.getAssets()).size());
412: AssetItem item2 = (AssetItem) dest.getAssets().next();
413:
414: assertEquals("testCopyPackage", item.getPackageName());
415: assertEquals("testCopyPackage2", item2.getPackageName());
416:
417: item2.updateContent("goober choo");
418: item2.checkin("yeah");
419:
420: assertEquals("la", item.getContent());
421:
422: try {
423: repo.copyPackage("testCopyPackage", "testCopyPackage2");
424: fail("should not be able to copy when existing.");
425:
426: } catch (RulesRepositoryException e) {
427: assertNotNull(e.getMessage());
428: }
429:
430: }
431:
432: public void testListStates() {
433: RulesRepository repo = RepositorySessionUtil.getRepository();
434: StateItem[] items = repo.listStates();
435: assertTrue(items.length > 0);
436:
437: repo.createState("testListStates");
438:
439: StateItem[] items2 = repo.listStates();
440: assertEquals(items.length + 1, items2.length);
441: }
442:
443: public void testImportExport() {
444: RulesRepository repo = RepositorySessionUtil.getRepository();
445: byte[] repository_unitest;
446: byte[] repository_backup;
447:
448: try {
449:
450: repository_backup = repo.dumpRepositoryXml();
451: assertNotNull(repository_backup);
452:
453: repo.createPackage("testImportExport", "nodescription");
454: repository_unitest = repo.dumpRepositoryXml();
455: repo.importRulesRepository(repository_backup);
456: assertFalse(repo.containsPackage("testImportExport"));
457: repo.importRulesRepository(repository_unitest);
458: assertTrue(repo.containsPackage("testImportExport"));
459: } catch (Exception e) {
460: fail("Can't throw any exception.");
461: e.printStackTrace();
462: }
463: }
464:
465: public void testExportZippedRepository()
466: throws PathNotFoundException, IOException,
467: RepositoryException {
468:
469: RulesRepository repo = RepositorySessionUtil.getRepository();
470: byte[] repository_unitest;
471:
472: repository_unitest = repo.exportRulesRepository();
473:
474: ByteArrayInputStream bin = new ByteArrayInputStream(
475: repository_unitest);
476: ZipInputStream zis = new ZipInputStream(bin);
477:
478: ZipEntry entry = zis.getNextEntry();
479: assertEquals(entry.getName(), "repository_export.xml");
480: assertFalse(entry.isDirectory());
481: }
482:
483: List iteratorToList(Iterator it) {
484: List list = new ArrayList();
485: while (it.hasNext()) {
486: list.add(it.next());
487: }
488: return list;
489: }
490: }
|