@@ -2,9 +2,10 @@ package tests
2
2
3
3
import (
4
4
"context"
5
+ "testing"
6
+
5
7
"github.com/project-flogo/rules/common/model"
6
8
"github.com/project-flogo/rules/ruleapi"
7
- "testing"
8
9
)
9
10
10
11
//Retract
@@ -19,7 +20,9 @@ func Test_Retract_1(t *testing.T) {
19
20
t .Logf ("%s" , err )
20
21
t .FailNow ()
21
22
}
22
- rule .SetAction (assert_action )
23
+ ruleActionCtx := make (map [string ]string )
24
+ rule .SetContext (ruleActionCtx )
25
+ rule .SetAction (assertAction )
23
26
rule .SetPriority (1 )
24
27
err = rs .AddRule (rule )
25
28
if err != nil {
@@ -34,7 +37,7 @@ func Test_Retract_1(t *testing.T) {
34
37
t .FailNow ()
35
38
}
36
39
37
- //assert a t1
40
+ // Case1: assert a t1
38
41
{
39
42
ctx := context .WithValue (context .TODO (), "key" , t )
40
43
tuple , _ := model .NewTupleWithKeyValues ("t1" , "t1" )
@@ -44,7 +47,8 @@ func Test_Retract_1(t *testing.T) {
44
47
t .FailNow ()
45
48
}
46
49
}
47
- //assert a t3 so that the rule fires for keys t1 and t3
50
+
51
+ // Case2: assert a t3 so that the rule fires for keys t1 and t3
48
52
{
49
53
ctx := context .WithValue (context .TODO (), "key" , t )
50
54
tuple , _ := model .NewTupleWithKeyValues ("t3" , "t3" )
@@ -53,18 +57,27 @@ func Test_Retract_1(t *testing.T) {
53
57
t .Logf ("%s" , err )
54
58
t .FailNow ()
55
59
}
60
+ // make sure that rule action got fired by inspecting the rule context
61
+ isActionFired , ok := ruleActionCtx ["isActionFired" ]
62
+ if ! ok || isActionFired != "Fired" {
63
+ t .Log ("Case2: rule action not fired" )
64
+ t .FailNow ()
65
+ }
66
+ delete (ruleActionCtx , "isActionFired" ) // clear the context for next test case
56
67
}
57
- //now retract t3
68
+
69
+ // Case3: now retract t3
58
70
{
59
71
ctx := context .WithValue (context .TODO (), "key" , t )
60
72
tuple , _ := model .NewTupleWithKeyValues ("t3" , "t3" )
61
73
rs .Retract (ctx , tuple )
62
74
}
75
+
63
76
/**
77
+ Case4:
64
78
now assert with same key again, see that the test does not fail, and rule fires
65
79
for keys t1 and t3
66
- */
67
-
80
+ */
68
81
{
69
82
ctx := context .WithValue (context .TODO (), "key" , t )
70
83
tuple , _ := model .NewTupleWithKeyValues ("t3" , "t3" )
@@ -73,21 +86,31 @@ func Test_Retract_1(t *testing.T) {
73
86
t .Logf ("%s" , err )
74
87
t .FailNow ()
75
88
}
89
+ // make sure that rule action got fired by inspecting the rule context
90
+ isActionFired , ok := ruleActionCtx ["isActionFired" ]
91
+ if ! ok || isActionFired != "Fired" {
92
+ t .Log ("Case4: rule action not fired" )
93
+ t .FailNow ()
94
+ }
95
+ delete (ruleActionCtx , "isActionFired" ) // clear the context for next test case
76
96
}
97
+
77
98
/**
99
+ Case5:
78
100
now retract t3 again, just to check if a subsequent t1 does not fire the rule
79
101
there by proving that t3 has been retracted
80
- */
102
+ */
81
103
{
82
104
ctx := context .WithValue (context .TODO (), "key" , t )
83
105
tuple , _ := model .NewTupleWithKeyValues ("t3" , "t3" )
84
106
rs .Retract (ctx , tuple )
85
107
}
86
108
87
109
/**
110
+ Case6:
88
111
now assert another t1 with a different key and observe that rule does not fire
89
112
for keys t3 and t11 (since t3 has been retracted)
90
- */
113
+ */
91
114
{
92
115
ctx := context .WithValue (context .TODO (), "key" , t )
93
116
tuple , _ := model .NewTupleWithKeyValues ("t1" , "t11" )
@@ -96,14 +119,24 @@ func Test_Retract_1(t *testing.T) {
96
119
t .Logf ("%s" , err )
97
120
t .FailNow ()
98
121
}
122
+ // make sure that rule action doesn't fire by inspecting the rule context
123
+ _ , ok := ruleActionCtx ["isActionFired" ]
124
+ if ok {
125
+ t .Log ("Case6: rule action should not fire" )
126
+ t .FailNow ()
127
+ }
99
128
}
100
129
rs .Unregister ()
101
130
102
131
}
103
132
104
- func assert_action (ctx context.Context , rs model.RuleSession , ruleName string , tuples map [model.TupleType ]model.Tuple , ruleCtx model.RuleContext ) {
133
+ func assertAction (ctx context.Context , rs model.RuleSession , ruleName string , tuples map [model.TupleType ]model.Tuple , ruleCtx model.RuleContext ) {
105
134
t := ctx .Value ("key" ).(* testing.T )
106
135
t1 := tuples ["t1" ]
107
136
t3 := tuples ["t3" ]
108
137
t .Logf ("Rule fired.. [%s], [%s]\n " , t1 .GetKey ().String (), t3 .GetKey ().String ())
109
- }
138
+
139
+ // add isActionFired to rule context
140
+ usableCtx := ruleCtx .(map [string ]string )
141
+ usableCtx ["isActionFired" ] = "Fired"
142
+ }
0 commit comments