Search in sources :

Example 21 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class ApolloPrefetchCallbackTest method onHttpError.

@Test
public void onHttpError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    server.enqueue(new MockResponse().setResponseCode(401).setBody("Unauthorized request!"));
    apolloClient.prefetch(EMPTY_QUERY).enqueue(ApolloPrefetchCallback.wrap(new ApolloPrefetch.Callback() {

        @Override
        public void onSuccess() {
            fail("Expected onHttpError");
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            fail("Expected onHttpError");
        }

        @Override
        public void onHttpError(@Nonnull ApolloHttpException e) {
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(2, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) Handler(android.os.Handler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 22 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class ApolloPrefetchCallbackTest method onResponse.

@Test
public void onResponse() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("{" + "  \"errors\": [" + "    {" + "      \"message\": \"Cannot query field \\\"names\\\" on type \\\"Species\\\".\"," + "      \"locations\": [" + "        {" + "          \"line\": 3," + "          \"column\": 5" + "        }" + "      ]" + "    }" + "  ]" + "}"));
    apolloClient.prefetch(EMPTY_QUERY).enqueue(ApolloPrefetchCallback.wrap(new ApolloPrefetch.Callback() {

        @Override
        public void onSuccess() {
            countDownLatch.countDown();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            fail("Expected onResponse");
        }
    }, callbackHandler));
    countDownLatch.await(2, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Handler(android.os.Handler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class ApolloIdlingResourceTest method checkIsIdleNow_whenCallIsQueued.

@Test
public void checkIsIdleNow_whenCallIsQueued() throws IOException, TimeoutException, InterruptedException {
    server.enqueue(mockResponse());
    final CountDownLatch latch = new CountDownLatch(1);
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    apolloClient = ApolloClient.builder().okHttpClient(okHttpClient).dispatcher(executorService).serverUrl(server.url("/")).build();
    idlingResource = ApolloIdlingResource.create(IDLING_RESOURCE_NAME, apolloClient);
    assertThat(idlingResource.isIdleNow()).isTrue();
    apolloClient.query(EMPTY_QUERY).enqueue(new ApolloCall.Callback<Object>() {

        @Override
        public void onResponse(@Nonnull Response<Object> response) {
            latch.countDown();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            throw new AssertionError("This callback can't be called.");
        }
    });
    assertThat(idlingResource.isIdleNow()).isFalse();
    latch.await(TIME_OUT_SECONDS, TimeUnit.SECONDS);
    executorService.shutdown();
    executorService.awaitTermination(TIME_OUT_SECONDS, TimeUnit.SECONDS);
    Thread.sleep(100);
    assertThat(idlingResource.isIdleNow()).isTrue();
}
Also used : ApolloException(com.apollographql.apollo.exception.ApolloException) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ApolloCall(com.apollographql.apollo.ApolloCall) Test(org.junit.Test)

Example 24 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class ApolloWatcherTest method testQueryWatcherNotUpdated_SameQuery_SameResults.

@Test
public void testQueryWatcherNotUpdated_SameQuery_SameResults() throws Exception {
    final List<String> heroNameList = new ArrayList<>();
    EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
    server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
    ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).watcher();
    watcher.enqueueAndWatch(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {

        @Override
        public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
            heroNameList.add(response.data().hero().name());
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            Assert.fail(e.getMessage());
        }
    });
    server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
    apolloClient.query(query).responseFetcher(NETWORK_ONLY).enqueue(null);
    watcher.cancel();
    assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
    assertThat(heroNameList.size()).isEqualTo(1);
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 25 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class ApolloWatcherTest method testQueryWatcherUpdated_SameQuery_DifferentResults.

@Test
public void testQueryWatcherUpdated_SameQuery_DifferentResults() throws Exception {
    final List<String> heroNameList = new ArrayList<>();
    EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
    server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
    ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).watcher();
    watcher.enqueueAndWatch(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {

        @Override
        public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
            heroNameList.add(response.data().hero().name());
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            Assert.fail(e.getMessage());
        }
    });
    // Another newer call gets updated information
    enqueueAndAssertResponse(server, "EpisodeHeroNameResponseNameChange.json", apolloClient.query(query).responseFetcher(NETWORK_ONLY), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {

        @Override
        public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    watcher.cancel();
    assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
    assertThat(heroNameList.get(1)).isEqualTo("Artoo");
    assertThat(heroNameList.size()).isEqualTo(2);
}
Also used : ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Aggregations

ApolloException (com.apollographql.apollo.exception.ApolloException)37 Test (org.junit.Test)31 Response (com.apollographql.apollo.api.Response)20 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)16 Nonnull (javax.annotation.Nonnull)15 MockResponse (okhttp3.mockwebserver.MockResponse)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Handler (android.os.Handler)7 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)6 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)6 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)6 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)5 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)5 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)5 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)5 Executor (java.util.concurrent.Executor)5 TimeoutException (java.util.concurrent.TimeoutException)5