Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public class RuleBasedSegmentMatcher implements Matcher {
private final String standardType = "standard";
private final String ruleBasedType = "rule-based";

private final String _segmentName;

Expand All @@ -44,8 +45,20 @@ public boolean match(Object matchValue, String bucketingKey, Map<String, Object>
if (segment.type.equals(standardType) && evaluationContext.getSegmentCache().isInSegment(segment.name, (String) matchValue)) {
return false;
}

if (segment.type.equals(ruleBasedType)) {
List<ParsedCondition> conditions = evaluationContext.getRuleBasedSegmentCache().get(segment.name).parsedConditions();
if (matchConditions(conditions, matchValue, bucketingKey, attributes, evaluationContext)) {
return true;
}
}
}
List<ParsedCondition> conditions = parsedRuleBasedSegment.parsedConditions();

return matchConditions(parsedRuleBasedSegment.parsedConditions(), matchValue, bucketingKey, attributes, evaluationContext);
}

private boolean matchConditions(List<ParsedCondition> conditions, Object matchValue, String bucketingKey,
Map<String, Object> attributes, EvaluationContext evaluationContext) {
for (ParsedCondition parsedCondition : conditions) {
if (parsedCondition.matcher().match((String) matchValue, bucketingKey, attributes, evaluationContext)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@
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;

Expand Down Expand Up @@ -47,7 +58,29 @@ public void works() {

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));
}
}
61 changes: 61 additions & 0 deletions client/src/test/resources/rule_base_segments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{"ff": {"d": [], "t": -1, "s": -1},
"rbs": {"t": -1, "s": -1, "d":
[{
"changeNumber": 5,
"name": "sample_rule_based_segment",
"status": "ACTIVE",
"trafficTypeName": "user",
"excluded":{"keys":["mauro@split.io","gaston@split.io"],"segments":[]},
"conditions": [
{
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": {
"trafficType": "user",
"attribute": "email"
},
"matcherType": "ENDS_WITH",
"negate": false,
"whitelistMatcherData": {
"whitelist": [
"@split.io"
]
}
}
]
}
}
]},
{
"changeNumber": 5,
"name": "dependent_rbs",
"status": "ACTIVE",
"trafficTypeName": "user",
"excluded": {
"keys": [],
"segments": []
},
"conditions": [
{
"conditionType": "ROLLOUT",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": {
"trafficType": "user"
},
"matcherType": "IN_RULE_BASED_SEGMENT",
"negate": false,
"userDefinedSegmentMatcherData": {
"segmentName": "sample_rule_based_segment"
}
}
]
}
}
]
}]
}}