Search in sources :

Example 6 with DefaultDatafileHandler

use of com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler in project android-sdk by optimizely.

the class OptimizelyManagerTest method initializeSyncWithResourceDatafileNoCache.

@Test
public void initializeSyncWithResourceDatafileNoCache() {
    boolean downloadToCache = true;
    boolean updateConfigOnNewDatafile = true;
    // enable polling
    int pollingInterval = 30;
    DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
    Logger logger = mock(Logger.class);
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    OptimizelyManager manager = spy(new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0, null, null, null, null, null));
    datafileHandler.removeSavedDatafile(context, manager.getDatafileConfig());
    OptimizelyClient client = manager.initialize(context, R.raw.datafile, downloadToCache, updateConfigOnNewDatafile);
    verify(manager).initialize(eq(context), eq(defaultDatafile), eq(downloadToCache), eq(updateConfigOnNewDatafile));
}
Also used : Context(android.content.Context) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 7 with DefaultDatafileHandler

use of com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler in project android-sdk by optimizely.

the class OptimizelyManagerTest method initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingDisabled.

@Test
public void initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingDisabled() {
    boolean downloadToCache = true;
    boolean updateConfigOnNewDatafile = true;
    // disable polling
    int pollingInterval = 0;
    DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
    Logger logger = mock(Logger.class);
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0, null, null, null, null, null);
    doAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            String newDatafile = manager.getDatafile(context, R.raw.datafile_api);
            datafileHandler.saveDatafile(context, manager.getDatafileConfig(), newDatafile);
            return null;
        }
    }).when(manager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class), any(DatafileLoadedListener.class));
    OptimizelyClient client = manager.initialize(context, defaultDatafile, downloadToCache, updateConfigOnNewDatafile);
    try {
        executor.awaitTermination(1, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
    // 
    }
    assertEquals(client.getOptimizelyConfig().getRevision(), "241");
}
Also used : Context(android.content.Context) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Logger(org.slf4j.Logger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 8 with DefaultDatafileHandler

use of com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler in project android-sdk by optimizely.

the class OptimizelyManagerTest method initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingEnabled.

@Test
public void initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingEnabled() {
    boolean downloadToCache = true;
    boolean updateConfigOnNewDatafile = false;
    // enable polling
    int pollingInterval = 30;
    DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
    Logger logger = mock(Logger.class);
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0, null, null, null, null, null);
    doAnswer((Answer<Object>) invocation -> {
        String newDatafile = manager.getDatafile(context, R.raw.datafile_api);
        datafileHandler.saveDatafile(context, manager.getDatafileConfig(), newDatafile);
        return null;
    }).when(manager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class), any(DatafileLoadedListener.class));
    OptimizelyClient client = manager.initialize(context, defaultDatafile, downloadToCache, updateConfigOnNewDatafile);
    try {
        executor.awaitTermination(1, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
    // 
    }
    // when periodic polling enabled, project config always updated on cache datafile update (regardless of "updateConfigOnNewDatafile" setting)
    // wait for first download.
    assertEquals(client.getOptimizelyConfig().getRevision(), "241");
}
Also used : Context(android.content.Context) Assert.fail(junit.framework.Assert.fail) Context(android.content.Context) Assert.assertEquals(junit.framework.Assert.assertEquals) EventHandler(com.optimizely.ab.event.EventHandler) PackageManager(android.content.pm.PackageManager) RunWith(org.junit.runner.RunWith) Intent(android.content.Intent) Mockito.spy(org.mockito.Mockito.spy) PackageInfo(android.content.pm.PackageInfo) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Assert.assertTrue(junit.framework.Assert.assertTrue) ArgumentCaptor(org.mockito.ArgumentCaptor) ProjectConfig(com.optimizely.ab.config.ProjectConfig) DatafileProjectConfig(com.optimizely.ab.config.DatafileProjectConfig) Assert.assertNotNull(junit.framework.Assert.assertNotNull) Matchers.eq(org.mockito.Matchers.eq) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Build(android.os.Build) ExecutorService(java.util.concurrent.ExecutorService) Assert.assertNull(junit.framework.Assert.assertNull) Before(org.junit.Before) DatafileService(com.optimizely.ab.android.datafile_handler.DatafileService) Logger(org.slf4j.Logger) SdkSuppress(androidx.test.filters.SdkSuppress) AlarmManager(android.app.AlarmManager) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) InstrumentationRegistry(androidx.test.platform.app.InstrumentationRegistry) ConfigParseException(com.optimizely.ab.config.parser.ConfigParseException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler) EventProcessor(com.optimizely.ab.event.EventProcessor) Executors(java.util.concurrent.Executors) DefaultUserProfileService(com.optimizely.ab.android.user_profile.DefaultUserProfileService) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) DatafileHandler(com.optimizely.ab.android.datafile_handler.DatafileHandler) Variation(com.optimizely.ab.config.Variation) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) Assert.assertFalse(junit.framework.Assert.assertFalse) Mockito.mock(org.mockito.Mockito.mock) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 9 with DefaultDatafileHandler

