-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedRulesTest.cs
195 lines (177 loc) · 8.23 KB
/
NestedRulesTest.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Newtonsoft.Json;
using RulesEngine.Models;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Xunit;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace RulesEngine.UnitTest;
[ExcludeFromCodeCoverage]
public class NestedRulesTest
{
[Theory]
[InlineData(NestedRuleExecutionMode.All)]
[InlineData(NestedRuleExecutionMode.Performance)]
public async Task NestedRulesShouldFollowExecutionMode(NestedRuleExecutionMode mode)
{
var workflow = GetWorkflow();
var reSettings = new ReSettings { NestedRuleExecutionMode = mode };
var rulesEngine = new RulesEngine(workflow, reSettings);
dynamic input1 = new ExpandoObject();
input1.trueValue = true;
List<RuleResultTree> result = await rulesEngine.ExecuteAllRulesAsync("NestedRulesTest", input1);
var andResults = result.Where(c => c.Rule.Operator == "And").ToList();
var orResults = result.Where(c => c.Rule.Operator == "Or").ToList();
Assert.All(andResults,
c => Assert.False(c.IsSuccess)
);
Assert.All(orResults,
c => Assert.True(c.IsSuccess));
if (mode == NestedRuleExecutionMode.All)
{
Assert.All(andResults,
c => Assert.Equal(c.Rule.Rules.Count(), c.ChildResults.Count()));
Assert.All(orResults,
c => Assert.Equal(c.Rule.Rules.Count(), c.ChildResults.Count()));
}
else if (mode == NestedRuleExecutionMode.Performance)
{
Assert.All(andResults,
c => {
Assert.Equal(c.IsSuccess, c.ChildResults.Last().IsSuccess);
Assert.Single(c.ChildResults.Where(d => c.IsSuccess == d.IsSuccess));
Assert.True(c.ChildResults.SkipLast(1).All(d => d.IsSuccess));
});
Assert.All(orResults,
c => {
Assert.Equal(c.IsSuccess, c.ChildResults.Last().IsSuccess);
Assert.Single(c.ChildResults.Where(d => c.IsSuccess == d.IsSuccess));
Assert.True(c.ChildResults.SkipLast(1).All(d => !d.IsSuccess));
});
}
}
[Fact]
private async Task NestedRulesWithNestedActions_ReturnsCorrectResults()
{
var workflow = GetWorkflow();
var reSettings = new ReSettings();
var rulesEngine = new RulesEngine(workflow, reSettings);
dynamic input1 = new ExpandoObject();
input1.trueValue = true;
List<RuleResultTree> result = await rulesEngine.ExecuteAllRulesAsync("NestedRulesActionsTest", input1);
Assert.False(result[0].IsSuccess);
Assert.Equal(input1.trueValue, result[0].ActionResult.Output);
Assert.All(result[0].ChildResults,
childResult => Assert.Equal(input1.trueValue, childResult.ActionResult.Output));
}
[Fact]
private async Task NestedRulesWithNestedActions_WorkflowParsedWithSystemTextJson_ReturnsCorrectResults()
{
var workflow = GetWorkflow();
var workflowStr = JsonConvert.SerializeObject(workflow);
var serializationOptions = new JsonSerializerOptions { Converters = { new JsonStringEnumConverter() } };
var workflowViaTextJson = JsonSerializer.Deserialize<Workflow[]>(workflowStr, serializationOptions);
var reSettings = new ReSettings();
var rulesEngine = new RulesEngine(workflowViaTextJson, reSettings);
dynamic input1 = new ExpandoObject();
input1.trueValue = true;
List<RuleResultTree> result = await rulesEngine.ExecuteAllRulesAsync("NestedRulesActionsTest", input1);
Assert.False(result[0].IsSuccess);
Assert.Equal(input1.trueValue, result[0].ActionResult.Output);
Assert.All(result[0].ChildResults,
childResult => Assert.Equal(input1.trueValue, childResult.ActionResult.Output));
}
private Workflow[] GetWorkflow()
{
return new[] {
new Workflow {
WorkflowName = "NestedRulesTest",
Rules =
new Rule[] {
new() {
RuleName = "AndRuleTrueFalse",
Operator = "And",
Rules =
new Rule[] {
new() { RuleName = "trueRule1", Expression = "input1.TrueValue == true" },
new() { RuleName = "falseRule1", Expression = "input1.TrueValue == false" }
}
},
new() {
RuleName = "OrRuleTrueFalse",
Operator = "Or",
Rules =
new Rule[] {
new() { RuleName = "trueRule2", Expression = "input1.TrueValue == true" },
new() { RuleName = "falseRule2", Expression = "input1.TrueValue == false" }
}
},
new() {
RuleName = "AndRuleFalseTrue",
Operator = "And",
Rules =
new Rule[] {
new() { RuleName = "trueRule3", Expression = "input1.TrueValue == false" },
new() { RuleName = "falseRule4", Expression = "input1.TrueValue == true" }
}
},
new() {
RuleName = "OrRuleFalseTrue",
Operator = "Or",
Rules =
new Rule[] {
new() { RuleName = "trueRule3", Expression = "input1.TrueValue == false" },
new() { RuleName = "falseRule4", Expression = "input1.TrueValue == true" }
}
}
}
},
new Workflow {
WorkflowName = "NestedRulesActionsTest",
Rules = new Rule[] {
new() {
RuleName = "AndRuleTrueFalse",
Operator = "And",
Rules = new Rule[] {
new() {
RuleName = "trueRule1",
Expression = "input1.TrueValue == true",
Actions =
new RuleActions {
OnSuccess = new ActionInfo {
Name = "OutputExpression",
Context =
new Dictionary<string, object> { { "Expression", "input1.TrueValue" } }
}
}
},
new() {
RuleName = "falseRule1",
Expression = "input1.TrueValue == false",
Actions = new RuleActions {
OnFailure = new ActionInfo {
Name = "OutputExpression",
Context =
new Dictionary<string, object> { { "Expression", "input1.TrueValue" } }
}
}
}
},
Actions = new RuleActions {
OnFailure = new ActionInfo {
Name = "OutputExpression",
Context = new Dictionary<string, object> { { "Expression", "input1.TrueValue" } }
}
}
}
}
}
};
}
}