Package org.drools.ide.common.client.modeldriven.testing

Examples of org.drools.ide.common.client.modeldriven.testing.Scenario


     * Do a kind of end to end test with some real rules.
     */
    @Test
    public void testIntegrationWithSuccess() throws Exception {

        Scenario sc = new Scenario();
        FactData[] facts = new FactData[]{new FactData( "Cheese",
                                                        "c1",
                                                        ls( new FieldData( "type",
                                                                           "cheddar" ),
                                                            new FieldData( "price",
                                                                           "42" ) ),
                                                        false )

        };
        sc.getGlobals().add( new FactData( "Person",
                                           "p",
                                           new ArrayList(),
                                           false ) );
        sc.getFixtures().addAll( Arrays.asList( facts ) );

        ExecutionTrace executionTrace = new ExecutionTrace();

        sc.getRules().add( "rule1" );
        sc.getRules().add( "rule2" );
        sc.setInclusive( true );
        sc.getFixtures().add( executionTrace );

        Expectation[] assertions = new Expectation[5];

        assertions[0] = new VerifyFact( "c1",
                                        ls( new VerifyField( "type",
                                                             "cheddar",
                                                             "==" )

                                        ) );

        assertions[1] = new VerifyFact( "p",
                                        ls( new VerifyField( "name",
                                                             "rule1",
                                                             "==" ),
                                            new VerifyField( "status",
                                                             "rule2",
                                                             "==" ) )

        );

        assertions[2] = new VerifyRuleFired( "rule1",
                                             1,
                                             null );
        assertions[3] = new VerifyRuleFired( "rule2",
                                             1,
                                             null );
        assertions[4] = new VerifyRuleFired( "rule3",
                                             0,
                                             null );

        sc.getFixtures().addAll( Arrays.asList( assertions ) );

        TypeResolver resolver = new ClassTypeResolver( new HashSet<String>(),
                                                       Thread.currentThread().getContextClassLoader() );
        resolver.addImport( "org.drools.Cheese" );
        resolver.addImport( "org.drools.Person" );

        WorkingMemory wm = getWorkingMemory( "test_rules2.drl" );

        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        assertEquals( 2,
                      executionTrace.getNumberOfRulesFired().intValue() );

        assertSame( run.getScenario(),
                    sc );

        assertTrue( sc.wasSuccessful() );

        Person p = (Person) run.getGlobalData().get( "p" );
        assertEquals( "rule1",
                      p.getName() );
        assertEquals( "rule2",
                      p.getStatus() );
        assertEquals( 0,
                      p.getAge() );

        Thread.sleep( 50 );

        assertTrue( (new Date()).after( sc.getLastRunResult() ) );
        assertTrue( executionTrace.getExecutionTimeResult() != null );

        assertTrue( executionTrace.getRulesFired().length > 0 );

    }
View Full Code Here


    }

    @Test
    public void testIntegrationInfiniteLoop() throws Exception {

        Scenario sc = new Scenario();
        FactData[] facts = new FactData[]{new FactData( "Cheese",
                                                        "c1",
                                                        ls( new FieldData( "type",
                                                                           "cheddar" ),
                                                            new FieldData( "price",
                                                                           "42" ) ),
                                                        false )

        };
        sc.getGlobals().add( new FactData( "Person",
                                           "p",
                                           new ArrayList(),
                                           false ) );
        sc.getFixtures().addAll( Arrays.asList( facts ) );

        ExecutionTrace executionTrace = new ExecutionTrace();

        sc.getRules().add( "rule1" );
        sc.getRules().add( "rule2" );
        sc.setInclusive( true );
        sc.getFixtures().add( executionTrace );

        Expectation[] assertions = new Expectation[5];

        assertions[0] = new VerifyFact( "c1",
                                        ls( new VerifyField( "type",
                                                             "cheddar",
                                                             "==" )

                                        ) );

        assertions[1] = new VerifyFact( "p",
                                        ls( new VerifyField( "name",
                                                             "rule1",
                                                             "==" ),
                                            new VerifyField( "status",
                                                             "rule2",
                                                             "==" ) )

        );

        assertions[2] = new VerifyRuleFired( "rule1",
                                             1,
                                             null );
        assertions[3] = new VerifyRuleFired( "rule2",
                                             1,
                                             null );
        assertions[4] = new VerifyRuleFired( "rule3",
                                             0,
                                             null );

        sc.getFixtures().addAll( Arrays.asList( assertions ) );

        TypeResolver resolver = new ClassTypeResolver( new HashSet<String>(),
                                                       Thread.currentThread().getContextClassLoader() );
        resolver.addImport( "org.drools.Cheese" );
        resolver.addImport( "org.drools.Person" );

        WorkingMemory wm = getWorkingMemory( "test_rules_infinite_loop.drl" );

        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        assertEquals( sc.getMaxRuleFirings(),
                      executionTrace.getNumberOfRulesFired().intValue() );

    }
