Search in sources :

Example 31 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class NormalizedCacheTestCase method masterDetailSuccess.

@Test
public void masterDetailSuccess() throws Exception {
    enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))).responseFetcher(NETWORK_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertResponse(apolloClient.query(new CharacterNameByIdQuery("1002")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterNameByIdQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterNameByIdQuery.Data> response) throws Exception {
            assertThat(response.data().character()).isNotNull();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Han Solo");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.cacheAndAssertCachedResponse(com.apollographql.apollo.Utils.cacheAndAssertCachedResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) CharacterNameByIdQuery(com.apollographql.apollo.integration.normalizer.CharacterNameByIdQuery) Test(org.junit.Test)

Example 32 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class NormalizedCacheTestCase method cacheResponseWithNullableFields.

@Test
public void cacheResponseWithNullableFields() throws Exception {
    enqueueAndAssertResponse(server, "AllPlanetsNullableField.json", apolloClient.query(new AllPlanetsQuery()).responseFetcher(NETWORK_ONLY), new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            assertThat(response).isNotNull();
            assertThat(response.hasErrors()).isFalse();
            return true;
        }
    });
    assertResponse(apolloClient.query(new AllPlanetsQuery()).responseFetcher(CACHE_ONLY), new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            assertThat(response).isNotNull();
            assertThat(response.hasErrors()).isFalse();
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.cacheAndAssertCachedResponse(com.apollographql.apollo.Utils.cacheAndAssertCachedResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Example 33 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class Rx2ApolloTest method callIsCanceledWhenDisposed.

@Test
public void callIsCanceledWhenDisposed() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<Response<EpisodeHeroNameQuery.Data>> testObserver = new TestObserver<>();
    Disposable disposable = Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE)))).subscribeWith(testObserver);
    disposable.dispose();
    testObserver.assertComplete();
    assertThat(testObserver.isDisposed()).isTrue();
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) Disposable(io.reactivex.disposables.Disposable) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 34 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherUpdatedSameQueryDifferentResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherUpdatedSameQueryDifferentResults() 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_CHANGE));
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).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;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) Function(io.reactivex.functions.Function) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestObserver(io.reactivex.observers.TestObserver) Predicate(io.reactivex.functions.Predicate) Test(org.junit.Test)

Example 35 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherNotCalledWhenCanceled.

@Test
public void queryWatcherNotCalledWhenCanceled() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<EpisodeHeroNameQuery.Data> testObserver = new TestObserver<>();
    TestScheduler scheduler = new TestScheduler();
    Disposable disposable = 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();
        }
    }).observeOn(scheduler).subscribeWith(testObserver);
    scheduler.triggerActions();
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_CHANGE));
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    disposable.dispose();
    scheduler.triggerActions();
    testObserver.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;
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestObserver(io.reactivex.observers.TestObserver) Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

Response (com.apollographql.apollo.api.Response)67 Test (org.junit.Test)55 ApolloException (com.apollographql.apollo.exception.ApolloException)34 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)29 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)26 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)26 IOException (java.io.IOException)23 MockResponse (okhttp3.mockwebserver.MockResponse)22 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)18 ParseException (java.text.ParseException)17 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)12 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)12 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)11 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Nonnull (javax.annotation.Nonnull)7 TestObserver (io.reactivex.observers.TestObserver)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 TestSubscriber (rx.observers.TestSubscriber)5 Handler (android.os.Handler)4