0001: package org.drools.brms.server;
0002:
0003: /*
0004: * Copyright 2005 JBoss Inc
0005: *
0006: * Licensed under the Apache License, Version 2.0 (the "License");
0007: * you may not use this file except in compliance with the License.
0008: * You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing, software
0013: * distributed under the License is distributed on an "AS IS" BASIS,
0014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015: * See the License for the specific language governing permissions and
0016: * limitations under the License.
0017: */
0018:
0019: import java.io.ByteArrayInputStream;
0020: import java.io.ObjectInputStream;
0021: import java.util.ArrayList;
0022: import java.util.Calendar;
0023: import java.util.Date;
0024: import java.util.Iterator;
0025: import java.util.List;
0026:
0027: import junit.framework.TestCase;
0028:
0029: import org.drools.Person;
0030: import org.drools.RuleBase;
0031: import org.drools.StatelessSession;
0032: import org.drools.brms.client.common.AssetFormats;
0033: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
0034: import org.drools.brms.client.modeldriven.brl.ActionFieldValue;
0035: import org.drools.brms.client.modeldriven.brl.ActionSetField;
0036: import org.drools.brms.client.modeldriven.brl.FactPattern;
0037: import org.drools.brms.client.modeldriven.brl.RuleModel;
0038: import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
0039: import org.drools.brms.client.rpc.BuilderResult;
0040: import org.drools.brms.client.rpc.PackageConfigData;
0041: import org.drools.brms.client.rpc.RepositoryService;
0042: import org.drools.brms.client.rpc.RuleAsset;
0043: import org.drools.brms.client.rpc.RuleContentText;
0044: import org.drools.brms.client.rpc.SnapshotInfo;
0045: import org.drools.brms.client.rpc.TableConfig;
0046: import org.drools.brms.client.rpc.TableDataResult;
0047: import org.drools.brms.client.rpc.TableDataRow;
0048: import org.drools.brms.client.rpc.ValidatedResponse;
0049: import org.drools.brms.server.util.BRXMLPersistence;
0050: import org.drools.brms.server.util.TableDisplayHandler;
0051: import org.drools.brms.server.util.TestEnvironmentSessionHelper;
0052: import org.drools.common.DroolsObjectInputStream;
0053: import org.drools.repository.AssetItem;
0054: import org.drools.repository.CategoryItem;
0055: import org.drools.repository.PackageItem;
0056: import org.drools.repository.RulesRepository;
0057: import org.drools.repository.RulesRepositoryException;
0058: import org.drools.repository.StateItem;
0059: import org.drools.rule.Package;
0060: import org.drools.util.BinaryRuleBaseLoader;
0061:
0062: import com.google.gwt.user.client.rpc.SerializableException;
0063:
0064: public class ServiceImplementationTest extends TestCase {
0065:
0066: public void testCategory() throws Exception {
0067: //ServiceImpl impl = new ServiceImpl(new RulesRepository(SessionHelper.getSession()));
0068:
0069: RepositoryService impl = getService();
0070:
0071: String[] originalCats = impl.loadChildCategories("/");
0072:
0073: Boolean result = impl.createCategory("/", "TopLevel1",
0074: "a description");
0075: assertTrue(result.booleanValue());
0076:
0077: result = impl.createCategory("/", "TopLevel2", "a description");
0078: assertTrue(result.booleanValue());
0079:
0080: String[] cats = impl.loadChildCategories("/");
0081: assertTrue(cats.length == originalCats.length + 2);
0082:
0083: result = impl.createCategory("", "Top3", "description");
0084: assertTrue(result.booleanValue());
0085:
0086: result = impl.createCategory(null, "Top4", "description");
0087: assertTrue(result.booleanValue());
0088:
0089: }
0090:
0091: public void testDeleteUnversionedRule() throws Exception {
0092: ServiceImplementation impl = getService();
0093:
0094: impl.repository.loadDefaultPackage();
0095: impl.repository.createPackage("anotherPackage", "woot");
0096:
0097: CategoryItem cat = impl.repository.loadCategory("/");
0098: cat.addCategory("testDeleteUnversioned", "yeah");
0099:
0100: String uuid = impl.createNewRule("test Delete Unversioned",
0101: "a description", "testDeleteUnversioned",
0102: "anotherPackage", "txt");
0103: assertNotNull(uuid);
0104: assertFalse("".equals(uuid));
0105:
0106: AssetItem localItem = impl.repository.loadAssetByUUID(uuid);
0107:
0108: // String drl = "package org.drools.repository\n\ndialect 'mvel'\n\n" +
0109: // "rule Rule1 \n when \n AssetItem(description != null) \n then \n System.out.println(\"yeah\");\nend";
0110: // RuleBase rb = RuleBaseLoader.getInstance().loadFromReader(new StringReader(drl));
0111: // rb.newStatelessSession().execute(localItem);
0112:
0113: assertEquals("test Delete Unversioned", localItem.getName());
0114:
0115: localItem.remove();
0116: impl.repository.save();
0117:
0118: try {
0119: localItem = impl.repository.loadAssetByUUID(uuid);
0120: fail();
0121: } catch (Exception e) {
0122: }
0123: }
0124:
0125: public void testAddRuleAndListPackages() throws Exception {
0126: //ServiceImpl impl = new ServiceImpl(new RulesRepository(SessionHelper.getSession()));
0127:
0128: ServiceImplementation impl = getService();
0129:
0130: impl.repository.loadDefaultPackage();
0131: impl.repository.createPackage("another", "woot");
0132:
0133: CategoryItem cat = impl.repository.loadCategory("/");
0134: cat.addCategory("testAddRule", "yeah");
0135:
0136: String result = impl.createNewRule("test AddRule",
0137: "a description", "testAddRule", "another", "txt");
0138: assertNotNull(result);
0139: assertFalse("".equals(result));
0140:
0141: PackageConfigData[] packages = impl.listPackages();
0142: assertTrue(packages.length > 0);
0143:
0144: boolean found = false;
0145: for (int i = 0; i < packages.length; i++) {
0146: if (packages[i].name.equals("another")) {
0147: found = true;
0148: }
0149: }
0150:
0151: assertTrue(found);
0152:
0153: assertFalse(packages[0].uuid == null);
0154: assertFalse(packages[0].uuid.equals(""));
0155:
0156: //just for performance testing with scaling up numbers of rules
0157: // for (int i=1; i <= 1000; i++) {
0158: // impl.createNewRule( "somerule_" + i, "description",
0159: // "testAddRule", "another", "drl" );
0160: // }
0161:
0162: result = impl.createNewRule("testDTSample", "a description",
0163: "testAddRule", "another",
0164: AssetFormats.DECISION_SPREADSHEET_XLS);
0165: AssetItem dtItem = impl.repository.loadAssetByUUID(result);
0166: assertNotNull(dtItem.getBinaryContentAsBytes());
0167: assertTrue(dtItem.getBinaryContentAttachmentFileName()
0168: .endsWith(".xls"));
0169: }
0170:
0171: public void testAttemptDupeRule() throws Exception {
0172: ServiceImplementation impl = getService();
0173: CategoryItem cat = impl.repository.loadCategory("/");
0174: cat.addCategory("testAttemptDupeRule", "yeah");
0175:
0176: impl.repository.createPackage("dupes", "yeah");
0177:
0178: impl.createNewRule("testAttemptDupeRule", "ya",
0179: "testAttemptDupeRule", "dupes", "rule");
0180:
0181: try {
0182: impl.createNewRule("testAttemptDupeRule", "ya",
0183: "testAttemptDupeRule", "dupes", "rule");
0184: fail("should not allow duplicates.");
0185: } catch (SerializableException e) {
0186: assertNotNull(e.getMessage());
0187: }
0188:
0189: }
0190:
0191: public void testRuleTableLoad() throws Exception {
0192: ServiceImplementation impl = getService();
0193: TableConfig conf = impl
0194: .loadTableConfig(TableDisplayHandler.DEFAULT_TABLE_TEMPLATE);
0195: assertNotNull(conf.headers);
0196:
0197: CategoryItem cat = impl.repository.loadCategory("/");
0198: cat.addCategory("testRuleTableLoad", "yeah");
0199:
0200: impl.repository.createPackage("testRuleTableLoad", "yeah");
0201: impl.createNewRule("testRuleTableLoad", "ya",
0202: "testRuleTableLoad", "testRuleTableLoad", "rule");
0203: impl.createNewRule("testRuleTableLoad2", "ya",
0204: "testRuleTableLoad", "testRuleTableLoad", "rule");
0205:
0206: TableDataResult result = impl
0207: .loadRuleListForCategories("testRuleTableLoad");
0208: assertEquals(2, result.data.length);
0209:
0210: String key = result.data[0].id;
0211: assertFalse(key.startsWith("testRule"));
0212:
0213: assertEquals(result.data[0].format, "rule");
0214: assertTrue(result.data[0].values[0].startsWith("rule"));
0215: }
0216:
0217: public void testDateFormatting() throws Exception {
0218: Calendar cal = Calendar.getInstance();
0219: TableDisplayHandler handler = new TableDisplayHandler(
0220: TableDisplayHandler.DEFAULT_TABLE_TEMPLATE);
0221: String fmt = handler.formatDate(cal);
0222: assertNotNull(fmt);
0223:
0224: assertTrue(fmt.length() > 8);
0225: }
0226:
0227: public void testLoadRuleAsset() throws Exception {
0228: ServiceImplementation impl = getService();
0229: impl.repository.createPackage("testLoadRuleAsset", "desc");
0230: impl.createCategory("", "testLoadRuleAsset", "this is a cat");
0231:
0232: impl.createNewRule("testLoadRuleAsset", "description",
0233: "testLoadRuleAsset", "testLoadRuleAsset", "drl");
0234:
0235: TableDataResult res = impl
0236: .loadRuleListForCategories("testLoadRuleAsset");
0237: assertEquals(1, res.data.length);
0238:
0239: TableDataRow row = res.data[0];
0240: String uuid = row.id;
0241:
0242: RuleAsset asset = impl.loadRuleAsset(uuid);
0243: assertNotNull(asset);
0244:
0245: assertEquals(uuid, asset.uuid);
0246:
0247: assertEquals("description", asset.metaData.description);
0248:
0249: assertNotNull(asset.content);
0250: assertTrue(asset.content instanceof RuleContentText);
0251: assertEquals("testLoadRuleAsset", asset.metaData.name);
0252: assertEquals("testLoadRuleAsset", asset.metaData.title);
0253: assertEquals("testLoadRuleAsset", asset.metaData.packageName);
0254: assertEquals("drl", asset.metaData.format);
0255: assertNotNull(asset.metaData.createdDate);
0256:
0257: assertEquals(1, asset.metaData.categories.length);
0258: assertEquals("testLoadRuleAsset", asset.metaData.categories[0]);
0259:
0260: AssetItem rule = impl.repository.loadPackage(
0261: "testLoadRuleAsset").loadAsset("testLoadRuleAsset");
0262: impl.repository.createState("whee");
0263: rule.updateState("whee");
0264: rule.checkin("changed state");
0265: asset = impl.loadRuleAsset(uuid);
0266:
0267: assertEquals("whee", asset.metaData.status);
0268: assertEquals("changed state", asset.metaData.checkinComment);
0269:
0270: uuid = impl.createNewRule("testBRLFormatSugComp",
0271: "description", "testLoadRuleAsset",
0272: "testLoadRuleAsset", AssetFormats.BUSINESS_RULE);
0273: asset = impl.loadRuleAsset(uuid);
0274: assertTrue(asset.content instanceof RuleModel);
0275:
0276: uuid = impl.createNewRule("testLoadRuleAssetBRL",
0277: "description", "testLoadRuleAsset",
0278: "testLoadRuleAsset", AssetFormats.DSL_TEMPLATE_RULE);
0279: asset = impl.loadRuleAsset(uuid);
0280: assertTrue(asset.content instanceof RuleContentText);
0281: }
0282:
0283: public void testLoadAssetHistoryAndRestore() throws Exception {
0284: ServiceImplementation impl = getService();
0285: impl.repository.createPackage("testLoadAssetHistory", "desc");
0286: impl
0287: .createCategory("", "testLoadAssetHistory",
0288: "this is a cat");
0289:
0290: String uuid = impl.createNewRule("testLoadAssetHistory",
0291: "description", "testLoadAssetHistory",
0292: "testLoadAssetHistory", "drl");
0293: RuleAsset asset = impl.loadRuleAsset(uuid);
0294: impl.checkinVersion(asset); //1
0295: asset = impl.loadRuleAsset(uuid);
0296: impl.checkinVersion(asset); //2
0297: asset = impl.loadRuleAsset(uuid);
0298: impl.checkinVersion(asset); //HEAD
0299:
0300: TableDataResult result = impl.loadAssetHistory(uuid);
0301: assertNotNull(result);
0302: TableDataRow[] rows = result.data;
0303: assertEquals(2, rows.length);
0304: assertFalse(rows[0].id.equals(uuid));
0305: assertFalse(rows[1].id.equals(uuid));
0306:
0307: RuleAsset old = impl.loadRuleAsset(rows[0].id);
0308: RuleAsset newer = impl.loadRuleAsset(rows[1].id);
0309: assertFalse(old.metaData.versionNumber == newer.metaData.versionNumber);
0310:
0311: RuleAsset head = impl.loadRuleAsset(uuid);
0312:
0313: long oldVersion = old.metaData.versionNumber;
0314: assertFalse(oldVersion == head.metaData.versionNumber);
0315:
0316: impl.restoreVersion(old.uuid, head.uuid,
0317: "this was cause of a mistake");
0318:
0319: RuleAsset newHead = impl.loadRuleAsset(uuid);
0320:
0321: assertEquals("this was cause of a mistake",
0322: newHead.metaData.checkinComment);
0323:
0324: }
0325:
0326: public void testCheckin() throws Exception {
0327: RepositoryService serv = getService();
0328:
0329: serv.listPackages();
0330:
0331: serv.createCategory("/", "testCheckinCategory",
0332: "this is a description");
0333: serv.createCategory("/", "testCheckinCategory2",
0334: "this is a description");
0335: serv.createCategory("testCheckinCategory", "deeper",
0336: "description");
0337:
0338: String uuid = serv.createNewRule("testChecking",
0339: "this is a description", "testCheckinCategory",
0340: RulesRepository.DEFAULT_PACKAGE, "drl");
0341:
0342: RuleAsset asset = serv.loadRuleAsset(uuid);
0343:
0344: assertNotNull(asset.metaData.lastModifiedDate);
0345:
0346: asset.metaData.coverage = "boo";
0347: asset.content = new RuleContentText();
0348: ((RuleContentText) asset.content).content = "yeah !";
0349:
0350: Date start = new Date();
0351: Thread.sleep(100);
0352:
0353: String uuid2 = serv.checkinVersion(asset);
0354: assertEquals(uuid, uuid2);
0355:
0356: RuleAsset asset2 = serv.loadRuleAsset(uuid);
0357: assertNotNull(asset2.metaData.lastModifiedDate);
0358: assertTrue(asset2.metaData.lastModifiedDate.after(start));
0359:
0360: assertEquals("boo", asset2.metaData.coverage);
0361: assertEquals(1, asset2.metaData.versionNumber);
0362:
0363: assertEquals("yeah !",
0364: ((RuleContentText) asset2.content).content);
0365:
0366: asset2.metaData.coverage = "ya";
0367: asset2.metaData.checkinComment = "checked in";
0368:
0369: String cat = asset2.metaData.categories[0];
0370: asset2.metaData.categories = new String[3];
0371: asset2.metaData.categories[0] = cat;
0372: asset2.metaData.categories[1] = "testCheckinCategory2";
0373: asset2.metaData.categories[2] = "testCheckinCategory/deeper";
0374:
0375: serv.checkinVersion(asset2);
0376:
0377: asset2 = serv.loadRuleAsset(uuid);
0378: assertEquals("ya", asset2.metaData.coverage);
0379: assertEquals(2, asset2.metaData.versionNumber);
0380: assertEquals("checked in", asset2.metaData.checkinComment);
0381: assertEquals(3, asset2.metaData.categories.length);
0382: assertEquals("testCheckinCategory",
0383: asset2.metaData.categories[0]);
0384: assertEquals("testCheckinCategory2",
0385: asset2.metaData.categories[1]);
0386: assertEquals("testCheckinCategory/deeper",
0387: asset2.metaData.categories[2]);
0388:
0389: //now lets try a concurrent edit of an asset.
0390: //asset3 will be loaded and edited, and then asset2 will try to clobber, it, which should fail.
0391: //as it is optimistically locked.
0392: RuleAsset asset3 = serv.loadRuleAsset(asset2.uuid);
0393: asset3.metaData.subject = "new sub";
0394: serv.checkinVersion(asset3);
0395:
0396: asset3 = serv.loadRuleAsset(asset2.uuid);
0397: assertFalse(asset3.metaData.versionNumber == asset2.metaData.versionNumber);
0398:
0399: try {
0400: serv.checkinVersion(asset2);
0401: fail("should have failed optimistic lock.");
0402: } catch (SerializableException e) {
0403: assertNotNull(e.getMessage());
0404: assertEquals(-1, e.getMessage().indexOf("server"));
0405: }
0406:
0407: }
0408:
0409: public void testArchivePackage() throws Exception {
0410: ServiceImplementation impl = getService();
0411:
0412: PackageConfigData[] pkgs = impl.listPackages();
0413:
0414: String uuid = impl.createPackage("testCreateArchivedPackage",
0415: "this is a new package");
0416: PackageItem item = impl.repository
0417: .loadPackage("testCreateArchivedPackage");
0418: item.archiveItem(true);
0419: assertEquals(pkgs.length, impl.listPackages().length);
0420: }
0421:
0422: public void testCreatePackage() throws Exception {
0423: ServiceImplementation impl = getService();
0424: PackageConfigData[] pkgs = impl.listPackages();
0425: String uuid = impl.createPackage("testCreatePackage",
0426: "this is a new package");
0427: assertNotNull(uuid);
0428:
0429: PackageItem item = impl.repository
0430: .loadPackage("testCreatePackage");
0431: assertNotNull(item);
0432: assertEquals("this is a new package", item.getDescription());
0433:
0434: assertEquals(pkgs.length + 1, impl.listPackages().length);
0435:
0436: PackageConfigData conf = impl.loadPackageConfig(uuid);
0437: assertEquals("this is a new package", conf.description);
0438: assertNotNull(conf.lastModified);
0439:
0440: pkgs = impl.listPackages();
0441:
0442: impl.copyPackage("testCreatePackage", "testCreatePackage_COPY");
0443:
0444: assertEquals(pkgs.length + 1, impl.listPackages().length);
0445: try {
0446: impl.copyPackage("testCreatePackage",
0447: "testCreatePackage_COPY");
0448: } catch (RulesRepositoryException e) {
0449: assertNotNull(e.getMessage());
0450: }
0451: }
0452:
0453: public void testLoadPackageConfig() throws Exception {
0454: ServiceImplementation impl = getService();
0455: PackageItem it = impl.repository.loadDefaultPackage();
0456: String uuid = it.getUUID();
0457: it.updateCoverage("xyz");
0458: it.updateExternalURI("ext");
0459: it.updateHeader("header");
0460: impl.repository.save();
0461:
0462: PackageConfigData data = impl.loadPackageConfig(uuid);
0463: assertNotNull(data);
0464:
0465: assertEquals(RulesRepository.DEFAULT_PACKAGE, data.name);
0466: assertEquals("header", data.header);
0467: assertEquals("ext", data.externalURI);
0468:
0469: assertNotNull(data.uuid);
0470: assertFalse(data.isSnapshot);
0471:
0472: assertNotNull(data.dateCreated);
0473: Date original = data.lastModified;
0474:
0475: Thread.sleep(100);
0476:
0477: impl.createPackageSnapshot(RulesRepository.DEFAULT_PACKAGE,
0478: "TEST SNAP 2.0", false, "ya");
0479: PackageItem loaded = impl.repository.loadPackageSnapshot(
0480: RulesRepository.DEFAULT_PACKAGE, "TEST SNAP 2.0");
0481:
0482: data = impl.loadPackageConfig(loaded.getUUID());
0483: assertTrue(data.isSnapshot);
0484: assertEquals("TEST SNAP 2.0", data.snapshotName);
0485: assertFalse(original.equals(data.lastModified));
0486: assertEquals("ya", data.checkinComment);
0487: }
0488:
0489: public void testPackageConfSave() throws Exception {
0490: RepositoryService impl = getService();
0491: String uuid = impl.createPackage("testPackageConfSave",
0492: "a desc");
0493: PackageConfigData data = impl.loadPackageConfig(uuid);
0494:
0495: data.description = "new desc";
0496: data.header = "wa";
0497: data.externalURI = "new URI";
0498:
0499: ValidatedResponse res = impl.savePackage(data);
0500: assertNotNull(res);
0501: assertTrue(res.hasErrors);
0502: assertNotNull(res.errorMessage);
0503:
0504: data = impl.loadPackageConfig(uuid);
0505: assertEquals("new desc", data.description);
0506: assertEquals("wa", data.header);
0507: assertEquals("new URI", data.externalURI);
0508:
0509: data.header = "";
0510: res = impl.savePackage(data);
0511: if (res.hasErrors) {
0512: System.out
0513: .println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
0514: System.out.println(res.errorMessage);
0515: System.out
0516: .println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
0517:
0518: }
0519:
0520: assertFalse(res.hasErrors);
0521: }
0522:
0523: public void testListByFormat() throws Exception {
0524: RepositoryService impl = getService();
0525: String cat = "testListByFormat";
0526: impl.createCategory("/", cat, "ya");
0527: String pkgUUID = impl.createPackage("testListByFormat",
0528: "used for listing by format.");
0529:
0530: String uuid = impl.createNewRule("testListByFormat", "x", cat,
0531: "testListByFormat", "testListByFormat");
0532: String uuid2 = impl.createNewRule("testListByFormat2", "x",
0533: cat, "testListByFormat", "testListByFormat");
0534: String uuid3 = impl.createNewRule("testListByFormat3", "x",
0535: cat, "testListByFormat", "testListByFormat");
0536: String uuid4 = impl.createNewRule("testListByFormat4", "x",
0537: cat, "testListByFormat", "testListByFormat");
0538:
0539: TableDataResult res = impl.listAssets(pkgUUID,
0540: arr("testListByFormat"), -1, 0);
0541: assertEquals(4, res.data.length);
0542: assertEquals(uuid, res.data[0].id);
0543: assertEquals("testListByFormat", res.data[0].values[0]);
0544:
0545: res = impl.listAssets(pkgUUID, arr("testListByFormat"), 4, 0);
0546: assertEquals(4, res.data.length);
0547:
0548: res = impl.listAssets(pkgUUID, arr("testListByFormat"), 2, 0);
0549: assertEquals(2, res.data.length);
0550: assertEquals(uuid, res.data[0].id);
0551:
0552: res = impl.listAssets(pkgUUID, arr("testListByFormat"), 2, 2);
0553: assertEquals(2, res.data.length);
0554: assertEquals(uuid3, res.data[0].id);
0555:
0556: uuid = impl.createNewRule("testListByFormat5", "x", cat,
0557: "testListByFormat", "otherFormat");
0558:
0559: res = impl.listAssets(pkgUUID, arr("otherFormat"), 40, 0);
0560: assertEquals(1, res.data.length);
0561: assertEquals(uuid, res.data[0].id);
0562:
0563: res = impl.listAssets(pkgUUID, new String[] { "otherFormat",
0564: "testListByFormat" }, 40, 0);
0565: assertEquals(5, res.data.length);
0566:
0567: TableDataResult result = impl.quickFindAsset("testListByForma",
0568: 5, false);
0569: assertEquals(5, result.data.length);
0570:
0571: assertNotNull(result.data[0].id);
0572: assertTrue(result.data[0].values[0]
0573: .startsWith("testListByFormat"));
0574:
0575: result = impl.quickFindAsset("testListByForma", 3, false);
0576: assertEquals(4, result.data.length);
0577:
0578: assertEquals("MORE", result.data[3].id);
0579:
0580: }
0581:
0582: public String[] arr(String s) {
0583: return new String[] { s };
0584: }
0585:
0586: public void testStatus() throws Exception {
0587: RepositoryService impl = getService();
0588: String uuid = impl.createState("testStatus1");
0589: assertNotNull(uuid);
0590:
0591: String[] states = impl.listStates();
0592: assertTrue(states.length > 0);
0593:
0594: impl.createState("testStatus2");
0595: String[] states2 = impl.listStates();
0596: assertEquals(states.length + 1, states2.length);
0597:
0598: int match = 0;
0599: for (int i = 0; i < states2.length; i++) {
0600: if (states2[i].equals("testStatus2")) {
0601: match++;
0602: } else if (states2[i].equals("testStatus1")) {
0603: match++;
0604: }
0605: }
0606:
0607: assertEquals(2, match);
0608:
0609: String packagUUID = impl.createPackage("testStatus",
0610: "description");
0611: String ruleUUID = impl.createNewRule("testStatus", "desc",
0612: null, "testStatus", "drl");
0613: String ruleUUID2 = impl.createNewRule("testStatus2", "desc",
0614: null, "testStatus", "drl");
0615: impl.createState("testState");
0616:
0617: RuleAsset asset = impl.loadRuleAsset(ruleUUID);
0618: assertEquals(StateItem.DRAFT_STATE_NAME, asset.metaData.status);
0619: impl.changeState(ruleUUID, "testState", false);
0620: asset = impl.loadRuleAsset(ruleUUID);
0621: assertEquals("testState", asset.metaData.status);
0622: asset = impl.loadRuleAsset(ruleUUID2);
0623: assertEquals(StateItem.DRAFT_STATE_NAME, asset.metaData.status);
0624:
0625: impl.createState("testState2");
0626: impl.changeState(packagUUID, "testState2", true);
0627:
0628: PackageConfigData pkg = impl.loadPackageConfig(packagUUID);
0629: assertEquals("testState2", pkg.state);
0630:
0631: asset = impl.loadRuleAsset(ruleUUID2);
0632: assertEquals("testState2", asset.metaData.status);
0633:
0634: impl.checkinVersion(asset);
0635: asset = impl.loadRuleAsset(asset.uuid);
0636: assertEquals("testState2", asset.metaData.status);
0637:
0638: }
0639:
0640: public void testMovePackage() throws Exception {
0641: RepositoryService impl = getService();
0642: String[] cats = impl.loadChildCategories("/");
0643: if (cats.length == 0) {
0644: impl.createCategory("/", "la", "d");
0645: }
0646: String sourcePkgId = impl.createPackage("sourcePackage",
0647: "description");
0648: String destPkgId = impl.createPackage("targetPackage",
0649: "description");
0650:
0651: String cat = impl.loadChildCategories("/")[0];
0652:
0653: String uuid = impl.createNewRule("testMovePackage", "desc",
0654: cat, "sourcePackage", "drl");
0655:
0656: TableDataResult res = impl.listAssets(destPkgId,
0657: new String[] { "drl" }, 2, 0);
0658: assertEquals(0, res.data.length);
0659:
0660: impl.changeAssetPackage(uuid, "targetPackage", "yeah");
0661: res = impl.listAssets(destPkgId, new String[] { "drl" }, 2, 0);
0662:
0663: assertEquals(1, res.data.length);
0664:
0665: res = impl
0666: .listAssets(sourcePkgId, new String[] { "drl" }, 2, 0);
0667:
0668: assertEquals(0, res.data.length);
0669:
0670: }
0671:
0672: public void testCopyAsset() throws Exception {
0673: RepositoryService impl = getService();
0674: impl.createCategory("/", "templates", "ya");
0675: String uuid = impl.createNewRule("testCopyAsset", "",
0676: "templates", RulesRepository.DEFAULT_PACKAGE, "drl");
0677: String uuid2 = impl.copyAsset(uuid,
0678: RulesRepository.DEFAULT_PACKAGE, "testCopyAsset2");
0679: assertNotSame(uuid, uuid2);
0680:
0681: RuleAsset asset = impl.loadRuleAsset(uuid2);
0682: assertNotNull(asset);
0683: assertEquals(RulesRepository.DEFAULT_PACKAGE,
0684: asset.metaData.packageName);
0685: assertEquals("testCopyAsset2", asset.metaData.name);
0686: }
0687:
0688: public void testSnapshot() throws Exception {
0689: RepositoryService impl = getService();
0690: impl.createCategory("/", "snapshotTesting", "y");
0691: impl.createPackage("testSnapshot", "d");
0692: String uuid = impl.createNewRule("testSnapshotRule", "",
0693: "snapshotTesting", "testSnapshot", "drl");
0694:
0695: impl.createPackageSnapshot("testSnapshot", "X", false, "ya");
0696: SnapshotInfo[] snaps = impl.listSnapshots("testSnapshot");
0697: assertEquals(1, snaps.length);
0698: assertEquals("X", snaps[0].name);
0699: assertEquals("ya", snaps[0].comment);
0700: assertNotNull(snaps[0].uuid);
0701: PackageConfigData confSnap = impl
0702: .loadPackageConfig(snaps[0].uuid);
0703: assertEquals("testSnapshot", confSnap.name);
0704:
0705: impl.createPackageSnapshot("testSnapshot", "Y", false, "we");
0706: assertEquals(2, impl.listSnapshots("testSnapshot").length);
0707: impl.createPackageSnapshot("testSnapshot", "X", true, "we");
0708: assertEquals(2, impl.listSnapshots("testSnapshot").length);
0709:
0710: impl.copyOrRemoveSnapshot("testSnapshot", "X", false, "Q");
0711: assertEquals(3, impl.listSnapshots("testSnapshot").length);
0712:
0713: try {
0714: impl.copyOrRemoveSnapshot("testSnapshot", "X", false, "");
0715: fail("should not be able to copy snapshot to empty detination");
0716: } catch (SerializableException e) {
0717: assertNotNull(e.getMessage());
0718: }
0719:
0720: impl.copyOrRemoveSnapshot("testSnapshot", "X", true, null);
0721: assertEquals(2, impl.listSnapshots("testSnapshot").length);
0722:
0723: }
0724:
0725: public void testRemoveCategory() throws Exception {
0726:
0727: RepositoryService impl = getService();
0728: String[] children = impl.loadChildCategories("/");
0729: impl.createCategory("/", "testRemoveCategory", "foo");
0730:
0731: impl.removeCategory("testRemoveCategory");
0732: String[] _children = impl.loadChildCategories("/");
0733: assertEquals(children.length, _children.length);
0734:
0735: }
0736:
0737: public void testRemoveAsset() throws Exception {
0738: RepositoryService impl = getService();
0739: String cat = "testRemoveAsset";
0740: impl.createCategory("/", cat, "ya");
0741: String pkgUUID = impl.createPackage("testRemoveAsset", "");
0742:
0743: String uuid = impl.createNewRule("testRemoveAsset", "x", cat,
0744: "testRemoveAsset", "testRemoveAsset");
0745:
0746: String uuid2 = impl.createNewRule("testRemoveAsset2", "x", cat,
0747: "testRemoveAsset", "testRemoveAsset");
0748:
0749: String uuid3 = impl.createNewRule("testRemoveAsset3", "x", cat,
0750: "testRemoveAsset", "testRemoveAsset");
0751: String uuid4 = impl.createNewRule("testRemoveAsset4", "x", cat,
0752: "testRemoveAsset", "testRemoveAsset");
0753:
0754: TableDataResult res = impl.listAssets(pkgUUID,
0755: arr("testRemoveAsset"), -1, 0);
0756: assertEquals(4, res.data.length);
0757:
0758: impl.removeAsset(uuid4);
0759:
0760: res = impl.listAssets(pkgUUID, arr("testRemoveAsset"), -1, 0);
0761: assertEquals(3, res.data.length);
0762: }
0763:
0764: public void testArchiveAsset() throws Exception {
0765: RepositoryService impl = getService();
0766: String cat = "testArchiveAsset";
0767: impl.createCategory("/", cat, "ya");
0768: String pkgUUID = impl.createPackage("testArchiveAsset", "");
0769:
0770: String uuid = impl.createNewRule("testArchiveAsset", "x", cat,
0771: "testArchiveAsset", "testArchiveAsset");
0772:
0773: String uuid2 = impl.createNewRule("testArchiveAsset2", "x",
0774: cat, "testArchiveAsset", "testArchiveAsset");
0775:
0776: String uuid3 = impl.createNewRule("testArchiveAsset3", "x",
0777: cat, "testArchiveAsset", "testArchiveAsset");
0778: String uuid4 = impl.createNewRule("testArchiveAsset4", "x",
0779: cat, "testArchiveAsset", "testArchiveAsset");
0780:
0781: TableDataResult res = impl.listAssets(pkgUUID,
0782: arr("testArchiveAsset"), -1, 0);
0783: assertEquals(4, res.data.length);
0784:
0785: impl.archiveAsset(uuid4, true);
0786:
0787: res = impl.listAssets(pkgUUID, arr("testArchiveAsset"), -1, 0);
0788: assertEquals(3, res.data.length);
0789:
0790: impl.archiveAsset(uuid4, false);
0791:
0792: res = impl.listAssets(pkgUUID, arr("testArchiveAsset"), -1, 0);
0793: assertEquals(4, res.data.length);
0794:
0795: }
0796:
0797: public void testLoadSuggestionCompletionEngine() throws Exception {
0798: RepositoryService impl = getService();
0799: String uuid = impl.createPackage("testSuggestionComp", "x");
0800: PackageConfigData conf = impl.loadPackageConfig(uuid);
0801: conf.header = "import java.util.List";
0802:
0803: SuggestionCompletionEngine eng = impl
0804: .loadSuggestionCompletionEngine("testSuggestionComp");
0805: assertNotNull(eng);
0806:
0807: }
0808:
0809: /**
0810: * This will test creating a package, check it compiles, and can exectute rules,
0811: * then take a snapshot, and check that it reports errors.
0812: */
0813: public void testBinaryPackageCompileAndExecute() throws Exception {
0814: ServiceImplementation impl = getService();
0815: RulesRepository repo = impl.repository;
0816:
0817: //create our package
0818: PackageItem pkg = repo.createPackage(
0819: "testBinaryPackageCompile", "");
0820: pkg.updateHeader("import org.drools.Person");
0821: AssetItem rule1 = pkg.addAsset("rule_1", "");
0822: rule1.updateFormat(AssetFormats.DRL);
0823: rule1
0824: .updateContent("rule 'rule1' \n when \np : Person() \n then \np.setAge(42); \n end");
0825: rule1.checkin("");
0826: repo.save();
0827:
0828: BuilderResult[] results = impl
0829: .buildPackage(pkg.getUUID(), null);
0830: assertNull(results);
0831:
0832: pkg = repo.loadPackage("testBinaryPackageCompile");
0833: byte[] binPackage = pkg.getCompiledPackageBytes();
0834:
0835: assertNotNull(binPackage);
0836:
0837: ByteArrayInputStream bin = new ByteArrayInputStream(binPackage);
0838: ObjectInputStream in = new DroolsObjectInputStream(bin);
0839: Package binPkg = (Package) in.readObject();
0840:
0841: assertNotNull(binPkg);
0842: assertTrue(binPkg.isValid());
0843:
0844: Person p = new Person();
0845:
0846: BinaryRuleBaseLoader loader = new BinaryRuleBaseLoader();
0847: loader.addPackage(new ByteArrayInputStream(binPackage));
0848: RuleBase rb = loader.getRuleBase();
0849:
0850: StatelessSession sess = rb.newStatelessSession();
0851: sess.execute(p);
0852: assertEquals(42, p.getAge());
0853:
0854: impl.createPackageSnapshot("testBinaryPackageCompile", "SNAP1",
0855: false, "");
0856:
0857: rule1
0858: .updateContent("rule 'rule1' \n when p:PersonX() \n then System.err.println(42); \n end");
0859: rule1.checkin("");
0860:
0861: results = impl.buildPackage(pkg.getUUID(), null);
0862: assertNotNull(results);
0863: assertEquals(1, results.length);
0864: assertEquals(rule1.getName(), results[0].assetName);
0865: assertEquals(AssetFormats.DRL, results[0].assetFormat);
0866: assertNotNull(results[0].message);
0867: assertEquals(rule1.getUUID(), results[0].uuid);
0868:
0869: pkg = repo.loadPackageSnapshot("testBinaryPackageCompile",
0870: "SNAP1");
0871: results = impl.buildPackage(pkg.getUUID(), null);
0872: assertNull(results);
0873:
0874: }
0875:
0876: /**
0877: * This will test creating a package with a BRL rule, check it compiles, and can exectute rules,
0878: * then take a snapshot, and check that it reports errors.
0879: */
0880: public void testBinaryPackageCompileAndExecuteWithBRXML()
0881: throws Exception {
0882: ServiceImplementation impl = getService();
0883: RulesRepository repo = impl.repository;
0884:
0885: //create our package
0886: PackageItem pkg = repo.createPackage(
0887: "testBinaryPackageCompileBRL", "");
0888: pkg.updateHeader("import org.drools.Person");
0889: AssetItem rule2 = pkg.addAsset("rule2", "");
0890: rule2.updateFormat(AssetFormats.BUSINESS_RULE);
0891:
0892: RuleModel model = new RuleModel();
0893: model.name = "rule2";
0894: FactPattern pattern = new FactPattern("Person");
0895: pattern.boundName = "p";
0896: ActionSetField action = new ActionSetField("p");
0897: ActionFieldValue value = new ActionFieldValue("age", "42",
0898: SuggestionCompletionEngine.TYPE_NUMERIC);
0899: action.addFieldValue(value);
0900:
0901: model.addLhsItem(pattern);
0902: model.addRhsItem(action);
0903:
0904: rule2.updateContent(BRXMLPersistence.getInstance().marshal(
0905: model));
0906: rule2.checkin("");
0907: repo.save();
0908:
0909: BuilderResult[] results = impl
0910: .buildPackage(pkg.getUUID(), null);
0911: if (results != null) {
0912: for (int i = 0; i < results.length; i++) {
0913: System.err.println(results[i].message);
0914: }
0915: }
0916: assertNull(results);
0917:
0918: pkg = repo.loadPackage("testBinaryPackageCompileBRL");
0919: byte[] binPackage = pkg.getCompiledPackageBytes();
0920:
0921: //Here is where we write it out if needed... UNCOMMENT if needed for the binary test
0922: // FileOutputStream out = new FileOutputStream("/home/michael/RepoBinPackage.pkg");
0923: // out.write( binPackage );
0924: // out.flush();
0925: // out.close();
0926:
0927: assertNotNull(binPackage);
0928:
0929: ByteArrayInputStream bin = new ByteArrayInputStream(binPackage);
0930: ObjectInputStream in = new DroolsObjectInputStream(bin);
0931: Package binPkg = (Package) in.readObject();
0932:
0933: assertNotNull(binPkg);
0934: assertTrue(binPkg.isValid());
0935:
0936: Person p = new Person();
0937:
0938: BinaryRuleBaseLoader loader = new BinaryRuleBaseLoader();
0939: loader.addPackage(new ByteArrayInputStream(binPackage));
0940: RuleBase rb = loader.getRuleBase();
0941:
0942: StatelessSession sess = rb.newStatelessSession();
0943: sess.execute(p);
0944: assertEquals(42, p.getAge());
0945:
0946: impl.createPackageSnapshot("testBinaryPackageCompileBRL",
0947: "SNAP1", false, "");
0948:
0949: pattern.factType = "PersonX";
0950: rule2.updateContent(BRXMLPersistence.getInstance().marshal(
0951: model));
0952: rule2.checkin("");
0953:
0954: results = impl.buildPackage(pkg.getUUID(), null);
0955: assertNotNull(results);
0956: assertTrue(results.length > 0);
0957: //assertEquals(2, results.length);
0958: assertEquals(rule2.getName(), results[0].assetName);
0959: assertEquals(AssetFormats.BUSINESS_RULE, results[0].assetFormat);
0960: assertNotNull(results[0].message);
0961: assertEquals(rule2.getUUID(), results[0].uuid);
0962:
0963: pkg = repo.loadPackageSnapshot("testBinaryPackageCompileBRL",
0964: "SNAP1");
0965: results = impl.buildPackage(pkg.getUUID(), null);
0966: assertNull(results);
0967:
0968: //check that the rule name in the model is being set
0969: AssetItem asset2 = pkg.addAsset("testSetRuleName", "");
0970: asset2.updateFormat(AssetFormats.BUSINESS_RULE);
0971: asset2.checkin("");
0972:
0973: RuleModel model2 = new RuleModel();
0974: assertNull(model2.name);
0975: RuleAsset asset = impl.loadRuleAsset(asset2.getUUID());
0976: asset.content = model2;
0977:
0978: impl.checkinVersion(asset);
0979:
0980: asset = impl.loadRuleAsset(asset2.getUUID());
0981:
0982: model2 = (RuleModel) asset.content;
0983: assertNotNull(model2);
0984: assertNotNull(model2.name);
0985: assertEquals(asset2.getName(), model2.name);
0986:
0987: }
0988:
0989: /**
0990: * this loads up a precompile binary package. If this fails,
0991: * then it means it needs to be updated. It gets the package form the BRL example above.
0992: */
0993: public void testLoadAndExecBinary() throws Exception {
0994: Person p = new Person();
0995: BinaryRuleBaseLoader loader = new BinaryRuleBaseLoader();
0996: loader.addPackage(this .getClass().getResourceAsStream(
0997: "/RepoBinPackage.pkg"));
0998: RuleBase rb = loader.getRuleBase();
0999: StatelessSession sess = rb.newStatelessSession();
1000: sess.execute(p);
1001: assertEquals(42, p.getAge());
1002: }
1003:
1004: public void testPackageSource() throws Exception {
1005: ServiceImplementation impl = getService();
1006: RulesRepository repo = impl.repository;
1007:
1008: //create our package
1009: PackageItem pkg = repo.createPackage("testPackageSource", "");
1010: pkg.updateHeader("import org.goo.Ber");
1011: AssetItem rule1 = pkg.addAsset("rule_1", "");
1012: rule1.updateFormat(AssetFormats.DRL);
1013: rule1
1014: .updateContent("rule 'rule1' \n when p:Person() \n then p.setAge(42); \n end");
1015: rule1.checkin("");
1016: repo.save();
1017:
1018: AssetItem func = pkg.addAsset("funky", "");
1019: func.updateFormat(AssetFormats.FUNCTION);
1020: func.updateContent("this is a func");
1021: func.checkin("");
1022:
1023: String drl = impl.buildPackageSource(pkg.getUUID());
1024: assertNotNull(drl);
1025:
1026: assertTrue(drl.indexOf("import org.goo.Ber") > -1);
1027: assertTrue(drl.indexOf("package testPackageSource") > -1);
1028: assertTrue(drl.indexOf("rule 'rule1'") > -1);
1029: assertTrue(drl.indexOf("this is a func") > -1);
1030: assertTrue(drl.indexOf("this is a func") < drl
1031: .indexOf("rule 'rule1'"));
1032: assertTrue(drl.indexOf("package testPackageSource") < drl
1033: .indexOf("this is a func"));
1034: assertTrue(drl.indexOf("package testPackageSource") < drl
1035: .indexOf("import org.goo.Ber"));
1036:
1037: AssetItem dsl = pkg.addAsset("MyDSL", "");
1038: dsl.updateFormat(AssetFormats.DSL);
1039: dsl
1040: .updateContent("[when]This is foo=bar()\n[then]do something=yeahMan();");
1041: dsl.checkin("");
1042:
1043: AssetItem asset = pkg.addAsset("MyDSLRule", "");
1044: asset.updateFormat(AssetFormats.DSL_TEMPLATE_RULE);
1045: asset
1046: .updateContent("when \n This is foo \n then \n do something");
1047: asset.checkin("");
1048:
1049: drl = impl.buildPackageSource(pkg.getUUID());
1050: assertNotNull(drl);
1051:
1052: assertTrue(drl.indexOf("import org.goo.Ber") > -1);
1053: assertTrue(drl.indexOf("This is foo") == -1);
1054: assertTrue(drl.indexOf("do something") == -1);
1055: assertTrue(drl.indexOf("bar()") > 0);
1056: assertTrue(drl.indexOf("yeahMan();") > 0);
1057:
1058: }
1059:
1060: public void testAssetSource() throws Exception {
1061: ServiceImplementation impl = getService();
1062: RulesRepository repo = impl.repository;
1063:
1064: //create our package
1065: PackageItem pkg = repo.createPackage("testAssetSource", "");
1066: AssetItem asset = pkg.addAsset("testRule", "");
1067: asset.updateFormat(AssetFormats.DRL);
1068: asset
1069: .updateContent("rule 'n' \n when Foo() then bar(); \n end");
1070: asset.checkin("");
1071: repo.save();
1072:
1073: RuleAsset rule = impl.loadRuleAsset(asset.getUUID());
1074: String drl = impl.buildAssetSource(rule);
1075: assertEquals("rule 'n' \n when Foo() then bar(); \n end", drl);
1076:
1077: asset = pkg.addAsset("DT", "");
1078: asset.updateFormat(AssetFormats.DECISION_SPREADSHEET_XLS);
1079: asset.updateBinaryContentAttachment(this .getClass()
1080: .getResourceAsStream("/SampleDecisionTable.xls"));
1081: asset.checkin("");
1082:
1083: rule = impl.loadRuleAsset(asset.getUUID());
1084: drl = impl.buildAssetSource(rule);
1085: assertNotNull(drl);
1086: assertTrue(drl.indexOf("rule") > -1);
1087: assertTrue(drl.indexOf("policy: Policy") > -1);
1088:
1089: AssetItem dsl = pkg.addAsset("MyDSL", "");
1090: dsl.updateFormat(AssetFormats.DSL);
1091: dsl
1092: .updateContent("[when]This is foo=bar()\n[then]do something=yeahMan();");
1093: dsl.checkin("");
1094:
1095: asset = pkg.addAsset("MyDSLRule", "");
1096: asset.updateFormat(AssetFormats.DSL_TEMPLATE_RULE);
1097: asset
1098: .updateContent("when \n This is foo \n then \n do something");
1099: asset.checkin("");
1100:
1101: rule = impl.loadRuleAsset(asset.getUUID());
1102: drl = impl.buildAssetSource(rule);
1103: assertNotNull(drl);
1104: assertTrue(drl.indexOf("This is foo") == -1);
1105: assertTrue(drl.indexOf("do something") == -1);
1106: assertTrue(drl.indexOf("bar()") > -1);
1107: assertTrue(drl.indexOf("yeahMan();") > -1);
1108:
1109: rule = impl.loadRuleAsset(repo.copyAsset(asset.getUUID(),
1110: "testAssetSource", "newRuleName"));
1111: //System.err.println(((RuleContentText)rule.content).content);
1112: drl = impl.buildAssetSource(rule);
1113: assertNotNull(drl);
1114: assertTrue(drl.indexOf("newRuleName") > 0);
1115:
1116: }
1117:
1118: public void testBuildAsset() throws Exception {
1119: ServiceImplementation impl = getService();
1120: RulesRepository repo = impl.repository;
1121:
1122: //create our package
1123: PackageItem pkg = repo.createPackage("testBuildAsset", "");
1124: AssetItem model = pkg.addAsset("MyModel", "");
1125: model.updateFormat(AssetFormats.MODEL);
1126: model.updateBinaryContentAttachment(this .getClass()
1127: .getResourceAsStream("/billasurf.jar"));
1128: model.checkin("");
1129:
1130: pkg.updateHeader("import com.billasurf.Person");
1131:
1132: AssetItem asset = pkg.addAsset("testRule", "");
1133: asset.updateFormat(AssetFormats.DRL);
1134: asset
1135: .updateContent("rule 'MyGoodRule' \n when Person() then System.err.println(42); \n end");
1136: asset.checkin("");
1137: repo.save();
1138:
1139: RuleAsset rule = impl.loadRuleAsset(asset.getUUID());
1140:
1141: //check its all OK
1142: BuilderResult[] result = impl.buildAsset(rule);
1143: assertNull(result);
1144:
1145: //try it with a bad rule
1146: RuleContentText text = new RuleContentText();
1147: text.content = "rule 'MyBadRule' \n when Personx() then System.err.println(42); \n end";
1148: rule.content = text;
1149:
1150: result = impl.buildAsset(rule);
1151: assertNotNull(result);
1152: assertNotNull(result[0].message);
1153: assertEquals(AssetFormats.DRL, result[0].assetFormat);
1154:
1155: //now mix in a DSL
1156: AssetItem dsl = pkg.addAsset("MyDSL", "");
1157: dsl.updateFormat(AssetFormats.DSL);
1158: dsl
1159: .updateContent("[when]There is a person=Person()\n[then]print out 42=System.err.println(42);");
1160: dsl.checkin("");
1161:
1162: AssetItem dslRule = pkg.addAsset("dslRule", "");
1163: dslRule.updateFormat(AssetFormats.DSL_TEMPLATE_RULE);
1164: dslRule
1165: .updateContent("when \n There is a person \n then \n print out 42");
1166: dslRule.checkin("");
1167:
1168: rule = impl.loadRuleAsset(dslRule.getUUID());
1169:
1170: result = impl.buildAsset(rule);
1171: assertNull(result);
1172:
1173: asset = pkg.addAsset("someEnumThing", "");
1174: asset.updateFormat(AssetFormats.ENUMERATION);
1175: asset.updateContent("goober boy");
1176: asset.checkin("");
1177: result = impl.buildAsset(impl.loadRuleAsset(asset.getUUID()));
1178: assertFalse(result.length == 0);
1179:
1180: }
1181:
1182: public void testBuildAssetBRXMLAndCopy() throws Exception {
1183: ServiceImplementation impl = getService();
1184: RulesRepository repo = impl.repository;
1185:
1186: //create our package
1187: PackageItem pkg = repo.createPackage("testBuildAssetBRL", "");
1188: AssetItem model = pkg.addAsset("MyModel", "");
1189: model.updateFormat(AssetFormats.MODEL);
1190: model.updateBinaryContentAttachment(this .getClass()
1191: .getResourceAsStream("/billasurf.jar"));
1192: model.checkin("");
1193:
1194: pkg.updateHeader("import com.billasurf.Person");
1195: impl.createCategory("/", "brl", "");
1196:
1197: String uuid = impl.createNewRule("testBRL", "", "brl",
1198: "testBuildAssetBRL", AssetFormats.BUSINESS_RULE);
1199:
1200: RuleAsset rule = impl.loadRuleAsset(uuid);
1201:
1202: RuleModel m = (RuleModel) rule.content;
1203: assertNotNull(m);
1204: m.name = "testBRL";
1205:
1206: FactPattern p = new FactPattern("Person");
1207: p.boundName = "p";
1208: SingleFieldConstraint con = new SingleFieldConstraint();
1209: con.fieldName = "name";
1210: con.value = "mark";
1211: con.operator = "==";
1212: con.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
1213:
1214: p.addConstraint(con);
1215:
1216: m.addLhsItem(p);
1217:
1218: ActionSetField set = new ActionSetField("p");
1219: ActionFieldValue f = new ActionFieldValue("name", "42-ngoo",
1220: SuggestionCompletionEngine.TYPE_STRING);
1221: set.addFieldValue(f);
1222:
1223: m.addRhsItem(set);
1224:
1225: impl.checkinVersion(rule);
1226:
1227: //check its all OK
1228: BuilderResult[] result = impl.buildAsset(rule);
1229: if (result != null) {
1230: for (int i = 0; i < result.length; i++) {
1231: System.err.println(result[i].message);
1232: }
1233: }
1234: assertNull(result);
1235:
1236: List assets = iteratorToList(pkg.getAssets());
1237: assertEquals(2, assets.size());
1238: //now lets copy...
1239: String newUUID = impl.copyAsset(rule.uuid,
1240: rule.metaData.packageName, "ruleName2");
1241:
1242: assets = iteratorToList(pkg.getAssets());
1243: assertEquals(3, assets.size());
1244: RuleAsset asset = impl.loadRuleAsset(newUUID);
1245:
1246: String pkgSource = impl.buildPackageSource(pkg.getUUID());
1247:
1248: assertTrue(pkgSource.indexOf("ruleName2") > 0);
1249: assertTrue(impl.buildAssetSource(asset).indexOf("ruleName2") > 0);
1250: assertTrue(impl.buildAssetSource(asset).indexOf("testBRL") == -1);
1251:
1252: // RuleModel model2 = (RuleModel) asset.content;
1253: // assertEquals("ruleName2", model2.name);
1254:
1255: }
1256:
1257: private List iteratorToList(Iterator assets) {
1258: List result = new ArrayList();
1259: while (assets.hasNext()) {
1260: result.add(assets.next());
1261:
1262: }
1263: return result;
1264: }
1265:
1266: public void testBuildAssetWithPackageConfigError() throws Exception {
1267: ServiceImplementation impl = getService();
1268: RulesRepository repo = impl.repository;
1269:
1270: PackageItem pkg = repo.createPackage(
1271: "testBuildAssetWithPackageConfigError", "");
1272: // AssetItem model = pkg.addAsset( "MyModel", "" );
1273: // model.updateFormat( AssetFormats.MODEL );
1274: // model.updateBinaryContentAttachment( this.getClass().getResourceAsStream( "/billasurf.jar" ) );
1275: // model.checkin( "" );
1276:
1277: // pkg.updateHeader( "import com.billasurf.Person" );
1278:
1279: AssetItem asset = pkg.addAsset("testRule", "");
1280: asset.updateFormat(AssetFormats.DRL);
1281: asset.updateContent("rule 'MyGoodRule' \n when \n then \n end");
1282: asset.checkin("");
1283: repo.save();
1284:
1285: RuleAsset rule = impl.loadRuleAsset(asset.getUUID());
1286:
1287: //check its all OK
1288: BuilderResult[] result = impl.buildAsset(rule);
1289: if (!(result == null)) {
1290: System.err.println(result[0].assetName + " "
1291: + result[0].message);
1292: }
1293: assertNull(result);
1294:
1295: pkg.updateHeader("importxxxx");
1296: repo.save();
1297: result = impl.buildAsset(rule);
1298: assertNotNull(result);
1299:
1300: assertEquals(1, result.length);
1301: assertEquals("package", result[0].assetFormat);
1302: assertNotNull(result[0].message);
1303:
1304: }
1305:
1306: private ServiceImplementation getService() throws Exception {
1307: ServiceImplementation impl = new ServiceImplementation();
1308: impl.repository = new RulesRepository(
1309: TestEnvironmentSessionHelper.getSession());
1310: return impl;
1311: }
1312:
1313: }
|