Search in sources :

Example 21 with BarCode

use of com.nytimes.android.external.store3.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).blockingGet();
    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.store3.middleware.moshi.data.Foo) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 22 with BarCode

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

the class GsonSourceListParserTest method testSimple.

@Test
public void testSimple() {
    MockitoAnnotations.initMocks(this);
    Parser<BufferedSource, List<Foo>> parser = GsonParserFactory.createSourceParser(new Gson(), new TypeToken<List<Foo>>() {
    }.getType());
    Store<List<Foo>, BarCode> simpleStore = StoreBuilder.<BarCode, BufferedSource, List<Foo>>parsedWithKey().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo foo = new Foo("a");
    Foo foo2 = new Foo("b");
    Foo foo3 = new Foo("c");
    List<Foo> data = Arrays.asList(foo, foo2, foo3);
    String sourceData = new Gson().toJson(data);
    BufferedSource source = source(sourceData);
    Single<BufferedSource> value = Single.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(persister.read(barCode)).thenReturn(Maybe.<BufferedSource>empty()).thenReturn(value.toMaybe());
    when(persister.write(barCode, source)).thenReturn(Single.just(true));
    List<Foo> result = simpleStore.get(barCode).blockingGet();
    assertThat(result.get(0).value).isEqualTo("a");
    assertThat(result.get(1).value).isEqualTo("b");
    assertThat(result.get(2).value).isEqualTo("c");
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Gson(com.google.gson.Gson) List(java.util.List) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 23 with BarCode

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

the class ClearStoreMemoryTest method testClearSingleBarCode.

@Test
public void testClearSingleBarCode() {
    // one request should produce one call
    BarCode barcode = new BarCode("type", "key");
    store.get(barcode).test().awaitTerminalEvent();
    assertThat(networkCalls).isEqualTo(1);
    // after clearing the memory another call should be made
    store.clearMemory(barcode);
    store.get(barcode).test().awaitTerminalEvent();
    assertThat(networkCalls).isEqualTo(2);
}
Also used : BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Test(org.junit.Test)

Example 24 with BarCode

use of com.nytimes.android.external.store3.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
    Maybe.<Integer>empty()).thenReturn(// read from disk after fetching from network
    Maybe.just(1)).thenReturn(// read from disk after clearing
    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));
    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.store3.base.impl.BarCode) Test(org.junit.Test)

Example 25 with BarCode

use of com.nytimes.android.external.store3.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().assertTerminated().assertError(Exception.class).awaitTerminalEvent();
    shouldThrow = false;
    store.get(barcode).test().assertNoErrors().awaitTerminalEvent();
}
Also used : BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Test(org.junit.Test)

Aggregations

BarCode (com.nytimes.android.external.store3.base.impl.BarCode)30 Test (org.junit.Test)28 BufferedSource (okio.BufferedSource)7 Foo (com.nytimes.android.external.store3.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 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 Single (io.reactivex.Single)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Persister (com.nytimes.android.external.store3.base.Persister)2 GsonSourceParser (com.nytimes.android.external.store3.middleware.GsonSourceParser)2 Foo (com.nytimes.android.external.store3.middleware.moshi.data.Foo)2 RedditData (com.nytimes.android.sample.data.model.RedditData)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Nonnull (javax.annotation.Nonnull)2 Before (org.junit.Before)2