Search in sources :

Example 16 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileServiceTest method testBinding.

@Test
@Ignore
public void testBinding() throws TimeoutException {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    Intent intent = new Intent(context, DatafileService.class);
    IBinder binder = null;
    int it = 0;
    while ((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION) {
        it++;
    }
    final Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    Logger logger = mock(Logger.class);
    DatafileCache datafileCache = new DatafileCache("1", new Cache(targetContext, logger), logger);
    Client client = mock(Client.class);
    DatafileClient datafileClient = new DatafileClient(client, logger);
    DatafileLoadedListener datafileLoadedListener = mock(DatafileLoadedListener.class);
    DatafileService datafileService = ((DatafileService.LocalBinder) binder).getService();
    DatafileLoader datafileLoader = new DatafileLoader(targetContext, datafileClient, datafileCache, mock(Logger.class));
    datafileService.getDatafile("1", datafileLoader, datafileLoadedListener);
    assertTrue(datafileService.isBound());
}
Also used : Context(android.content.Context) Intent(android.content.Intent) Logger(org.slf4j.Logger) IBinder(android.os.IBinder) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileWorkerTest method testDatafileFetch.

@Test
public void testDatafileFetch() {
    DatafileWorker worker = mockDatafileWorker(sdkKey);
    worker.datafileLoader = mock(DatafileLoader.class);
    ListenableWorker.Result result = worker.doWork();
    DatafileConfig datafileConfig = new DatafileConfig(null, sdkKey);
    String datafileUrl = datafileConfig.getUrl();
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(context, logger), logger);
    verify(worker.datafileLoader).getDatafile(eq(datafileUrl), eq(datafileCache), eq(null));
    // success
    assertThat(result, is(ListenableWorker.Result.success()));
}
Also used : ListenableWorker(androidx.work.ListenableWorker) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Matchers.anyString(org.mockito.Matchers.anyString) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 18 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class BackgroundWatchersCacheTest method setup.

@Before
public void setup() {
    logger = mock(Logger.class);
    cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger);
    backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
}
Also used : Logger(org.slf4j.Logger) Cache(com.optimizely.ab.android.shared.Cache) Before(org.junit.Before)

Example 19 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class BackgroundWatchersCacheTest method testExceptionHandling.

@Test
public void testExceptionHandling() throws IOException {
    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("{");
    assertFalse(backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true));
    verify(logger).error(contains("Unable to update watching state for project id"), any(JSONException.class));
    assertFalse(backgroundWatchersCache.isWatching(new DatafileConfig("1", null)));
    verify(logger).error(contains("Unable check if project id is being watched"), any(JSONException.class));
    List<DatafileConfig> watchingProjectIds = backgroundWatchersCache.getWatchingDatafileConfigs();
    assertTrue(watchingProjectIds.isEmpty());
    verify(logger).error(contains("Unable to get watching project ids"), any(JSONException.class));
}
Also used : JSONException(org.json.JSONException) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 20 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileCacheTest method setup.

@Before
public void setup() {
    logger = mock(Logger.class);
    Cache cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger);
    datafileCache = new DatafileCache("1", cache, logger);
}
Also used : Logger(org.slf4j.Logger) Cache(com.optimizely.ab.android.shared.Cache) Before(org.junit.Before)

Aggregations

Cache (com.optimizely.ab.android.shared.Cache)28 Test (org.junit.Test)10 Logger (org.slf4j.Logger)9 Before (org.junit.Before)8 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)6 Client (com.optimizely.ab.android.shared.Client)5 ExecutorService (java.util.concurrent.ExecutorService)5 Context (android.content.Context)3 OptlyStorage (com.optimizely.ab.android.shared.OptlyStorage)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 JSONObject (org.json.JSONObject)3 SdkSuppress (androidx.test.filters.SdkSuppress)2 JSONException (org.json.JSONException)2 Intent (android.content.Intent)1 FileObserver (android.os.FileObserver)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1 ListenableWorker (androidx.work.ListenableWorker)1 File (java.io.File)1