use of com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler in project android-sdk by optimizely.

the class OptimizelyManagerTest method initializeSyncWithUpdateOnNewDatafileDisabled.

// Init Sync Flows
@Test
public void initializeSyncWithUpdateOnNewDatafileDisabled() {
    boolean downloadToCache = true;
    boolean updateConfigOnNewDatafile = false;
    // disable polling
    int pollingInterval = 0;
    DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
    Logger logger = mock(Logger.class);
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0, null, null, null, null, null);
    doAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            String newDatafile = manager.getDatafile(context, R.raw.datafile_api);
            datafileHandler.saveDatafile(context, manager.getDatafileConfig(), newDatafile);
            return null;
        }
    }).when(manager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class), any(DatafileLoadedListener.class));
    OptimizelyClient client = manager.initialize(context, defaultDatafile, downloadToCache, updateConfigOnNewDatafile);
    try {
        executor.awaitTermination(1, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
    // 
    }
    assertEquals(client.getOptimizelyConfig().getRevision(), "7");
}
Also used : Context(android.content.Context) DatafileLoadedListener(com.optimizely.ab.android.datafile_handler.DatafileLoadedListener) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Logger(org.slf4j.Logger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 10 with DefaultDatafileHandler

use of com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler in project android-sdk by optimizely.

the class OptimizelyManager method buildOptimizely.

private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull String datafile) throws ConfigParseException {
    EventHandler eventHandler = getEventHandler(context);
    EventBatch.ClientEngine clientEngine = OptimizelyClientEngine.getClientEngineFromContext(context);
    Optimizely.Builder builder = Optimizely.builder();
    builder.withEventHandler(eventHandler);
    builder.withEventProcessor(eventProcessor);
    if (datafileHandler instanceof DefaultDatafileHandler) {
        DefaultDatafileHandler handler = (DefaultDatafileHandler) datafileHandler;
        handler.setDatafile(datafile);
        builder.withConfigManager(handler);
    } else {
        builder.withDatafile(datafile);
    }
    // override client sdk name/version to be included in events
    builder.withClientInfo(clientEngine, sdkVersion);
    if (errorHandler != null) {
        builder.withErrorHandler(errorHandler);
    }
    builder.withUserProfileService(userProfileService);
    builder.withNotificationCenter(notificationCenter);
    builder.withDefaultDecideOptions(defaultDecideOptions);
    Optimizely optimizely = builder.build();
    return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));
}
Also used : EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) Optimizely(com.optimizely.ab.Optimizely) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)

Aggregations

DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)16 Test (org.junit.Test)14 Logger (org.slf4j.Logger)10 Context (android.content.Context)9 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)8 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 OptimizelyRuntimeException (com.optimizely.ab.OptimizelyRuntimeException)4 ErrorHandler (com.optimizely.ab.error.ErrorHandler)4 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)3 EventHandler (com.optimizely.ab.event.EventHandler)3 DatafileProjectConfig (com.optimizely.ab.config.DatafileProjectConfig)2 ProjectConfig (com.optimizely.ab.config.ProjectConfig)2 EventProcessor (com.optimizely.ab.event.EventProcessor)2 Before (org.junit.Before)2 AlarmManager (android.app.AlarmManager)1 Intent (android.content.Intent)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 Build (android.os.Build)1