22
33import com .google .gson .Gson ;
44import com .google .gson .reflect .TypeToken ;
5+ import io .split .TestHelper ;
56import io .split .client .dtos .ImpressionCount ;
67import io .split .client .dtos .KeyImpression ;
78import 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 ;
811import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
912import org .apache .hc .client5 .http .impl .classic .HttpClients ;
13+ import org .apache .hc .core5 .http .HttpStatus ;
1014import org .hamcrest .Matchers ;
1115import org .junit .Assert ;
1216import org .junit .Test ;
1519
1620import java .io .IOException ;
1721import java .io .InputStreamReader ;
22+ import java .lang .reflect .InvocationTargetException ;
1823import java .net .URI ;
1924import java .net .URISyntaxException ;
2025import java .util .Arrays ;
2732import static org .hamcrest .core .IsEqual .equalTo ;
2833import static org .hamcrest .core .IsInstanceOf .instanceOf ;
2934import static org .mockito .Mockito .verify ;
30- import static org .mockito .Mockito .when ;
3135
3236public 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