use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.
the class OptimizelyManagerTest method initializeAsyncWithEnvironment.
@Test
public void initializeAsyncWithEnvironment() {
Logger logger = mock(Logger.class);
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L, eventHandler, eventProcessor, null, null, null);
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
*/
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
((DatafileLoadedListener) invocation.getArguments()[2]).onDatafileLoaded(null);
return null;
}
}).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class), any(DatafileLoadedListener.class));
OptimizelyStartListener listener = new OptimizelyStartListener() {
@Override
public void onStart(OptimizelyClient optimizely) {
assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());
assertNull(optimizelyManager.getOptimizelyStartListener());
}
};
optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getContext(), R.raw.datafile, listener);
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(new DatafileConfig(testProjectId, testSdkKey)), eq(3600L), any(DatafileLoadedListener.class));
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getInstrumentation().getTargetContext()), false);
assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey));
}
use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.
the class BackgroundWatchersCacheTest method testLoadFileNotFound.
@Test
public void testLoadFileNotFound() {
Cache cache = mock(Cache.class);
BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
// Cause a JSONException to be thrown
when(cache.load(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME)).thenReturn(null);
assertFalse(backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true));
verify(logger).info("Creating background watchers file {}.", BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME);
}
use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.
the class DatafileClientTest method handlesEmptyStringResponse.
@Test
public void handlesEmptyStringResponse() throws MalformedURLException {
URL url = new URL(new DatafileConfig("1", null).getUrl());
when(client.execute(any(Client.Request.class), eq(DatafileClient.REQUEST_BACKOFF_TIMEOUT), eq(DatafileClient.REQUEST_RETRIES_POWER))).thenReturn("");
assertEquals("", datafileClient.request(url.toString()));
}
use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.
the class DatafileClientTest method request200.
@Test
public void request200() throws IOException {
URL url = new URL(new DatafileConfig("1", null).getUrl());
when(client.openConnection(url)).thenReturn(urlConnection);
when(urlConnection.getResponseCode()).thenReturn(200);
when(client.readStream(urlConnection)).thenReturn("{}");
datafileClient.request(url.toString());
ArgumentCaptor<Client.Request> captor1 = ArgumentCaptor.forClass(Client.Request.class);
ArgumentCaptor<Integer> captor2 = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<Integer> captor3 = ArgumentCaptor.forClass(Integer.class);
verify(client).execute(captor1.capture(), captor2.capture(), captor3.capture());
assertEquals(Integer.valueOf(DatafileClient.REQUEST_BACKOFF_TIMEOUT), captor2.getValue());
assertEquals(Integer.valueOf(DatafileClient.REQUEST_RETRIES_POWER), captor3.getValue());
Object response = captor1.getValue().execute();
assertTrue(String.class.isInstance(response));
assertEquals("{}", response);
verify(logger).info("Requesting data file from {}", url);
verify(client).saveLastModified(urlConnection);
verify(client).readStream(urlConnection);
verify(urlConnection).disconnect();
}
use of com.optimizely.ab.android.shared.DatafileConfig in project android-sdk by optimizely.
the class DatafileClientTest method request299.
@Test
public void request299() throws IOException {
URL url = new URL(new DatafileConfig("1", null).getUrl());
when(client.openConnection(url)).thenReturn(urlConnection);
when(urlConnection.getResponseCode()).thenReturn(299);
when(client.readStream(urlConnection)).thenReturn("{}");
datafileClient.request(url.toString());
ArgumentCaptor<Client.Request> captor1 = ArgumentCaptor.forClass(Client.Request.class);
ArgumentCaptor<Integer> captor2 = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<Integer> captor3 = ArgumentCaptor.forClass(Integer.class);
verify(client).execute(captor1.capture(), captor2.capture(), captor3.capture());
assertEquals(Integer.valueOf(DatafileClient.REQUEST_BACKOFF_TIMEOUT), captor2.getValue());
assertEquals(Integer.valueOf(DatafileClient.REQUEST_RETRIES_POWER), captor3.getValue());
Object response = captor1.getValue().execute();
assertTrue(String.class.isInstance(response));
assertEquals("{}", response);
verify(logger).info("Requesting data file from {}", url);
verify(client).saveLastModified(urlConnection);
verify(client).readStream(urlConnection);
verify(urlConnection).disconnect();
}
Aggregations