Search in sources :

Example 11 with BarCode

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

the class ClearStoreMemoryTest method testClearAllBarCodes.

@Test
public void testClearAllBarCodes() {
    BarCode b1 = new BarCode("type1", "key1");
    BarCode b2 = new BarCode("type2", "key2");
    //each request should produce one call
    store.get(b1).test().awaitTerminalEvent();
    store.get(b2).test().awaitTerminalEvent();
    assertThat(networkCalls).isEqualTo(2);
    store.clearMemory();
    //after everything is cleared each request should produce another 2 calls
    store.get(b1).test().awaitTerminalEvent();
    store.get(b2).test().awaitTerminalEvent();
    assertThat(networkCalls).isEqualTo(4);
}
Also used : BarCode(com.nytimes.android.external.store.base.impl.BarCode) Test(org.junit.Test)

Example 12 with BarCode

use of com.nytimes.android.external.store.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(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)

Example 13 with BarCode

use of com.nytimes.android.external.store.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
    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));
    // 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.store.base.impl.BarCode) Test(org.junit.Test)

Example 14 with BarCode

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

the class JacksonReaderParserStoreTest method testDefaultJacksonReaderParser.

@Test
public void testDefaultJacksonReaderParser() {
    Parser<Reader, Foo> parser = JacksonParserFactory.createReaderParser(Foo.class);
    Store<Foo, BarCode> store = StoreBuilder.<BarCode, Reader, 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) Reader(java.io.Reader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 15 with BarCode

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

the class JacksonReaderParserStoreTest method testCustomJsonFactoryReaderParser.

@Test
public void testCustomJsonFactoryReaderParser() {
    JsonFactory jsonFactory = new JsonFactory();
    Parser<Reader, Foo> parser = JacksonParserFactory.createReaderParser(jsonFactory, Foo.class);
    Store<Foo, BarCode> store = StoreBuilder.<BarCode, Reader, 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) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Reader(java.io.Reader) StringReader(java.io.StringReader) 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