Search in sources :

Example 1 with BarCode

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

the class JacksonSourceParserStoreTest method testDefaultJacksonSourceParser.

@Test
public void testDefaultJacksonSourceParser() {
    Parser<BufferedSource, Foo> parser = JacksonParserFactory.createSourceParser(Foo.class);
    Store<Foo, BarCode> store = StoreBuilder.<BarCode, BufferedSource, Foo>parsedWithKey().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo result = store.get(barCode).toBlocking().first();
    validateFoo(result);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : Foo(com.nytimes.android.external.store.middleware.jackson.data.Foo) BarCode(com.nytimes.android.external.store.base.impl.BarCode) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 2 with BarCode

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

the class MoshiSourceParserTest method testSourceParser.

@Test
public void testSourceParser() throws Exception {
    Parser<BufferedSource, Foo> parser = MoshiParserFactory.createSourceParser(Foo.class);
    Store<Foo, BarCode> store = ParsingStoreBuilder.<BufferedSource, Foo>builder().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo result = store.get(barCode).toBlocking().first();
    assertEquals(result.number, 123);
    assertEquals(result.string, "abc");
    assertEquals(result.bars.size(), 2);
    assertEquals(result.bars.get(0).string, "def");
    assertEquals(result.bars.get(1).string, "ghi");
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : Foo(com.nytimes.android.external.store.middleware.moshi.data.Foo) BarCode(com.nytimes.android.external.store.base.impl.BarCode) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 3 with BarCode

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

the class ClearStoreTest method testClearSingleBarCode.

@Test
public void testClearSingleBarCode() {
    // one request should produce one call
    BarCode barcode = new BarCode("type", "key");
    when(persister.read(barcode)).thenReturn(//read from disk on get
    Observable.<Integer>empty()).thenReturn(//read from disk after fetching from network
    Observable.just(1)).thenReturn(//read from disk after clearing
    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));
    store.get(barcode).test().awaitTerminalEvent();
    assertThat(networkCalls.intValue()).isEqualTo(1);
    // after clearing the memory another call should be made
    store.clear(barcode);
    store.get(barcode).test().awaitTerminalEvent();
    verify(persister).clear(barcode);
    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 4 with BarCode

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

the class DontCacheErrorsTest method testStoreDoesntCacheErrors.

@Test
public void testStoreDoesntCacheErrors() throws InterruptedException {
    BarCode barcode = new BarCode("bar", "code");
    shouldThrow = true;
    store.get(barcode).test().awaitTerminalEvent().assertError(Exception.class);
    shouldThrow = false;
    store.get(barcode).test().awaitTerminalEvent().assertNoErrors();
}
Also used : BarCode(com.nytimes.android.external.store.base.impl.BarCode) Test(org.junit.Test)

Example 5 with BarCode

use of com.nytimes.android.external.store.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(new Fetcher<Integer, BarCode>() {

        @Nonnull
        @Override
        public Observable<Integer> fetch(@Nonnull BarCode barCode) {
            return Observable.fromCallable(new Callable<Integer>() {

                @Override
                public Integer call() {
                    return networkCalls.incrementAndGet();
                }
            });
        }
    }).persister(persister).open();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nonnull(javax.annotation.Nonnull) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Fetcher(com.nytimes.android.external.store.base.Fetcher) Before(org.junit.Before)

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