Skip to content

Commit cd37b6a

Browse files
author
Matias Melograno
committed
added uts
1 parent cbb8a75 commit cd37b6a

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

client/src/test/java/io/split/client/HttpSegmentChangeFetcherTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package io.split.client;
22

3+
import io.split.client.dtos.SegmentChange;
34
import io.split.engine.metrics.Metrics;
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.StatusLine;
7+
import org.apache.http.client.methods.CloseableHttpResponse;
8+
import org.apache.http.client.methods.HttpUriRequest;
49
import org.apache.http.impl.client.CloseableHttpClient;
510
import org.apache.http.impl.client.HttpClients;
611
import org.hamcrest.Matchers;
712
import org.junit.Assert;
813
import org.junit.Test;
14+
import org.mockito.Mockito;
915

16+
import java.io.IOException;
1017
import java.net.URI;
1118
import java.net.URISyntaxException;
1219

@@ -48,4 +55,31 @@ public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException {
4855
Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges")));
4956
}
5057

58+
@Test
59+
public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException {
60+
URI rootTarget = URI.create("https://api.split.io/api/segmentChanges");
61+
62+
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
63+
CloseableHttpResponse httpResponseMock = Mockito.mock(CloseableHttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
64+
StatusLine statusLineMock = Mockito.mock(StatusLine.class);
65+
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
66+
67+
Mockito.when(statusLineMock.getStatusCode()).thenReturn(200);
68+
Mockito.when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
69+
Mockito.when(entityMock.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream("segment-change-special-chatacters.json"));
70+
Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock);
71+
72+
Mockito.when(httpClientMock.execute((HttpUriRequest) Mockito.anyObject())).thenReturn(httpResponseMock);
73+
74+
Metrics.NoopMetrics metrics = new Metrics.NoopMetrics();
75+
HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClientMock, rootTarget, metrics);
76+
77+
SegmentChange change = fetcher.fetch("some_segment", 1234567);
78+
79+
Assert.assertNotNull(change);
80+
Assert.assertEquals(1, change.added.size());
81+
Assert.assertEquals("grüne_Straße", change.added.get(0));
82+
Assert.assertEquals(1, change.removed.size());
83+
Assert.assertEquals("other_user", change.removed.get(0));
84+
}
5185
}

client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package io.split.client;
22

3+
import io.split.client.dtos.Split;
4+
import io.split.client.dtos.SplitChange;
35
import io.split.engine.metrics.Metrics;
6+
import org.apache.http.HttpEntity;
7+
import org.apache.http.StatusLine;
8+
import org.apache.http.client.methods.CloseableHttpResponse;
9+
import org.apache.http.client.methods.HttpUriRequest;
410
import org.apache.http.impl.client.CloseableHttpClient;
511
import org.apache.http.impl.client.HttpClients;
612
import org.hamcrest.Matchers;
713
import org.junit.Assert;
814
import org.junit.Test;
15+
import org.mockito.Mockito;
916

17+
import java.io.IOException;
1018
import java.net.URI;
1119
import java.net.URISyntaxException;
20+
import java.util.Map;
1221

1322
public class HttpSplitChangeFetcherTest {
1423

@@ -48,4 +57,35 @@ public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException {
4857
Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/splitChanges")));
4958
}
5059

60+
@Test
61+
public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException {
62+
URI rootTarget = URI.create("https://api.split.io");
63+
64+
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
65+
CloseableHttpResponse httpResponseMock = Mockito.mock(CloseableHttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
66+
StatusLine statusLineMock = Mockito.mock(StatusLine.class);
67+
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
68+
69+
Mockito.when(statusLineMock.getStatusCode()).thenReturn(200);
70+
Mockito.when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
71+
Mockito.when(entityMock.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream("split-change-special-characters.json"));
72+
Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock);
73+
74+
Mockito.when(httpClientMock.execute((HttpUriRequest) Mockito.anyObject())).thenReturn(httpResponseMock);
75+
76+
Metrics.NoopMetrics metrics = new Metrics.NoopMetrics();
77+
HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClientMock, rootTarget, metrics);
78+
79+
SplitChange change = fetcher.fetch(1234567);
80+
81+
Assert.assertNotNull(change);
82+
Assert.assertEquals(1, change.splits.size());
83+
Assert.assertNotNull(change.splits.get(0));
84+
85+
Split split = change.splits.get(0);
86+
Map<String, String> configs = split.configurations;
87+
Assert.assertEquals(2, configs.size());
88+
Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on"));
89+
Assert.assertEquals("{\"test\": \"blue\",\"size\": 15}", configs.get("off"));
90+
}
5191
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "employees",
3+
"added": [
4+
"grüne_Straße"
5+
],
6+
"removed": [
7+
"other_user"
8+
],
9+
"since": -1,
10+
"till": 1489542661161
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"splits": [
3+
{
4+
"trafficTypeName": "user",
5+
"name": "DEMO_MURMUR2",
6+
"trafficAllocation": 100,
7+
"trafficAllocationSeed": 1314112417,
8+
"seed": -2059033614,
9+
"status": "ACTIVE",
10+
"killed": false,
11+
"defaultTreatment": "of",
12+
"changeNumber": 1491244291288,
13+
"algo": 2,
14+
"configurations": {
15+
"on": "{\"test\": \"blue\",\"grüne Straße\": 13}",
16+
"off": "{\"test\": \"blue\",\"size\": 15}"
17+
},
18+
"conditions": [
19+
{
20+
"conditionType": "ROLLOUT",
21+
"matcherGroup": {
22+
"combiner": "AND",
23+
"matchers": [
24+
{
25+
"keySelector": {
26+
"trafficType": "user",
27+
"attribute": null
28+
},
29+
"matcherType": "ALL_KEYS",
30+
"negate": false,
31+
"userDefinedSegmentMatcherData": null,
32+
"whitelistMatcherData": null,
33+
"unaryNumericMatcherData": null,
34+
"betweenMatcherData": null
35+
}
36+
]
37+
},
38+
"partitions": [
39+
{
40+
"treatment": "on",
41+
"size": 0
42+
},
43+
{
44+
"treatment": "of",
45+
"size": 100
46+
}
47+
],
48+
"label": "in segment all"
49+
}
50+
]
51+
}
52+
],
53+
"since": 1491244291288,
54+
"till": 1491244291288
55+
}

0 commit comments

Comments
 (0)