Search in sources :

Example 41 with EpisodeHeroNameQuery

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

the class ApolloInterceptorChainTest method onProceedAsyncCalled_chainPassesControlToInterceptor.

@Test
public void onProceedAsyncCalled_chainPassesControlToInterceptor() throws TimeoutException, InterruptedException {
    final AtomicInteger counter = new AtomicInteger(1);
    EpisodeHeroNameQuery query = createQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
            counter.decrementAndGet();
        }

        @Override
        public void dispose() {
        }
    };
    List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
    RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
    chain.proceedAsync(ApolloInterceptor.InterceptorRequest.builder(query).fetchFromCache(false).build(), Utils.immediateExecutor(), new CallBack() {

        @Override
        public void onResponse(@Nonnull InterceptorResponse response) {
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
        }
    });
    // which means the test should fail.
    if (counter.get() != 0) {
        Assert.fail("Control not passed to the interceptor");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 42 with EpisodeHeroNameQuery

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

the class ResponseNormalizationTest method testHeroNameWithVariable.

@Test
public void testHeroNameWithVariable() throws Exception {
    assertHasNoErrors("EpisodeHeroNameResponse.json", new EpisodeHeroNameQuery(Input.fromNullable(JEDI)));
    Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
    CacheReference reference = (CacheReference) record.field(TEST_FIELD_KEY_JEDI);
    assertThat(reference).isEqualTo(new CacheReference(TEST_FIELD_KEY_JEDI));
    final Record heroRecord = normalizedCache.loadRecord(reference.key(), CacheHeaders.NONE);
    assertThat(heroRecord.field("name")).isEqualTo("R2-D2");
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Test(org.junit.Test)

Example 43 with EpisodeHeroNameQuery

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

the class Rx2ApolloTest method queryWatcherNotUpdatedSameQuerySameResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherNotUpdatedSameQuerySameResults() 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_WITH_ID));
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    observer.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 : 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) Test(org.junit.Test)

Example 44 with EpisodeHeroNameQuery

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

the class Rx2ApolloTest method queryWatcherUpdatedDifferentQueryDifferentResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherUpdatedDifferentQueryDifferentResults() 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("HeroAndFriendsNameWithIdsNameChange.json"));
    apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(NEWHOPE))).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) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) TestObserver(io.reactivex.observers.TestObserver) Predicate(io.reactivex.functions.Predicate) Test(org.junit.Test)

Example 45 with EpisodeHeroNameQuery

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

the class CacheFirstFetcherTest method enqueue.

@Test
public void enqueue() throws IOException, ApolloException, TimeoutException, InterruptedException {
    EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
    TrackingCallback trackingCallback;
    // Has error when cache empty, and network error
    server.enqueue(new MockResponse().setResponseCode(HTTP_INTERNAL_ERROR).setBody("Server Error"));
    trackingCallback = new TrackingCallback();
    apolloClient.query(query).responseFetcher(CACHE_FIRST).enqueue(trackingCallback);
    assertThat(trackingCallback.exceptions.size()).isEqualTo(1);
    // Goes to network when empty
    trackingCallback = new TrackingCallback();
    server.enqueue(mockResponse("HeroNameResponse.json"));
    apolloClient.query(query).responseFetcher(CACHE_FIRST).enqueue(trackingCallback);
    assertThat(trackingCallback.exceptions).isEmpty();
    assertThat(trackingCallback.responseList.size()).isEqualTo(1);
    assertThat(trackingCallback.responseList.get(0).fromCache()).isFalse();
    assertThat(trackingCallback.responseList.get(0).data().hero().name()).isEqualTo("R2-D2");
    assertThat(server.getRequestCount()).isEqualTo(2);
    // Hits only cache after populated
    trackingCallback = new TrackingCallback();
    apolloClient.query(query).responseFetcher(CACHE_FIRST).enqueue(trackingCallback);
    assertThat(trackingCallback.exceptions).isEmpty();
    assertThat(trackingCallback.responseList.size()).isEqualTo(1);
    assertThat(trackingCallback.responseList.get(0).fromCache()).isTrue();
    assertThat(trackingCallback.responseList.get(0).data().hero().name()).isEqualTo("R2-D2");
    assertThat(server.getRequestCount()).isEqualTo(2);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) 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