use of com.apollographql.apollo.cache.CacheHeaders 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();
}
use of com.apollographql.apollo.cache.CacheHeaders 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();
}
use of com.apollographql.apollo.cache.CacheHeaders in project apollo-android by apollographql.
the class ApolloStoreTest method storeClearAllCallsNormalizedCacheClearAll.
@Test
public void storeClearAllCallsNormalizedCacheClearAll() throws Exception {
final NamedCountDownLatch latch = new NamedCountDownLatch("storeClearAllCallsNormalizedCacheClearAll", 1);
final RealApolloStore realApolloStore = new RealApolloStore(new NormalizedCache() {
@Nullable
@Override
public Record loadRecord(@Nonnull String key, @Nonnull CacheHeaders cacheHeaders) {
return null;
}
@Nonnull
@Override
public Set<String> merge(@Nonnull Record record, @Nonnull CacheHeaders cacheHeaders) {
return emptySet();
}
@Override
public void clearAll() {
latch.countDown();
}
@Override
public boolean remove(@Nonnull CacheKey cacheKey) {
return false;
}
@Nonnull
@Override
protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
return emptySet();
}
}, CacheKeyResolver.DEFAULT, new ScalarTypeAdapters(Collections.EMPTY_MAP), Executors.newSingleThreadExecutor(), new ApolloLogger(Optional.<Logger>absent()));
realApolloStore.clearAll().execute();
latch.awaitOrThrowWithTimeout(3, TimeUnit.SECONDS);
}
Aggregations