Search in sources :

Example 21 with BarCode

use of com.nytimes.android.external.store.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
    Observable.<Integer>empty()).thenReturn(//read from disk after fetching from network
    Observable.just(1)).thenReturn(//read from disk after clearing disk cache
    Observable.<Integer>empty()).thenReturn(//read from disk after making additional network call
    Observable.just(1));
    when(persister.write(barcode, 1)).thenReturn(Observable.just(true));
    when(persister.write(barcode, 2)).thenReturn(Observable.just(true));
    AssertableSubscriber<Integer> refreshingObservable = store.getRefreshing(barcode).test();
    assertThat(refreshingObservable.getValueCount()).isEqualTo(1);
    assertThat(networkCalls.intValue()).isEqualTo(1);
    //clearing the store should produce another network call
    store.clear(barcode);
    assertThat(refreshingObservable.getValueCount()).isEqualTo(2);
    assertThat(networkCalls.intValue()).isEqualTo(2);
    store.get(barcode).test().awaitTerminalEvent();
    assertThat(refreshingObservable.getValueCount()).isEqualTo(2);
    assertThat(networkCalls.intValue()).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Test(org.junit.Test)

Example 22 with BarCode

use of com.nytimes.android.external.store.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
    Observable.<Integer>empty()).thenReturn(//read from disk after fetching from network
    Observable.just(1)).thenReturn(//read from disk after clearing disk cache
    Observable.<Integer>empty()).thenReturn(//read from disk after making additional network call
    Observable.just(1));
    when(persister.write(barcode1, 1)).thenReturn(Observable.just(true));
    when(persister.write(barcode1, 2)).thenReturn(Observable.just(true));
    when(persister.read(barcode2)).thenReturn(//read from disk
    Observable.<Integer>empty()).thenReturn(//read from disk after fetching from network
    Observable.just(1)).thenReturn(//read from disk after clearing disk cache
    Observable.<Integer>empty()).thenReturn(//read from disk after making additional network call
    Observable.just(1));
    when(persister.write(barcode2, 1)).thenReturn(Observable.just(true));
    when(persister.write(barcode2, 2)).thenReturn(Observable.just(true));
    AssertableSubscriber<Integer> testObservable1 = store.getRefreshing(barcode1).test();
    AssertableSubscriber<Integer> testObservable2 = store.getRefreshing(barcode2).test();
    assertThat(testObservable1.getValueCount()).isEqualTo(1);
    assertThat(testObservable2.getValueCount()).isEqualTo(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.store.base.impl.BarCode) Test(org.junit.Test)

Example 23 with BarCode

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

the class SequentialTest method sequentially.

@Test
public void sequentially() {
    BarCode b = new BarCode("one", "two");
    store.get(b).test().awaitTerminalEvent();
    store.get(b).test().awaitTerminalEvent();
    assertThat(networkCalls).isEqualTo(1);
}
Also used : BarCode(com.nytimes.android.external.store.base.impl.BarCode) Test(org.junit.Test)

Example 24 with BarCode

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

the class StoreBuilderTest method testBuildersBuildWithCorrectTypes.

@Test
public void testBuildersBuildWithCorrectTypes() {
    //test  is checking whether types are correct in builders
    Store<Date, Integer> store = StoreBuilder.<Integer, String, Date>parsedWithKey().fetcher(new Fetcher<String, Integer>() {

        @Nonnull
        @Override
        public Observable<String> fetch(@Nonnull Integer key) {
            return Observable.just(String.valueOf(key));
        }
    }).persister(new Persister<String, Integer>() {

        @Nonnull
        @Override
        public Observable<String> read(@Nonnull Integer key) {
            return Observable.just(String.valueOf(key));
        }

        @Nonnull
        @Override
        public Observable<Boolean> write(@Nonnull Integer key, @Nonnull String s) {
            return Observable.empty();
        }
    }).parser(new Parser<String, Date>() {

        @Override
        public Date call(String s) {
            return DATE;
        }
    }).open();
    Store<Date, BarCode> barCodeStore = StoreBuilder.<Date>barcode().fetcher(new Fetcher<Date, BarCode>() {

        @Nonnull
        @Override
        public Observable<Date> fetch(@Nonnull BarCode barCode) {
            return Observable.just(DATE);
        }
    }).open();
    Store<Date, Integer> keyStore = StoreBuilder.<Integer, Date>key().fetcher(new Fetcher<Date, Integer>() {

        @Nonnull
        @Override
        public Observable<Date> fetch(@Nonnull Integer key) {
            return Observable.just(DATE);
        }
    }).open();
    Date result = store.get(5).toBlocking().first();
    result = barCodeStore.get(new BarCode("test", "5")).toBlocking().first();
    result = keyStore.get(5).toBlocking().first();
    assertThat(result).isNotNull();
}
Also used : Nonnull(javax.annotation.Nonnull) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Fetcher(com.nytimes.android.external.store.base.Fetcher) Date(java.util.Date) Observable(rx.Observable) Parser(com.nytimes.android.external.store.base.Parser) Test(org.junit.Test)

Aggregations

BarCode (com.nytimes.android.external.store.base.impl.BarCode)24 Test (org.junit.Test)22 BufferedSource (okio.BufferedSource)7 Foo (com.nytimes.android.external.store.middleware.jackson.data.Foo)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Gson (com.google.gson.Gson)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 Fetcher (com.nytimes.android.external.store.base.Fetcher)3 Nonnull (javax.annotation.Nonnull)3 GsonSourceParser (com.nytimes.android.external.store.middleware.GsonSourceParser)2 Foo (com.nytimes.android.external.store.middleware.moshi.data.Foo)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Before (org.junit.Before)2 TypeToken (com.google.gson.reflect.TypeToken)1 Parser (com.nytimes.android.external.store.base.Parser)1 Date (java.util.Date)1 List (java.util.List)1 Observable (rx.Observable)1