diff --git a/ruleapi/tests/retract_1_test.go b/ruleapi/tests/retract_1_test.go index 29b8b52..43d3f61 100644 --- a/ruleapi/tests/retract_1_test.go +++ b/ruleapi/tests/retract_1_test.go @@ -2,9 +2,10 @@ package tests import ( "context" + "testing" + "github.com/project-flogo/rules/common/model" "github.com/project-flogo/rules/ruleapi" - "testing" ) //Retract @@ -19,7 +20,9 @@ func Test_Retract_1(t *testing.T) { t.Logf("%s", err) t.FailNow() } - rule.SetAction(assert_action) + ruleActionCtx := make(map[string]string) + rule.SetContext(ruleActionCtx) + rule.SetAction(assertAction) rule.SetPriority(1) err = rs.AddRule(rule) if err != nil { @@ -34,7 +37,7 @@ func Test_Retract_1(t *testing.T) { t.FailNow() } - //assert a t1 + // Case1: assert a t1 { ctx := context.WithValue(context.TODO(), "key", t) tuple, _ := model.NewTupleWithKeyValues("t1", "t1") @@ -44,7 +47,8 @@ func Test_Retract_1(t *testing.T) { t.FailNow() } } - //assert a t3 so that the rule fires for keys t1 and t3 + + // Case2: assert a t3 so that the rule fires for keys t1 and t3 { ctx := context.WithValue(context.TODO(), "key", t) tuple, _ := model.NewTupleWithKeyValues("t3", "t3") @@ -53,18 +57,27 @@ func Test_Retract_1(t *testing.T) { t.Logf("%s", err) t.FailNow() } + // make sure that rule action got fired by inspecting the rule context + isActionFired, ok := ruleActionCtx["isActionFired"] + if !ok || isActionFired != "Fired" { + t.Log("Case2: rule action not fired") + t.FailNow() + } + delete(ruleActionCtx, "isActionFired") // clear the context for next test case } - //now retract t3 + + // Case3: now retract t3 { ctx := context.WithValue(context.TODO(), "key", t) tuple, _ := model.NewTupleWithKeyValues("t3", "t3") rs.Retract(ctx, tuple) } + /** + Case4: now assert with same key again, see that the test does not fail, and rule fires for keys t1 and t3 - */ - + */ { ctx := context.WithValue(context.TODO(), "key", t) tuple, _ := model.NewTupleWithKeyValues("t3", "t3") @@ -73,11 +86,20 @@ func Test_Retract_1(t *testing.T) { t.Logf("%s", err) t.FailNow() } + // make sure that rule action got fired by inspecting the rule context + isActionFired, ok := ruleActionCtx["isActionFired"] + if !ok || isActionFired != "Fired" { + t.Log("Case4: rule action not fired") + t.FailNow() + } + delete(ruleActionCtx, "isActionFired") // clear the context for next test case } + /** + Case5: now retract t3 again, just to check if a subsequent t1 does not fire the rule there by proving that t3 has been retracted - */ + */ { ctx := context.WithValue(context.TODO(), "key", t) tuple, _ := model.NewTupleWithKeyValues("t3", "t3") @@ -85,9 +107,10 @@ func Test_Retract_1(t *testing.T) { } /** + Case6: now assert another t1 with a different key and observe that rule does not fire for keys t3 and t11 (since t3 has been retracted) - */ + */ { ctx := context.WithValue(context.TODO(), "key", t) tuple, _ := model.NewTupleWithKeyValues("t1", "t11") @@ -96,14 +119,24 @@ func Test_Retract_1(t *testing.T) { t.Logf("%s", err) t.FailNow() } + // make sure that rule action doesn't fire by inspecting the rule context + _, ok := ruleActionCtx["isActionFired"] + if ok { + t.Log("Case6: rule action should not fire") + t.FailNow() + } } rs.Unregister() } -func assert_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) { +func assertAction(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) { t := ctx.Value("key").(*testing.T) t1 := tuples["t1"] t3 := tuples["t3"] t.Logf("Rule fired.. [%s], [%s]\n", t1.GetKey().String(), t3.GetKey().String()) -} \ No newline at end of file + + // add isActionFired to rule context + usableCtx := ruleCtx.(map[string]string) + usableCtx["isActionFired"] = "Fired" +}