Search in sources :

Example 6 with HeroAndFriendsNamesWithIDsQuery

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

the class NormalizedCacheTestCase method removeMultipleFromStore.

@Test
public void removeMultipleFromStore() 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 {
            assertThat(response.data().hero().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().friends()).hasSize(3);
            return true;
        }
    });
    assertResponse(apolloClient.query(new CharacterNameByIdQuery("1000")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterNameByIdQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterNameByIdQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Luke Skywalker");
            return true;
        }
    });
    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.fromCache()).isTrue();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Han Solo");
            return true;
        }
    });
    assertResponse(apolloClient.query(new CharacterNameByIdQuery("1003")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterNameByIdQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterNameByIdQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Leia Organa");
            return true;
        }
    });
    assertThat(apolloClient.apolloStore().remove(asList(CacheKey.from("1002"), CacheKey.from("1000"))).execute()).isEqualTo(2);
    assertResponse(apolloClient.query(new CharacterNameByIdQuery("1000")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterNameByIdQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterNameByIdQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(response.data()).isNull();
            return true;
        }
    });
    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.fromCache()).isTrue();
            assertThat(response.data()).isNull();
            return true;
        }
    });
    assertResponse(apolloClient.query(new CharacterNameByIdQuery("1003")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterNameByIdQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterNameByIdQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Leia Organa");
            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 7 with HeroAndFriendsNamesWithIDsQuery

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

the class NormalizedCacheTestCase method removeFromStore.

@Test
public void removeFromStore() 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 {
            assertThat(response.data().hero().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().friends()).hasSize(3);
            return true;
        }
    });
    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.fromCache()).isTrue();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Han Solo");
            return true;
        }
    });
    // test remove root query object
    assertThat(apolloClient.apolloStore().remove(CacheKey.from("2001")).execute()).isTrue();
    assertResponse(apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(response.data()).isNull();
            return true;
        }
    });
    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.fromCache()).isTrue();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Han Solo");
            return true;
        }
    });
    // test remove object from the list
    assertThat(apolloClient.apolloStore().remove(CacheKey.from("1002")).execute()).isTrue();
    assertResponse(apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(response.data()).isNull();
            return true;
        }
    });
    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.fromCache()).isTrue();
            assertThat(response.data()).isNull();
            return true;
        }
    });
    assertResponse(apolloClient.query(new CharacterNameByIdQuery("1003")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterNameByIdQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterNameByIdQuery.Data> response) throws Exception {
            assertThat(response.fromCache()).isTrue();
            assertThat(response.data()).isNotNull();
            assertThat(((CharacterNameByIdQuery.AsHuman) response.data().character()).name()).isEqualTo("Leia Organa");
            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 8 with HeroAndFriendsNamesWithIDsQuery

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

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

the class ResponseNormalizationTest method testHeroAndFriendsNamesQueryWithIDs.

@Test
public void testHeroAndFriendsNamesQueryWithIDs() throws Exception {
    assertHasNoErrors("HeroAndFriendsNameWithIdsResponse.json", new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(JEDI)));
    Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
    CacheReference heroReference = (CacheReference) record.field(TEST_FIELD_KEY_JEDI);
    assertThat(heroReference).isEqualTo(new CacheReference("2001"));
    final Record heroRecord = normalizedCache.loadRecord(heroReference.key(), CacheHeaders.NONE);
    assertThat(heroRecord.field("name")).isEqualTo("R2-D2");
    assertThat(heroRecord.field("friends")).isEqualTo(Arrays.asList(new CacheReference("1000"), new CacheReference("1002"), new CacheReference("1003")));
    final Record luke = normalizedCache.loadRecord("1000", CacheHeaders.NONE);
    assertThat(luke.field("name")).isEqualTo("Luke Skywalker");
}
Also used : HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Test(org.junit.Test)

Example 10 with HeroAndFriendsNamesWithIDsQuery

use of com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery 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)

Aggregations

HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)13 Test (org.junit.Test)13 Response (com.apollographql.apollo.api.Response)11 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)10 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)9 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)8 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)6 ApolloException (com.apollographql.apollo.exception.ApolloException)4 CharacterNameByIdQuery (com.apollographql.apollo.integration.normalizer.CharacterNameByIdQuery)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 IOException (java.io.IOException)3 HeroNameWithIdQuery (com.apollographql.apollo.integration.normalizer.HeroNameWithIdQuery)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)1 Record (com.apollographql.apollo.cache.normalized.Record)1 CharacterDetailsQuery (com.apollographql.apollo.integration.normalizer.CharacterDetailsQuery)1 HeroWithFriendsFragment (com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment)1 HumanWithIdFragment (com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment)1