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 @@ -1101,6 +1101,80 @@ public MockResponse dispatch(RecordedRequest request) {
Assert.assertTrue(check2);
}

@Test
public void getTreatmentWithPrerequisites() throws Exception {
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_prereq.json")), StandardCharsets.UTF_8);
List<RecordedRequest> allRequests = new ArrayList<>();
Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
allRequests.add(request);
switch (request.getPath()) {
case "/api/splitChanges?s=1.3&since=-1&rbSince=-1":
return new MockResponse().setResponseCode(200).setBody(splits);
case "/api/splitChanges?s=1.3&since=1585948850109&rbSince=1585948850109":
return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109},\"rbs\":{\"d\":[],\"t\":1585948850109,\"s\":1585948850109}}");
case "/api/segmentChanges/segment-test?since=-1":
return new MockResponse().setResponseCode(200).setBody("{\"name\":\"segment-test\",\"added\":[\"user-1\"],\"removed\":[],\"since\":-1,\"till\":-1}");
case "/api/testImpressions/bulk":
return new MockResponse().setResponseCode(200);
case "/api/testImpressions/count":
return new MockResponse().setResponseCode(200);
case "/v1/keys/ss":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/usage":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/config":
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
}
};

MockWebServer splitServer = new MockWebServer();
splitServer.setDispatcher(dispatcher);
splitServer.start();
String serverURL = String.format("http://%s:%s", splitServer.getHostName(), splitServer.getPort());

SplitClientConfig config = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.endpoint(serverURL, serverURL)
.telemetryURL(serverURL + "/v1")
.authServiceURL(String.format("%s/api/auth/enabled", serverURL))
.streamingEnabled(false)
.featuresRefreshRate(5)
.impressionsMode(ImpressionsManager.Mode.DEBUG)
.build();

SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config);
SplitClient client = factory.client();
client.blockUntilReady();

Assert.assertEquals("on", client.getTreatment("bilal@split.io", "test_prereq", new HashMap<String, Object>() {{
put("email", "bilal@@split.io");
}}));
Assert.assertEquals("def_treatment", client.getTreatment("bilal@split.io", "test_prereq"));
Assert.assertEquals("def_treatment", client.getTreatment("mauro@split.io", "test_prereq", new HashMap<String, Object>() {{
put("email", "mauro@@split.io");
}}));
Assert.assertEquals("on", client.getTreatment("pato@split.io", "test_prereq", new HashMap<String, Object>() {{
put("email", "pato@@split.io");
}}));

Assert.assertEquals("on_whitelist", client.getTreatment("bilal@split.io", "prereq_chain", new HashMap<String, Object>() {{
put("email", "bilal@@split.io");
}}));
Assert.assertEquals("on", client.getTreatment("pato@split.io", "prereq_chain", new HashMap<String, Object>() {{
put("email", "pato@@split.io");
}}));
Assert.assertEquals("on_default", client.getTreatment("mauro@split.io", "prereq_chain", new HashMap<String, Object>() {{
put("email", "mauro@@split.io");
}}));

client.destroy();
splitServer.shutdown();
}

