1919import com .google .common .base .Charsets ;
2020import com .google .common .io .Resources ;
2121import com .optimizely .ab .OptimizelyHttpClient ;
22+ import org .apache .http .HttpHeaders ;
2223import org .apache .http .HttpResponse ;
2324import org .apache .http .ProtocolVersion ;
25+ import org .apache .http .StatusLine ;
2426import org .apache .http .client .ClientProtocolException ;
25- import org .apache .http .client .ResponseHandler ;
27+ import org .apache .http .client .methods . CloseableHttpResponse ;
2628import org .apache .http .client .methods .HttpGet ;
2729import org .apache .http .entity .StringEntity ;
2830import org .apache .http .message .BasicHttpResponse ;
@@ -56,8 +58,20 @@ public class HttpProjectConfigManagerTest {
5658 @ Before
5759 public void setUp () throws Exception {
5860 datafileString = Resources .toString (Resources .getResource ("valid-project-config-v4.json" ), Charsets .UTF_8 );
59- when (mockHttpClient .execute (any (HttpGet .class ), any (ResponseHandler .class )))
60- .thenReturn (datafileString );
61+ CloseableHttpResponse httpResponse = mock (CloseableHttpResponse .class );
62+ StatusLine statusLine = mock (StatusLine .class );
63+
64+ when (statusLine .getStatusCode ()).thenReturn (200 );
65+ when (httpResponse .getStatusLine ()).thenReturn (statusLine );
66+ when (httpResponse .getEntity ()).thenReturn (new StringEntity (datafileString ));
67+
68+ when (mockHttpClient .execute (any (HttpGet .class )))
69+ .thenReturn (httpResponse );
70+
71+ projectConfigManager = builder ()
72+ .withOptimizelyHttpClient (mockHttpClient )
73+ .withSdkKey ("sdk-key" )
74+ .build ();
6175 }
6276
6377 @ After
@@ -128,53 +142,64 @@ public void testBuildDefer() throws Exception {
128142
129143 @ Test
130144 @ Ignore
131- public void testProjectConfigResponseHandler2XX () throws Exception {
132- ResponseHandler <String > handler = new ProjectConfigResponseHandler ();
133-
145+ public void testGetDatafileHttpResponse2XX () throws Exception {
146+ String modifiedStamp = "Wed, 24 Apr 2019 07:07:07 GMT" ;
134147 HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 200 , "TEST" );
135148 getResponse .setEntity (new StringEntity (datafileString ));
149+ getResponse .setHeader (HttpHeaders .LAST_MODIFIED , modifiedStamp );
136150
137- String datafile = handler . handleResponse (getResponse );
151+ String datafile = projectConfigManager . getDatafileFromResponse (getResponse );
138152 assertNotNull (datafile );
139153
140154 assertEquals ("4" , parseProjectConfig (datafile ).getVersion ());
155+ // Confirm last modified time is set
156+ assertEquals (modifiedStamp , projectConfigManager .getLastModified ());
141157 }
142158
143159 @ Test (expected = ClientProtocolException .class )
144- public void testProjectConfigResponseHandler3XX () throws Exception {
145- ResponseHandler <String > handler = new ProjectConfigResponseHandler ();
146-
160+ public void testGetDatafileHttpResponse3XX () throws Exception {
147161 HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 300 , "TEST" );
148162 getResponse .setEntity (new StringEntity (datafileString ));
149163
150- handler . handleResponse (getResponse );
164+ projectConfigManager . getDatafileFromResponse (getResponse );
151165 }
152166
153- @ Test (expected = ClientProtocolException .class )
154- public void testProjectConfigResponseHandler4XX () throws Exception {
155- ResponseHandler <String > handler = new ProjectConfigResponseHandler ();
167+ @ Test
168+ public void testGetDatafileHttpResponse304 () throws Exception {
169+ HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 304 , "TEST" );
170+ getResponse .setEntity (new StringEntity (datafileString ));
156171
172+ String datafile = projectConfigManager .getDatafileFromResponse (getResponse );
173+ assertNull (datafile );
174+ }
175+
176+ @ Test (expected = ClientProtocolException .class )
177+ public void testGetDatafileHttpResponse4XX () throws Exception {
157178 HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 400 , "TEST" );
158179 getResponse .setEntity (new StringEntity (datafileString ));
159180
160- handler . handleResponse (getResponse );
181+ projectConfigManager . getDatafileFromResponse (getResponse );
161182 }
162183
163184 @ Test (expected = ClientProtocolException .class )
164- public void testProjectConfigResponseHandler5XX () throws Exception {
165- ResponseHandler <String > handler = new ProjectConfigResponseHandler ();
166-
185+ public void testGetDatafileHttpResponse5XX () throws Exception {
167186 HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 500 , "TEST" );
168187 getResponse .setEntity (new StringEntity (datafileString ));
169188
170- handler . handleResponse (getResponse );
189+ projectConfigManager . getDatafileFromResponse (getResponse );
171190 }
172191
173- @ Test
174192 public void testInvalidPayload () throws Exception {
175193 reset (mockHttpClient );
176- when (mockHttpClient .execute (any (HttpGet .class ), any (ResponseHandler .class )))
177- .thenReturn ("I am an invalid response!" );
194+ CloseableHttpResponse invalidPayloadResponse = mock (CloseableHttpResponse .class );
195+ StatusLine statusLine = mock (StatusLine .class );
196+
197+ when (statusLine .getStatusCode ()).thenReturn (200 );
198+ when (invalidPayloadResponse .getStatusLine ()).thenReturn (statusLine );
199+ when (invalidPayloadResponse .getEntity ()).thenReturn (new StringEntity ("I am an invalid response!" ));
200+
201+ when (mockHttpClient .execute (any (HttpGet .class )))
202+ .thenReturn (invalidPayloadResponse );
178203
179204 projectConfigManager = builder ()
180205 .withOptimizelyHttpClient (mockHttpClient )
@@ -196,4 +221,20 @@ public void testBasicFetch() throws Exception {
196221 assertNotNull (actual );
197222 assertNotNull (actual .getVersion ());
198223 }
224+
225+ @ Test
226+ @ Ignore
227+ public void testBasicFetchTwice () throws Exception {
228+ projectConfigManager = builder ()
229+ .withSdkKey ("7vPf3v7zye3fY4PcbejeCz" )
230+ .build ();
231+
232+ ProjectConfig actual = projectConfigManager .getConfig ();
233+ assertNotNull (actual );
234+ assertNotNull (actual .getVersion ());
235+
236+ // Assert ProjectConfig when refetched as datafile has not changed
237+ ProjectConfig latestConfig = projectConfigManager .getConfig ();
238+ assertEquals (actual , latestConfig );
239+ }
199240}
0 commit comments