Search in sources :

Example 26 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery 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 27 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method prepareInterceptorResponse.

@NonNull
private InterceptorResponse prepareInterceptorResponse(EpisodeHeroNameQuery query) {
    Request request = new Request.Builder().url("https://localhost:8080/").build();
    okhttp3.Response okHttpResponse = new okhttp3.Response.Builder().request(request).protocol(Protocol.HTTP_2).code(200).message("Intercepted").body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "fakeResponse")).build();
    Response<EpisodeHeroNameQuery.Data> apolloResponse = Response.<EpisodeHeroNameQuery.Data>builder(query).build();
    return new InterceptorResponse(okHttpResponse, apolloResponse, Collections.<Record>emptyList());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) Request(okhttp3.Request) NonNull(android.support.annotation.NonNull)

Example 28 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery 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 29 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class ApolloInterceptorTest method prepareInterceptorResponse.

@NonNull
private InterceptorResponse prepareInterceptorResponse(EpisodeHeroNameQuery query) {
    Request request = new Request.Builder().url(server.url("/")).build();
    okhttp3.Response okHttpResponse = new okhttp3.Response.Builder().request(request).protocol(Protocol.HTTP_2).code(200).message("Intercepted").body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "fakeResponse")).build();
    Response<EpisodeHeroNameQuery.Data> apolloResponse = Response.<EpisodeHeroNameQuery.Data>builder(query).build();
    return new InterceptorResponse(okHttpResponse, apolloResponse, Collections.<Record>emptyList());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) Request(okhttp3.Request) NonNull(android.support.annotation.NonNull)

Example 30 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class ApolloInterceptorTest method asyncApplicationInterceptorThrowsRuntimeException.

@Test
public void asyncApplicationInterceptorThrowsRuntimeException() throws TimeoutException, InterruptedException {
    final String message = "RuntimeException";
    EpisodeHeroNameQuery query = createHeroNameQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

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

                @Override
                public void run() {
                    throw new RuntimeException(message);
                }
            });
        }

        @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 RuntimeException && message.equals(throwable.getMessage());
        }
    });
}
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

EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)46 Test (org.junit.Test)44 ApolloException (com.apollographql.apollo.exception.ApolloException)19 Response (com.apollographql.apollo.api.Response)18 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)14 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)10 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 TimeoutException (java.util.concurrent.TimeoutException)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)7 TestObserver (io.reactivex.observers.TestObserver)7 Executor (java.util.concurrent.Executor)7 Nonnull (javax.annotation.Nonnull)7 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)6 TestSubscriber (rx.observers.TestSubscriber)6 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)5 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)3 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)3