use of com.apollographql.apollo.internal.response.RealResponseReader 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
}
}
use of com.apollographql.apollo.internal.response.RealResponseReader 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
}
}
use of com.apollographql.apollo.internal.response.RealResponseReader 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.internal.response.RealResponseReader 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.internal.response.RealResponseReader 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
}
}
Aggregations