Search in sources :

Example 16 with BarCode

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

the class JacksonSourceParserStoreTest method testCustomJsonFactorySourceParser.

@Test
public void testCustomJsonFactorySourceParser() {
    JsonFactory jsonFactory = new JsonFactory();
    Parser<BufferedSource, Foo> parser = JacksonParserFactory.createSourceParser(jsonFactory, 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) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 17 with BarCode

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

the class JacksonStringParserStoreTest method testCustomJsonFactoryStringParser.

@Test
public void testCustomJsonFactoryStringParser() {
    JsonFactory jsonFactory = new JsonFactory();
    Parser<String, Foo> parser = JacksonParserFactory.createStringParser(jsonFactory, Foo.class);
    Store<Foo, BarCode> store = StoreBuilder.<BarCode, String, 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) Test(org.junit.Test)

Example 18 with BarCode

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

the class JacksonStringParserStoreTest method testDefaultJacksonStringParser.

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

Example 19 with BarCode

use of com.nytimes.android.external.store.base.impl.BarCode 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);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(diskDAO.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(diskDAO.write(barCode, source)).thenReturn(Observable.just(true));
    Foo result = store.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    result = store.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : GsonSourceParser(com.nytimes.android.external.store.middleware.GsonSourceParser) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 20 with BarCode

use of com.nytimes.android.external.store.base.impl.BarCode 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);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(fileReader.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(fileWriter.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 : GsonSourceParser(com.nytimes.android.external.store.middleware.GsonSourceParser) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) 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