private SSEMockServer buildSSEMockServer(SSEMockServer.SseEventQueue eventQueue) {
return new SSEMockServer(eventQueue, (token, version, channel) -> {
if (!"1.1".equals(version)) {
Expand Down
293 changes: 293 additions & 0 deletions client/src/test/resources/splits_prereq.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
{"ff": {
"d": [
{
"trafficTypeName": "user",
"name": "test_prereq",
"prerequisites": [
{ "n": "feature_segment", "ts": ["off", "def_test"] },
{ "n": "rbs_flag", "ts": ["on"] }
],
"trafficAllocation": 100,
"trafficAllocationSeed": 1582960494,
"seed": 1842944006,
"status": "ACTIVE",
"killed": false,
"defaultTreatment": "def_treatment",
"changeNumber": 1582741588594,
"algo": 2,
"configurations": {},
"conditions": [
{
"conditionType": "ROLLOUT",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": {
"trafficType": "user",
"attribute": null
},
"matcherType": "ALL_KEYS",
"negate": false,
"userDefinedSegmentMatcherData": null,
"whitelistMatcherData": null,
"unaryNumericMatcherData": null,
"betweenMatcherData": null,
"booleanMatcherData": null,
"dependencyMatcherData": null,
"stringMatcherData": null
}
]
},
"partitions": [
{
"treatment": "on",
"size": 100
},
{
"treatment": "off",
"size": 0
}
],
"label": "default rule"
}
]
},
{
"name":"feature_segment",
"trafficTypeId":"u",
"trafficTypeName":"User",
"trafficAllocation": 100,
"trafficAllocationSeed": 1582960494,
"seed":-1177551240,
"status":"ACTIVE",
"killed":false,
"defaultTreatment":"def_test",
"changeNumber": 1582741588594,
"algo": 2,
"configurations": {},
"conditions":[
{
"matcherGroup":{
"combiner":"AND",
"matchers":[
{
"matcherType":"IN_SEGMENT",
"negate":false,
"userDefinedSegmentMatcherData":{
"segmentName":"segment-test"
},
"whitelistMatcherData":null
}
]
},
"partitions":[
{
"treatment":"on",
"size":100
},
{
"treatment":"off",
"size":0
}
],
"label": "default label"
}
]
},
{
"changeNumber": 10,
"trafficTypeName": "user",
"name": "rbs_flag",
"trafficAllocation": 100,
"trafficAllocationSeed": 1828377380,
"seed": -286617921,
"status": "ACTIVE",
"killed": false,
"defaultTreatment": "off",
"algo": 2,
"conditions": [
{
"conditionType": "ROLLOUT",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": {
"trafficType": "user"
},
"matcherType": "IN_RULE_BASED_SEGMENT",
"negate": false,
"userDefinedSegmentMatcherData": {
"segmentName": "sample_rule_based_segment"
}
}
]
},
"partitions": [
{
"treatment": "on",
"size": 100
},
{
"treatment": "off",
"size": 0
}
],
"label": "in rule based segment sample_rule_based_segment"
},
{
"conditionType": "ROLLOUT",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": {
"trafficType": "user"
},
"matcherType": "ALL_KEYS",
"negate": false
}
]
},
"partitions": [
{
"treatment": "on",
"size": 0
},
{
"treatment": "off",
"size": 100
}
],
"label": "default rule"
}
],
"configurations": {},
"sets": [],
"impressionsDisabled": false
},
{
"trafficTypeName": "user",
"name": "prereq_chain",
"prerequisites": [
{ "n": "test_prereq", "ts": ["on"] }
],
"trafficAllocation": 100,
"trafficAllocationSeed": -2092979940,
"seed": 105482719,
"status": "ACTIVE",
"killed": false,
"defaultTreatment": "on_default",
"changeNumber": 1585948850109,
"algo": 2,
"configurations": {},
"conditions": [
{
"conditionType": "WHITELIST",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": null,
"matcherType": "WHITELIST",
"negate": false,
"userDefinedSegmentMatcherData": null,
"whitelistMatcherData": {
"whitelist": [
"bilal@split.io"
]
},
"unaryNumericMatcherData": null,
"betweenMatcherData": null,
"booleanMatcherData": null,
"dependencyMatcherData": null,
"stringMatcherData": null
}
]
},
"partitions": [
{
"treatment": "on_whitelist",
"size": 100
}
],
"label": "whitelisted"
},
{
"conditionType": "ROLLOUT",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": {
"trafficType": "user",
"attribute": null
},
"matcherType": "ALL_KEYS",
"negate": false,
"userDefinedSegmentMatcherData": null,
"whitelistMatcherData": null,
"unaryNumericMatcherData": null,
"betweenMatcherData": null,
"booleanMatcherData": null,
"dependencyMatcherData": null,
"stringMatcherData": null
}
]
},
"partitions": [
{
"treatment": "on",
"size": 100
},
{
"treatment": "off",
"size": 0
},
{
"treatment": "V1",
"size": 0
}
],
"label": "default rule"
}
]
}
],
"s": -1,
"t": 1585948850109
}, "rbs":{"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"
]
}
}
]
}
}
]
}], "s": -1, "t": 1585948850109}
}