Search in sources :

Example 6 with Persister

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

the class StoreUtil method persisterIsStale.

static <Raw, Key> boolean persisterIsStale(@Nonnull Key key, Persister<Raw, Key> persister) {
    if (persister instanceof RecordProvider) {
        RecordProvider<Key> provider = (RecordProvider<Key>) persister;
        RecordState recordState = provider.getRecordState(key);
        return recordState == STALE;
    }
    return false;
}
Also used : RecordProvider(com.nytimes.android.external.store3.base.RecordProvider) RecordState(com.nytimes.android.external.store3.base.RecordState)

Example 7 with Persister

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

the class ClearStoreTest method setUp.

@Before
public void setUp() {
    networkCalls = new AtomicInteger(0);
    store = StoreBuilder.<Integer>barcode().fetcher(barCode -> Single.fromCallable(() -> networkCalls.incrementAndGet())).persister(persister).open();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mock(org.mockito.Mock) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Maybe(io.reactivex.Maybe) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Single(io.reactivex.Single) Mockito.verify(org.mockito.Mockito.verify) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreBuilder(com.nytimes.android.external.store3.base.impl.StoreBuilder) Store(com.nytimes.android.external.store3.base.impl.Store) ClearingPersister(com.nytimes.android.external.store3.GetRefreshingTest.ClearingPersister) Before(org.junit.Before) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before)

Example 8 with Persister

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

the class GetRefreshingTest method setUp.

@Before
public void setUp() {
    networkCalls = new AtomicInteger(0);
    store = StoreBuilder.<Integer>barcode().fetcher(barCode -> Single.fromCallable(() -> networkCalls.incrementAndGet())).persister(persister).open();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Clearable(com.nytimes.android.external.store3.base.Clearable) Mock(org.mockito.Mock) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Maybe(io.reactivex.Maybe) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) Mockito.when(org.mockito.Mockito.when) Single(io.reactivex.Single) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreBuilder(com.nytimes.android.external.store3.base.impl.StoreBuilder) Persister(com.nytimes.android.external.store3.base.Persister) Store(com.nytimes.android.external.store3.base.impl.Store) Nonnull(javax.annotation.Nonnull) Before(org.junit.Before) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before)

Example 9 with Persister

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

the class NoopPersisterTest method writeReadTest.

@Test
public void writeReadTest() {
    BarCode barCode = new BarCode("key", "value");
    NoopPersister<String, BarCode> persister = NoopPersister.create();
    boolean success = persister.write(barCode, "foo").blockingGet();
    assertThat(success).isTrue();
    String rawValue = persister.read(barCode).blockingGet();
    assertThat(rawValue).isEqualTo("foo");
}
Also used : BarCode(com.nytimes.android.external.store3.base.impl.BarCode) Test(org.junit.Test)

Example 10 with Persister

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

BarCode (com.nytimes.android.external.store3.base.impl.BarCode)17 Test (org.junit.Test)17 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