Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Informer : test case

  • Loading branch information...
commit e933926ec9ac487bce2b5d241c8ac9d74530eec9 1 parent cbccd77
Davide Sottara authored August 07, 2012
97  drools-informer/drools-informer-core/src/test/java/org/drools/informer/presentation/GUIAdapterTest.java
... ...
@@ -0,0 +1,97 @@
  1
+package org.drools.informer.presentation;
  2
+
  3
+import org.drools.KnowledgeBase;
  4
+import org.drools.KnowledgeBaseFactory;
  5
+import org.drools.builder.KnowledgeBuilder;
  6
+import org.drools.builder.KnowledgeBuilderFactory;
  7
+import org.drools.builder.ResourceType;
  8
+import org.drools.informer.MultipleChoiceQuestion;
  9
+import org.drools.informer.Note;
  10
+import org.drools.io.ResourceFactory;
  11
+import org.drools.runtime.StatefulKnowledgeSession;
  12
+import org.drools.runtime.rule.ConsequenceException;
  13
+import org.drools.runtime.rule.QueryResults;
  14
+import org.drools.runtime.rule.Variable;
  15
+import org.junit.Before;
  16
+import org.junit.Test;
  17
+import org.slf4j.Logger;
  18
+import org.slf4j.LoggerFactory;
  19
+
  20
+import java.io.PrintStream;
  21
+import java.util.Arrays;
  22
+
  23
+import static junit.framework.Assert.assertEquals;
  24
+import static junit.framework.Assert.assertTrue;
  25
+import static org.junit.Assert.assertFalse;
  26
+
  27
+
  28
+public class GUIAdapterTest {
  29
+
  30
+
  31
+    private KnowledgeBase knowledgeBase;
  32
+
  33
+    private static final Logger logger = LoggerFactory.getLogger( GUIAdapterTest.class );
  34
+
  35
+    @Before
  36
+    public void setUp() throws Exception {
  37
+        KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
  38
+        knowledgeBuilder.add(ResourceFactory.newClassPathResource( "org/drools/informer/informer-changeset.xml" ), ResourceType.CHANGE_SET);
  39
+        knowledgeBuilder.add(ResourceFactory.newClassPathResource( "org/drools/informer/dynamicMCQ_test.drl" ), ResourceType.DRL);
  40
+        if (knowledgeBuilder.hasErrors()) {
  41
+            System.out.println( knowledgeBuilder.getErrors().toString() );
  42
+            logger.debug( Arrays.toString( knowledgeBuilder.getErrors().toArray() ) );
  43
+        }
  44
+        assertFalse(knowledgeBuilder.hasErrors());
  45
+        knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
  46
+        knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
  47
+    }
  48
+
  49
+
  50
+    @Test
  51
+    public void testUpdateMCQ() {
  52
+        MultipleChoiceQuestion mcq;
  53
+        StatefulKnowledgeSession knowledgeSession = knowledgeBase.newStatefulKnowledgeSession();
  54
+
  55
+        knowledgeSession.fireAllRules();
  56
+
  57
+        mcq = getMCQ( knowledgeSession, "kwestion", "id1" );
  58
+        assertEquals( 1, mcq.getNumOfPossibleAnswers() );
  59
+        assertEquals( "OldValue", mcq.getPossibleAnswers()[0].getValue() );
  60
+
  61
+
  62
+        knowledgeSession.insert( "change" );
  63
+        knowledgeSession.fireAllRules();
  64
+
  65
+        mcq = getMCQ( knowledgeSession, "kwestion", "id1" );
  66
+        assertEquals( 2, mcq.getNumOfPossibleAnswers() );
  67
+        assertEquals( "NewValue1", mcq.getPossibleAnswers()[0].getValue() );
  68
+        assertEquals( "NewValue2", mcq.getPossibleAnswers()[1].getValue() );
  69
+
  70
+
  71
+        report(knowledgeSession, System.err);
  72
+        
  73
+        knowledgeSession.dispose();
  74
+
  75
+    }
  76
+    
  77
+    private MultipleChoiceQuestion getMCQ( StatefulKnowledgeSession knowledgeSession, String qid, String sid ) {
  78
+        QueryResults qr = knowledgeSession.getQueryResults( "getItem", qid, sid, Variable.v );
  79
+        assertEquals( 1, qr.size() );
  80
+        Object x = qr.iterator().next().get( "$item" );
  81
+        assertTrue( x instanceof MultipleChoiceQuestion );
  82
+
  83
+        return (MultipleChoiceQuestion) x;
  84
+    }
  85
+
  86
+    private void report( StatefulKnowledgeSession knowledgeSession, PrintStream out ) {
  87
+        out.println( "---------------------------------------------------" );
  88
+        out.println( knowledgeSession.getObjects().size() );
  89
+        out.println( "---------------------------------------------------" );
  90
+        for ( Object o : knowledgeSession.getObjects() ) {
  91
+            out.println( "\t" + o );
  92
+        }
  93
+        out.println( "---------------------------------------------------" );
  94
+    }
  95
+
  96
+
  97
+}
5  drools-informer/drools-informer-core/src/test/java/org/drools/informer/rules/ItemRulesTest.java
@@ -35,10 +35,7 @@
35 35
 import org.slf4j.Logger;
