Search in sources :

Example 1 with ApolloInterceptor

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

the class ApolloInterceptorTest method applicationInterceptorCanMakeMultipleRequestsToServer.

@Test
public void applicationInterceptorCanMakeMultipleRequestsToServer() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_CHANGE));
    EpisodeHeroNameQuery query = createHeroNameQuery();
    ApolloInterceptor interceptor = createChainInterceptor();
    client = createApolloClient(interceptor);
    enqueueAndAssertResponse(server, FILE_EPISODE_HERO_NAME_WITH_ID, client.query(query), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {

        @Override
        public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("Artoo");
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) MockResponse(okhttp3.mockwebserver.MockResponse) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 2 with ApolloInterceptor

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

the class ApolloInterceptorTest method asyncApplicationInterceptorCanShortCircuitResponses.

@Test
public void asyncApplicationInterceptorCanShortCircuitResponses() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    EpisodeHeroNameQuery query = createHeroNameQuery();
    final InterceptorResponse expectedResponse = prepareInterceptorResponse(query);
    ApolloInterceptor interceptor = createShortcutInterceptor(expectedResponse);
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertValue(expectedResponse.parsedResponse.get());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 3 with ApolloInterceptor

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

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

the class ApolloInterceptorTest method onShortCircuitingResponseSubsequentInterceptorsAreNotCalled.

@Test
public void onShortCircuitingResponseSubsequentInterceptorsAreNotCalled() throws IOException, ApolloException {
    EpisodeHeroNameQuery query = createHeroNameQuery();
    final InterceptorResponse expectedResponse = prepareInterceptorResponse(query);
    ApolloInterceptor firstInterceptor = createShortcutInterceptor(expectedResponse);
    ApolloInterceptor secondInterceptor = createChainInterceptor();
    client = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).addApplicationInterceptor(firstInterceptor).addApplicationInterceptor(secondInterceptor).build();
    assertResponse(client.query(query), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {

        @Override
        public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            assertThat(expectedResponse.parsedResponse.get()).isEqualTo(response);
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) MockResponse(okhttp3.mockwebserver.MockResponse) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 5 with ApolloInterceptor

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

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