View Full Code Here

    }

    @Test
    public void testIntegrationWithDeclaredTypes() throws Exception {
        Scenario sc = new Scenario();
        FactData[] facts = new FactData[]{new FactData( "Coolness",
                                                        "c",
                                                        ls( new FieldData( "num",
                                                                           "42" ),
                                                            new FieldData( "name",
                                                                           "mic" ) ),
                                                        false )

        };
        sc.getFixtures().addAll( Arrays.asList( facts ) );

        ExecutionTrace executionTrace = new ExecutionTrace();

        sc.getRules().add( "rule1" );
        sc.setInclusive( true );
        sc.getFixtures().add( executionTrace );

        Expectation[] assertions = new Expectation[2];

        assertions[0] = new VerifyFact( "c",
                                        ls( new VerifyField( "num",
                                                             "42",
                                                             "==" )

                                        ) );

        assertions[1] = new VerifyRuleFired( "rule1",
                                             1,
                                             null );

        sc.getFixtures().addAll( Arrays.asList( assertions ) );

        WorkingMemory wm = getWorkingMemory( "test_rules3.drl" );
        ClassLoader cl = ((InternalRuleBase) wm.getRuleBase()).getRootClassLoader();

        HashSet<String> imports = new HashSet<String>();
        imports.add( "foo.bar.*" );

        TypeResolver resolver = new ClassTypeResolver( imports,
                                                       cl );

        Class cls = cl.loadClass( "foo.bar.Coolness" );
        assertNotNull( cls );

        ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( cl );

        //resolver will need to have generated beans in it - possibly using a composite classloader from the package,
        //including whatever CL has the generated beans...
        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        assertEquals( 1,
                      executionTrace.getNumberOfRulesFired().intValue() );

        assertSame( run.getScenario(),
                    sc );

        assertTrue( sc.wasSuccessful() );

        Thread.currentThread().setContextClassLoader( cl_ );

    }
