Search in sources :

Example 6 with ResponseField

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

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

the class ResponseReaderTest method readCustomObjectMap.

@Test
public void readCustomObjectMap() 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);
    objectMap.put("double", 1.99D);
    objectMap.put("float", 2.99F);
    objectMap.put("long", 3L);
    objectMap.put("int", 4);
    objectMap.put("stringList", asList("string1", "string2"));
    objectMap.put("booleanList", asList("true", "false"));
    objectMap.put("doubleList", asList(1.99D, 2.99D));
    objectMap.put("floatList", asList(3.99F, 4.99F, 5.99F));
    objectMap.put("longList", asList(5L, 7L));
    objectMap.put("intList", asList(8, 9, 10));
    objectMap.put("object", new HashMap<>(objectMap));
    objectMap.put("objectList", asList(new HashMap<>(objectMap), new HashMap<>(objectMap)));
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", objectMap);
    recordSet.put("successFieldName", objectMap);
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) successField)).isEqualTo("{\"string\":\"string\",\"double\":1.99,\"intList\":[8,9,10],\"doubleList\":[1.99,2.99]," + "\"float\":2.99,\"longList\":[5,7],\"long\":3,\"int\":4,\"objectList\":[{\"string\":\"string\"," + "\"double\":1.99,\"intList\":[8,9,10],\"doubleList\":[1.99,2.99],\"float\":2.99,\"longList\":[5,7]," + "\"long\":3,\"int\":4,\"boolean\":true,\"stringList\":[\"string1\",\"string2\"]," + "\"floatList\":[3.99,4.99,5.99],\"booleanList\":[\"true\",\"false\"],\"object\":{\"string\":\"string\"," + "\"double\":1.99,\"intList\":[8,9,10],\"doubleList\":[1.99,2.99],\"float\":2.99,\"longList\":[5,7]," + "\"long\":3,\"int\":4,\"boolean\":true,\"stringList\":[\"string1\",\"string2\"]," + "\"floatList\":[3.99,4.99,5.99],\"booleanList\":[\"true\",\"false\"]}},{\"string\":\"string\"," + "\"double\":1.99,\"intList\":[8,9,10],\"doubleList\":[1.99,2.99],\"float\":2.99,\"longList\":[5,7]," + "\"long\":3,\"int\":4,\"boolean\":true,\"stringList\":[\"string1\",\"string2\"]," + "\"floatList\":[3.99,4.99,5.99],\"booleanList\":[\"true\",\"false\"],\"object\":{\"string\":\"string\"," + "\"double\":1.99,\"intList\":[8,9,10],\"doubleList\":[1.99,2.99],\"float\":2.99,\"longList\":[5,7]," + "\"long\":3,\"int\":4,\"boolean\":true,\"stringList\":[\"string1\",\"string2\"]," + "\"floatList\":[3.99,4.99,5.99],\"booleanList\":[\"true\",\"false\"]}}],\"boolean\":true," + "\"stringList\":[\"string1\",\"string2\"],\"floatList\":[3.99,4.99,5.99]," + "\"booleanList\":[\"true\",\"false\"],\"object\":{\"string\":\"string\",\"double\":1.99," + "\"intList\":[8,9,10],\"doubleList\":[1.99,2.99],\"float\":2.99,\"longList\":[5,7],\"long\":3,\"int\":4," + "\"boolean\":true,\"stringList\":[\"string1\",\"string2\"],\"floatList\":[3.99,4.99,5.99]," + "\"booleanList\":[\"true\",\"false\"]}}");
}
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 8 with ResponseField

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

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

Example 10 with ResponseField

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

the class ResponseReaderTest method readCustom.

@Test
public void readCustom() throws Exception {
    ResponseField successField = ResponseField.forCustomType("successFieldResponseName", "successFieldName", null, false, DATE_CUSTOM_TYPE, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forCustomType("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, DATE_CUSTOM_TYPE, NO_CONDITIONS);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", "2017-04-16");
    recordSet.put("successFieldName", "2018-04-16");
    recordSet.put("classCastExceptionFieldResponseName", 0);
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) successField)).isEqualTo(DATE_TIME_FORMAT.parse("2017-04-16"));
    try {
        responseReader.readCustomType((ResponseField.CustomTypeField) 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