use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherUpdated_SameQuery_DifferentResults_cacheOnly.
@Test
public void testQueryWatcherUpdated_SameQuery_DifferentResults_cacheOnly() throws Exception {
final List<String> heroNameList = new ArrayList<>();
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
apolloClient.query(query).enqueue(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override
public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Assert.fail(e.getMessage());
}
});
ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).responseFetcher(CACHE_ONLY).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
server.enqueue(mockResponse("EpisodeHeroNameResponseNameChange.json"));
apolloClient.query(query).responseFetcher(NETWORK_ONLY).enqueue(null);
watcher.cancel();
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.get(1)).isEqualTo("Artoo");
assertThat(heroNameList.size()).isEqualTo(2);
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherNotUpdated_DifferentQueries.
@Test
public void testQueryWatcherNotUpdated_DifferentQueries() throws Exception {
final List<String> heroNameList = new ArrayList<>();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
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());
}
});
HeroAndFriendsNamesWithIDsQuery friendsQuery = HeroAndFriendsNamesWithIDsQuery.builder().episode(Episode.NEWHOPE).build();
server.enqueue(mockResponse("HeroAndFriendsNameWithIdsResponse.json"));
apolloClient.query(friendsQuery).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_Store_write.
@Test
public void testQueryWatcherUpdated_Store_write() throws IOException, InterruptedException, TimeoutException, ApolloException {
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());
}
});
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
// Someone writes to the store directly
Set<String> changedKeys = apolloClient.apolloStore().writeTransaction(new Transaction<WriteableStore, Set<String>>() {
@Nullable
@Override
public Set<String> execute(WriteableStore cache) {
Record record = Record.builder("2001").addField("name", "Artoo").build();
return cache.merge(Collections.singletonList(record), CacheHeaders.NONE);
}
});
apolloClient.apolloStore().publish(changedKeys);
assertThat(heroNameList.get(1)).isEqualTo("Artoo");
watcher.cancel();
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloWatcherTest method testRefetchCacheControl.
@Test
public void testRefetchCacheControl() throws Exception {
final List<String> heroNameList = new ArrayList<>();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).watcher();
// Force network instead of CACHE_FIRST default
watcher.refetchResponseFetcher(NETWORK_ONLY).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.getCause().getMessage());
}
});
// A different call gets updated information.
server.enqueue(mockResponse("EpisodeHeroNameResponseNameChange.json"));
// To verify that the updated response comes from server use a different name change
// -- this is for the refetch
server.enqueue(mockResponse("EpisodeHeroNameResponseNameChangeTwo.json"));
apolloClient.query(query).responseFetcher(NETWORK_ONLY).enqueue(null);
watcher.cancel();
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.get(1)).isEqualTo("ArTwo");
assertThat(heroNameList.size()).isEqualTo(2);
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherNotCalled_WhenCanceled.
@Test
public void testQueryWatcherNotCalled_WhenCanceled() 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());
}
});
watcher.cancel();
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();
}
});
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.size()).isEqualTo(1);
}
Aggregations