Search in sources :

Example 56 with Response

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();
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Subscription(rx.Subscription) Test(org.junit.Test)

Example 57 with Response

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));
}
Also used : Response(com.apollographql.apollo.api.Response) ApolloException(com.apollographql.apollo.exception.ApolloException) Record(com.apollographql.apollo.cache.normalized.Record)

Example 58 with Response

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();
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) OkHttpClient(okhttp3.OkHttpClient) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Dispatcher(okhttp3.Dispatcher) Test(org.junit.Test)

Example 59 with Response

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);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApolloException(com.apollographql.apollo.exception.ApolloException) Nonnull(javax.annotation.Nonnull) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException) Test(org.junit.Test)

Example 60 with Response

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");
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.cacheAndAssertCachedResponse(com.apollographql.apollo.Utils.cacheAndAssertCachedResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Response (com.apollographql.apollo.api.Response)67 Test (org.junit.Test)55 ApolloException (com.apollographql.apollo.exception.ApolloException)34 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)29 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)26 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)26 IOException (java.io.IOException)23 MockResponse (okhttp3.mockwebserver.MockResponse)22 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)18 ParseException (java.text.ParseException)17 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)12 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)12 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)11 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Nonnull (javax.annotation.Nonnull)7 TestObserver (io.reactivex.observers.TestObserver)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 TestSubscriber (rx.observers.TestSubscriber)5 Handler (android.os.Handler)4