Search in sources :

Example 26 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class AsyncNormalizedCacheTestCase method testAsync.

@Test
public void testAsync() throws IOException, InterruptedException, ApolloException {
    EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
    for (int i = 0; i < 500; i++) {
        server.enqueue(mockResponse("HeroNameResponse.json"));
    }
    List<Observable<Response<EpisodeHeroNameQuery.Data>>> calls = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        ApolloQueryCall<EpisodeHeroNameQuery.Data> queryCall = apolloClient.query(query).responseFetcher(i % 2 == 0 ? ApolloResponseFetchers.NETWORK_FIRST : ApolloResponseFetchers.CACHE_ONLY);
        calls.add(Rx2Apollo.from(queryCall));
    }
    TestObserver<Response<EpisodeHeroNameQuery.Data>> observer = new TestObserver<>();
    Observable.merge(calls).subscribe(observer);
    observer.awaitTerminalEvent();
    observer.assertNoErrors();
    observer.assertValueCount(1000);
    observer.assertNever(new Predicate<Response<EpisodeHeroNameQuery.Data>>() {

        @Override
        public boolean test(Response<EpisodeHeroNameQuery.Data> dataResponse) throws Exception {
            return dataResponse.hasErrors();
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) TestObserver(io.reactivex.observers.TestObserver) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Test(org.junit.Test)

Example 27 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class HttpCacheTest method fileSystemReadFailure.

@Test
public void fileSystemReadFailure() throws IOException, ApolloException {
    FaultyHttpCacheStore faultyCacheStore = new FaultyHttpCacheStore(inMemoryFileSystem);
    cacheStore.delegate = faultyCacheStore;
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    faultyCacheStore.failStrategy(FaultyHttpCacheStore.FailStrategy.FAIL_HEADER_READ);
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertThat(server.getRequestCount()).isEqualTo(2);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    faultyCacheStore.failStrategy(FaultyHttpCacheStore.FailStrategy.FAIL_BODY_READ);
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test().assertError(Exception.class);
    assertThat(server.getRequestCount()).isEqualTo(2);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 28 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class HttpCacheTest method fileSystemUnavailable.

@Test
public void fileSystemUnavailable() throws IOException, ApolloException {
    cacheStore.delegate = new DiskLruHttpCacheStore(new NoFileSystem(), new File("/cache/"), Integer.MAX_VALUE);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkNoCachedResponse();
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) File(java.io.File) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 29 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class HttpCacheTest method expireAfterRead.

@Test
public void expireAfterRead() throws IOException, ApolloException {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY.expireAfterRead())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkNoCachedResponse();
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY)).test().assertError(Exception.class);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 30 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class OptimisticCacheTestCase method mutation_and_query_watcher.

@Test
public void mutation_and_query_watcher() throws Exception {
    server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
    final List<ReviewsByEpisodeQuery.Data> watcherData = new ArrayList<>();
    apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(ApolloResponseFetchers.NETWORK_FIRST).watcher().refetchResponseFetcher(ApolloResponseFetchers.CACHE_FIRST).enqueueAndWatch(new ApolloCall.Callback<ReviewsByEpisodeQuery.Data>() {

        @Override
        public void onResponse(@Nonnull Response<ReviewsByEpisodeQuery.Data> response) {
            watcherData.add(response.data());
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }
    });
    server.enqueue(mockResponse("UpdateReviewResponse.json"));
    UpdateReviewMutation updateReviewMutation = new UpdateReviewMutation("empireReview2", ReviewInput.builder().commentary("Great").stars(5).favoriteColor(ColorInput.builder().build()).build());
    apolloClient.mutate(updateReviewMutation, new UpdateReviewMutation.Data(new UpdateReviewMutation.UpdateReview("Review", "empireReview2", 5, "Great"))).enqueue(new ApolloCall.Callback<UpdateReviewMutation.Data>() {

        @Override
        public void onResponse(@Nonnull Response<UpdateReviewMutation.Data> response) {
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }
    });
    assertThat(watcherData).hasSize(3);
    // before mutation and optimistic updates
    assertThat(watcherData.get(0).reviews()).hasSize(3);
    assertThat(watcherData.get(0).reviews().get(0).id()).isEqualTo("empireReview1");
    assertThat(watcherData.get(0).reviews().get(0).stars()).isEqualTo(1);
    assertThat(watcherData.get(0).reviews().get(0).commentary()).isEqualTo("Boring");
    assertThat(watcherData.get(0).reviews().get(1).id()).isEqualTo("empireReview2");
    assertThat(watcherData.get(0).reviews().get(1).stars()).isEqualTo(2);
    assertThat(watcherData.get(0).reviews().get(1).commentary()).isEqualTo("So-so");
    assertThat(watcherData.get(0).reviews().get(2).id()).isEqualTo("empireReview3");
    assertThat(watcherData.get(0).reviews().get(2).stars()).isEqualTo(5);
    assertThat(watcherData.get(0).reviews().get(2).commentary()).isEqualTo("Amazing");
    // optimistic updates
    assertThat(watcherData.get(1).reviews()).hasSize(3);
    assertThat(watcherData.get(1).reviews().get(0).id()).isEqualTo("empireReview1");
    assertThat(watcherData.get(1).reviews().get(0).stars()).isEqualTo(1);
    assertThat(watcherData.get(1).reviews().get(0).commentary()).isEqualTo("Boring");
    assertThat(watcherData.get(1).reviews().get(1).id()).isEqualTo("empireReview2");
    assertThat(watcherData.get(1).reviews().get(1).stars()).isEqualTo(5);
    assertThat(watcherData.get(1).reviews().get(1).commentary()).isEqualTo("Great");
    assertThat(watcherData.get(1).reviews().get(2).id()).isEqualTo("empireReview3");
    assertThat(watcherData.get(1).reviews().get(2).stars()).isEqualTo(5);
    assertThat(watcherData.get(1).reviews().get(2).commentary()).isEqualTo("Amazing");
    // after mutation with rolled back optimistic updates
    assertThat(watcherData.get(2).reviews()).hasSize(3);
    assertThat(watcherData.get(2).reviews().get(0).id()).isEqualTo("empireReview1");
    assertThat(watcherData.get(2).reviews().get(0).stars()).isEqualTo(1);
    assertThat(watcherData.get(2).reviews().get(0).commentary()).isEqualTo("Boring");
    assertThat(watcherData.get(2).reviews().get(1).id()).isEqualTo("empireReview2");
    assertThat(watcherData.get(2).reviews().get(1).stars()).isEqualTo(4);
    assertThat(watcherData.get(2).reviews().get(1).commentary()).isEqualTo("Not Bad");
    assertThat(watcherData.get(2).reviews().get(2).id()).isEqualTo("empireReview3");
    assertThat(watcherData.get(2).reviews().get(2).stars()).isEqualTo(5);
    assertThat(watcherData.get(2).reviews().get(2).commentary()).isEqualTo("Amazing");
}
Also used : ReviewsByEpisodeQuery(com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery) ArrayList(java.util.ArrayList) ApolloException(com.apollographql.apollo.exception.ApolloException) UpdateReviewMutation(com.apollographql.apollo.integration.normalizer.UpdateReviewMutation) 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