Search in sources :

Example 21 with Response

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

the class ResponseWriteTestCase method listOfList.

@Test
public void listOfList() throws Exception {
    StarshipByIdQuery query = new StarshipByIdQuery("Starship1");
    enqueueAndAssertResponse(server, "StarshipByIdResponse.json", apolloClient.query(query), new Predicate<Response<StarshipByIdQuery.Data>>() {

        @Override
        public boolean test(Response<StarshipByIdQuery.Data> response) throws Exception {
            assertThat(response.data().starship().__typename()).isEqualTo("Starship");
            assertThat(response.data().starship().name()).isEqualTo("SuperRocket");
            assertThat(response.data().starship().coordinates()).hasSize(3);
            assertThat(response.data().starship().coordinates()).containsExactly(asList(100d, 200d), asList(300d, 400d), asList(500d, 600d));
            return true;
        }
    });
    StarshipByIdQuery.Starship starship = new StarshipByIdQuery.Starship("Starship", "Starship1", "SuperRocket", asList(asList(900d, 800d), asList(700d, 600d)));
    apolloClient.apolloStore().write(query, new StarshipByIdQuery.Data(starship)).execute();
    assertCachedQueryResponse(query, new Predicate<Response<StarshipByIdQuery.Data>>() {

        @Override
        public boolean test(Response<StarshipByIdQuery.Data> response) throws Exception {
            assertThat(response.data().starship().__typename()).isEqualTo("Starship");
            assertThat(response.data().starship().name()).isEqualTo("SuperRocket");
            assertThat(response.data().starship().coordinates()).hasSize(2);
            assertThat(response.data().starship().coordinates()).containsExactly(asList(900d, 800d), asList(700d, 600d));
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) StarshipByIdQuery(com.apollographql.apollo.integration.normalizer.StarshipByIdQuery) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 22 with Response

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

the class ResponseWriteTestCase method operation_with_fragments.

@Test
public void operation_with_fragments() throws Exception {
    HeroAndFriendsWithFragmentsQuery query = new HeroAndFriendsWithFragmentsQuery(Input.fromNullable(Episode.NEWHOPE));
    enqueueAndAssertResponse(server, "HeroAndFriendsWithFragmentResponse.json", apolloClient.query(query), new Predicate<Response<HeroAndFriendsWithFragmentsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsWithFragmentsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().id()).isEqualTo("2001");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends()).hasSize(3);
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1000");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("Luke Skywalker");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1002");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Han Solo");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().id()).isEqualTo("1003");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().name()).isEqualTo("Leia Organa");
            return true;
        }
    });
    HeroAndFriendsWithFragmentsQuery.Hero hero = new HeroAndFriendsWithFragmentsQuery.Hero("Droid", new HeroAndFriendsWithFragmentsQuery.Hero.Fragments(new HeroWithFriendsFragment("Droid", "2001", "R222-D222", asList(new HeroWithFriendsFragment.Friend("Human", new HeroWithFriendsFragment.Friend.Fragments(new HumanWithIdFragment("Human", "1006", "SuperMan"))), new HeroWithFriendsFragment.Friend("Human", new HeroWithFriendsFragment.Friend.Fragments(new HumanWithIdFragment("Human", "1004", "Beast")))))));
    apolloClient.apolloStore().write(query, new HeroAndFriendsWithFragmentsQuery.Data(hero)).execute();
    assertCachedQueryResponse(query, new Predicate<Response<HeroAndFriendsWithFragmentsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsWithFragmentsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().id()).isEqualTo("2001");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends()).hasSize(2);
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1006");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("SuperMan");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1004");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Beast");
            return true;
        }
    });
}
Also used : HeroAndFriendsWithFragmentsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsWithFragmentsQuery) ParseException(java.text.ParseException) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) HeroWithFriendsFragment(com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment) HumanWithIdFragment(com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment) Test(org.junit.Test)

Example 23 with Response

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

the class RxApolloTest method prefetchCompletes.

@Test
public void prefetchCompletes() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
    RxApollo.from(apolloClient.prefetch(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE)))).subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(0);
    testSubscriber.assertCompleted();
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 24 with Response

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

the class RxApolloTest method queryWatcherNotCalledWhenCanceled.

@Test
public void queryWatcherNotCalledWhenCanceled() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_CHANGE));
    final TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
    TestScheduler scheduler = new TestScheduler();
    Subscription subscription = RxApollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).observeOn(scheduler).subscribe(testSubscriber);
    scheduler.triggerActions();
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    subscription.unsubscribe();
    scheduler.triggerActions();
    testSubscriber.assertValueCount(1);
    assertThat(testSubscriber.getOnNextEvents().get(0).data().hero().name()).isEqualTo("R2-D2");
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Subscription(rx.Subscription) Test(org.junit.Test)

Example 25 with Response

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

the class RxApolloTest method queryWatcherUpdatedSameQueryDifferentResults.

@Test
public void queryWatcherUpdatedSameQueryDifferentResults() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_CHANGE));
    final TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
    RxApollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).subscribe(testSubscriber);
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    testSubscriber.assertValueCount(2);
    assertThat(testSubscriber.getOnNextEvents().get(0).data().hero().name()).isEqualTo("R2-D2");
    assertThat(testSubscriber.getOnNextEvents().get(1).data().hero().name()).isEqualTo("Artoo");
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestSubscriber(rx.observers.TestSubscriber) 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