Search in sources :

Example 11 with ResponseField

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

the class ResponseReaderTest method readScalarListWithNulls.

@Test
public void readScalarListWithNulls() throws Exception {
    ResponseField scalarList = ResponseField.forList("list", "list", null, false, NO_CONDITIONS);
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("list", asList(null, "item1", "item2", null, "item3", null));
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(scalarList, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readString();
        }
    })).isEqualTo(asList("item1", "item2", "item3"));
}
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 12 with ResponseField

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

the class ResponseReaderTest method readCustomObjectList.

@Test
public void readCustomObjectList() throws Exception {
    ResponseField successField = ResponseField.forCustomType("successFieldResponseName", "successFieldName", null, false, OBJECT_CUSTOM_TYPE, NO_CONDITIONS);
    Map<String, Object> objectMap = new HashMap<>();
    objectMap.put("string", "string");
    objectMap.put("boolean", true);
    List<?> objectList = asList(objectMap, objectMap);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", objectList);
    recordSet.put("successFieldName", objectList);
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) successField)).isEqualTo("[{\"boolean\":true,\"string\":\"string\"},{\"boolean\":true,\"string\":\"string\"}]");
}
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 13 with ResponseField

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

the class ResponseReaderTest method readLongList.

@Test
public void readLongList() 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.readInt();
        }
    })).isEqualTo(asList(1, 2, 3));
    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 14 with ResponseField

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

the class ResponseReaderTest method readCustomList.

@Test
public void readCustomList() 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("2017-04-16", "2017-04-17", "2017-04-18"));
    recordSet.put("successFieldName", asList("2017-04-19", "2017-04-20"));
    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.readCustomType(DATE_CUSTOM_TYPE);
        }
    })).isEqualTo(asList(DATE_TIME_FORMAT.parse("2017-04-16"), DATE_TIME_FORMAT.parse("2017-04-17"), DATE_TIME_FORMAT.parse("2017-04-18")));
    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 15 with ResponseField

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

the class ResponseReaderTest method readInt.

@Test
public void readInt() throws Exception {
    ResponseField successField = ResponseField.forInt("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forInt("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", BigDecimal.valueOf(1));
    recordSet.put("successFieldName", BigDecimal.valueOf(2));
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readInt(successField)).isEqualTo(1);
    try {
        responseReader.readInt(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)

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