Search in sources :

Example 31 with ResponseField

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

the class ResponseReaderTest method readDoubleList.

@Test
public void readDoubleList() throws Exception {
    ResponseField successField = ResponseField.forList("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forList("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", asList(BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(3)));
    recordSet.put("successFieldName", asList(BigDecimal.valueOf(4), BigDecimal.valueOf(5)));
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(successField, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readDouble();
        }
    })).isEqualTo(asList(1D, 2D, 3D));
    try {
        responseReader.readList(classCastExceptionField, new ResponseReader.ListReader() {

            @Override
            public Object read(ResponseReader.ListItemReader reader) {
                return null;
            }
        });
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 32 with ResponseField

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

the class ResponseReaderTest method read_object_list_with_nulls.

@Test
public void read_object_list_with_nulls() throws Exception {
    final ResponseField listField = ResponseField.forList("list", "list", null, false, NO_CONDITIONS);
    final ResponseField indexField = ResponseField.forList("index", "index", null, false, NO_CONDITIONS);
    final List responseObjects = asList(new Object(), new Object(), new Object());
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("list", asList(null, new UnmodifiableMapBuilder<String, Object>(1).put("index", "0").build(), new UnmodifiableMapBuilder<String, Object>(1).put("index", "1").build(), null, new UnmodifiableMapBuilder<String, Object>(1).put("index", "2").build(), null));
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(listField, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readObject(new ResponseReader.ObjectReader<Object>() {

                @Override
                public Object read(ResponseReader reader) {
                    return responseObjects.get(Integer.parseInt(reader.readString(indexField)));
                }
            });
        }
    })).isEqualTo(responseObjects);
}
Also used : HashMap(java.util.HashMap) UnmodifiableMapBuilder(com.apollographql.apollo.api.internal.UnmodifiableMapBuilder) ResponseField(com.apollographql.apollo.api.ResponseField) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 33 with ResponseField

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

the class ResponseReaderTest method readDouble.

@Test
public void readDouble() throws Exception {
    ResponseField successField = ResponseField.forDouble("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forDouble("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", BigDecimal.valueOf(1.1));
    recordSet.put("successFieldName", BigDecimal.valueOf(2.2));
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readDouble(successField)).isEqualTo(1.1D);
    try {
        responseReader.readDouble(classCastExceptionField);
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 34 with ResponseField

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

the class ResponseReaderTest method readCustomListWithDecodedNull.

@Test
public void readCustomListWithDecodedNull() throws Exception {
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("responseName", asList("http://", "http://"));
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    ResponseField field = ResponseField.forList("responseName", "fieldName", null, false, NO_CONDITIONS);
    Truth.assertThat(responseReader.readList(field, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readCustomType(URL_CUSTOM_TYPE);
        }
    })).isEmpty();
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ResponseField (com.apollographql.apollo.api.ResponseField)34 Test (org.junit.Test)33 HashMap (java.util.HashMap)25 Map (java.util.Map)23 ResponseReader (com.apollographql.apollo.api.ResponseReader)13 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)13 Operation (com.apollographql.apollo.api.Operation)11 UnmodifiableMapBuilder (com.apollographql.apollo.api.internal.UnmodifiableMapBuilder)6 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 CacheKeyResolver (com.apollographql.apollo.cache.normalized.CacheKeyResolver)1 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)1 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)1 ApolloSqlHelper (com.apollographql.apollo.cache.normalized.sql.ApolloSqlHelper)1 SqlNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory)1 WebSocketSubscriptionTransport (com.apollographql.apollo.subscription.WebSocketSubscriptionTransport)1 Nonnull (javax.annotation.Nonnull)1 OkHttpClient (okhttp3.OkHttpClient)1