Search in sources :

Example 51 with Response

use of com.apollographql.apollo.api.Response 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 52 with Response

use of com.apollographql.apollo.api.Response 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 53 with Response

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

the class OptimisticCacheTestCase method simple.

@Test
public void simple() throws Exception {
    HeroAndFriendsNamesQuery query = new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.JEDI));
    enqueueAndAssertResponse(server, "HeroAndFriendsNameResponse.json", apolloClient.query(query), new Predicate<Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    UUID mutationId = UUID.randomUUID();
    HeroAndFriendsNamesQuery.Data data = new HeroAndFriendsNamesQuery.Data(new HeroAndFriendsNamesQuery.Hero("Droid", "R222-D222", Arrays.asList(new HeroAndFriendsNamesQuery.Friend("Human", "SuperMan"), new HeroAndFriendsNamesQuery.Friend("Human", "Batman"))));
    apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query, data, mutationId).execute();
    assertResponse(apolloClient.query(query).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().friends()).hasSize(2);
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("SuperMan");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Batman");
            return true;
        }
    });
    apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId).execute();
    assertResponse(apolloClient.query(query).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().friends()).hasSize(3);
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Luke Skywalker");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Han Solo");
            assertThat(response.data().hero().friends().get(2).name()).isEqualTo("Leia Organa");
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) UUID(java.util.UUID) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 54 with Response

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

the class OptimisticCacheTestCase method two_optimistic_two_rollback.

@Test
public void two_optimistic_two_rollback() throws Exception {
    HeroAndFriendsNamesWithIDsQuery query1 = new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.JEDI));
    UUID mutationId1 = UUID.randomUUID();
    HeroNameWithIdQuery query2 = new HeroNameWithIdQuery();
    UUID mutationId2 = UUID.randomUUID();
    enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(query1), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    HeroAndFriendsNamesWithIDsQuery.Data data1 = new HeroAndFriendsNamesWithIDsQuery.Data(new HeroAndFriendsNamesWithIDsQuery.Hero("Droid", "2001", "R222-D222", Arrays.asList(new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1000", "SuperMan"), new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1003", "Batman"))));
    apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();
    // check if query1 see optimistic updates
    assertResponse(apolloClient.query(query1).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().id()).isEqualTo("2001");
            assertThat(response.data().hero().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().friends()).hasSize(2);
            assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("SuperMan");
            assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1003");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Batman");
            return true;
        }
    });
    enqueueAndAssertResponse(server, "HeroNameWithIdResponse.json", apolloClient.query(query2), new Predicate<Response<HeroNameWithIdQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    HeroNameWithIdQuery.Data data2 = new HeroNameWithIdQuery.Data(new HeroNameWithIdQuery.Hero("Human", "1000", "Beast"));
    apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute();
    // check if query1 see the latest optimistic updates
    assertResponse(apolloClient.query(query1).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().id()).isEqualTo("2001");
            assertThat(response.data().hero().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().friends()).hasSize(2);
            assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Beast");
            assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1003");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Batman");
            return true;
        }
    });
    // check if query2 see the latest optimistic updates
    assertResponse(apolloClient.query(query2).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithIdQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
            assertThat(response.data().hero().id()).isEqualTo("1000");
            assertThat(response.data().hero().name()).isEqualTo("Beast");
            return true;
        }
    });
    // rollback query1 optimistic updates
    apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId1).execute();
    // check if query1 see the latest optimistic updates
    assertResponse(apolloClient.query(query1).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().id()).isEqualTo("2001");
            assertThat(response.data().hero().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().friends()).hasSize(3);
            assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Beast");
            assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1002");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Han Solo");
            assertThat(response.data().hero().friends().get(2).id()).isEqualTo("1003");
            assertThat(response.data().hero().friends().get(2).name()).isEqualTo("Leia Organa");
            return true;
        }
    });
    // check if query2 see the latest optimistic updates
    assertResponse(apolloClient.query(query2).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithIdQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
            assertThat(response.data().hero().id()).isEqualTo("1000");
            assertThat(response.data().hero().name()).isEqualTo("Beast");
            return true;
        }
    });
    // rollback query2 optimistic updates
    apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId2).execute();
    // check if query2 see the latest optimistic updates
    assertResponse(apolloClient.query(query2).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithIdQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
            assertThat(response.data().hero().id()).isEqualTo("1000");
            assertThat(response.data().hero().name()).isEqualTo("SuperMan");
            return true;
        }
    });
}
Also used : IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) HeroNameWithIdQuery(com.apollographql.apollo.integration.normalizer.HeroNameWithIdQuery) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) UUID(java.util.UUID) Test(org.junit.Test)

Example 55 with Response

use of com.apollographql.apollo.api.Response 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

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