Permalink
Browse files

Informer : test case

  • Loading branch information...
1 parent cbccd77 commit e933926ec9ac487bce2b5d241c8ac9d74530eec9 @sotty sotty committed Aug 8, 2012
@@ -0,0 +1,97 @@
+package org.drools.informer.presentation;
+
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderFactory;
+import org.drools.builder.ResourceType;
+import org.drools.informer.MultipleChoiceQuestion;
+import org.drools.informer.Note;
+import org.drools.io.ResourceFactory;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.rule.ConsequenceException;
+import org.drools.runtime.rule.QueryResults;
+import org.drools.runtime.rule.Variable;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.PrintStream;
+import java.util.Arrays;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+
+public class GUIAdapterTest {
+
+
+ private KnowledgeBase knowledgeBase;
+
+ private static final Logger logger = LoggerFactory.getLogger( GUIAdapterTest.class );
+
+ @Before
+ public void setUp() throws Exception {
+ KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ knowledgeBuilder.add(ResourceFactory.newClassPathResource( "org/drools/informer/informer-changeset.xml" ), ResourceType.CHANGE_SET);
+ knowledgeBuilder.add(ResourceFactory.newClassPathResource( "org/drools/informer/dynamicMCQ_test.drl" ), ResourceType.DRL);
+ if (knowledgeBuilder.hasErrors()) {
+ System.out.println( knowledgeBuilder.getErrors().toString() );
+ logger.debug( Arrays.toString( knowledgeBuilder.getErrors().toArray() ) );
+ }
+ assertFalse(knowledgeBuilder.hasErrors());
+ knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
+ knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
+ }
+
+
+ @Test
+ public void testUpdateMCQ() {
+ MultipleChoiceQuestion mcq;
+ StatefulKnowledgeSession knowledgeSession = knowledgeBase.newStatefulKnowledgeSession();
+
+ knowledgeSession.fireAllRules();
+
+ mcq = getMCQ( knowledgeSession, "kwestion", "id1" );
+ assertEquals( 1, mcq.getNumOfPossibleAnswers() );
+ assertEquals( "OldValue", mcq.getPossibleAnswers()[0].getValue() );
+
+
+ knowledgeSession.insert( "change" );
+ knowledgeSession.fireAllRules();
+
+ mcq = getMCQ( knowledgeSession, "kwestion", "id1" );
+ assertEquals( 2, mcq.getNumOfPossibleAnswers() );
+ assertEquals( "NewValue1", mcq.getPossibleAnswers()[0].getValue() );
+ assertEquals( "NewValue2", mcq.getPossibleAnswers()[1].getValue() );
+
+
+ report(knowledgeSession, System.err);
+
+ knowledgeSession.dispose();
+
+ }
+
+ private MultipleChoiceQuestion getMCQ( StatefulKnowledgeSession knowledgeSession, String qid, String sid ) {
+ QueryResults qr = knowledgeSession.getQueryResults( "getItem", qid, sid, Variable.v );
+ assertEquals( 1, qr.size() );
+ Object x = qr.iterator().next().get( "$item" );
+ assertTrue( x instanceof MultipleChoiceQuestion );
+
+ return (MultipleChoiceQuestion) x;
+ }
+
+ private void report( StatefulKnowledgeSession knowledgeSession, PrintStream out ) {
+ out.println( "---------------------------------------------------" );
+ out.println( knowledgeSession.getObjects().size() );
+ out.println( "---------------------------------------------------" );
+ for ( Object o : knowledgeSession.getObjects() ) {
+ out.println( "\t" + o );
+ }
+ out.println( "---------------------------------------------------" );
+ }
+
+
+}
@@ -35,10 +35,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/**
- * @author Damon
- *
- */
+
public class ItemRulesTest {
private static final Logger logger = LoggerFactory.getLogger(ItemRulesTest.class);
@@ -26,8 +26,10 @@
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
+import org.drools.builder.conf.PropertySpecificOption;
import org.drools.informer.*;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
@@ -0,0 +1,43 @@
+package org.drools.informer.presentation.test;
+
+import org.drools.informer.*;
+
+import org.drools.informer.generator.Surveyable;
+import org.drools.informer.xml.ChangeCollector;
+import org.drools.informer.generator.annotations.*;
+
+
+
+declare Surveyable end
+
+declare QuestBean extends Surveyable
+ @Questionable( label = "Manage Task Transition", language = "EN" )
+
+ questionnaireId : String @key
+
+ kwestion : String
+ @QuestionMark( type = Question.QuestionType.TYPE_TEXT, label = " Please answer ", required = false )
+ @AllowedAnswers( values = { "OldValue" } )
+end
+
+
+rule "Init"
+when
+then
+ insert( new QuestBean( "id1" ) );
+end
+
+rule "Change"
+when
+ String( this == "change" )
+ ?getItem( "kwestion", "id1", $mcq ; )
+then
+ insert( new ChangeCollector() );
+ MultipleChoiceQuestion mcq = (MultipleChoiceQuestion) $mcq;
+
+ modify ( mcq ) {
+ setPossibleAnswersByValue( java.util.Arrays.asList( "NewValue1", "NewValue2" ) );
+ }
+end
+
+

0 comments on commit e933926

Please sign in to comment.