Search in sources :

Example 11 with ApolloInterceptor

use of com.apollographql.apollo.interceptor.ApolloInterceptor 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 12 with ApolloInterceptor

use of com.apollographql.apollo.interceptor.ApolloInterceptor 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)12 Test (org.junit.Test)11 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)10 ApolloException (com.apollographql.apollo.exception.ApolloException)9 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)8 Executor (java.util.concurrent.Executor)8 Nonnull (javax.annotation.Nonnull)8 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)7 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)5 RealApolloInterceptorChain (com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain)5 IOException (java.io.IOException)5 TimeoutException (java.util.concurrent.TimeoutException)5 CallBack (com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)2 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)2 Response (com.apollographql.apollo.api.Response)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Query (com.apollographql.apollo.api.Query)1 ResponseFieldMapper (com.apollographql.apollo.api.ResponseFieldMapper)1