Search in sources :

Example 16 with Store

use of com.nytimes.android.external.store3.base.impl.Store 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 17 with Store

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

the class StoreBuilderTest method testBuildersBuildWithCorrectTypes.

@Test
public void testBuildersBuildWithCorrectTypes() {
    // test  is checking whether types are correct in builders
    Store<Date, Integer> store = StoreBuilder.<Integer, String, Date>parsedWithKey().fetcher(key -> Single.just(String.valueOf(key))).persister(new Persister<String, Integer>() {

        @Nonnull
        @Override
        public Maybe<String> read(@Nonnull Integer key) {
            return Maybe.just(String.valueOf(key));
        }

        @Nonnull
        @Override
        public Single<Boolean> write(@Nonnull Integer key, @Nonnull String s) {
            return Single.just(true);
        }
    }).parser(s -> DATE).open();
    Store<Date, BarCode> barCodeStore = StoreBuilder.<Date>barcode().fetcher(new Fetcher<Date, BarCode>() {

        @Nonnull
        @Override
        public Single<Date> fetch(@Nonnull BarCode barCode) {
            return Single.just(DATE);
        }
    }).open();
    Store<Date, Integer> keyStore = StoreBuilder.<Integer, Date>key().fetcher(new Fetcher<Date, Integer>() {

        @Nonnull
        @Override
        public Single<Date> fetch(@Nonnull Integer key) {
            return Single.just(DATE);
        }
    }).open();
    Date result = store.get(5).blockingGet();
    result = barCodeStore.get(new BarCode("test", "5")).blockingGet();
    result = keyStore.get(5).blockingGet();
    assertThat(result).isNotNull();
}
Also used : Date(java.util.Date) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Maybe(io.reactivex.Maybe) StoreBuilder(com.nytimes.android.external.store3.base.impl.StoreBuilder) Fetcher(com.nytimes.android.external.store3.base.Fetcher) Persister(com.nytimes.android.external.store3.base.Persister) Test(org.junit.Test) Store(com.nytimes.android.external.store3.base.impl.Store) Nonnull(javax.annotation.Nonnull) Single(io.reactivex.Single) Maybe(io.reactivex.Maybe) Nonnull(javax.annotation.Nonnull) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Date(java.util.Date) Single(io.reactivex.Single) Fetcher(com.nytimes.android.external.store3.base.Fetcher) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)16 BufferedSource (okio.BufferedSource)7 Foo (com.nytimes.android.external.store3.middleware.jackson.data.Foo)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 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)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 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Nonnull (javax.annotation.Nonnull)2 Before (org.junit.Before)2 RunWith (org.junit.runner.RunWith)2