Search in sources :

Example 11 with ResponseReader

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

the class ResponseReaderTest method readConditional.

@Test
public void readConditional() throws Exception {
    final Object responseObject1 = new Object();
    final Object responseObject2 = new Object();
    ResponseField successField = ResponseField.forFragment("successFieldResponseName", "successFieldName", Collections.<String>emptyList());
    ResponseField classCastExceptionField = ResponseField.forFragment("classCastExceptionFieldResponseName", "classCastExceptionFieldName", Collections.<String>emptyList());
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", "responseObject1");
    recordSet.put("successFieldName", "responseObject2");
    recordSet.put("classCastExceptionFieldResponseName", 1);
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readConditional(successField, new ResponseReader.ConditionalTypeReader<Object>() {

        @Override
        public Object read(String conditionalType, ResponseReader reader) {
            if (conditionalType.equals("responseObject1")) {
                return responseObject1;
            } else {
                return responseObject2;
            }
        }
    })).isEqualTo(responseObject1);
    try {
        responseReader.readConditional(classCastExceptionField, new ResponseReader.ConditionalTypeReader<Object>() {

            @Override
            public Object read(String conditionalType, ResponseReader 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 12 with ResponseReader

use of com.apollographql.apollo.api.ResponseReader 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 13 with ResponseReader

use of com.apollographql.apollo.api.ResponseReader 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 14 with ResponseReader

use of com.apollographql.apollo.api.ResponseReader 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)

Example 15 with ResponseReader

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

the class ApolloExceptionTest method setUp.

@Before
public void setUp() {
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().connectTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS).readTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS).build()).build();
    emptyQuery = new Query() {

        OperationName operationName = new OperationName() {

            @Override
            public String name() {
                return "emptyQuery";
            }
        };

        @Override
        public String queryDocument() {
            return "";
        }

        @Override
        public Variables variables() {
            return EMPTY_VARIABLES;
        }

        @Override
        public ResponseFieldMapper<Data> responseFieldMapper() {
            return new ResponseFieldMapper<Data>() {

                @Override
                public Data map(ResponseReader responseReader) {
                    return null;
                }
            };
        }

        @Nonnull
        @Override
        public OperationName name() {
            return operationName;
        }

        @Nonnull
        @Override
        public String operationId() {
            return "";
        }

        @Override
        public Object wrapData(Data data) {
            return data;
        }
    };
}
Also used : ResponseFieldMapper(com.apollographql.apollo.api.ResponseFieldMapper) Query(com.apollographql.apollo.api.Query) OperationName(com.apollographql.apollo.api.OperationName) Nonnull(javax.annotation.Nonnull) ResponseReader(com.apollographql.apollo.api.ResponseReader) Before(org.junit.Before)

Aggregations

ResponseReader (com.apollographql.apollo.api.ResponseReader)15 ResponseField (com.apollographql.apollo.api.ResponseField)13 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)13 HashMap (java.util.HashMap)13 Map (java.util.Map)13 Test (org.junit.Test)13 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 OperationName (com.apollographql.apollo.api.OperationName)2 Query (com.apollographql.apollo.api.Query)2 ResponseFieldMapper (com.apollographql.apollo.api.ResponseFieldMapper)2 Nonnull (javax.annotation.Nonnull)2 Before (org.junit.Before)2 UnmodifiableMapBuilder (com.apollographql.apollo.api.internal.UnmodifiableMapBuilder)1