use of com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack 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.interceptor.ApolloInterceptor.CallBack 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.interceptor.ApolloInterceptor.CallBack in project apollo-android by apollographql.
the class ApolloInterceptorChainTest method onDisposeCalled_interceptorIsDisposed.
@Test
public void onDisposeCalled_interceptorIsDisposed() {
final AtomicInteger counter = new AtomicInteger(1);
ApolloInterceptor interceptor = new ApolloInterceptor() {
@Override
public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
}
@Override
public void dispose() {
counter.decrementAndGet();
}
};
List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
chain.dispose();
if (counter.get() != 0) {
Assert.fail("Interceptor's dispose method not called");
}
}
use of com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack in project apollo-android by apollographql.
the class ApolloInterceptorChainTest method onProceedAsyncCalled_chainPassesControlToInterceptor.
@Test
public void onProceedAsyncCalled_chainPassesControlToInterceptor() throws TimeoutException, InterruptedException {
final AtomicInteger counter = new AtomicInteger(1);
EpisodeHeroNameQuery query = createQuery();
ApolloInterceptor interceptor = new ApolloInterceptor() {
@Override
public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
counter.decrementAndGet();
}
@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) {
}
@Override
public void onCompleted() {
}
@Override
public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
}
});
// which means the test should fail.
if (counter.get() != 0) {
Assert.fail("Control not passed to the interceptor");
}
}
Aggregations