Search in sources :

Example 1 with ApolloInterceptorChain

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

the class ApolloInterceptorTest method asyncApplicationInterceptorThrowsApolloException.

@Test
public void asyncApplicationInterceptorThrowsApolloException() throws Exception {
    final String message = "ApolloException";
    EpisodeHeroNameQuery query = createHeroNameQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
            ApolloException apolloException = new ApolloParseException(message);
            callBack.onFailure(apolloException);
        }

        @Override
        public void dispose() {
        }
    };
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertError(new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable throwable) throws Exception {
            return message.equals(throwable.getMessage()) && throwable instanceof ApolloParseException;
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 2 with ApolloInterceptorChain

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

the class ApolloInterceptorTest method asyncApplicationInterceptorReturnsNull.

@Test
public void asyncApplicationInterceptorReturnsNull() throws TimeoutException, InterruptedException {
    EpisodeHeroNameQuery query = createHeroNameQuery();
    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(null);
                }
            });
        }

        @Override
        public void dispose() {
        }
    };
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertError(new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable throwable) throws Exception {
            return throwable instanceof NullPointerException;
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 3 with ApolloInterceptorChain

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

the class ApolloInterceptorTest method asyncApplicationInterceptorRewritesResponsesFromServer.

@Test
public void asyncApplicationInterceptorRewritesResponsesFromServer() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    EpisodeHeroNameQuery query = createHeroNameQuery();
    final InterceptorResponse rewrittenResponse = prepareInterceptorResponse(query);
    ApolloInterceptor interceptor = new ApolloInterceptor() {

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

                @Override
                public void onResponse(@Nonnull InterceptorResponse response) {
                    callBack.onResponse(rewrittenResponse);
                }

                @Override
                public void onFailure(@Nonnull ApolloException e) {
                    throw new RuntimeException(e);
                }

                @Override
                public void onCompleted() {
                    callBack.onCompleted();
                }

                @Override
                public void onFetch(FetchSourceType sourceType) {
                    callBack.onFetch(sourceType);
                }
            });
        }

        @Override
        public void dispose() {
        }
    };
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertValue(rewrittenResponse.parsedResponse.get());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 4 with ApolloInterceptorChain

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

use of com.apollographql.apollo.interceptor.ApolloInterceptorChain 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)

Aggregations

ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)8 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)8 Executor (java.util.concurrent.Executor)8 Nonnull (javax.annotation.Nonnull)8 Test (org.junit.Test)8 ApolloException (com.apollographql.apollo.exception.ApolloException)7 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 CallBack (com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack)4 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)4 RealApolloInterceptorChain (com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)3 IOException (java.io.IOException)3 TimeoutException (java.util.concurrent.TimeoutException)3