Search in sources :

Example 6 with ApolloException

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);
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with ApolloException

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);
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with ApolloException

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();
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) Record(com.apollographql.apollo.cache.normalized.Record) WriteableStore(com.apollographql.apollo.internal.cache.normalized.WriteableStore) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 9 with ApolloException

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);
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with ApolloException

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);
}
Also used : ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Aggregations

ApolloException (com.apollographql.apollo.exception.ApolloException)37 Test (org.junit.Test)31 Response (com.apollographql.apollo.api.Response)20 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)16 Nonnull (javax.annotation.Nonnull)15 MockResponse (okhttp3.mockwebserver.MockResponse)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Handler (android.os.Handler)7 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)6 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)6 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)6 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)5 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)5 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)5 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)5 Executor (java.util.concurrent.Executor)5 TimeoutException (java.util.concurrent.TimeoutException)5