-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleEvaluatorTest.java
227 lines (176 loc) · 8.71 KB
/
RuleEvaluatorTest.java
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package cloud.eppo.android;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Assert.*;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import cloud.eppo.android.dto.EppoValue;
import cloud.eppo.android.dto.OperatorType;
import cloud.eppo.android.dto.SubjectAttributes;
import cloud.eppo.android.dto.TargetingCondition;
import cloud.eppo.android.dto.TargetingRule;
public class RuleEvaluatorTest {
public TargetingRule createRule(List<TargetingCondition> conditions) {
final TargetingRule targetingRule = new TargetingRule();
targetingRule.setConditions(conditions);
return targetingRule;
}
public void addConditionToRule(TargetingRule TargetingRule, TargetingCondition condition) {
TargetingRule.getConditions().add(condition);
}
public void addNumericConditionToRule(TargetingRule TargetingRule) {
TargetingCondition condition1 = new TargetingCondition();
condition1.setValue(EppoValue.valueOf(10));
condition1.setAttribute("price");
condition1.setOperator(OperatorType.GreaterThanEqualTo);
TargetingCondition condition2 = new TargetingCondition();
condition2.setValue(EppoValue.valueOf(20));
condition2.setAttribute("price");
condition2.setOperator(OperatorType.LessThanEqualTo);
addConditionToRule(TargetingRule, condition1);
addConditionToRule(TargetingRule, condition2);
}
public void addSemVerConditionToRule(TargetingRule TargetingRule) {
TargetingCondition condition1 = new TargetingCondition();
condition1.setValue(EppoValue.valueOf("1.5.0"));
condition1.setAttribute("appVersion");
condition1.setOperator(OperatorType.GreaterThanEqualTo);
TargetingCondition condition2 = new TargetingCondition();
condition2.setValue(EppoValue.valueOf("2.2.0"));
condition2.setAttribute("appVersion");
condition2.setOperator(OperatorType.LessThan);
addConditionToRule(TargetingRule, condition1);
addConditionToRule(TargetingRule, condition2);
}
public void addRegexConditionToRule(TargetingRule TargetingRule) {
TargetingCondition condition = new TargetingCondition();
condition.setValue(EppoValue.valueOf("[a-z]+"));
condition.setAttribute("match");
condition.setOperator(OperatorType.Matches);
addConditionToRule(TargetingRule, condition);
}
public void addOneOfTargetingCondition(TargetingRule TargetingRule) {
TargetingCondition condition = new TargetingCondition();
List<String> values = new ArrayList<>();
values.add("value1");
values.add("value2");
condition.setValue(EppoValue.valueOf(values));
condition.setAttribute("oneOf");
condition.setOperator(OperatorType.OneOf);
addConditionToRule(TargetingRule, condition);
}
public void addNotOneOfTargetingCondition(TargetingRule TargetingRule) {
TargetingCondition condition = new TargetingCondition();
List<String> values = new ArrayList<>();
values.add("value1");
values.add("value2");
condition.setValue(EppoValue.valueOf(values));
condition.setAttribute("oneOf");
condition.setOperator(OperatorType.NotOneOf);
addConditionToRule(TargetingRule, condition);
}
public void addNameToSubjectAttribute(SubjectAttributes subjectAttributes) {
subjectAttributes.put("name", "test");
}
public void addPriceToSubjectAttribute(SubjectAttributes subjectAttributes) {
subjectAttributes.put("price", "30");
}
@Test
public void testMatchesAnyRuleWithEmptyConditions() {
List<TargetingRule> targetingRules = new ArrayList<>();
final TargetingRule targetingRuleWithEmptyConditions = createRule(new ArrayList<>());
targetingRules.add(targetingRuleWithEmptyConditions);
SubjectAttributes subjectAttributes = new SubjectAttributes();
addNameToSubjectAttribute(subjectAttributes);
assertEquals(targetingRuleWithEmptyConditions, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWithEmptyRules() {
List<TargetingRule> targetingRules = new ArrayList<>();
SubjectAttributes subjectAttributes = new SubjectAttributes();
addNameToSubjectAttribute(subjectAttributes);
assertNull(RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWhenNoRuleMatches() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addNumericConditionToRule(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
addPriceToSubjectAttribute(subjectAttributes);
assertNull(RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWhenRuleMatches() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addNumericConditionToRule(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("price", 15);
assertEquals(targetingRule, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWhenRuleMatchesWithSemVer() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addSemVerConditionToRule(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("appVersion", "1.15.5");
assertEquals(targetingRule, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWhenThrowInvalidSubjectAttribute() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addNumericConditionToRule(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("price", EppoValue.valueOf("abcd"));
assertNull(RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWithRegexCondition() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addRegexConditionToRule(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("match", EppoValue.valueOf("abcd"));
assertEquals(targetingRule, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWithRegexConditionNotMatched() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addRegexConditionToRule(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("match", EppoValue.valueOf("123"));
assertNull(RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWithNotOneOfRule() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addNotOneOfTargetingCondition(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("oneOf", EppoValue.valueOf("value3"));
assertEquals(targetingRule, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
@Test
public void testMatchesAnyRuleWithNotOneOfRuleNotPassed() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addNotOneOfTargetingCondition(targetingRule);
targetingRules.add(targetingRule);
SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("oneOf", EppoValue.valueOf("value1"));
assertNull(RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}
}