forked from rulego/rulego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_test.go
189 lines (178 loc) · 4.98 KB
/
engine_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright 2023 The RuleGo Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rulego
/*
* Copyright 2023 The RuleGo Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"github.com/rulego/rulego/api/types"
"github.com/rulego/rulego/test/assert"
"strings"
"testing"
)
var rootRuleChain = `
{
"ruleChain": {
"name": "测试规则链",
"root": true,
"debugMode": false
},
"metadata": {
"nodes": [
{
"Id":"s1",
"type": "jsFilter",
"name": "过滤",
"debugMode": true,
"configuration": {
"jsScript": "return msg!='aa';"
}
},
{
"Id":"s2",
"type": "jsTransform",
"name": "转换",
"debugMode": true,
"configuration": {
"jsScript": "msgType='TEST_MSG_TYPE2';var msg2={};\n msg2['aa']=66\n return {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
}
],
"connections": [
{
"fromId": "s1",
"toId": "s2",
"type": "True"
}
],
"ruleChainConnections": [
{
"fromId": "s1",
"toRuleChainId": "subChain01",
"type": "True"
}
]
}
}
`
var subRuleChain = `
{
"ruleChain": {
"name": "测试子规则链",
"debugMode": false
},
"metadata": {
"nodes": [
{
"Id":"s1",
"type": "jsFilter",
"name": "过滤",
"debugMode": true,
"configuration": {
"jsScript": "return msg!='aa';"
}
},
{"Id":"s2",
"type": "jsTransform",
"name": "转换",
"debugMode": true,
"configuration": {
"jsScript": "msgType='TEST_MSG_TYPE2';var msg2={};\n msg2['aa']=66\n return {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
}
],
"connections": [
{
"fromId": "s1",
"toId": "s2",
"type": "True"
}
],
"ruleChainConnections": null
}
}
`
var s1NodeFile = `
{
"Id":"s1",
"type": "jsFilter",
"name": "过滤-更改",
"debugMode": true,
"configuration": {
"jsScript": "return msg!='bb';"
}
}
`
//TestEngine 测试规则引擎
func TestEngine(t *testing.T) {
config := NewConfig()
ruleEngine, err := New("rule01", []byte(rootRuleChain), WithConfig(config), WithAddSubChain("subChain01", []byte(subRuleChain)))
if err != nil {
t.Errorf("%v", err)
}
assert.True(t, ruleEngine.Initialized())
//获取节点
s1NodeId := types.RuleNodeId{Id: "s1"}
s1Node, ok := ruleEngine.rootRuleChainCtx.nodes[s1NodeId]
assert.True(t, ok)
s1RuleNodeCtx, ok := s1Node.(*RuleNodeCtx)
assert.True(t, ok)
assert.Equal(t, "过滤", s1RuleNodeCtx.SelfDefinition.Name)
assert.Equal(t, "return msg!='aa';", s1RuleNodeCtx.SelfDefinition.Configuration["jsScript"])
//获取子规则链
subChain01Id := types.RuleNodeId{Id: "subChain01", Type: types.CHAIN}
subChain01Node, ok := ruleEngine.rootRuleChainCtx.nodes[subChain01Id]
assert.True(t, ok)
subChain01NodeCtx, ok := subChain01Node.(*RuleChainCtx)
assert.True(t, ok)
assert.Equal(t, "测试子规则链", subChain01NodeCtx.SelfDefinition.RuleChain.Name)
//修改根规则链节点
ruleEngine.ReloadChild(types.EmptyRuleNodeId, s1NodeId, []byte(s1NodeFile))
s1Node, ok = ruleEngine.rootRuleChainCtx.nodes[s1NodeId]
assert.True(t, ok)
s1RuleNodeCtx, ok = s1Node.(*RuleNodeCtx)
assert.True(t, ok)
assert.Equal(t, "过滤-更改", s1RuleNodeCtx.SelfDefinition.Name)
assert.Equal(t, "return msg!='bb';", s1RuleNodeCtx.SelfDefinition.Configuration["jsScript"])
//修改子规则链
ruleEngine.ReloadChild(types.EmptyRuleNodeId, subChain01Id, []byte(strings.Replace(subRuleChain, "测试子规则链", "测试子规则链-更改", -1)))
subChain01Node, ok = ruleEngine.rootRuleChainCtx.nodes[subChain01Id]
assert.True(t, ok)
subChain01NodeCtx, ok = subChain01Node.(*RuleChainCtx)
assert.True(t, ok)
assert.Equal(t, "测试子规则链-更改", subChain01NodeCtx.SelfDefinition.RuleChain.Name)
//获取规则引擎实例
ruleEngineNew, ok := Get("rule01")
assert.True(t, ok)
assert.Equal(t, ruleEngine, ruleEngineNew)
//删除对应规则引擎实例
Del("rule01")
_, ok = Get("rule01")
assert.False(t, ok)
assert.False(t, ruleEngine.Initialized())
}