use of com.nytimes.android.external.store3.base.Fetcher 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);
}
use of com.nytimes.android.external.store3.base.Fetcher 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);
}
use of com.nytimes.android.external.store3.base.Fetcher in project Store by NYTimes.
the class MoshiSourceParserTest method testSourceParser.
@Test
public void testSourceParser() throws Exception {
Parser<BufferedSource, Foo> parser = MoshiParserFactory.createSourceParser(Foo.class);
Store<Foo, BarCode> store = ParsingStoreBuilder.<BufferedSource, Foo>builder().persister(persister).fetcher(fetcher).parser(parser).open();
Foo result = store.get(barCode).blockingGet();
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);
}
use of com.nytimes.android.external.store3.base.Fetcher 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();
}
use of com.nytimes.android.external.store3.base.Fetcher 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);
}
Aggregations