Search in sources :

Example 1 with NormalizedCache

use of com.apollographql.apollo.cache.normalized.NormalizedCache in project apollo-android by apollographql.

the class CacheHeadersTest method testHeadersReceived.

@Test
@SuppressWarnings("CheckReturnValue")
public void testHeadersReceived() throws ApolloException, IOException {
    final AtomicBoolean hasHeader = new AtomicBoolean();
    final NormalizedCache normalizedCache = new NormalizedCache() {

        @Nullable
        @Override
        public Record loadRecord(@NonNull String key, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return null;
        }

        @Nonnull
        @Override
        public Set<String> merge(@NonNull Record record, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return emptySet();
        }

        @Override
        public void clearAll() {
        }

        @Override
        public boolean remove(@Nonnull CacheKey cacheKey) {
            return false;
        }

        @Nonnull
        @Override
        protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }
    };
    final NormalizedCacheFactory<NormalizedCache> cacheFactory = new NormalizedCacheFactory<NormalizedCache>() {

        @Override
        public NormalizedCache create(RecordFieldJsonAdapter recordFieldAdapter) {
            return normalizedCache;
        }
    };
    ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).build();
    server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
    CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
    Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
    assertThat(hasHeader.get()).isTrue();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Nonnull(javax.annotation.Nonnull) Dispatcher(okhttp3.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordFieldJsonAdapter(com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter) ApolloCacheHeaders(com.apollographql.apollo.cache.ApolloCacheHeaders) CacheHeaders(com.apollographql.apollo.cache.CacheHeaders) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) NonNull(android.support.annotation.NonNull) NormalizedCacheFactory(com.apollographql.apollo.cache.normalized.NormalizedCacheFactory) Record(com.apollographql.apollo.cache.normalized.Record) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey) Test(org.junit.Test)

Example 2 with NormalizedCache

use of com.apollographql.apollo.cache.normalized.NormalizedCache in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testDualCacheMultipleRecord.

@Test
public void testDualCacheMultipleRecord() {
    LruNormalizedCacheFactory secondaryCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION);
    NormalizedCache primaryCache = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION).chain(secondaryCacheFactory).createChain(basicFieldAdapter);
    Record.Builder recordBuilder = Record.builder("root1");
    recordBuilder.addField("bar", "bar");
    final Record record1 = recordBuilder.build();
    recordBuilder = Record.builder("root2");
    recordBuilder.addField("bar", "bar");
    final Record record2 = recordBuilder.build();
    recordBuilder = Record.builder("root3");
    recordBuilder.addField("bar", "bar");
    final Record record3 = recordBuilder.build();
    Collection<Record> records = Arrays.asList(record1, record2, record3);
    Collection<String> keys = Arrays.asList(record1.key(), record2.key(), record3.key());
    primaryCache.merge(records, CacheHeaders.NONE);
    assertThat(primaryCache.loadRecords(keys, CacheHeaders.NONE).size()).isEqualTo(3);
    // verify write through behavior
    assertThat(primaryCache.loadRecords(keys, CacheHeaders.NONE).size()).isEqualTo(3);
    assertThat(primaryCache.nextCache().get().loadRecords(keys, CacheHeaders.NONE).size()).isEqualTo(3);
}
Also used : NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 3 with NormalizedCache

use of com.apollographql.apollo.cache.normalized.NormalizedCache in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testClearAll.

@Test
public void testClearAll() {
    LruNormalizedCacheFactory secondaryCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION);
    NormalizedCache primaryCacheStore = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION).chain(secondaryCacheFactory).createChain(basicFieldAdapter);
    Record record = Record.builder("key").build();
    primaryCacheStore.merge(record, CacheHeaders.NONE);
    primaryCacheStore.clearAll();
    assertThat(primaryCacheStore.loadRecord("key", CacheHeaders.NONE)).isNull();
}
Also used : NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 4 with NormalizedCache

use of com.apollographql.apollo.cache.normalized.NormalizedCache in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testClearSecondaryCache.

@Test
public void testClearSecondaryCache() {
    LruNormalizedCacheFactory secondaryCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION);
    NormalizedCache primaryCache = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION).chain(secondaryCacheFactory).createChain(basicFieldAdapter);
    Record record = Record.builder("key").build();
    primaryCache.merge(record, CacheHeaders.NONE);
    primaryCache.nextCache().get().clearAll();
    assertThat(primaryCache.nextCache().get().loadRecord("key", CacheHeaders.NONE)).isNull();
}
Also used : NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 5 with NormalizedCache

use of com.apollographql.apollo.cache.normalized.NormalizedCache in project apollo-android by apollographql.

the class CacheHeadersTest method testDefaultHeadersReceived.

@Test
@SuppressWarnings("CheckReturnValue")
public void testDefaultHeadersReceived() throws Exception {
    final AtomicBoolean hasHeader = new AtomicBoolean();
    final NormalizedCache normalizedCache = new NormalizedCache() {

        @Nullable
        @Override
        public Record loadRecord(@NonNull String key, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return null;
        }

        @Nonnull
        @Override
        public Set<String> merge(@NonNull Record record, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return emptySet();
        }

        @Override
        public void clearAll() {
        }

        @Override
        public boolean remove(@Nonnull CacheKey cacheKey) {
            return false;
        }

        @Nonnull
        @Override
        protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }
    };
    final NormalizedCacheFactory<NormalizedCache> cacheFactory = new NormalizedCacheFactory<NormalizedCache>() {

        @Override
        public NormalizedCache create(RecordFieldJsonAdapter recordFieldAdapter) {
            return normalizedCache;
        }
    };
    CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
    ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).defaultCacheHeaders(cacheHeaders).build();
    server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
    Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
    assertThat(hasHeader.get()).isTrue();
}
Also used : Nonnull(javax.annotation.Nonnull) Dispatcher(okhttp3.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordFieldJsonAdapter(com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter) ApolloCacheHeaders(com.apollographql.apollo.cache.ApolloCacheHeaders) CacheHeaders(com.apollographql.apollo.cache.CacheHeaders) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) NonNull(android.support.annotation.NonNull) NormalizedCacheFactory(com.apollographql.apollo.cache.normalized.NormalizedCacheFactory) Record(com.apollographql.apollo.cache.normalized.Record) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey) Test(org.junit.Test)

Aggregations

NormalizedCache (com.apollographql.apollo.cache.normalized.NormalizedCache)8 Test (org.junit.Test)8 Record (com.apollographql.apollo.cache.normalized.Record)7 CacheHeaders (com.apollographql.apollo.cache.CacheHeaders)3 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)3 Nonnull (javax.annotation.Nonnull)3 NonNull (android.support.annotation.NonNull)2 ApolloCacheHeaders (com.apollographql.apollo.cache.ApolloCacheHeaders)2 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)2 RecordFieldJsonAdapter (com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter)2 HeroAndFriendsNamesQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Dispatcher (okhttp3.Dispatcher)2 Logger (com.apollographql.apollo.Logger)1 NamedCountDownLatch (com.apollographql.apollo.NamedCountDownLatch)1 ApolloLogger (com.apollographql.apollo.internal.ApolloLogger)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 Collections.emptySet (java.util.Collections.emptySet)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1