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
}
}
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\"]}}");
}
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
}
}
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
}
}
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
}
}
Aggregations