Search in sources :

Example 1 with ResponseField

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

the class ResponseReaderTest method readCustomWithDecodedNullValue.

@Test
public void readCustomWithDecodedNullValue() throws Exception {
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("responseName", "http:://");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    ResponseField field = ResponseField.forCustomType("responseName", "fieldName", null, false, URL_CUSTOM_TYPE, NO_CONDITIONS);
    try {
        responseReader.readCustomType((ResponseField.CustomTypeField) field);
        fail("expected NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    field = ResponseField.forCustomType("responseName", "fieldName", null, true, URL_CUSTOM_TYPE, NO_CONDITIONS);
    Truth.assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) field)).isNull();
}
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 2 with ResponseField

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

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

the class ResponseReaderTest method readBoolean.

@Test
public void readBoolean() throws Exception {
    ResponseField successField = ResponseField.forBoolean("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forBoolean("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", true);
    recordSet.put("successFieldName", false);
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readBoolean(successField)).isTrue();
    try {
        responseReader.readBoolean(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 4 with ResponseField

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

the class ResponseReaderTest method readLong.

@Test
public void readLong() throws Exception {
    ResponseField successField = ResponseField.forLong("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forLong("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.readLong(successField)).isEqualTo(1);
    try {
        responseReader.readLong(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 5 with ResponseField

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

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