use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery 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");
}
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class ResponseNormalizationTest method testHeroNameWithVariable.
@Test
public void testHeroNameWithVariable() throws Exception {
assertHasNoErrors("EpisodeHeroNameResponse.json", new EpisodeHeroNameQuery(Input.fromNullable(JEDI)));
Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
CacheReference reference = (CacheReference) record.field(TEST_FIELD_KEY_JEDI);
assertThat(reference).isEqualTo(new CacheReference(TEST_FIELD_KEY_JEDI));
final Record heroRecord = normalizedCache.loadRecord(reference.key(), CacheHeaders.NONE);
assertThat(heroRecord.field("name")).isEqualTo("R2-D2");
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class Rx2ApolloTest method queryWatcherNotUpdatedSameQuerySameResults.
@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherNotUpdatedSameQuerySameResults() throws Exception {
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {
@Override
public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.data();
}
}).subscribeWith(observer);
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
observer.assertValueCount(1).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {
@Override
public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
assertThat(data.hero().name()).isEqualTo("R2-D2");
return true;
}
});
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class Rx2ApolloTest method queryWatcherUpdatedDifferentQueryDifferentResults.
@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherUpdatedDifferentQueryDifferentResults() throws Exception {
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {
@Override
public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.data();
}
}).subscribeWith(observer);
server.enqueue(mockResponse("HeroAndFriendsNameWithIdsNameChange.json"));
apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(NEWHOPE))).enqueue(null);
observer.assertValueCount(2).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {
@Override
public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
assertThat(data.hero().name()).isEqualTo("R2-D2");
return true;
}
}).assertValueAt(1, new Predicate<EpisodeHeroNameQuery.Data>() {
@Override
public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
assertThat(data.hero().name()).isEqualTo("Artoo");
return true;
}
});
}
use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.
the class CacheFirstFetcherTest 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_FIRST).enqueue(trackingCallback);
assertThat(trackingCallback.exceptions.size()).isEqualTo(1);
// Goes to network when empty
trackingCallback = new TrackingCallback();
server.enqueue(mockResponse("HeroNameResponse.json"));
apolloClient.query(query).responseFetcher(CACHE_FIRST).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");
assertThat(server.getRequestCount()).isEqualTo(2);
// Hits only cache after populated
trackingCallback = new TrackingCallback();
apolloClient.query(query).responseFetcher(CACHE_FIRST).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");
assertThat(server.getRequestCount()).isEqualTo(2);
}
Aggregations