19
19
import com .google .common .base .Charsets ;
20
20
import com .google .common .io .Resources ;
21
21
import com .optimizely .ab .OptimizelyHttpClient ;
22
+ import org .apache .http .HttpHeaders ;
22
23
import org .apache .http .HttpResponse ;
23
24
import org .apache .http .ProtocolVersion ;
25
+ import org .apache .http .StatusLine ;
24
26
import org .apache .http .client .ClientProtocolException ;
25
- import org .apache .http .client .ResponseHandler ;
27
+ import org .apache .http .client .methods . CloseableHttpResponse ;
26
28
import org .apache .http .client .methods .HttpGet ;
27
29
import org .apache .http .entity .StringEntity ;
28
30
import org .apache .http .message .BasicHttpResponse ;
@@ -56,8 +58,20 @@ public class HttpProjectConfigManagerTest {
56
58
@ Before
57
59
public void setUp () throws Exception {
58
60
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 ();
61
75
}
62
76
63
77
@ After
@@ -128,53 +142,64 @@ public void testBuildDefer() throws Exception {
128
142
129
143
@ Test
130
144
@ 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" ;
134
147
HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 200 , "TEST" );
135
148
getResponse .setEntity (new StringEntity (datafileString ));
149
+ getResponse .setHeader (HttpHeaders .LAST_MODIFIED , modifiedStamp );
136
150
137
- String datafile = handler . handleResponse (getResponse );
151
+ String datafile = projectConfigManager . getDatafileFromResponse (getResponse );
138
152
assertNotNull (datafile );
139
153
140
154
assertEquals ("4" , parseProjectConfig (datafile ).getVersion ());
155
+ // Confirm last modified time is set
156
+ assertEquals (modifiedStamp , projectConfigManager .getLastModified ());
141
157
}
142
158
143
159
@ Test (expected = ClientProtocolException .class )
144
- public void testProjectConfigResponseHandler3XX () throws Exception {
145
- ResponseHandler <String > handler = new ProjectConfigResponseHandler ();
146
-
160
+ public void testGetDatafileHttpResponse3XX () throws Exception {
147
161
HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 300 , "TEST" );
148
162
getResponse .setEntity (new StringEntity (datafileString ));
149
163
150
- handler . handleResponse (getResponse );
164
+ projectConfigManager . getDatafileFromResponse (getResponse );
151
165
}
152
166
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 ));
156
171
172
+ String datafile = projectConfigManager .getDatafileFromResponse (getResponse );
173
+ assertNull (datafile );
174
+ }
175
+
176
+ @ Test (expected = ClientProtocolException .class )
177
+ public void testGetDatafileHttpResponse4XX () throws Exception {
157
178
HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 400 , "TEST" );
158
179
getResponse .setEntity (new StringEntity (datafileString ));
159
180
160
- handler . handleResponse (getResponse );
181
+ projectConfigManager . getDatafileFromResponse (getResponse );
161
182
}
162
183
163
184
@ Test (expected = ClientProtocolException .class )
164
- public void testProjectConfigResponseHandler5XX () throws Exception {
165
- ResponseHandler <String > handler = new ProjectConfigResponseHandler ();
166
-
185
+ public void testGetDatafileHttpResponse5XX () throws Exception {
167
186
HttpResponse getResponse = new BasicHttpResponse (new ProtocolVersion ("TEST" , 0 , 0 ), 500 , "TEST" );
168
187
getResponse .setEntity (new StringEntity (datafileString ));
169
188
170
- handler . handleResponse (getResponse );
189
+ projectConfigManager . getDatafileFromResponse (getResponse );
171
190
}
172
191
173
- @ Test
174
192
public void testInvalidPayload () throws Exception {
175
193
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 );
178
203
179
204
projectConfigManager = builder ()
180
205
.withOptimizelyHttpClient (mockHttpClient )
@@ -196,4 +221,20 @@ public void testBasicFetch() throws Exception {
196
221
assertNotNull (actual );
197
222
assertNotNull (actual .getVersion ());
198
223
}
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
+ }
199
240
}
0 commit comments