View Full Code Here

    }

    @Test
    public void testRuleFlowGroupActivation() throws Exception {
        Scenario scenario = new Scenario();
        Fixture[] given = new Fixture[]{new FactData( "Coolness",
                                                      "c",
                                                      ls( new FieldData( "num",
                                                                         "42" ),
                                                          new FieldData( "name",
                                                                         "mic" ) ),
                                                      false )

        };
        scenario.getFixtures().addAll( Arrays.asList( given ) );

        ExecutionTrace executionTrace = new ExecutionTrace();

        scenario.getRules().add( "rule1" );
        scenario.setInclusive( true );
        scenario.getFixtures().add( executionTrace );

        Expectation[] assertions = new Expectation[2];

        assertions[0] = new VerifyFact( "c",
                                        ls( new VerifyField( "num",
                                                             "42",
                                                             "==" ) ) );

        assertions[1] = new VerifyRuleFired( "rule1",
                                             1,
                                             null );

        scenario.getFixtures().addAll( Arrays.asList( assertions ) );

        WorkingMemory workingMemory = getWorkingMemory( "rule_flow_actication.drl" );
        ClassLoader classLoader = ((InternalRuleBase) workingMemory.getRuleBase()).getRootClassLoader();

        HashSet<String> imports = new HashSet<String>();
        imports.add( "foo.bar.*" );

        TypeResolver resolver = new ClassTypeResolver( imports,
                                                       classLoader );

        Class< ? > coolnessClass = classLoader.loadClass( "foo.bar.Coolness" );
        assertNotNull( coolnessClass );

        ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( classLoader );

        //resolver will need to have generated beans in it - possibly using a composite classloader from the package,
        //including whatever CL has the generated beans...
        ScenarioRunner scenarioRunner = new ScenarioRunner( scenario,
                                                            resolver,
                                                            (InternalWorkingMemory) workingMemory );

        assertEquals( 0,
                      executionTrace.getNumberOfRulesFired().intValue() );

        assertSame( scenarioRunner.getScenario(),
                    scenario );

        assertFalse( scenario.wasSuccessful() );

        // Activate rule flow
        scenario.getFixtures().clear();
        given = new Fixture[]{new FactData( "Coolness",
                                            "c",
                                            ls( new FieldData( "num",
                                                               "42" ),
                                                new FieldData( "name",
                                                               "mic" ) ),
                                            false ), new ActivateRuleFlowGroup( "asdf" )};
        workingMemory.clearAgenda();
        scenario.getFixtures().addAll( Arrays.asList( given ) );
        scenario.getFixtures().add( executionTrace );
        workingMemory.getAgenda().getRuleFlowGroup( "asdf" ).setAutoDeactivate( false );
        scenarioRunner = new ScenarioRunner( scenario,
                                             resolver,
                                             (InternalWorkingMemory) workingMemory );

        assertEquals( 1,
                      executionTrace.getNumberOfRulesFired().intValue() );

        assertSame( scenarioRunner.getScenario(),
                    scenario );

        assertTrue( scenario.wasSuccessful() );

        Thread.currentThread().setContextClassLoader( cl_ );
    }
View Full Code Here

        Thread.currentThread().setContextClassLoader( cl_ );
    }

    @Test
    public void testIntgerationStateful() throws Exception {
        Scenario sc = new Scenario();
        sc.getFixtures().add( new FactData( "Cheese",
                                            "c1",
                                            ls( new FieldData( "price",
                                                               "1" ) ),
                                            false ) );
        ExecutionTrace ex = new ExecutionTrace();
        sc.getFixtures().add( ex );
        sc.getFixtures().add( new FactData( "Cheese",
                                            "c2",
                                            ls( new FieldData( "price",
                                                               "2" ) ),
                                            false ) );
        sc.getFixtures().add( new VerifyFact( "c1",
                                              ls( new VerifyField( "type",
                                                                   "rule1",
                                                                   "==" ) ) ) );
        ex = new ExecutionTrace();
        sc.getFixtures().add( ex );
        sc.getFixtures().add( new VerifyFact( "c1",
                                              ls( new VerifyField( "type",
                                                                   "rule2",
                                                                   "==" ) ) ) );

        TypeResolver resolver = new ClassTypeResolver( new HashSet<String>(),
                                                       Thread.currentThread().getContextClassLoader() );
        resolver.addImport( "org.drools.Cheese" );

        WorkingMemory wm = getWorkingMemory( "test_stateful.drl" );
        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        Cheese c1 = (Cheese) run.getPopulatedData().get( "c1" );
        Cheese c2 = (Cheese) run.getPopulatedData().get( "c2" );

        assertEquals( "rule2",
                      c1.getType() );
        assertEquals( "rule2",
                      c2.getType() );

        assertTrue( sc.wasSuccessful() );

    }
View Full Code Here

    }

    @Test
    public void testIntegrationWithModify() throws Exception {
        Scenario sc = new Scenario();
        sc.getFixtures().add( new FactData( "Cheese",
                                            "c1",
                                            ls( new FieldData( "price",
                                                               "1" ) ),
                                            false ) );

        sc.getFixtures().add( new ExecutionTrace() );

        sc.getFixtures().add( new VerifyFact( "c1",
                                              ls( new VerifyField( "type",
                                                                   "rule1",
                                                                   "==" ) ) ) );

        sc.getFixtures().add( new FactData( "Cheese",
                                            "c1",
                                            ls( new FieldData( "price",
                                                               "42" ) ),
                                            true ) );
        sc.getFixtures().add( new ExecutionTrace() );

        sc.getFixtures().add( new VerifyFact( "c1",
                                              ls( new VerifyField( "type",
                                                                   "rule3",
                                                                   "==" ) ) ) );

        TypeResolver resolver = new ClassTypeResolver( new HashSet<String>(),
                                                       Thread.currentThread().getContextClassLoader() );
        resolver.addImport( "org.drools.Cheese" );

        WorkingMemory wm = getWorkingMemory( "test_stateful.drl" );
        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        Cheese c1 = (Cheese) run.getPopulatedData().get( "c1" );

        assertEquals( "rule3",
                      c1.getType() );

        assertTrue( sc.wasSuccessful() );
    }
