Search in sources :

Example 6 with BarCode

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

the class SequentialTest method parallel.

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

Example 7 with BarCode

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

the class MoshiStringParserStoreTest method testMoshiString.

@Test
public void testMoshiString() {
    Store<Foo, BarCode> store = ParsingStoreBuilder.<String, Foo>builder().persister(persister).fetcher(fetcher).parser(MoshiParserFactory.createStringParser(Foo.class)).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) Test(org.junit.Test)

Example 8 with BarCode

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

the class GenericParserStoreTest method testSimple.

@Test
public void testSimple() {
    MockitoAnnotations.initMocks(this);
    Parser<BufferedSource, Foo> parser = GsonParserFactory.createSourceParser(new Gson(), Foo.class);
    Store<Foo, BarCode> simpleStore = StoreBuilder.<BarCode, BufferedSource, Foo>parsedWithKey().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo foo = new Foo();
    foo.bar = barCode.getKey();
    String sourceData = new Gson().toJson(foo);
    BufferedSource source = source(sourceData);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(persister.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(persister.write(barCode, source)).thenReturn(Observable.just(true));
    Foo result = simpleStore.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    result = simpleStore.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 9 with BarCode

use of com.nytimes.android.external.store.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);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(persister.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(persister.write(barCode, source)).thenReturn(Observable.just(true));
    List<Foo> result = simpleStore.get(barCode).toBlocking().first();
    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.store.base.impl.BarCode) Gson(com.google.gson.Gson) List(java.util.List) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 10 with BarCode

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