Search in sources :

Example 16 with VerifierReport

use of org.drools.verifier.data.VerifierReport in project drools by kiegroup.

the class RangeCheckIntegersTest method testSmallerOrEqual.

@Test
public void testSmallerOrEqual() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("rangeChecks/Integers.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(this.getClass().getResourceAsStream("MissingRangesForInts.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Range check for integers, if smaller than or equal is missing"));
    Set<String> rulesThatHadErrors = new HashSet<String>();
    for (Object o : session.getObjects()) {
        if (o instanceof Gap) {
            rulesThatHadErrors.add(((Gap) o).getRuleName());
        }
    // System.out.println(o);
    }
    assertTrue(rulesThatHadErrors.remove("Integer gap rule 4a"));
    assertTrue(rulesThatHadErrors.remove("Integer gap rule 5a"));
    if (!rulesThatHadErrors.isEmpty()) {
        for (String string : rulesThatHadErrors) {
            fail("Rule " + string + " caused an error.");
        }
    }
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) VerifierReport(org.drools.verifier.data.VerifierReport) Gap(org.drools.verifier.report.components.Gap) KieSession(org.kie.api.runtime.KieSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with VerifierReport

use of org.drools.verifier.data.VerifierReport in project drools by kiegroup.

the class VerifierTest method testVerifier.

@Test
public void testVerifier() {
    VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
    // Check that the builder works.
    assertFalse(vBuilder.hasErrors());
    assertEquals(0, vBuilder.getErrors().size());
    Verifier verifier = vBuilder.newVerifier();
    verifier.addResourcesToVerify(new ClassPathResource("Misc3.drl", Verifier.class), ResourceType.DRL);
    assertFalse(verifier.hasErrors());
    assertEquals(0, verifier.getErrors().size());
    boolean works = verifier.fireAnalysis();
    assertTrue(works);
    VerifierReport result = verifier.getResult();
    assertNotNull(result);
    assertEquals(0, result.getBySeverity(Severity.ERROR).size());
    assertEquals(6, result.getBySeverity(Severity.WARNING).size());
    assertEquals(1, result.getBySeverity(Severity.NOTE).size());
}
Also used : VerifierReport(org.drools.verifier.data.VerifierReport) VerifierBuilder(org.drools.verifier.builder.VerifierBuilder) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Test(org.junit.Test)

Example 18 with VerifierReport

use of org.drools.verifier.data.VerifierReport in project drools by kiegroup.

the class VerifierTest method testFactTypesFromJar.

@Test
public void testFactTypesFromJar() {
    VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
    // Check that the builder works.
    assertFalse(vBuilder.hasErrors());
    assertEquals(0, vBuilder.getErrors().size());
    Verifier verifier = vBuilder.newVerifier();
    try {
        JarInputStream jar = new JarInputStream(this.getClass().getResourceAsStream("model.jar"));
        verifier.addObjectModel(jar);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    verifier.addResourcesToVerify(new ClassPathResource("imports.drl", Verifier.class), ResourceType.DRL);
    assertFalse(verifier.hasErrors());
    assertEquals(0, verifier.getErrors().size());
    boolean works = verifier.fireAnalysis();
    assertTrue(works);
    VerifierReport result = verifier.getResult();
    Collection<ObjectType> objectTypes = result.getVerifierData().getAll(VerifierComponentType.OBJECT_TYPE);
    assertNotNull(objectTypes);
    assertEquals(3, objectTypes.size());
    Collection<Field> fields = result.getVerifierData().getAll(VerifierComponentType.FIELD);
    assertNotNull(fields);
    assertEquals(10, fields.size());
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) JarInputStream(java.util.jar.JarInputStream) VerifierReport(org.drools.verifier.data.VerifierReport) VerifierBuilder(org.drools.verifier.builder.VerifierBuilder) IOException(java.io.IOException) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Test(org.junit.Test)

Example 19 with VerifierReport

use of org.drools.verifier.data.VerifierReport in project drools by kiegroup.

the class VerifierTestStandalone method main.

public static final void main(String[] args) {
    try {
        Collection<String> fileNames = new ArrayList<String>();
        // Test data
        // fileNames.add("MissingRangesForDates.drl");
        // fileNames.add("MissingRangesForDoubles.drl");
        // fileNames.add("MissingRangesForInts.drl");
        // fileNames.add("MissingRangesForVariables.drl");
        // fileNames.add("Misc.drl");
        // fileNames.add("Misc2.drl");
        // fileNames.add("Misc3.drl");
        fileNames.add("Enums.drl");
        // fileNames.add("ConsequenceTest.drl");
        // fileNames.add("optimisation/OptimisationRestrictionOrderTest.drl");
        // fileNames.add("optimisation/OptimisationPatternOrderTest.drl");
        Verifier verifier = VerifierBuilderFactory.newVerifierBuilder().newVerifier();
        for (String s : fileNames) {
            verifier.addResourcesToVerify(ResourceFactory.newClassPathResource(s, Verifier.class), ResourceType.DRL);
        }
        verifier.fireAnalysis();
        // System.out.print(a.getResultAsPlainText());
        // System.out.print(a.getResultAsXML());
        // a.writeComponentsHTML("/stash/");
        // a.writeComponentsHTML("/Users/michaelneale/foo.html");
        // a.writeComponentsHTML("/home/trikkola/");
        // a.writeComponentsHTML("c:/");
        VerifierReport result = verifier.getResult();
        VerifierReportWriter reportwriter = VerifierReportWriterFactory.newHTMLReportWriter();
        FileOutputStream out = new FileOutputStream("/Users/rikkola/Desktop/testReport.zip");
        reportwriter.writeReport(out, result);
        Collection<VerifierMessageBase> msgs = result.getBySeverity(Severity.ERROR);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("ERR: " + msg.getMessage());
        }
        msgs = result.getBySeverity(Severity.WARNING);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("WARN (" + msg.getClass().getSimpleName() + "): " + msg.getMessage());
            System.out.println("\t FAULT: [" + msg.getClass().getSimpleName() + "] " + msg.getFaulty());
            if (msg instanceof VerifierMessage) {
                System.out.println("\t CAUSES (message):");
                VerifierMessage amsg = (VerifierMessage) msg;
                for (Iterator iterator2 = amsg.getCauses().iterator(); iterator2.hasNext(); ) {
                    Cause c = (Cause) iterator2.next();
                    System.out.println("\t\t [" + c.getClass().getSimpleName() + "]" + c);
                }
            } else if (msg instanceof VerifierRangeCheckMessage) {
                System.out.println("\t CAUSES (range):");
                VerifierRangeCheckMessage amsg = (VerifierRangeCheckMessage) msg;
                for (Iterator iterator2 = amsg.getCauses().iterator(); iterator2.hasNext(); ) {
                    Cause c = (Cause) iterator2.next();
                    System.out.println("\t\t" + c);
                }
            }
        }
        msgs = result.getBySeverity(Severity.NOTE);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("NOTE: " + msg.getMessage());
            System.out.println("\t" + msg.getFaulty());
        }
        Collection<ObjectType> classes = result.getVerifierData().getAll(VerifierComponentType.OBJECT_TYPE);
        for (Iterator iterator = classes.iterator(); iterator.hasNext(); ) {
            ObjectType c = (ObjectType) iterator.next();
            Collection<VerifierRule> cr = result.getVerifierData().getRulesByObjectTypePath(c.getPath());
            System.err.println("Class rules:" + cr);
            Set<Field> flds = c.getFields();
            for (Iterator iterator2 = flds.iterator(); iterator2.hasNext(); ) {
                Field f = (Field) iterator2.next();
                cr = result.getVerifierData().getRulesByFieldPath(f.getPath());
                System.err.println("Field rules: " + cr);
            }
        }
    // System.err.println(a.getResultAsPlainText());
    // System.out.println(result.toString());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : VerifierMessage(org.drools.verifier.report.components.VerifierMessage) VerifierRangeCheckMessage(org.drools.verifier.report.components.VerifierRangeCheckMessage) ArrayList(java.util.ArrayList) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VerifierRule(org.drools.verifier.components.VerifierRule) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) VerifierReport(org.drools.verifier.data.VerifierReport) FileOutputStream(java.io.FileOutputStream) Cause(org.drools.verifier.report.components.Cause) VerifierReportWriter(org.drools.verifier.report.VerifierReportWriter) Iterator(java.util.Iterator)

Example 20 with VerifierReport

use of org.drools.verifier.data.VerifierReport in project drools by kiegroup.

the class AlwaysFalseTest method testPatterns.

@Test
public void testPatterns() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<Object> data = new ArrayList<Object>();
    session.setGlobal("result", result);
    // This pattern has an error.
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
    Restriction r1 = LiteralRestriction.createRestriction(pattern1, "");
    Restriction r2 = LiteralRestriction.createRestriction(pattern1, "");
    Incompatibility i1 = new Incompatibility(r1, r2);
    SubPattern pp1 = new SubPattern(pattern1, 0);
    pp1.add(r1);
    pp1.add(r2);
    Restriction r3 = new VariableRestriction(pattern1);
    Restriction r4 = new VariableRestriction(pattern1);
    Incompatibility i2 = new Incompatibility(r1, r2);
    SubPattern pp2 = new SubPattern(pattern1, 1);
    pp2.add(r1);
    pp2.add(r2);
    // This pattern does not have an error.
    Pattern pattern2 = VerifierComponentMockFactory.createPattern2();
    Restriction r5 = LiteralRestriction.createRestriction(pattern2, "");
    Restriction r6 = LiteralRestriction.createRestriction(pattern2, "");
    SubPattern pp3 = new SubPattern(pattern2, 0);
    pp3.add(r5);
    pp3.add(r6);
    Restriction r7 = new VariableRestriction(pattern2);
    Restriction r8 = new VariableRestriction(pattern2);
    Incompatibility i4 = new Incompatibility(r7, r8);
    SubPattern pp4 = new SubPattern(pattern2, 1);
    pp4.add(r7);
    pp4.add(r8);
    data.add(VerifierComponentMockFactory.createRule1());
    data.add(pattern1);
    data.add(r1);
    data.add(r2);
    data.add(r3);
    data.add(r4);
    data.add(i1);
    data.add(i2);
    data.add(pp1);
    data.add(pp2);
    data.add(pattern2);
    data.add(r5);
    data.add(r6);
    data.add(r7);
    data.add(r8);
    data.add(i4);
    data.add(pp3);
    data.add(pp4);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Pattern that is always false"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.ERROR).iterator();
    boolean works = false;
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            VerifierMessage message = (VerifierMessage) o;
            if (message.getFaulty().equals(pattern1)) {
                works = true;
            } else {
                fail("There can be only one. (And this is not the one)");
            }
        }
    }
    assertEquals(1, result.getBySeverity(Severity.ERROR).size());
    assertEquals(0, result.getBySeverity(Severity.WARNING).size());
    assertEquals(0, result.getBySeverity(Severity.NOTE).size());
    assertTrue(works);
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) ArrayList(java.util.ArrayList) VerifierReport(org.drools.verifier.data.VerifierReport) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

VerifierReport (org.drools.verifier.data.VerifierReport)60 Test (org.junit.Test)59 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)45 KieSession (org.kie.api.runtime.KieSession)45 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)32 HashSet (java.util.HashSet)29 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)26 ArrayList (java.util.ArrayList)14 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)13 Gap (org.drools.verifier.report.components.Gap)12 VerifierRule (org.drools.verifier.components.VerifierRule)11 ClassPathResource (org.drools.core.io.impl.ClassPathResource)10 Pattern (org.drools.verifier.components.Pattern)8 Cause (org.drools.verifier.report.components.Cause)7 Verifier (org.drools.verifier.Verifier)4 ScopesAgendaFilter (org.drools.verifier.builder.ScopesAgendaFilter)4 Ignore (org.junit.Ignore)4 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)4 Field (org.drools.verifier.components.Field)3 ObjectType (org.drools.verifier.components.ObjectType)3