Skip to content

Commit f4eaa1b

Browse files
committed
fixed tests
1 parent d1d4fbf commit f4eaa1b

File tree

5 files changed

+111
-171
lines changed

5 files changed

+111
-171
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.split;
2+
3+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
4+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
5+
import org.apache.hc.core5.http.ClassicHttpResponse;
6+
import org.apache.hc.core5.http.HttpEntity;
7+
import org.mockito.Mockito;
8+
9+
import java.io.IOException;
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
13+
public class TestHelper {
14+
public CloseableHttpClient mockHttpClient(String jsonName, int httpStatus) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
15+
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
16+
Mockito.when(entityMock.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream(jsonName));
17+
18+
ClassicHttpResponse httpResponseMock = Mockito.mock(ClassicHttpResponse.class);
19+
Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock);
20+
Mockito.when(httpResponseMock.getCode()).thenReturn(httpStatus);
21+
22+
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
23+
Mockito.when(httpClientMock.execute(Mockito.anyObject())).thenReturn(classicResponseToCloseableMock(httpResponseMock));
24+
25+
return httpClientMock;
26+
}
27+
28+
private CloseableHttpResponse classicResponseToCloseableMock(ClassicHttpResponse mocked) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
29+
Method adaptMethod = CloseableHttpResponse.class.getDeclaredMethod("adapt", ClassicHttpResponse.class);
30+
adaptMethod.setAccessible(true);
31+
return (CloseableHttpResponse) adaptMethod.invoke(null, mocked);
32+
}
33+
}

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

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

3+
import io.split.TestHelper;
34
import io.split.client.dtos.SegmentChange;
45
import io.split.engine.metrics.Metrics;
56
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
6-
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
77
import org.apache.hc.client5.http.impl.classic.HttpClients;
8+
import org.apache.hc.core5.http.HttpStatus;
89
import org.hamcrest.Matchers;
910
import org.junit.Assert;
1011
import org.junit.Test;
11-
import org.mockito.Mockito;
1212

1313
import java.io.IOException;
14+
import java.lang.reflect.InvocationTargetException;
1415
import java.net.URI;
1516
import java.net.URISyntaxException;
1617

