-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcondition_test.go
196 lines (178 loc) · 5.54 KB
/
condition_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
190
191
192
193
194
195
196
package rulesengine
import (
"encoding/json"
"testing"
)
func TestCondition(t *testing.T) {
// Test a valid RuleConfig with a valid Condition
t.Run("TestValidRuleConfig", func(t *testing.T) {
priority := 1
ruleConfig := RuleConfig{
Name: "Test Rule",
Priority: nil, // optional priority
Conditions: Condition{
Priority: &priority,
Operator: "equal",
Fact: "factName",
Value: ValueNode{Type: String, String: "someValue"},
},
Event: EventConfig{Type: "TestEvent"},
}
if err := ruleConfig.Conditions.Validate(); err != nil {
t.Errorf("Expected RuleConfig to be valid, but got error: %v", err)
}
})
// Test that RuleConfig returns an error when Condition's priority is invalid
t.Run("TestRuleConfigInvalidPriority", func(t *testing.T) {
priority := 0
ruleConfig := RuleConfig{
Name: "Test Rule",
Conditions: Condition{
Priority: &priority,
Operator: "equal",
Fact: "factName",
Value: ValueNode{Type: String, String: "someValue"},
},
Event: EventConfig{Type: "TestEvent"},
}
err := ruleConfig.Conditions.Validate()
if err == nil || err.Error() != "priority must be greater than zero" {
t.Errorf("Expected priority validation error, but got: %v", err)
}
})
// Test that RuleConfig returns an error when Value, Fact, or Operator are missing
t.Run(" TestRuleConfigMissingValueFactOperator", func(t *testing.T) {
priority := 1
testCases := []struct {
name string
conditions Condition
errMsg string
}{
{
name: "Missing Fact",
conditions: Condition{
Priority: &priority,
Operator: "equal",
Value: ValueNode{Type: String, String: "someValue"},
Fact: "", // missing fact
},
errMsg: "if value, operator, or fact are set, all three must be provided",
},
{
name: "Missing Operator",
conditions: Condition{
Priority: &priority,
Operator: "",
Value: ValueNode{Type: String, String: "someValue"},
Fact: "factName", // missing operator
},
errMsg: "if value, operator, or fact are set, all three must be provided",
},
{
name: "Missing Value",
conditions: Condition{
Priority: &priority,
Operator: "equal",
Value: ValueNode{Type: Null}, // missing value
Fact: "factName",
},
errMsg: "if value, operator, or fact are set, all three must be provided",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ruleConfig := RuleConfig{
Name: "Test Rule",
Conditions: tc.conditions,
Event: EventConfig{Type: "TestEvent"},
}
err := ruleConfig.Conditions.Validate()
if err == nil || err.Error() != tc.errMsg {
t.Errorf("Expected error: %v, but got: %v", tc.errMsg, err)
}
})
}
})
// Test mutual exclusion of Any, All, and Not with Value, Fact, and Operator
t.Run("TestRuleConfigMutualExclusion", func(t *testing.T) {
priority := 1
ruleConfig := RuleConfig{
Name: "Test Rule",
Conditions: Condition{
Priority: &priority,
Operator: "equal",
Fact: "factName",
Value: ValueNode{Type: String, String: "someValue"},
All: []*Condition{{Priority: &priority}}, // All is set, but Value, Fact, Operator are also set
},
Event: EventConfig{Type: "TestEvent"},
}
err := ruleConfig.Conditions.Validate()
if err == nil || err.Error() != "value, operator, and fact must not be set if any, all, or not conditions are provided" {
t.Errorf("Expected mutual exclusion validation error, but got: %v", err)
}
})
// Test that Path can only be set if Value is provided
t.Run("TestRuleConfigFactRequiresValue", func(t *testing.T) {
priority := 1
ruleConfig := RuleConfig{
Name: "Test Rule",
Conditions: Condition{
Priority: &priority,
Operator: "equal",
Fact: "",
Value: ValueNode{Type: String, String: "someValue"},
},
Event: EventConfig{Type: "TestEvent"},
}
err := ruleConfig.Conditions.Validate()
if err == nil || err.Error() != "if value, operator, or fact are set, all three must be provided" {
t.Errorf("Expected path validation error, but got: %v", err)
}
})
// Test unmarshalling valid RuleConfig JSON
t.Run("TestUnmarshalValidRuleConfig", func(t *testing.T) {
jsonData := []byte(`{
"name": "Test Rule",
"conditions": {
"priority": 1,
"operator": "equal",
"fact": "factName",
"value": ""
},
"event": {
"type": "TestEvent"
}
}`)
var ruleConfig RuleConfig
err := json.Unmarshal(jsonData, &ruleConfig)
if err != nil {
t.Errorf("Expected successful unmarshal, but got error: %v", err)
}
if *ruleConfig.Conditions.Priority != 1 {
t.Errorf("Expected priority to be 1, got %d", ruleConfig.Conditions.Priority)
}
if ruleConfig.Conditions.Operator != "equal" {
t.Errorf("Expected operator to be 'equal', got %s", ruleConfig.Conditions.Operator)
}
})
// Test unmarshalling invalid RuleConfig JSON (missing fact)
t.Run("TestUnmarshalInvalidRuleConfig", func(t *testing.T) {
jsonData := []byte(`{
"name": "Test Rule",
"conditions": {
"priority": 1,
"operator": "equal",
"value": "someValue"
},
"event": {
"type": "TestEvent"
}
}`)
var ruleConfig RuleConfig
err := json.Unmarshal(jsonData, &ruleConfig)
if err == nil || err.Error() != "if value, operator, or fact are set, all three must be provided" {
t.Errorf("Expected unmarshalling error for missing fact, but got: %v", err)
}
})
}