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();
}
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();
}
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();
}
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);
}
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);
}
Aggregations