001: package org.drools.brms.server;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.InputStream;
020:
021: import junit.framework.TestCase;
022:
023: import org.drools.brms.client.common.AssetFormats;
024: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
025: import org.drools.brms.client.rpc.PackageConfigData;
026: import org.drools.brms.server.files.FileManagerUtils;
027: import org.drools.brms.server.util.TestEnvironmentSessionHelper;
028: import org.drools.repository.AssetItem;
029: import org.drools.repository.PackageItem;
030: import org.drools.repository.RulesRepository;
031:
032: import com.google.gwt.user.client.rpc.SerializableException;
033:
034: /**
035: * This class will setup the data in a test state, which is
036: * good for screenshots/playing around.
037: *
038: * If you run this by itself, the database will be wiped, and left with only this data in it.
039: * If it is run as part of the suite, it will just augment the data.
040: *
041: * This sets up the data for a fictional company Billasurf, dealing with surfwear and equipment
042: * (for surfing, boarding etc).
043: *
044: * @author Michael Neale
045: */
046: public class PopulateDataTest extends TestCase {
047:
048: public void testPopulate() throws Exception {
049: ServiceImplementation serv = new ServiceImplementation();
050: serv.repository = new RulesRepository(
051: TestEnvironmentSessionHelper.getSession());
052:
053: createCategories(serv);
054: createStates(serv);
055: createPackages(serv);
056: createModel(serv);
057:
058: createSomeRules(serv);
059: createPackageSnapshots(serv);
060:
061: }
062:
063: private void createModel(ServiceImplementation serv)
064: throws Exception {
065: RulesRepository repo = serv.repository;
066: String uuid = serv
067: .createNewRule("DomainModel",
068: "This is the business object model", null,
069: "com.billasurf.manufacturing.plant",
070: AssetFormats.MODEL);
071: InputStream file = this .getClass().getResourceAsStream(
072: "/billasurf.jar");
073: assertNotNull(file);
074:
075: FileManagerUtils fm = new FileManagerUtils();
076: fm.repository = repo;
077:
078: fm.attachFileToAsset(uuid, file, "billasurf.jar");
079:
080: AssetItem item = repo.loadAssetByUUID(uuid);
081: assertNotNull(item.getBinaryContentAsBytes());
082: assertEquals(item.getBinaryContentAttachmentFileName(),
083: "billasurf.jar");
084:
085: PackageItem pkg = repo
086: .loadPackage("com.billasurf.manufacturing.plant");
087: pkg
088: .updateHeader("import com.billasurf.Board\nimport com.billasurf.Person"
089: + "\n\nglobal com.billasurf.Person prs");
090: pkg.checkin("added imports");
091:
092: SuggestionCompletionEngine eng = serv
093: .loadSuggestionCompletionEngine("com.billasurf.manufacturing.plant");
094: assertNotNull(eng);
095:
096: assertEquals(2, eng.factTypes.length);
097: String[] fields = (String[]) eng.fieldsForType.get("Board");
098: assertTrue(fields.length == 3);
099:
100: String[] globalVars = eng.getGlobalVariables();
101: assertEquals(1, globalVars.length);
102: assertEquals("prs", globalVars[0]);
103: assertEquals(2,
104: eng.getFieldCompletionsForGlobalVariable("prs").length);
105:
106: fields = (String[]) eng.fieldsForType.get("Person");
107:
108: assertTrue(fields.length == 2);
109:
110: }
111:
112: private void createPackageSnapshots(ServiceImplementation serv) {
113: serv.createPackageSnapshot("com.billasurf.finance", "TEST",
114: false, "The testing region.");
115: serv.createPackageSnapshot("com.billasurf.finance",
116: "PRODUCTION", false, "The testing region.");
117: serv.createPackageSnapshot("com.billasurf.finance",
118: "PRODUCTION ROLLBACK", false, "The testing region.");
119: }
120:
121: private void createSomeRules(ServiceImplementation serv)
122: throws SerializableException {
123: String uuid = serv.createNewRule(
124: "Surfboard_Colour_Combination",
125: "allowable combinations for basic boards.",
126: "Manufacturing/Boards", "com.billasurf.manufacturing",
127: AssetFormats.BUSINESS_RULE);
128: serv.changeState(uuid, "Pending", false);
129: uuid = serv.createNewRule("Premium_Colour_Combinations",
130: "This defines XXX.", "Manufacturing/Boards",
131: "com.billasurf.manufacturing",
132: AssetFormats.BUSINESS_RULE);
133: serv.changeState(uuid, "Approved", false);
134: uuid = serv.createNewRule("Fibreglass supplier selection",
135: "This defines XXX.", "Manufacturing/Boards",
136: "com.billasurf.manufacturing",
137: AssetFormats.BUSINESS_RULE);
138: uuid = serv.createNewRule("Recommended wax",
139: "This defines XXX.", "Manufacturing/Boards",
140: "com.billasurf.manufacturing",
141: AssetFormats.BUSINESS_RULE);
142:
143: }
144:
145: private void createPackages(ServiceImplementation serv)
146: throws SerializableException {
147: String uuid = serv.createPackage("com.billasurf.manufacturing",
148: "Rules for manufacturing.");
149:
150: PackageConfigData conf = serv.loadPackageConfig(uuid);
151: conf.header = "import com.billasurf.manuf.materials.*";
152: serv.savePackage(conf);
153:
154: serv.createPackage("com.billasurf.manufacturing.plant",
155: "Rules for manufacturing plants.");
156: serv.createPackage("com.billasurf.finance",
157: "All financial rules.");
158: serv.createPackage("com.billasurf.hrman",
159: "Rules for in house HR application.");
160: serv
161: .createPackage("com.billasurf.sales",
162: "Rules exposed as a service for pricing, and discounting.");
163:
164: }
165:
166: private void createStates(ServiceImplementation serv)
167: throws SerializableException {
168: serv.createState("Approved");
169: serv.createState("Pending");
170: }
171:
172: private void createCategories(ServiceImplementation serv) {
173: serv.createCategory("/", "HR", "");
174: serv.createCategory("/", "Sales", "");
175: serv.createCategory("/", "Manufacturing", "");
176: serv.createCategory("/", "Finance", "");
177:
178: serv.createCategory("HR", "Leave", "");
179: serv.createCategory("HR", "Training", "");
180: serv.createCategory("Sales", "Promotions", "");
181: serv.createCategory("Sales", "Old promotions", "");
182: serv.createCategory("Sales", "Boogie boards", "");
183: serv.createCategory("Sales", "Surf boards", "");
184: serv.createCategory("Sales", "Surf wear", "");
185: serv.createCategory("Manufacturing", "Surf wear", "");
186: serv.createCategory("Manufacturing", "Boards", "");
187: serv.createCategory("Finance", "Employees", "");
188: serv.createCategory("Finance", "Payables", "");
189: serv.createCategory("Finance", "Receivables", "");
190: }
191:
192: }
|