Search in sources :

Example 1 with BarCode

use of com.nytimes.android.external.store3.base.impl.BarCode in project Store by NYTimes.

the class ClearStoreTest method testClearAllBarCodes.

@Test
public void testClearAllBarCodes() {
    BarCode barcode1 = new BarCode("type1", "key1");
    BarCode barcode2 = new BarCode("type2", "key2");
    when(persister.read(barcode1)).thenReturn(// read from disk
    Maybe.<Integer>empty()).thenReturn(// read from disk after fetching from network
    Maybe.just(1)).thenReturn(// read from disk after clearing disk cache
    Maybe.<Integer>empty()).thenReturn(// read from disk after making additional network call
    Maybe.just(1));
    when(persister.write(barcode1, 1)).thenReturn(Single.just(true));
    when(persister.write(barcode1, 2)).thenReturn(Single.just(true));
    when(persister.read(barcode2)).thenReturn(// read from disk
    Maybe.<Integer>empty()).thenReturn(// read from disk after fetching from network
    Maybe.just(1)).thenReturn(// read from disk after clearing disk cache
    Maybe.<Integer>empty()).thenReturn(// read from disk after making additional network call
    Maybe.just(1));
    when(persister.write(barcode2, 1)).thenReturn(Single.just(true));
    when(persister.write(barcode2, 2)).thenReturn(Single.just(true));
    // each request should produce one call
    store.get(barcode1).test().awaitTerminalEvent();
    store.get(barcode2).test().awaitTerminalEvent();
    assertThat(networkCalls.intValue()).isEqualTo(2);
    store.clear();
    // after everything is cleared each request should produce another 2 calls
    store.get(barcode1).test().awaitTerminalEvent();
    store.get(barcode2).test().awaitTerminalEvent();
    assertThat(networkCalls.intValue()).isEqualTo(4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Test(org.junit.Test)

Example 2 with BarCode

use of com.nytimes.android.external.store3.base.impl.BarCode in project Store by NYTimes.

the class ClearStoreTest method setUp.

@Before
public void setUp() {
    networkCalls = new AtomicInteger(0);
    store = StoreBuilder.<Integer>barcode().fetcher(barCode -> Single.fromCallable(() -> networkCalls.incrementAndGet())).persister(persister).open();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mock(org.mockito.Mock) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Maybe(io.reactivex.Maybe) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Single(io.reactivex.Single) Mockito.verify(org.mockito.Mockito.verify) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreBuilder(com.nytimes.android.external.store3.base.impl.StoreBuilder) Store(com.nytimes.android.external.store3.base.impl.Store) ClearingPersister(com.nytimes.android.external.store3.GetRefreshingTest.ClearingPersister) Before(org.junit.Before) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before)

Example 3 with BarCode

use of com.nytimes.android.external.store3.base.impl.BarCode in project Store by NYTimes.

the class GetRefreshingTest method setUp.

@Before
public void setUp() {
    networkCalls = new AtomicInteger(0);
    store = StoreBuilder.<Integer>barcode().fetcher(barCode -> Single.fromCallable(() -> networkCalls.incrementAndGet())).persister(persister).open();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Clearable(com.nytimes.android.external.store3.base.Clearable) Mock(org.mockito.Mock) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Maybe(io.reactivex.Maybe) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) Mockito.when(org.mockito.Mockito.when) Single(io.reactivex.Single) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreBuilder(com.nytimes.android.external.store3.base.impl.StoreBuilder) Persister(com.nytimes.android.external.store3.base.Persister) Store(com.nytimes.android.external.store3.base.impl.Store) Nonnull(javax.annotation.Nonnull) Before(org.junit.Before) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before)

Example 4 with BarCode

use of com.nytimes.android.external.store3.base.impl.BarCode in project Store by NYTimes.

the class GetRefreshingTest method testRefreshOnClear.

@Test
public void testRefreshOnClear() {
    BarCode barcode = new BarCode("type", "key");
    when(persister.read(barcode)).thenReturn(// read from disk
    Maybe.<Integer>empty()).thenReturn(// read from disk after fetching from network
    Maybe.just(1)).thenReturn(// read from disk after clearing disk cache
    Maybe.<Integer>empty()).thenReturn(// read from disk after making additional network call
    Maybe.just(1));
    when(persister.write(barcode, 1)).thenReturn(Single.just(true));
    when(persister.write(barcode, 2)).thenReturn(Single.just(true));
    TestObserver<Integer> refreshingObservable = store.getRefreshing(barcode).test();
    refreshingObservable.assertValueCount(1);
    assertThat(networkCalls.intValue()).isEqualTo(1);
    // clearing the store should produce another network call
    store.clear(barcode);
    refreshingObservable.assertValueCount(2);
    assertThat(networkCalls.intValue()).isEqualTo(2);
    store.get(barcode).test().awaitTerminalEvent();
    refreshingObservable.assertValueCount(2);
    assertThat(networkCalls.intValue()).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Test(org.junit.Test)

Example 5 with BarCode

use of com.nytimes.android.external.store3.base.impl.BarCode in project Store by NYTimes.

the class GetRefreshingTest method testRefreshOnClearAll.

@Test
public void testRefreshOnClearAll() {
    BarCode barcode1 = new BarCode("type", "key");
    BarCode barcode2 = new BarCode("type", "key2");
    when(persister.read(barcode1)).thenReturn(// read from disk
    Maybe.<Integer>empty()).thenReturn(// read from disk after fetching from network
    Maybe.just(1)).thenReturn(// read from disk after clearing disk cache
    Maybe.<Integer>empty()).thenReturn(// read from disk after making additional network call
    Maybe.just(1));
    when(persister.write(barcode1, 1)).thenReturn(Single.just(true));
    when(persister.write(barcode1, 2)).thenReturn(Single.just(true));
    when(persister.read(barcode2)).thenReturn(// read from disk
    Maybe.<Integer>empty()).thenReturn(// read from disk after fetching from network
    Maybe.just(1)).thenReturn(// read from disk after clearing disk cache
    Maybe.<Integer>empty()).thenReturn(// read from disk after making additional network call
    Maybe.just(1));
    when(persister.write(barcode2, 1)).thenReturn(Single.just(true));
    when(persister.write(barcode2, 2)).thenReturn(Single.just(true));
    TestObserver<Integer> testObservable1 = store.getRefreshing(barcode1).test();
    TestObserver<Integer> testObservable2 = store.getRefreshing(barcode2).test();
    testObservable1.assertValueCount(1);
    testObservable2.assertValueCount(1);
    assertThat(networkCalls.intValue()).isEqualTo(2);
    store.clear();
    assertThat(networkCalls.intValue()).isEqualTo(4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Test(org.junit.Test)

Aggregations

BarCode (com.nytimes.android.external.store3.base.impl.BarCode)33 Test (org.junit.Test)31 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BufferedSource (okio.BufferedSource)7 Foo (com.nytimes.android.external.store3.middleware.jackson.data.Foo)6 Gson (com.google.gson.Gson)4 Single (io.reactivex.Single)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 Store (com.nytimes.android.external.store3.base.impl.Store)3 StoreBuilder (com.nytimes.android.external.store3.base.impl.StoreBuilder)3 Maybe (io.reactivex.Maybe)3 Nonnull (javax.annotation.Nonnull)3 Before (org.junit.Before)3 RunWith (org.junit.runner.RunWith)3 Mock (org.mockito.Mock)3 Mockito.when (org.mockito.Mockito.when)3 MockitoJUnitRunner (org.mockito.runners.MockitoJUnitRunner)3 Clearable (com.nytimes.android.external.store3.base.Clearable)2 Persister (com.nytimes.android.external.store3.base.Persister)2