use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class RxApolloTest method callIsCanceledWhenUnsubscribe.
@Test
public void callIsCanceledWhenUnsubscribe() throws Exception {
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
TestScheduler scheduler = new TestScheduler();
Subscription subscription = RxApollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE)))).observeOn(scheduler).subscribe(testSubscriber);
subscription.unsubscribe();
scheduler.triggerActions();
testSubscriber.assertNotCompleted();
testSubscriber.assertValueCount(0);
testSubscriber.assertNoErrors();
assertThat(testSubscriber.isUnsubscribed()).isTrue();
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class CacheAndNetworkFetcherTest method enqueue.
@Test
public void enqueue() throws IOException, ApolloException, TimeoutException, InterruptedException {
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
TrackingCallback trackingCallback;
// Has error when cache empty, and network error
server.enqueue(new MockResponse().setResponseCode(HTTP_INTERNAL_ERROR).setBody("Server Error"));
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_AND_NETWORK).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions.size()).isEqualTo(1);
// Goes to network when cache empty, one response
server.enqueue(mockResponse("HeroNameResponse.json"));
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_AND_NETWORK).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions).isEmpty();
assertThat(trackingCallback.responseList.size()).isEqualTo(1);
assertThat(trackingCallback.responseList.get(0).fromCache()).isFalse();
assertThat(trackingCallback.responseList.get(0).data().hero().name()).isEqualTo("R2-D2");
// Goes to network and cache after cache populated
server.enqueue(mockResponse("HeroNameResponse.json"));
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_AND_NETWORK).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions).isEmpty();
assertThat(trackingCallback.responseList.size()).isEqualTo(2);
// Cache is always first
assertThat(trackingCallback.responseList.get(0).fromCache()).isTrue();
assertThat(trackingCallback.responseList.get(0).data().hero().name()).isEqualTo("R2-D2");
assertThat(trackingCallback.responseList.get(1).fromCache()).isFalse();
assertThat(trackingCallback.responseList.get(1).data().hero().name()).isEqualTo("R2-D2");
// Falls back to cache if network error
server.enqueue(new MockResponse().setResponseCode(HTTP_INTERNAL_ERROR).setBody("Server Error"));
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_AND_NETWORK).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions).isEmpty();
assertThat(trackingCallback.responseList.size()).isEqualTo(1);
assertThat(trackingCallback.responseList.get(0).fromCache()).isTrue();
assertThat(trackingCallback.responseList.get(0).data().hero().name()).isEqualTo("R2-D2");
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class CacheOnlyFetcherTest method enqueue.
@Test
public void enqueue() throws IOException, ApolloException, TimeoutException, InterruptedException {
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
TrackingCallback trackingCallback;
// Is null when cache empty
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_ONLY).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions.size()).isEqualTo(0);
assertThat(trackingCallback.responseList.size()).isEqualTo(1);
assertThat(trackingCallback.responseList.get(0).fromCache()).isTrue();
assertThat(trackingCallback.responseList.get(0).data()).isNull();
assertThat(server.getRequestCount()).isEqualTo(0);
// Populate cache
server.enqueue(mockResponse("HeroNameResponse.json"));
final Response<EpisodeHeroNameQuery.Data> responseData = Rx2Apollo.from(apolloClient.query(query).responseFetcher(NETWORK_ONLY)).blockingFirst();
assertThat(responseData.hasErrors()).isFalse();
assertThat(responseData.data().hero().name()).isEqualTo("R2-D2");
assertThat(server.getRequestCount()).isEqualTo(1);
// Success after cache populated
server.enqueue(mockResponse("HeroNameResponse.json"));
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_ONLY).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions.size()).isEqualTo(0);
assertThat(trackingCallback.responseList.size()).isEqualTo(1);
assertThat(trackingCallback.responseList.get(0).fromCache()).isTrue();
assertThat(trackingCallback.responseList.get(0).data().hero().name()).isEqualTo("R2-D2");
assertThat(server.getRequestCount()).isEqualTo(1);
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class ApolloCancelCallTest method cancelCallAfterEnqueueNoCallback.
@Test
public void cancelCallAfterEnqueueNoCallback() throws Exception {
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).httpCache(new ApolloHttpCache(cacheStore, null)).build();
server.enqueue(mockResponse("EpisodeHeroNameResponse.json"));
final ApolloCall<EpisodeHeroNameQuery.Data> call = apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE)));
final TestObserver<Response<EpisodeHeroNameQuery.Data>> test = Rx2Apollo.from(call).test();
call.cancel();
test.awaitDone(1, TimeUnit.SECONDS).assertNoErrors().assertNoValues().assertNotComplete();
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class ApolloCancelCallTest method cancelPrefetchAfterEnqueueNoCallback.
@Test
public void cancelPrefetchAfterEnqueueNoCallback() throws Exception {
server.enqueue(mockResponse("EpisodeHeroNameResponse.json"));
final ApolloPrefetch call = apolloClient.prefetch(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE)));
Rx2Apollo.from(call).test().assertNotComplete();
}
Aggregations