36 36
 import org.slf4j.LoggerFactory;
37 37
 
38  
-/**
39  
- * @author Damon
40  
- * 
41  
- */
  38
+
42 39
 public class ItemRulesTest {
43 40
 
44 41
 	private static final Logger logger = LoggerFactory.getLogger(ItemRulesTest.class);
2  drools-informer/drools-informer-core/src/test/java/org/drools/informer/rules/QuestionRulesTest.java
@@ -26,8 +26,10 @@
26 26
 import org.drools.KnowledgeBase;
27 27
 import org.drools.KnowledgeBaseFactory;
28 28
 import org.drools.builder.KnowledgeBuilder;
  29
+import org.drools.builder.KnowledgeBuilderConfiguration;
29 30
 import org.drools.builder.KnowledgeBuilderFactory;
30 31
 import org.drools.builder.ResourceType;
  32
+import org.drools.builder.conf.PropertySpecificOption;
31 33
 import org.drools.informer.*;
32 34
 import org.drools.io.ResourceFactory;
33 35
 import org.drools.runtime.StatefulKnowledgeSession;
43  drools-informer/drools-informer-core/src/test/resources/org/drools/informer/dynamicMCQ_test.drl
... ...
@@ -0,0 +1,43 @@
  1
+package org.drools.informer.presentation.test;
  2
+
  3
+import org.drools.informer.*;
  4
+
  5
+import org.drools.informer.generator.Surveyable;
  6
+import org.drools.informer.xml.ChangeCollector;
  7
+import org.drools.informer.generator.annotations.*;
  8
+
  9
+
  10
+
  11
+declare Surveyable end
  12
+
  13
+declare QuestBean extends Surveyable
  14
+    @Questionable( label = "Manage Task Transition", language = "EN" )
  15
+
  16
+    questionnaireId     : String  @key
  17
+
  18
+    kwestion            : String
  19
+        @QuestionMark( type = Question.QuestionType.TYPE_TEXT, label = " Please answer ", required = false )
  20
+        @AllowedAnswers( values = { "OldValue" } )
  21
+end
  22
+
  23
+
  24
+rule "Init"
  25
+when
  26
+then
  27
+    insert( new QuestBean( "id1" ) );
  28
+end
  29
+
  30
+rule "Change"
  31
+when
  32
+    String( this == "change" )
  33
+    ?getItem( "kwestion", "id1", $mcq ; )
  34
+then
  35
+    insert( new ChangeCollector() );
  36
+    MultipleChoiceQuestion mcq = (MultipleChoiceQuestion) $mcq;
  37
+
  38
+    modify ( mcq ) {
  39
+        setPossibleAnswersByValue( java.util.Arrays.asList( "NewValue1", "NewValue2" ) );
  40
+    }
  41
+end
  42
+
  43
+

0 notes on commit e933926

Please sign in to comment.
Something went wrong with that request. Please try again.