1718
public class HttpSegmentChangeFetcherTest {
19+
private final TestHelper _testHelper = new TestHelper();
1820

1921
@Test
2022
public void testDefaultURL() throws URISyntaxException {
@@ -52,23 +54,11 @@ public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException {
5254
Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges")));
5355
}
5456

55-
// TODO: Fix this test by mocking .getCode() instead of the status line of the request
56-
/*
5757
@Test
58-
public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException {
58+
public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
5959
URI rootTarget = URI.create("https://api.split.io/api/segmentChanges");
6060

61-
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
62-
CloseableHttpResponse httpResponseMock = Mockito.mock(CloseableHttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
63-
StatusLine statusLineMock = Mockito.mock(StatusLine.class);
64-
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
65-
66-
Mockito.when(statusLineMock.getStatusCode()).thenReturn(200);
67-
Mockito.when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
68-
Mockito.when(entityMock.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream("segment-change-special-chatacters.json"));
69-
Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock);
70-
71-
Mockito.when(httpClientMock.execute((HttpUriRequest) Mockito.anyObject())).thenReturn(httpResponseMock);
61+
CloseableHttpClient httpClientMock = _testHelper.mockHttpClient("segment-change-special-chatacters.json", HttpStatus.SC_OK);
7262

7363
Metrics.NoopMetrics metrics = new Metrics.NoopMetrics();
7464
HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClientMock, rootTarget, metrics);
@@ -81,7 +71,4 @@ public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOExce
8171
Assert.assertEquals(1, change.removed.size());
8272
Assert.assertEquals("other_user", change.removed.get(0));
8373
}
84-
85-
86-
*/
8774
}

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package io.split.client;
22

3+
import io.split.TestHelper;
34
import io.split.client.dtos.Split;
45
import io.split.client.dtos.SplitChange;
56
import io.split.engine.metrics.Metrics;
67
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
7-
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
88
import org.apache.hc.client5.http.impl.classic.HttpClients;
9+
import org.apache.hc.core5.http.HttpStatus;
910
import org.hamcrest.Matchers;
1011
import org.junit.Assert;
1112
import org.junit.Test;
12-
import org.mockito.Mockito;
1313

1414
import java.io.IOException;
15+
import java.lang.reflect.InvocationTargetException;
1516
import java.net.URI;
1617
import java.net.URISyntaxException;
1718
import java.util.Map;
1819

1920
public class HttpSplitChangeFetcherTest {
21+
private final TestHelper _testHelper = new TestHelper();
2022

2123
@Test
2224
public void testDefaultURL() throws URISyntaxException {
@@ -54,23 +56,11 @@ public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException {
5456
Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/splitChanges")));
5557
}
5658

57-
// TODO: Fix this test by mocking .getCode() instead of the status line of the request
58-
/*
5959
@Test
60-
public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException {
60+
public void testFetcherWithSpecialCharacters() throws URISyntaxException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
6161
URI rootTarget = URI.create("https://api.split.io");
6262

63-
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
64-
CloseableHttpResponse httpResponseMock = Mockito.mock(CloseableHttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
65-
StatusLine statusLineMock = Mockito.mock(StatusLine.class);
66-
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
67-
68-
Mockito.when(statusLineMock.getStatusCode()).thenReturn(200);
69-
Mockito.when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
70-
Mockito.when(entityMock.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream("split-change-special-characters.json"));
71-
Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock);
72-
73-
Mockito.when(httpClientMock.execute((HttpUriRequest) Mockito.anyObject())).thenReturn(httpResponseMock);
63+
CloseableHttpClient httpClientMock = _testHelper.mockHttpClient("split-change-special-characters.json", HttpStatus.SC_OK);
7464

7565
Metrics.NoopMetrics metrics = new Metrics.NoopMetrics();
7666
HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClientMock, rootTarget, metrics);
@@ -87,6 +77,4 @@ public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOExce
8777
Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on"));
8878
Assert.assertEquals("{\"test\": \"blue\",\"size\": 15}", configs.get("off"));
8979
}
90-
91-
*/
9280
}

client/src/test/java/io/split/client/impressions/HttpImpressionsSenderTest.java

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.reflect.TypeToken;
5+
import io.split.TestHelper;
56
import io.split.client.dtos.ImpressionCount;
67
import io.split.client.dtos.KeyImpression;
78
import io.split.client.dtos.TestImpressions;
9+
import org.apache.hc.client5.http.classic.methods.HttpPost;
10+
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
811
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
912
import org.apache.hc.client5.http.impl.classic.HttpClients;
13+
import org.apache.hc.core5.http.HttpStatus;
1014
import org.hamcrest.Matchers;
1115
import org.junit.Assert;
1216
import org.junit.Test;
@@ -15,6 +19,7 @@
1519

1620
import java.io.IOException;
1721
import java.io.InputStreamReader;
22+
import java.lang.reflect.InvocationTargetException;
1823
import java.net.URI;
1924
import java.net.URISyntaxException;
2025
import java.util.Arrays;
@@ -27,9 +32,9 @@
2732
import static org.hamcrest.core.IsEqual.equalTo;
2833
import static org.hamcrest.core.IsInstanceOf.instanceOf;
2934
import static org.mockito.Mockito.verify;
30-
import static org.mockito.Mockito.when;
3135

3236
public class HttpImpressionsSenderTest {
37+
private final TestHelper _testHelper = new TestHelper();
3338

3439
@Test
3540
public void testDefaultURL() throws URISyntaxException {
@@ -63,20 +68,12 @@ public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException {
6368
Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/testImpressions/bulk")));
6469
}
6570

66-
// TODO: Fix this test by mocking .getCode() instead of the status line of the request
67-
/*
68-
6971
@Test
70-
public void testImpressionCountsEndpointOptimized() throws URISyntaxException, IOException {
72+
public void testImpressionCountsEndpointOptimized() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
7173
URI rootTarget = URI.create("https://kubernetesturl.com/split");
7274

7375
// Setup response mock
74-
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
75-
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
76-
StatusLine statusLine = Mockito.mock(StatusLine.class);
77-
when(statusLine.getStatusCode()).thenReturn(200);
78-
when(response.getStatusLine()).thenReturn(statusLine);
79-
when(httpClient.execute(Mockito.any())).thenReturn(response);
76+
CloseableHttpClient httpClient = _testHelper.mockHttpClient("", HttpStatus.SC_OK);
8077

8178
// Send counters
8279
HttpImpressionsSender sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.OPTIMIZED);
@@ -89,8 +86,8 @@ public void testImpressionCountsEndpointOptimized() throws URISyntaxException, I
8986
ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
9087
verify(httpClient).execute(captor.capture());
9188
HttpUriRequest request = captor.getValue();
92-
assertThat(request.getURI(), is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/count"))));
93-
assertThat(request.getAllHeaders().length, is(0));
89+
assertThat(request.getUri(), is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/count"))));
90+
assertThat(request.getHeaders().length, is(0));
9491
assertThat(request, instanceOf(HttpPost.class));
9592
HttpPost asPostRequest = (HttpPost) request;
9693
InputStreamReader reader = new InputStreamReader(asPostRequest.getEntity().getContent());
@@ -102,16 +99,11 @@ public void testImpressionCountsEndpointOptimized() throws URISyntaxException, I
10299
}
103100

104101
@Test
105-
public void testImpressionCountsEndpointDebug() throws URISyntaxException, IOException {
102+
public void testImpressionCountsEndpointDebug() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
106103
URI rootTarget = URI.create("https://kubernetesturl.com/split");
107104

108105
// Setup response mock
109-
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
110-
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
111-
StatusLine statusLine = Mockito.mock(StatusLine.class);
112-
when(statusLine.getStatusCode()).thenReturn(200);
113-
when(response.getStatusLine()).thenReturn(statusLine);
114-
when(httpClient.execute(Mockito.any())).thenReturn(response);
106+
CloseableHttpClient httpClient = _testHelper.mockHttpClient("", HttpStatus.SC_OK);
115107

116108
// Send counters
117109
HttpImpressionsSender sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG);
@@ -125,16 +117,12 @@ public void testImpressionCountsEndpointDebug() throws URISyntaxException, IOExc
125117
}
126118

127119
@Test
128-
public void testImpressionBulksEndpoint() throws URISyntaxException, IOException {
120+
public void testImpressionBulksEndpoint() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
129121
URI rootTarget = URI.create("https://kubernetesturl.com/split");
130122

131123
// Setup response mock
132-
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
133-
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
134-
StatusLine statusLine = Mockito.mock(StatusLine.class);
135-
when(statusLine.getStatusCode()).thenReturn(200);
136-
when(response.getStatusLine()).thenReturn(statusLine);
137-
when(httpClient.execute(Mockito.any())).thenReturn(response);
124+
CloseableHttpClient httpClient = _testHelper.mockHttpClient("", HttpStatus.SC_OK);
125+
138126
HttpImpressionsSender sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.OPTIMIZED);
139127

140128
// Send impressions
@@ -153,8 +141,8 @@ public void testImpressionBulksEndpoint() throws URISyntaxException, IOException
153141
ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
154142
verify(httpClient).execute(captor.capture());
155143
HttpUriRequest request = captor.getValue();
156-
assertThat(request.getURI(), is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/bulk"))));
157-
assertThat(request.getAllHeaders().length, is(1));
144+
assertThat(request.getUri(), is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/bulk"))));
145+
assertThat(request.getHeaders().length, is(1));
158146
assertThat(request.getFirstHeader("SplitSDKImpressionsMode").getValue(), is(equalTo("OPTIMIZED")));
159147
assertThat(request, instanceOf(HttpPost.class));
160148
HttpPost asPostRequest = (HttpPost) request;
@@ -164,20 +152,14 @@ public void testImpressionBulksEndpoint() throws URISyntaxException, IOException
164152
assertThat(payload.size(), is(equalTo(2)));
165153

166154
// Do the same flow for imrpessionsMode = debug
167-
Mockito.reset(httpClient, response, statusLine);
168-
when(statusLine.getStatusCode()).thenReturn(200);
169-
when(response.getStatusLine()).thenReturn(statusLine);
170-
when(httpClient.execute(Mockito.any())).thenReturn(response);
171-
sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG);
155+
CloseableHttpClient httpClientDebugMode = _testHelper.mockHttpClient("", HttpStatus.SC_OK);
156+
157+
sender = HttpImpressionsSender.create(httpClientDebugMode, rootTarget, ImpressionsManager.Mode.DEBUG);
172158
sender.postImpressionsBulk(toSend);
173159
captor = ArgumentCaptor.forClass(HttpUriRequest.class);
174-
verify(httpClient).execute(captor.capture());
160+
verify(httpClientDebugMode).execute(captor.capture());
175161
request = captor.getValue();
176-
assertThat(request.getAllHeaders().length, is(1));
162+
assertThat(request.getHeaders().length, is(1));
177163
assertThat(request.getFirstHeader("SplitSDKImpressionsMode").getValue(), is(equalTo("DEBUG")));
178164
}
179-
180-
181-
*/
182-
183165
}

0 commit comments

Comments
 (0)