-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRuleBasedSegmentMatcherTest.java
More file actions
86 lines (76 loc) · 4.79 KB
/
RuleBasedSegmentMatcherTest.java
File metadata and controls
86 lines (76 loc) · 4.79 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
package io.split.engine.matchers;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.split.client.dtos.ConditionType;
import io.split.client.dtos.MatcherCombiner;
import io.split.client.dtos.SplitChange;
import io.split.client.utils.Json;
import io.split.client.utils.RuleBasedSegmentsToUpdate;
import io.split.engine.evaluator.EvaluationContext;
import io.split.engine.evaluator.Evaluator;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedRuleBasedSegment;
import io.split.engine.experiments.RuleBasedSegmentParser;
import io.split.engine.matchers.strings.WhitelistMatcher;
import io.split.storages.RuleBasedSegmentCache;
import io.split.storages.RuleBasedSegmentCacheConsumer;
import io.split.storages.SegmentCache;
import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp;
import io.split.storages.memory.SegmentCacheInMemoryImpl;
import okhttp3.mockwebserver.MockResponse;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Set;
import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class RuleBasedSegmentMatcherTest {
@Test
public void works() {
Evaluator evaluator = Mockito.mock(Evaluator.class);
SegmentCache segmentCache = new SegmentCacheInMemoryImpl();
RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp();
EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCache);
AttributeMatcher whiteListMatcher = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("test_1", "admin")));
CombiningMatcher whitelistCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(whiteListMatcher));
AttributeMatcher ruleBasedSegmentMatcher = AttributeMatcher.vanilla(new RuleBasedSegmentMatcher("sample_rule_based_segment"));
CombiningMatcher ruleBasedSegmentCombinerMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(ruleBasedSegmentMatcher));
ParsedCondition ruleBasedSegmentCondition = new ParsedCondition(ConditionType.ROLLOUT, ruleBasedSegmentCombinerMatcher, null, "test rbs rule");
ParsedRuleBasedSegment parsedRuleBasedSegment = new ParsedRuleBasedSegment("sample_rule_based_segment",
Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, null, "whitelist label")),"user",
123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList());
ruleBasedSegmentCache.update(Lists.newArrayList(parsedRuleBasedSegment), null, 123);
RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("sample_rule_based_segment");
assertThat(matcher.match("mauro@test.io", null, null, evaluationContext), is(false));
assertThat(matcher.match("admin", null, null, evaluationContext), is(true));
assertThat(matcher.match("foo", null, null, evaluationContext), is(false));
assertThat(matcher.match(null, null, null, evaluationContext), is(false));
}
@Test
public void usingRbsWithinExcludedTest() throws IOException {
String load = new String(Files.readAllBytes(Paths.get("src/test/resources/rule_base_segments.json")), StandardCharsets.UTF_8);
Evaluator evaluator = Mockito.mock(Evaluator.class);
SegmentCache segmentCache = new SegmentCacheInMemoryImpl();
RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp();
EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCache);
SplitChange change = Json.fromJson(load, SplitChange.class);
RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser();
RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(ruleBasedSegmentParser,
change.ruleBasedSegments.d);
ruleBasedSegmentCache.update(ruleBasedSegmentsToUpdate.getToAdd(), null, 123);
RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("dependent_rbs");
HashMap<String, Object> attrib1 = new HashMap<String, Object>() {{
put("email", "mauro@@split.io");
}};
HashMap<String, Object> attrib2 = new HashMap<String, Object>() {{
put("email", "bilal@@split.io");
}};
assertThat(matcher.match("mauro@split.io", null, attrib1, evaluationContext), is(false));
assertThat(matcher.match("bilal@split.io", null, attrib2, evaluationContext), is(true));
}
}