Search in sources :

Example 1 with GsonSourceParser

use of com.nytimes.android.external.store3.middleware.GsonSourceParser in project Store by NYTimes.

the class SourceDiskDaoStoreTest method testSimple.

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

Example 2 with GsonSourceParser

use of com.nytimes.android.external.store3.middleware.GsonSourceParser in project Store by NYTimes.

the class SourceFilerReaderWriterStoreTest method testSimple.

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

Aggregations

Gson (com.google.gson.Gson)2 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)2 GsonSourceParser (com.nytimes.android.external.store3.middleware.GsonSourceParser)2 BufferedSource (okio.BufferedSource)2 Test (org.junit.Test)2