Search in sources :

Example 1 with RealResponseReader

use of com.apollographql.apollo.internal.response.RealResponseReader in project apollo-android by apollographql.

the class ResponseReaderTest method readBooleanList.

@Test
public void readBooleanList() 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(true, false, true));
    recordSet.put("successFieldName", asList(false, false));
    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.readBoolean();
        }
    })).isEqualTo(asList(true, false, true));
    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 2 with RealResponseReader

use of com.apollographql.apollo.internal.response.RealResponseReader in project apollo-android by apollographql.

the class ResponseReaderTest method readStringList.

@Test
public void readStringList() 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("value1", "value2", "value3"));
    recordSet.put("successFieldName", asList("value4", "value5"));
    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.readString();
        }
    })).isEqualTo(asList("value1", "value2", "value3"));
    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 3 with RealResponseReader

use of com.apollographql.apollo.internal.response.RealResponseReader in project apollo-android by apollographql.

the class ResponseReaderTest method readObjectList.

@Test
public void readObjectList() throws Exception {
    ResponseField successField = ResponseField.forList("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forList("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    final Object responseObject1 = new Object();
    final Object responseObject2 = new Object();
    final Object responseObject3 = new Object();
    final Object responseObject4 = new Object();
    final Object responseObject5 = new Object();
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", asList(responseObject1, responseObject2, responseObject3));
    recordSet.put("successFieldName", asList(responseObject4, responseObject5));
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(successField, new ResponseReader.ListReader() {

        int index = 0;

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

                @Override
                public Object read(ResponseReader reader) {
                    return ((List) recordSet.get("successFieldResponseName")).get(index++);
                }
            });
        }
    })).isEqualTo(asList(responseObject1, responseObject2, responseObject3));
    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 : HashMap(java.util.HashMap) ResponseField(com.apollographql.apollo.api.ResponseField) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) Arrays.asList(java.util.Arrays.asList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with RealResponseReader

use of com.apollographql.apollo.internal.response.RealResponseReader in project apollo-android by apollographql.

the class ResponseReaderTest method readListOfScalarList.

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

        @Override
        public List<String> read(ResponseReader.ListItemReader reader) {
            return reader.readList(new ResponseReader.ListReader<String>() {

                @Override
                public String read(ResponseReader.ListItemReader reader) {
                    return reader.readString();
                }
            });
        }
    })).isEqualTo(asList(asList("1", "2"), asList("3", "4", "5")));
    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 : HashMap(java.util.HashMap) 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 5 with RealResponseReader

use of com.apollographql.apollo.internal.response.RealResponseReader in project apollo-android by apollographql.

the class ResponseReaderTest method readIntList.

@Test
public void readIntList() 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)

Aggregations

RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)16 HashMap (java.util.HashMap)16 Map (java.util.Map)15 ResponseField (com.apollographql.apollo.api.ResponseField)13 ResponseReader (com.apollographql.apollo.api.ResponseReader)13 Test (org.junit.Test)13 List (java.util.List)4 MapFieldValueResolver (com.apollographql.apollo.internal.field.MapFieldValueResolver)3 Arrays.asList (java.util.Arrays.asList)3 Error (com.apollographql.apollo.api.Error)2 ScalarType (com.apollographql.apollo.api.ScalarType)1 UnmodifiableMapBuilder (com.apollographql.apollo.api.internal.UnmodifiableMapBuilder)1 BufferedSourceJsonReader (com.apollographql.apollo.internal.json.BufferedSourceJsonReader)1 ResponseJsonStreamReader (com.apollographql.apollo.internal.json.ResponseJsonStreamReader)1 CustomTypeAdapter (com.apollographql.apollo.response.CustomTypeAdapter)1 CustomTypeValue (com.apollographql.apollo.response.CustomTypeValue)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1