use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class RxApolloTest method callIsCanceledWhenUnsubscribe.
@Test
public void callIsCanceledWhenUnsubscribe() throws Exception {
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
TestScheduler scheduler = new TestScheduler();
Subscription subscription = RxApollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE)))).observeOn(scheduler).subscribe(testSubscriber);
subscription.unsubscribe();
scheduler.triggerActions();
testSubscriber.assertNotCompleted();
testSubscriber.assertValueCount(0);
testSubscriber.assertNoErrors();
assertThat(testSubscriber.isUnsubscribed()).isTrue();
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class ApolloCacheInterceptor method resolveFromCache.
InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException {
ResponseNormalizer<Record> responseNormalizer = apolloStore.cacheResponseNormalizer();
// noinspection unchecked
ApolloStoreOperation<Response> apolloStoreOperation = apolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders);
Response cachedResponse = apolloStoreOperation.execute();
if (cachedResponse.data() != null) {
logger.d("Cache HIT for operation %s", request.operation);
return new InterceptorResponse(null, cachedResponse, responseNormalizer.records());
}
logger.d("Cache MISS for operation %s", request.operation);
throw new ApolloException(String.format("Cache miss for operation %s", request.operation));
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class ApolloCancelCallTest method cancelCallAfterEnqueueNoCallback.
@Test
public void cancelCallAfterEnqueueNoCallback() throws Exception {
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).httpCache(new ApolloHttpCache(cacheStore, null)).build();
server.enqueue(mockResponse("EpisodeHeroNameResponse.json"));
final ApolloCall<EpisodeHeroNameQuery.Data> call = apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE)));
final TestObserver<Response<EpisodeHeroNameQuery.Data>> test = Rx2Apollo.from(call).test();
call.cancel();
test.awaitDone(1, TimeUnit.SECONDS).assertNoErrors().assertNoValues().assertNotComplete();
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class ApolloCallbackTest method onNetworkError.
@Test
public void onNetworkError() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicBoolean invoked = new AtomicBoolean();
final Handler callbackHandler = mockCallbackHandler(invoked);
final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {
@Override
public void onResponse(@Nonnull Response response) {
countDownLatch.countDown();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
countDownLatch.countDown();
}
@Override
public void onNetworkError(@Nonnull ApolloNetworkException e) {
exceptionRef.set(e);
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
assertThat(exceptionRef.get()).isInstanceOf(ApolloNetworkException.class);
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class NormalizedCacheTestCase method dump.
@Test
public void dump() throws Exception {
enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))).responseFetcher(NETWORK_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
Map<Class, Map<String, Record>> dump = apolloClient.apolloStore().normalizedCache().dump();
assertThat(NormalizedCache.prettifyDump(dump)).isEqualTo("OptimisticNormalizedCache {}\n" + "LruNormalizedCache {\n" + " \"1002\" : {\n" + " \"__typename\" : Human\n" + " \"id\" : 1002\n" + " \"name\" : Han Solo\n" + " }\n" + "\n" + " \"QUERY_ROOT\" : {\n" + " \"hero({\"episode\":\"NEWHOPE\"})\" : CacheRecordRef(2001)\n" + " }\n" + "\n" + " \"1003\" : {\n" + " \"__typename\" : Human\n" + " \"id\" : 1003\n" + " \"name\" : Leia Organa\n" + " }\n" + "\n" + " \"1000\" : {\n" + " \"__typename\" : Human\n" + " \"id\" : 1000\n" + " \"name\" : Luke Skywalker\n" + " }\n" + "\n" + " \"2001\" : {\n" + " \"__typename\" : Droid\n" + " \"id\" : 2001\n" + " \"name\" : R2-D2\n" + " \"friends\" : [\n" + " CacheRecordRef(1000)\n" + " CacheRecordRef(1002)\n" + " CacheRecordRef(1003)\n" + " ]\n" + " }\n" + "}\n");
}
Aggregations