Search in sources :

Example 31 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class ApolloWatcherTest method testQueryWatcherNotUpdated_SameQuery_SameResults.

@Test
public void testQueryWatcherNotUpdated_SameQuery_SameResults() 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());
        }
    });
    server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
    apolloClient.query(query).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) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 32 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class ApolloWatcherTest method testQueryWatcherUpdated_SameQuery_DifferentResults.

@Test
public void testQueryWatcherUpdated_SameQuery_DifferentResults() 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());
        }
    });
    // Another newer call gets updated information
    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();
        }
    });
    watcher.cancel();
    assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
    assertThat(heroNameList.get(1)).isEqualTo("Artoo");
    assertThat(heroNameList.size()).isEqualTo(2);
}
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)

Example 33 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery 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 34 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class RxApolloTest method prefetchIsCanceledWhenDisposed.

@Test
public void prefetchIsCanceledWhenDisposed() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    final TestSubscriber<Void> testSubscriber = new TestSubscriber<>();
    TestScheduler scheduler = new TestScheduler();
    Subscription subscription = RxApollo.from(apolloClient.prefetch(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE)))).observeOn(scheduler).subscribe(new Action0() {

        @Override
        public void call() {
            testSubscriber.onCompleted();
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            testSubscriber.onError(throwable);
        }
    });
    subscription.unsubscribe();
    scheduler.triggerActions();
    testSubscriber.assertNotCompleted();
    testSubscriber.assertValueCount(0);
    testSubscriber.assertNoErrors();
    assertThat(subscription.isUnsubscribed()).isTrue();
}
Also used : Action0(rx.functions.Action0) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Subscription(rx.Subscription) Test(org.junit.Test)

Example 35 with EpisodeHeroNameQuery

use of com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery in project apollo-android by apollographql.

the class RxApolloTest method callProducesValue.

@Test
public void callProducesValue() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
    RxApollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE)))).subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertCompleted();
    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) Test(org.junit.Test)

Aggregations

EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)46 Test (org.junit.Test)44 ApolloException (com.apollographql.apollo.exception.ApolloException)19 Response (com.apollographql.apollo.api.Response)18 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)14 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)10 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 TimeoutException (java.util.concurrent.TimeoutException)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)7 TestObserver (io.reactivex.observers.TestObserver)7 Executor (java.util.concurrent.Executor)7 Nonnull (javax.annotation.Nonnull)7 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)6 TestSubscriber (rx.observers.TestSubscriber)6 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)5 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)3 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)3