Repository navigation
Expand file tree
/
Copy pathSegmentModelTest.java
More file actions
191 lines (174 loc) · 10.7 KB
/
Copy pathSegmentModelTest.java
File metadata and controls
191 lines (174 loc) · 10.7 KB
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
package com.flagsmith.flagengine.unit.segments;
import com.flagsmith.flagengine.EvaluationContext;
import com.flagsmith.flagengine.IdentityContext;
import com.flagsmith.flagengine.SegmentCondition;
import com.flagsmith.flagengine.SegmentContext;
import com.flagsmith.flagengine.SegmentRule;
import com.flagsmith.flagengine.Traits;
import com.flagsmith.flagengine.segments.SegmentEvaluator;
import com.flagsmith.flagengine.segments.constants.SegmentConditions;
import com.flagsmith.mappers.EngineMappers;
import com.flagsmith.FlagsmithTestHelper;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;
public class SegmentModelTest {
private static Stream<Arguments> conditionTestData() {
return Stream.of(
Arguments.of(SegmentConditions.EQUAL, "bar", "bar", true),
Arguments.of(SegmentConditions.EQUAL, "bar", "baz", false),
Arguments.of(SegmentConditions.EQUAL, 1, "1", true),
Arguments.of(SegmentConditions.EQUAL, 1, "2", false),
Arguments.of(SegmentConditions.EQUAL, true, "true", true),
Arguments.of(SegmentConditions.EQUAL, false, "false", true),
Arguments.of(SegmentConditions.EQUAL, false, "true", false),
Arguments.of(SegmentConditions.EQUAL, true, "false", false),
Arguments.of(SegmentConditions.EQUAL, 1.23, "1.23", true),
Arguments.of(SegmentConditions.EQUAL, 1.23, "4.56", false),
Arguments.of(SegmentConditions.GREATER_THAN, 2, "1", true),
Arguments.of(SegmentConditions.GREATER_THAN, 1, "1", false),
Arguments.of(SegmentConditions.GREATER_THAN, 0, "1", false),
Arguments.of(SegmentConditions.GREATER_THAN, 2.1, "2.0", true),
Arguments.of(SegmentConditions.GREATER_THAN, 2.1, "2.1", false),
Arguments.of(SegmentConditions.GREATER_THAN, 2.0, "2.1", false),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, 2, "1", true),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, 1, "1", true),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, 0, "1", false),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, 2.1, "2.0", true),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, 2.1, "2.1", true),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, 2.0, "2.1", false),
Arguments.of(SegmentConditions.LESS_THAN, 1, "2", true),
Arguments.of(SegmentConditions.LESS_THAN, 1, "1", false),
Arguments.of(SegmentConditions.LESS_THAN, 1, "0", false),
Arguments.of(SegmentConditions.LESS_THAN, 2.0, "2.1", true),
Arguments.of(SegmentConditions.LESS_THAN, 2.1, "2.1", false),
Arguments.of(SegmentConditions.LESS_THAN, 2.1, "2.0", false),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, 1, "2", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, 1, "1", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, 1, "0", false),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, 2.0, "2.1", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, 2.1, "2.1", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, 2.1, "2.0", false),
Arguments.of(SegmentConditions.NOT_EQUAL, "bar", "baz", true),
Arguments.of(SegmentConditions.NOT_EQUAL, "bar", "bar", false),
Arguments.of(SegmentConditions.NOT_EQUAL, 1, "2", true),
Arguments.of(SegmentConditions.NOT_EQUAL, 1, "1", false),
Arguments.of(SegmentConditions.NOT_EQUAL, true, "false", true),
Arguments.of(SegmentConditions.NOT_EQUAL, false, "true", true),
Arguments.of(SegmentConditions.NOT_EQUAL, false, "false", false),
Arguments.of(SegmentConditions.NOT_EQUAL, true, "true", false),
Arguments.of(SegmentConditions.CONTAINS, "bar", "b", true),
Arguments.of(SegmentConditions.CONTAINS, "bar", "bar", true),
Arguments.of(SegmentConditions.CONTAINS, "bar", "baz", false),
Arguments.of(SegmentConditions.CONTAINS, 1, "2", false),
Arguments.of(SegmentConditions.CONTAINS, 12, "1", true),
Arguments.of(SegmentConditions.NOT_CONTAINS, "bar", "b", false),
Arguments.of(SegmentConditions.NOT_CONTAINS, "bar", "bar", false),
Arguments.of(SegmentConditions.NOT_CONTAINS, "bar", "baz", true),
Arguments.of(SegmentConditions.NOT_CONTAINS, 1, "2", true),
Arguments.of(SegmentConditions.NOT_CONTAINS, 12, "1", false),
Arguments.of(SegmentConditions.REGEX, "foo", "[a-z]+", true),
Arguments.of(SegmentConditions.REGEX, "FOO", "[a-z]+", false),
Arguments.of(SegmentConditions.REGEX, 42, "[a-z]+", false),
Arguments.of(SegmentConditions.REGEX, 42, "\\d+", true),
Arguments.of(SegmentConditions.REGEX, "production", "prod", true),
Arguments.of(SegmentConditions.REGEX, "nonproduction", "prod", false),
Arguments.of(SegmentConditions.REGEX, "development", "prod|dev", true),
Arguments.of(SegmentConditions.REGEX, "nondev", "prod|dev", false),
Arguments.of(SegmentConditions.REGEX, "jane@example.com", "@example\\.com$", false),
Arguments.of(SegmentConditions.REGEX, "jane@example.com", "^[^@]+@example\\.com$", true),
Arguments.of(SegmentConditions.REGEX, "jane@example.com.br", "^[^@]+@example\\.com$", false),
Arguments.of(SegmentConditions.REGEX, "Production", "prod", false),
Arguments.of(SegmentConditions.REGEX, "Production", "[Pp]rod", true),
Arguments.of(SegmentConditions.REGEX, "a)(b", "a)(b", false),
Arguments.of(SegmentConditions.MODULO, 2, "2|0", true),
Arguments.of(SegmentConditions.MODULO, 3, "2|0", false),
Arguments.of(SegmentConditions.MODULO, 2.0, "2|0", true),
Arguments.of(SegmentConditions.MODULO, 2.0, "2.0|0.0", true),
Arguments.of(SegmentConditions.MODULO, "foo", "2|0", false),
Arguments.of(SegmentConditions.MODULO, "foo", "foo|bar", false),
Arguments.of(SegmentConditions.IN, "foo", "", false),
Arguments.of(SegmentConditions.IN, "foo", "foo,bar", true),
Arguments.of(SegmentConditions.IN, "bar", "foo,bar", true),
Arguments.of(SegmentConditions.IN, "ba", "foo,bar", false),
Arguments.of(SegmentConditions.IN, "foo", "foo", true),
Arguments.of(SegmentConditions.IN, 1, "1,2,3,4", true),
Arguments.of(SegmentConditions.IN, 1, "", false),
Arguments.of(SegmentConditions.IN, 1, "1", true),
Arguments.of(SegmentConditions.IN, 1, "[1]", true),
Arguments.of(SegmentConditions.IN, 1, "[\"1\"]", true),
Arguments.of(SegmentConditions.IN, "bar", "[\"bar\"]", true),
Arguments.of(SegmentConditions.IN, "bar", Arrays.asList("bar", "foo"), true),
Arguments.of(SegmentConditions.IN, 1.5, "1.5", true),
// Flagsmith's engine does not evaluate `IN` condition for booleans
// due to ambiguous serialization across supported platforms.
Arguments.of(SegmentConditions.IN, false, "false", false)
);
}
@ParameterizedTest
@MethodSource("conditionTestData")
public void testSegmentConditionMatchesTraitValue(
SegmentConditions condition,
Object traitValue,
Object conditionValue,
Boolean expectedResponse) {
// Given
final EvaluationContext context = EngineMappers.mapContextAndIdentityDataToContext(
FlagsmithTestHelper.evaluationContext(), "foo",
Collections.singletonMap("foo", traitValue));
SegmentContext segmentContext = new SegmentContext().withKey(
conditionValue.toString()).withRules(
Arrays.asList(new SegmentRule().withType(SegmentRule.Type.ALL).withConditions(
Arrays.asList(new SegmentCondition()
.withOperator(condition).withProperty("foo")
.withValue(conditionValue)))));
// When
Boolean actualResult = SegmentEvaluator.isContextInSegment(context, segmentContext);
// Then
assertEquals(expectedResponse, actualResult);
}
@ParameterizedTest
@MethodSource("semverTestData")
public void testSemverMatchesTraitValue(
SegmentConditions condition,
Object traitValue,
String conditionValue,
Boolean expectedResponse) {
final EvaluationContext context = EngineMappers.mapContextAndIdentityDataToContext(
FlagsmithTestHelper.evaluationContext(), "foo",
Collections.singletonMap("foo", traitValue));
SegmentContext segmentContext = new SegmentContext().withKey(conditionValue).withRules(
Arrays.asList(new SegmentRule().withType(SegmentRule.Type.ALL).withConditions(
Arrays.asList(new SegmentCondition()
.withOperator(condition).withProperty("foo")
.withValue(conditionValue)))));
Boolean actualResult = SegmentEvaluator.isContextInSegment(context, segmentContext);
assertEquals(expectedResponse, actualResult);
}
private static Stream<Arguments> semverTestData() {
return Stream.of(
Arguments.of(SegmentConditions.EQUAL, "1.0.0", "1.0.0:semver", true),
Arguments.of(SegmentConditions.EQUAL, "1.0.0", "1.0.1:semver", false),
Arguments.of(SegmentConditions.NOT_EQUAL, "1.0.0", "1.0.0:semver", false),
Arguments.of(SegmentConditions.NOT_EQUAL, "1.0.0", "1.0.1:semver", true),
Arguments.of(SegmentConditions.GREATER_THAN, "1.0.1", "1.0.0:semver", true),
Arguments.of(SegmentConditions.GREATER_THAN, "1.0.0", "1.0.0-beta:semver", true),
Arguments.of(SegmentConditions.GREATER_THAN, "1.0.1", "1.2.0:semver", false),
Arguments.of(SegmentConditions.GREATER_THAN, "1.0.1", "1.0.1:semver", false),
Arguments.of(SegmentConditions.GREATER_THAN, "1.2.4", "1.2.3-pre.2+build.4:semver", true),
Arguments.of(SegmentConditions.LESS_THAN, "1.0.0", "1.0.1:semver", true),
Arguments.of(SegmentConditions.LESS_THAN, "1.0.0", "1.0.0:semver", false),
Arguments.of(SegmentConditions.LESS_THAN, "1.0.1", "1.0.0:semver", false),
Arguments.of(SegmentConditions.LESS_THAN, "1.0.0-rc.2", "1.0.0-rc.3:semver", true),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", true),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, "1.0.1", "1.2.0:semver", false),
Arguments.of(SegmentConditions.GREATER_THAN_INCLUSIVE, "1.0.1", "1.0.1:semver", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.1:semver", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.0:semver", true),
Arguments.of(SegmentConditions.LESS_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", false));
}
}