Search in sources :

Example 1 with RealApolloInterceptorChain

use of com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method onProceedAsyncCalled_correctExceptionIsCaught.

@Test
public void onProceedAsyncCalled_correctExceptionIsCaught() throws TimeoutException, InterruptedException {
    final AtomicInteger counter = new AtomicInteger(1);
    final String message = "ApolloException";
    EpisodeHeroNameQuery query = createQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull final CallBack callBack) {
            dispatcher.execute(new Runnable() {

                @Override
                public void run() {
                    ApolloException apolloException = new ApolloException(message);
                    callBack.onFailure(apolloException);
                }
            });
        }

        @Override
        public void dispose() {
        }
    };
    List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
    RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
    chain.proceedAsync(ApolloInterceptor.InterceptorRequest.builder(query).fetchFromCache(false).build(), Utils.immediateExecutor(), new CallBack() {

        @Override
        public void onResponse(@Nonnull InterceptorResponse response) {
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            assertThat(e.getMessage()).isEqualTo(message);
            counter.decrementAndGet();
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
        }
    });
    if (counter.get() != 0) {
        Assert.fail("Exception thrown by Interceptor not caught");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 2 with RealApolloInterceptorChain

use of com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method onProceedAsyncCalled_correctInterceptorResponseIsReceived.

@Test
public void onProceedAsyncCalled_correctInterceptorResponseIsReceived() throws TimeoutException, InterruptedException {
    final AtomicInteger counter = new AtomicInteger(1);
    EpisodeHeroNameQuery query = createQuery();
    final InterceptorResponse expectedResponse = prepareInterceptorResponse(query);
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull final CallBack callBack) {
            dispatcher.execute(new Runnable() {

                @Override
                public void run() {
                    callBack.onResponse(expectedResponse);
                }
            });
        }

        @Override
        public void dispose() {
        }
    };
    List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
    RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
    chain.proceedAsync(ApolloInterceptor.InterceptorRequest.builder(query).fetchFromCache(false).build(), Utils.immediateExecutor(), new CallBack() {

        @Override
        public void onResponse(@Nonnull InterceptorResponse response) {
            assertThat(response).isEqualTo(expectedResponse);
            counter.decrementAndGet();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
        }
    });
    if (counter.get() != 0) {
        Assert.fail("Interceptor's response not received");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 3 with RealApolloInterceptorChain

use of com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain in project apollo-android by apollographql.

the class RealApolloCall method prepareInterceptorChain.

private ApolloInterceptorChain prepareInterceptorChain(Operation operation) {
    List<ApolloInterceptor> interceptors = new ArrayList<>();
    HttpCachePolicy.Policy httpCachePolicy = operation instanceof Query ? this.httpCachePolicy : null;
    ResponseFieldMapper responseFieldMapper = responseFieldMapperFactory.create(operation);
    interceptors.addAll(applicationInterceptors);
    interceptors.add(responseFetcher.provideInterceptor(logger));
    interceptors.add(new ApolloCacheInterceptor(apolloStore, responseFieldMapper, dispatcher, logger));
    interceptors.add(new ApolloParseInterceptor(httpCache, apolloStore.networkResponseNormalizer(), responseFieldMapper, scalarTypeAdapters, logger));
    interceptors.add(new ApolloServerInterceptor(serverUrl, httpCallFactory, httpCachePolicy, false, scalarTypeAdapters, logger, sendOperationdIdentifiers));
    return new RealApolloInterceptorChain(interceptors);
}
Also used : ResponseFieldMapper(com.apollographql.apollo.api.ResponseFieldMapper) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) Query(com.apollographql.apollo.api.Query) ApolloParseInterceptor(com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor) ArrayList(java.util.ArrayList) HttpCachePolicy(com.apollographql.apollo.api.cache.http.HttpCachePolicy) ApolloServerInterceptor(com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) ApolloCacheInterceptor(com.apollographql.apollo.internal.interceptor.ApolloCacheInterceptor)

Example 4 with RealApolloInterceptorChain

use of com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method onDisposeCalled_interceptorIsDisposed.

@Test
public void onDisposeCalled_interceptorIsDisposed() {
    final AtomicInteger counter = new AtomicInteger(1);
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
        }

        @Override
        public void dispose() {
            counter.decrementAndGet();
        }
    };
    List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
    RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
    chain.dispose();
    if (counter.get() != 0) {
        Assert.fail("Interceptor's dispose method not called");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) Executor(java.util.concurrent.Executor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 5 with RealApolloInterceptorChain

use of com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method onProceedAsyncCalled_chainPassesControlToInterceptor.

@Test
public void onProceedAsyncCalled_chainPassesControlToInterceptor() throws TimeoutException, InterruptedException {
    final AtomicInteger counter = new AtomicInteger(1);
    EpisodeHeroNameQuery query = createQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
            counter.decrementAndGet();
        }

        @Override
        public void dispose() {
        }
    };
    List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
    RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
    chain.proceedAsync(ApolloInterceptor.InterceptorRequest.builder(query).fetchFromCache(false).build(), Utils.immediateExecutor(), new CallBack() {

        @Override
        public void onResponse(@Nonnull InterceptorResponse response) {
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
        }
    });
    // which means the test should fail.
    if (counter.get() != 0) {
        Assert.fail("Control not passed to the interceptor");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Aggregations

ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)5 RealApolloInterceptorChain (com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain)5 CallBack (com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack)4 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)4 Executor (java.util.concurrent.Executor)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Nonnull (javax.annotation.Nonnull)4 Test (org.junit.Test)4 ApolloException (com.apollographql.apollo.exception.ApolloException)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)3 Query (com.apollographql.apollo.api.Query)1 ResponseFieldMapper (com.apollographql.apollo.api.ResponseFieldMapper)1 HttpCachePolicy (com.apollographql.apollo.api.cache.http.HttpCachePolicy)1 ApolloCacheInterceptor (com.apollographql.apollo.internal.interceptor.ApolloCacheInterceptor)1 ApolloParseInterceptor (com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor)1 ApolloServerInterceptor (com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor)1 ArrayList (java.util.ArrayList)1