use of com.apollographql.apollo.exception.ApolloException 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");
}
}
use of com.apollographql.apollo.exception.ApolloException 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");
}
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloCallbackTest method onHttpError.
@Test
public void onHttpError() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicBoolean invoked = new AtomicBoolean();
final Handler callbackHandler = mockCallbackHandler(invoked);
final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
server.enqueue(new MockResponse().setResponseCode(401).setBody("Unauthorized request!"));
apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {
@Override
public void onResponse(@Nonnull Response response) {
countDownLatch.countDown();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
exceptionRef.set(e);
countDownLatch.countDown();
}
@Override
public void onHttpError(@Nonnull ApolloHttpException e) {
exceptionRef.set(e);
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
assertThat(exceptionRef.get()).isInstanceOf(ApolloHttpException.class);
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloCallbackTest method onParseError.
@Test
public void onParseError() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicBoolean invoked = new AtomicBoolean();
final Handler callbackHandler = mockCallbackHandler(invoked);
final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
server.enqueue(new MockResponse().setResponseCode(200).setBody("nonsense"));
apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {
@Override
public void onResponse(@Nonnull Response response) {
countDownLatch.countDown();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
countDownLatch.countDown();
}
@Override
public void onParseError(@Nonnull ApolloParseException e) {
exceptionRef.set(e);
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
assertThat(exceptionRef.get()).isInstanceOf(ApolloParseException.class);
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloCallbackTest method onResponse.
@Test
public void onResponse() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicBoolean invoked = new AtomicBoolean();
final Handler callbackHandler = mockCallbackHandler(invoked);
final AtomicReference<Response> responseRef = new AtomicReference<>();
server.enqueue(new MockResponse().setResponseCode(200).setBody("{" + " \"errors\": [" + " {" + " \"message\": \"Cannot query field \\\"names\\\" on type \\\"Species\\\".\"," + " \"locations\": [" + " {" + " \"line\": 3," + " \"column\": 5" + " }" + " ]" + " }" + " ]" + "}"));
apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {
@Override
public void onResponse(@Nonnull Response response) {
responseRef.set(response);
countDownLatch.countDown();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
assertThat(responseRef.get()).isNotNull();
}
Aggregations