Search in sources :

Example 1 with DatafileConfig

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));
}
Also used : Context(android.content.Context) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Logger(org.slf4j.Logger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) EventProcessor(com.optimizely.ab.event.EventProcessor) DatafileHandler(com.optimizely.ab.android.datafile_handler.DatafileHandler) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Test(org.junit.Test)

Example 2 with DatafileConfig

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);
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 3 with DatafileConfig

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()));
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) URL(java.net.URL) Test(org.junit.Test)

Example 4 with DatafileConfig

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();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Client(com.optimizely.ab.android.shared.Client) URL(java.net.URL) Test(org.junit.Test)

Example 5 with DatafileConfig

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();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Client(com.optimizely.ab.android.shared.Client) URL(java.net.URL) Test(org.junit.Test)

Aggregations

DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)38 Test (org.junit.Test)34 Context (android.content.Context)15 URL (java.net.URL)8 Client (com.optimizely.ab.android.shared.Client)7 Cache (com.optimizely.ab.android.shared.Cache)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)4 Intent (android.content.Intent)2 Data (androidx.work.Data)2 DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)2 DefaultUserProfileService (com.optimizely.ab.android.user_profile.DefaultUserProfileService)2 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Logger (org.slf4j.Logger)2 AlarmManager (android.app.AlarmManager)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1