use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class NormalizedCacheTestCase method readFragmentFromStore.
@Test
public void readFragmentFromStore() throws Exception {
enqueueAndAssertResponse(server, "HeroAndFriendsWithFragmentResponse.json", apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
HeroWithFriendsFragment heroWithFriendsFragment = apolloClient.apolloStore().read(new HeroWithFriendsFragment.Mapper(), CacheKey.from("2001"), Operation.EMPTY_VARIABLES).execute();
assertThat(heroWithFriendsFragment.id()).isEqualTo("2001");
assertThat(heroWithFriendsFragment.name()).isEqualTo("R2-D2");
assertThat(heroWithFriendsFragment.friends()).hasSize(3);
assertThat(heroWithFriendsFragment.friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1000");
assertThat(heroWithFriendsFragment.friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("Luke Skywalker");
assertThat(heroWithFriendsFragment.friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1002");
assertThat(heroWithFriendsFragment.friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Han Solo");
assertThat(heroWithFriendsFragment.friends().get(2).fragments().humanWithIdFragment().id()).isEqualTo("1003");
assertThat(heroWithFriendsFragment.friends().get(2).fragments().humanWithIdFragment().name()).isEqualTo("Leia Organa");
HumanWithIdFragment fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1000"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1000");
assertThat(fragment.name()).isEqualTo("Luke Skywalker");
fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1002"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1002");
assertThat(fragment.name()).isEqualTo("Han Solo");
fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1003"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1003");
assertThat(fragment.name()).isEqualTo("Leia Organa");
}
use of com.apollographql.apollo.api.Response 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;
}
});
}
use of com.apollographql.apollo.api.Response 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;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class NormalizedCacheTestCase method listOfList.
@Test
public void listOfList() throws Exception {
enqueueAndAssertResponse(server, "StarshipByIdResponse.json", apolloClient.query(new StarshipByIdQuery("Starship1")), 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;
}
});
assertResponse(apolloClient.query(new StarshipByIdQuery("Starship1")).responseFetcher(CACHE_ONLY), 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;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class NormalizedCacheTestCase method fromCacheFlag.
@Test
public void fromCacheFlag() throws Exception {
enqueueAndAssertResponse(server, "HeroNameResponse.json", apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {
@Override
public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return !response.fromCache();
}
});
enqueueAndAssertResponse(server, "HeroNameResponse.json", apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))).responseFetcher(NETWORK_ONLY), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {
@Override
public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return !response.fromCache();
}
});
assertResponse(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))).responseFetcher(CACHE_ONLY), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {
@Override
public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.fromCache();
}
});
assertResponse(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))).responseFetcher(CACHE_FIRST), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {
@Override
public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.fromCache();
}
});
assertResponse(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE))).responseFetcher(NETWORK_FIRST), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {
@Override
public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.fromCache();
}
});
}
Aggregations