Search in sources :

Example 6 with StoreBuilder

use of com.nytimes.android.external.store3.base.impl.StoreBuilder 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);
    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));
    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 : BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 7 with StoreBuilder

use of com.nytimes.android.external.store3.base.impl.StoreBuilder 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).blockingGet();
    validateFoo(result);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : Foo(com.nytimes.android.external.store3.middleware.jackson.data.Foo) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 8 with StoreBuilder

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

the class JacksonSourceParserStoreTest method testDefaultJacksonSourceParser.

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

Example 9 with StoreBuilder

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

Example 10 with StoreBuilder

use of com.nytimes.android.external.store3.base.impl.StoreBuilder 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).blockingGet();
    validateFoo(result);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : Foo(com.nytimes.android.external.store3.middleware.jackson.data.Foo) BarCode(com.nytimes.android.external.store3.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.store3.base.impl.BarCode)13 Test (org.junit.Test)13 Foo (com.nytimes.android.external.store3.middleware.jackson.data.Foo)6 BufferedSource (okio.BufferedSource)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 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Nonnull (javax.annotation.Nonnull)2 Before (org.junit.Before)2 RunWith (org.junit.runner.RunWith)2 Mock (org.mockito.Mock)2