public void testXLSDecisionTable() throws Exception {

    RulesRepository repo = getRepo();

    // first, setup the package correctly:
    PackageItem pkg = repo.createPackage("testXLSDecisionTable", "");

    ServiceImplementation.updateDroolsHeader(
        "import org.acme.insurance.Policy\n import org.acme.insurance.Driver", pkg);
    repo.save();

    InputStream xls = this.getClass().getResourceAsStream("/SampleDecisionTable.xls");
    assertNotNull(xls);

    AssetItem asset = pkg.addAsset("MyDT", "");
    asset.updateFormat(AssetFormats.DECISION_SPREADSHEET_XLS);
    asset.updateBinaryContentAttachment(xls);
    asset.checkin("");

    ContentPackageAssembler asm = new ContentPackageAssembler(pkg);
    if (asm.hasErrors()) {
      System.err.println(asm.getErrors().get(0).errorReport);
      System.err.println(asm.getDRL());
    }
    assertFalse(asm.hasErrors());

    String drl = asm.getDRL();

    assertContains("policy: Policy", drl);

    Package bin = asm.getBinaryPackage();

    RuleBase rb = RuleBaseFactory.newRuleBase();
    rb.addPackage(bin);

    WorkingMemory wm = rb.newStatefulSession();

    // now create some test data
    Driver driver = new Driver();
    Policy policy = new Policy();

    wm.insert(driver);
    wm.insert(policy);

    wm.fireAllRules();

    assertEquals(120, policy.getBasePrice());

    asset.updateBinaryContentAttachment(
        this.getClass().getResourceAsStream("/SampleDecisionTable_WithError.xls"));
    asset.checkin("");
    asm = new ContentPackageAssembler(pkg);
    assertTrue(asm.hasErrors());
    assertEquals(asset.getName(), asm.getErrors().get(0).itemInError.getName());
    asm = new ContentPackageAssembler(pkg, false);
    assertFalse(asm.hasErrors());
    drl = asm.getDRL();
    assertNotNull(drl);
    assertContains("Driverx", drl);
  }
  /**
   * This this case we will test errors that occur in rule assets, not in functions or package
   * header.
   */
  public void testErrorsInRuleAsset() throws Exception {

    RulesRepository repo = getRepo();

    // first, setup the package correctly:
    PackageItem pkg = repo.createPackage("testErrorsInRuleAsset", "");
    AssetItem model = pkg.addAsset("model", "qed");
    model.updateFormat(AssetFormats.MODEL);
    model.updateBinaryContentAttachment(this.getClass().getResourceAsStream("/billasurf.jar"));
    model.checkin("");
    ServiceImplementation.updateDroolsHeader(
        "import com.billasurf.Board\n global com.billasurf.Person customer", pkg);
    repo.save();

    AssetItem goodRule = pkg.addAsset("goodRule", "");
    goodRule.updateFormat(AssetFormats.DRL);
    goodRule.updateContent("rule 'yeah' \n when \n Board() \n then \n System.out.println(42); end");
    goodRule.checkin("");

    AssetItem badRule = pkg.addAsset("badRule", "xxx");
    badRule.updateFormat(AssetFormats.DRL);
    badRule.updateContent("if something then another");
    badRule.checkin("");

    ContentPackageAssembler asm = new ContentPackageAssembler(pkg);
    assertTrue(asm.hasErrors());
    assertFalse(asm.isPackageConfigurationInError());

    for (ContentAssemblyError err : asm.getErrors()) {
      assertTrue(err.itemInError.getName().equals(badRule.getName()));
      assertNotEmpty(err.errorReport);
    }
  }
  public void testCustomSelector() throws Exception {
    RulesRepository repo = getRepo();

    // create our package
    PackageItem pkg = repo.createPackage("testCustomSelector", "");
    ServiceImplementation.updateDroolsHeader("import org.drools.Person", pkg);
    AssetItem rule1 = pkg.addAsset("rule1", "");
    rule1.updateFormat(AssetFormats.DRL);

    rule1.updateContent("when \n Person() \n then \n System.out.println(\"yeah\");\n");
    rule1.checkin("");

    AssetItem rule2 = pkg.addAsset("rule2", "");
    rule2.updateFormat(AssetFormats.DRL);

    rule2.updateContent("when \n Person() \n then \n System.out.println(\"yeah\");\n");
    rule2.checkin("");

    SelectorManager sm = SelectorManager.getInstance();
    sm.selectors.put(
        "testSelect",
        new AssetSelector() {
          public boolean isAssetAllowed(AssetItem asset) {
            return asset.getName().equals("rule2");
          }
        });

    ContentPackageAssembler asm = new ContentPackageAssembler(pkg, "testSelect");

    Package pk = asm.getBinaryPackage();
    assertEquals(1, pk.getRules().length);
    assertEquals("rule2", pk.getRules()[0].getName());

    asm = new ContentPackageAssembler(pkg, null);
    pk = asm.getBinaryPackage();
    assertEquals(2, pk.getRules().length);

    asm = new ContentPackageAssembler(pkg, "nothing valid");
    assertTrue(asm.hasErrors());
    assertEquals(1, asm.getErrors().size());
    assertEquals(pkg, asm.getErrors().get(0).itemInError);

    asm = new ContentPackageAssembler(pkg, "");
    pk = asm.getBinaryPackage();
    assertEquals(2, pk.getRules().length);
  }
  /** Test package configuration errors, including header, functions, DSL files. */
  public void testPackageConfigWithErrors() throws Exception {
    // test the config, no rule assets yet
    RulesRepository repo = getRepo();
    PackageItem pkg = repo.createPackage("testBuilderPackageConfig", "x");
    ServiceImplementation.updateDroolsHeader("import java.util.List", pkg);
    AssetItem func = pkg.addAsset("func1", "a function");
    func.updateFormat(AssetFormats.FUNCTION);
    func.updateContent(
        "function void doSomething() { \n System.err.println(List.class.toString()); }");
    func.checkin("yeah");

    func = pkg.addAsset("func2", "q");
    func.updateFormat(AssetFormats.FUNCTION);
    func.updateContent("function void foo() { \nSystem.err.println(42); \n}");
    func.checkin("");

    AssetItem ass = pkg.addAsset("dsl", "m");
    ass.updateFormat(AssetFormats.DSL);
    ass.updateContent("[when]Foo bar=String()");
    ass.checkin("");
    repo.save();

    // now lets light it up
    ContentPackageAssembler assembler = new ContentPackageAssembler(pkg);
    assertFalse(assembler.hasErrors());
    Package bin = assembler.getBinaryPackage();
    assertNotNull(bin);
    assertEquals("testBuilderPackageConfig", bin.getName());
    assertEquals(2, bin.getFunctions().size());

    assertTrue(bin.isValid());
    assertEquals(1, assembler.builder.getDSLMappingFiles().size());

    ServiceImplementation.updateDroolsHeader("koo koo ca choo", pkg);
    assembler = new ContentPackageAssembler(pkg);
    assertTrue(assembler.hasErrors());
    assertTrue(assembler.isPackageConfigurationInError());

    ServiceImplementation.updateDroolsHeader("import java.util.Date", pkg);
    assembler = new ContentPackageAssembler(pkg);
    assertTrue(assembler.hasErrors());
    assertTrue(assembler.getErrors().get(0).itemInError instanceof AssetItem);

    assertEquals("func1", assembler.getErrors().get(0).itemInError.getName());
    try {
      assembler.getBinaryPackage();
      fail("should not work as is in error.");
    } catch (IllegalStateException e) {
      assertNotNull(e.getMessage());
    }

    // fix it up
    ServiceImplementation.updateDroolsHeader("import java.util.List", pkg);
    assembler = new ContentPackageAssembler(pkg);
    assertFalse(assembler.hasErrors());

    // now break a DSL and check the error
    ass.updateContent("rubbish");
    ass.checkin("");
    assembler = new ContentPackageAssembler(pkg);
    assertTrue(assembler.hasErrors());
    assertTrue(assembler.getErrors().get(0).itemInError.getName().equals(ass.getName()));
    assertNotEmpty(assembler.getErrors().get(0).errorReport);
    assertFalse(assembler.isPackageConfigurationInError());

    // now fix it up
    ass.updateContent("[when]foo=String()");
    ass.checkin("");
    assembler = new ContentPackageAssembler(pkg);
    assertFalse(assembler.hasErrors());

    // break a func, and check for error
    func.updateContent("goo");
    func.checkin("");
    assembler = new ContentPackageAssembler(pkg);
    assertTrue(assembler.hasErrors());
    assertFalse(assembler.isPackageConfigurationInError());
    assertTrue(assembler.getErrors().get(0).itemInError.getName().equals(func.getName()));
    assertNotEmpty(assembler.getErrors().get(0).errorReport);
  }