use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class HttpCacheTest method networkFirst.
@Test
public void networkFirst() throws Exception {
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();
}
});
assertThat(server.getRequestCount()).isEqualTo(1);
assertThat(lastHttResponse.networkResponse()).isNotNull();
assertThat(lastHttResponse.cacheResponse()).isNull();
checkCachedResponse("/HttpCacheTestAllPlanets.json");
enqueueResponse("/HttpCacheTestAllPlanets.json");
Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.NETWORK_FIRST)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {
@Override
public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
assertThat(server.getRequestCount()).isEqualTo(2);
assertThat(lastHttResponse.networkResponse()).isNotNull();
assertThat(lastHttResponse.cacheResponse()).isNull();
checkCachedResponse("/HttpCacheTestAllPlanets.json");
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class HttpCacheTest method networkOnly_responseWithGraphError_noCached.
@Test
public void networkOnly_responseWithGraphError_noCached() throws Exception {
enqueueResponse("/ResponseError.json");
Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.NETWORK_ONLY)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {
@Override
public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
return response.hasErrors();
}
});
assertThat(server.getRequestCount()).isEqualTo(1);
assertThat(lastHttResponse.networkResponse()).isNotNull();
assertThat(lastHttResponse.cacheResponse()).isNull();
checkNoCachedResponse();
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class HttpCacheTest method cacheNetworkError.
@Test
public void cacheNetworkError() throws IOException, ApolloException {
server.enqueue(new MockResponse().setResponseCode(504).setBody(""));
Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertError(Exception.class);
checkNoCachedResponse();
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)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {
@Override
public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class OptimisticCacheTestCase method full_persisted_partial_optimistic.
@Test
public void full_persisted_partial_optimistic() throws Exception {
enqueueAndAssertResponse(server, "HeroNameWithEnumsResponse.json", apolloClient.query(new HeroNameWithEnumsQuery()), new Predicate<Response<HeroNameWithEnumsQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithEnumsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
UUID mutationId = UUID.randomUUID();
apolloClient.apolloStore().writeOptimisticUpdates(new HeroNameQuery(), new HeroNameQuery.Data(new HeroNameQuery.Hero("Droid", "R22-D22")), mutationId).execute();
assertResponse(apolloClient.query(new HeroNameWithEnumsQuery()).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithEnumsQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithEnumsQuery.Data> response) throws Exception {
assertThat(response.data().hero().name()).isEqualTo("R22-D22");
assertThat(response.data().hero().firstAppearsIn()).isEqualTo(Episode.EMPIRE);
assertThat(response.data().hero().appearsIn()).isEqualTo(Arrays.asList(Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI));
return true;
}
});
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId).execute();
assertResponse(apolloClient.query(new HeroNameWithEnumsQuery()).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithEnumsQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithEnumsQuery.Data> response) throws Exception {
assertThat(response.data().hero().name()).isEqualTo("R2-D2");
assertThat(response.data().hero().firstAppearsIn()).isEqualTo(Episode.EMPIRE);
assertThat(response.data().hero().appearsIn()).isEqualTo(Arrays.asList(Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI));
return true;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class OptimisticCacheTestCase method two_optimistic_reverse_rollback_order.
@Test
public void two_optimistic_reverse_rollback_order() 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();
}
});
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();
}
});
HeroAndFriendsNamesWithIDsQuery.Data data1 = new HeroAndFriendsNamesWithIDsQuery.Data(new HeroAndFriendsNamesWithIDsQuery.Hero("Droid", "2001", "R222-D222", Arrays.asList(new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1000", "Robocop"), new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1003", "Batman"))));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();
HeroNameWithIdQuery.Data data2 = new HeroNameWithIdQuery.Data(new HeroNameWithIdQuery.Hero("Human", "1000", "Spiderman"));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).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("Spiderman");
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("Spiderman");
return true;
}
});
// rollback query2 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(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("Robocop");
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("Robocop");
return true;
}
});
// rollback query1 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId1).execute();
// check if query1 see the latest non-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("SuperMan");
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 non-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;
}
});
}
Aggregations