View Full Code Here

        assertTrue( sc.wasSuccessful() );
    }

    @Test
    public void testIntegrationWithRetract() throws Exception {
        Scenario sc = new Scenario();
        sc.getFixtures().add( new FactData( "Cheese",
                                            "c1",
                                            ls( new FieldData( "price",
                                                               "46" ),
                                                new FieldData( "type",
                                                               "XXX" ) ),
                                            false ) );
        sc.getFixtures().add( new FactData( "Cheese",
                                            "c2",
                                            ls( new FieldData( "price",
                                                               "42" ) ),
                                            false ) );
        sc.getFixtures().add( new ExecutionTrace() );

        sc.getFixtures().add( new VerifyFact( "c1",
                                              ls( new VerifyField( "type",
                                                                   "XXX",
                                                                   "==" ) ) ) );

        sc.getFixtures().add( new RetractFact( "c2" ) );
        sc.getFixtures().add( new ExecutionTrace() );

        sc.getFixtures().add( new VerifyFact( "c1",
                                              ls( new VerifyField( "type",
                                                                   "rule4",
                                                                   "==" ) ) ) );

        TypeResolver resolver = new ClassTypeResolver( new HashSet<String>(),
                                                       Thread.currentThread().getContextClassLoader() );
        resolver.addImport( "org.drools.Cheese" );

        WorkingMemory wm = getWorkingMemory( "test_stateful.drl" );
        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        Cheese c1 = (Cheese) run.getPopulatedData().get( "c1" );

        assertEquals( "rule4",
                      c1.getType() );
        assertFalse( run.getPopulatedData().containsKey( "c2" ) );

        assertTrue( sc.wasSuccessful() );
    }
View Full Code Here

        assertTrue( sc.wasSuccessful() );
    }

    @Test
    public void testIntegrationWithFailure() throws Exception {
        Scenario sc = new Scenario();
        Expectation[] assertions = populateScenarioForFailure( sc );

        TypeResolver resolver = new ClassTypeResolver( new HashSet<String>(),
                                                       Thread.currentThread().getContextClassLoader() );
        resolver.addImport( "org.drools.Cheese" );
        resolver.addImport( "org.drools.Person" );

        WorkingMemory wm = getWorkingMemory( "test_rules2.drl" );

        ScenarioRunner run = new ScenarioRunner( sc,
                                                 resolver,
                                                 (InternalWorkingMemory) wm );

        assertSame( run.getScenario(),
                    sc );

        assertFalse( sc.wasSuccessful() );

        VerifyFact vf = (VerifyFact) assertions[1];
        assertFalse( ((VerifyField) vf.getFieldValues().get( 0 )).getSuccessResult() );
        assertEquals( "XXX",
                      ((VerifyField) vf.getFieldValues().get( 0 )).getExpected() );
View Full Code Here

    }

    @Test
    public void testCollectionFieldInFacts() throws Exception {

        ScenarioRunner runner = new ScenarioRunner( new Scenario(),
                                                    null,
                                                    new MockWorkingMemory() );
        Cheesery listChesse = new Cheesery();
        Cheese f1 = new Cheese( "cheddar",
                                42 );
View Full Code Here

    }

    @Test
    public void testCallMethodNoArgumentOnFact() throws Exception {

        ScenarioRunner runner = new ScenarioRunner( new Scenario(),
                                                    null,
                                                    new MockWorkingMemory() );
        Cheesery listChesse = new Cheesery();
        listChesse.setTotalAmount( 1000 );
        runner.getPopulatedData().put( "cheese",
View Full Code Here

TOP

Related Classes of org.drools.ide.common.client.modeldriven.testing.Scenario

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.