Search in sources :

Example 1 with DatafileHandler

use of com.optimizely.ab.android.datafile_handler.DatafileHandler 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 2 with DatafileHandler

use of com.optimizely.ab.android.datafile_handler.DatafileHandler 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 3 with DatafileHandler

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

the class OptimizelyManagerTest method initializeSyncWithEnvironment.

@Test
public void initializeSyncWithEnvironment() {
    Logger logger = mock(Logger.class);
    DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
    EventHandler eventHandler = mock(DefaultEventHandler.class);
    EventProcessor eventProcessor = mock(EventProcessor.class);
    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
        */
    optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getTargetContext(), R.raw.datafile);
    assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getInstrumentation().getTargetContext()), false);
    assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey));
    assertNotNull(optimizelyManager.getOptimizely());
    assertNotNull(optimizelyManager.getDatafileHandler());
    optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getTargetContext(), (Integer) null);
    verify(logger).error(eq("Invalid datafile resource ID."));
}
Also used : EventProcessor(com.optimizely.ab.event.EventProcessor) EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) Logger(org.slf4j.Logger) DatafileHandler(com.optimizely.ab.android.datafile_handler.DatafileHandler) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Test(org.junit.Test)

Aggregations

DatafileHandler (com.optimizely.ab.android.datafile_handler.DatafileHandler)3 DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)3 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)3 EventHandler (com.optimizely.ab.event.EventHandler)3 EventProcessor (com.optimizely.ab.event.EventProcessor)3 Test (org.junit.Test)3 Logger (org.slf4j.Logger)3 Context (android.content.Context)2 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)2 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)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 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1 SdkSuppress (androidx.test.filters.SdkSuppress)1 InstrumentationRegistry (androidx.test.platform.app.InstrumentationRegistry)1 DatafileService (com.optimizely.ab.android.datafile_handler.DatafileService)1