use of com.apollographql.apollo.api.ResponseReader 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.ResponseReader in project apollo-android by apollographql.
the class ResponseReaderTest method readScalarListWithNulls.
@Test
public void readScalarListWithNulls() throws Exception {
ResponseField scalarList = ResponseField.forList("list", "list", null, false, NO_CONDITIONS);
final Map<String, Object> recordSet = new HashMap<>();
recordSet.put("list", asList(null, "item1", "item2", null, "item3", null));
RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
assertThat(responseReader.readList(scalarList, new ResponseReader.ListReader() {
@Override
public Object read(ResponseReader.ListItemReader reader) {
return reader.readString();
}
})).isEqualTo(asList("item1", "item2", "item3"));
}
use of com.apollographql.apollo.api.ResponseReader in project apollo-android by apollographql.
the class ResponseReaderTest method readLongList.
@Test
public void readLongList() 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.ResponseReader in project apollo-android by apollographql.
the class ResponseReaderTest method readCustomList.
@Test
public void readCustomList() 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("2017-04-16", "2017-04-17", "2017-04-18"));
recordSet.put("successFieldName", asList("2017-04-19", "2017-04-20"));
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.readCustomType(DATE_CUSTOM_TYPE);
}
})).isEqualTo(asList(DATE_TIME_FORMAT.parse("2017-04-16"), DATE_TIME_FORMAT.parse("2017-04-17"), DATE_TIME_FORMAT.parse("2017-04-18")));
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.ResponseReader in project apollo-android by apollographql.
the class ResponseReaderTest method readObject.
@Test
public void readObject() throws Exception {
final Object responseObject1 = new Object();
final Object responseObject2 = new Object();
ResponseField successField = ResponseField.forObject("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
ResponseField classCastExceptionField = ResponseField.forObject("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
Map<String, Object> recordSet = new HashMap<>();
recordSet.put("successFieldResponseName", responseObject1);
recordSet.put("successFieldName", responseObject2);
recordSet.put("classCastExceptionFieldResponseName", "anything");
RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
assertThat(responseReader.readObject(successField, new ResponseReader.ObjectReader<Object>() {
@Override
public Object read(ResponseReader reader) {
return responseObject1;
}
})).isEqualTo(responseObject1);
try {
responseReader.readObject(classCastExceptionField, new ResponseReader.ObjectReader<Object>() {
@Override
public Object read(ResponseReader reader) {
return reader.readString(ResponseField.forString("anything", "anything", null, true, NO_CONDITIONS));
}
});
fail("expected ClassCastException");
} catch (ClassCastException expected) {
// expected
}
}
Aggregations