Search in sources :

Example 11 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method cacheOnlyMiss.

@Test
public void cacheOnlyMiss() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY)).test().assertError(ApolloHttpException.class);
}
Also used : AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Example 12 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class IntegrationTest method operationJsonWriter.

@Test
public void operationJsonWriter() throws Exception {
    String json = Utils.readFileToString(getClass(), "/OperationJsonWriter.json");
    AllPlanetsQuery query = new AllPlanetsQuery();
    Response<AllPlanetsQuery.Data> response = new OperationResponseParser<>(query, query.responseFieldMapper(), new ScalarTypeAdapters(Collections.EMPTY_MAP)).parse(new Buffer().writeUtf8(json));
    Buffer buffer = new Buffer();
    OperationJsonWriter writer = new OperationJsonWriter(response.data(), new ScalarTypeAdapters(Collections.EMPTY_MAP));
    JsonWriter jsonWriter = JsonWriter.of(buffer);
    jsonWriter.setIndent("  ");
    writer.write(jsonWriter);
    assertThat(buffer.readUtf8()).isEqualTo(json);
}
Also used : Buffer(okio.Buffer) OperationJsonWriter(com.apollographql.apollo.response.OperationJsonWriter) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) OperationJsonWriter(com.apollographql.apollo.response.OperationJsonWriter) JsonWriter(com.apollographql.apollo.internal.json.JsonWriter) Test(org.junit.Test)

Example 13 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class NormalizedCacheTestCase method cacheResponseWithNullableFields.

@Test
public void cacheResponseWithNullableFields() throws Exception {
    enqueueAndAssertResponse(server, "AllPlanetsNullableField.json", apolloClient.query(new AllPlanetsQuery()).responseFetcher(NETWORK_ONLY), new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            assertThat(response).isNotNull();
            assertThat(response.hasErrors()).isFalse();
            return true;
        }
    });
    assertResponse(apolloClient.query(new AllPlanetsQuery()).responseFetcher(CACHE_ONLY), new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            assertThat(response).isNotNull();
            assertThat(response.hasErrors()).isFalse();
            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) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Example 14 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method cacheStale.

@Test
@SuppressWarnings("CheckReturnValue")
public void cacheStale() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test();
    assertThat(server.getRequestCount()).isEqualTo(1);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test();
    assertThat(server.getRequestCount()).isEqualTo(2);
    assertThat(lastHttResponse.networkResponse()).isNotNull();
    assertThat(lastHttResponse.cacheResponse()).isNull();
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
}
Also used : AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Example 15 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method cacheNonStale.

@Test
@SuppressWarnings("CheckReturnValue")
public void cacheNonStale() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test();
    assertThat(server.takeRequest()).isNotNull();
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test();
    assertThat(server.getRequestCount()).isEqualTo(1);
    assertThat(lastHttResponse.networkResponse()).isNull();
    assertThat(lastHttResponse.cacheResponse()).isNotNull();
}
Also used : AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Aggregations

AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)21 Test (org.junit.Test)21 Response (com.apollographql.apollo.api.Response)12 MockResponse (okhttp3.mockwebserver.MockResponse)12 ApolloException (com.apollographql.apollo.exception.ApolloException)11 IOException (java.io.IOException)11 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)10 ParseException (java.text.ParseException)10 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)2 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)2 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)2 Buffer (okio.Buffer)2 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)1 DiskLruHttpCacheStore (com.apollographql.apollo.cache.http.DiskLruHttpCacheStore)1 Record (com.apollographql.apollo.cache.normalized.Record)1 JsonWriter (com.apollographql.apollo.internal.json.JsonWriter)1 OperationJsonWriter (com.apollographql.apollo.response.OperationJsonWriter)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 File (java.io.File)1 Dispatcher (okhttp3.Dispatcher)1