use of com.apollographql.apollo.api.ResponseReader in project apollo-android by apollographql.
the class ResponseFetcherTest method setUp.
@Before
public void setUp() {
okHttpClient = new OkHttpClient.Builder().build();
emptyQuery = new Query() {
OperationName operationName = new OperationName() {
@Override
public String name() {
return "emptyQuery";
}
};
@Override
public String queryDocument() {
return "";
}
@Override
public Variables variables() {
return EMPTY_VARIABLES;
}
@Override
public ResponseFieldMapper<Data> responseFieldMapper() {
return new ResponseFieldMapper<Data>() {
@Override
public Data map(ResponseReader responseReader) {
return null;
}
};
}
@Nonnull
@Override
public OperationName name() {
return operationName;
}
@Nonnull
@Override
public String operationId() {
return "";
}
@Override
public Object wrapData(Data data) {
return data;
}
};
}
use of com.apollographql.apollo.api.ResponseReader 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.api.ResponseReader 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.api.ResponseReader 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.ResponseReader 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
}
}
Aggregations