use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class CacheKeyForFieldTest method testFieldWithNonPrimitiveValue.
@Test
public void testFieldWithNonPrimitiveValue() {
// noinspection unchecked
ResponseField field = ResponseField.forString("hero", "hero", new UnmodifiableMapBuilder<String, Object>(1).put("episode", Episode.JEDI).build(), false, Collections.<ResponseField.Condition>emptyList());
Operation.Variables variables = new Operation.Variables() {
@Nonnull
@Override
public Map<String, Object> valueMap() {
return super.valueMap();
}
};
assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\"})");
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class CacheKeyForFieldTest method testFieldWithArgument.
@Test
public void testFieldWithArgument() {
// noinspection unchecked
Map<String, Object> arguments = new UnmodifiableMapBuilder<String, Object>(1).put("episode", "JEDI").build();
ResponseField field = createResponseField("hero", "hero", arguments);
Operation.Variables variables = new Operation.Variables() {
@Nonnull
@Override
public Map<String, Object> valueMap() {
return super.valueMap();
}
};
assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\"})");
}
use of com.apollographql.apollo.api.ResponseField 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
}
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class ResponseReaderTest method readString.
@Test
public void readString() throws Exception {
ResponseField successField = ResponseField.forString("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
ResponseField classCastExceptionField = ResponseField.forString("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
Map<String, Object> recordSet = new HashMap<>();
recordSet.put("successFieldResponseName", "response1");
recordSet.put("successFieldName", "response2");
recordSet.put("classCastExceptionFieldResponseName", 1);
RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
assertThat(responseReader.readString(successField)).isEqualTo("response1");
try {
responseReader.readString(classCastExceptionField);
fail("expected ClassCastException");
} catch (ClassCastException expected) {
// expected
}
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class ResponseReaderTest method readConditional.
@Test
public void readConditional() throws Exception {
final Object responseObject1 = new Object();
final Object responseObject2 = new Object();
ResponseField successField = ResponseField.forFragment("successFieldResponseName", "successFieldName", Collections.<String>emptyList());
ResponseField classCastExceptionField = ResponseField.forFragment("classCastExceptionFieldResponseName", "classCastExceptionFieldName", Collections.<String>emptyList());
Map<String, Object> recordSet = new HashMap<>();
recordSet.put("successFieldResponseName", "responseObject1");
recordSet.put("successFieldName", "responseObject2");
recordSet.put("classCastExceptionFieldResponseName", 1);
RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
assertThat(responseReader.readConditional(successField, new ResponseReader.ConditionalTypeReader<Object>() {
@Override
public Object read(String conditionalType, ResponseReader reader) {
if (conditionalType.equals("responseObject1")) {
return responseObject1;
} else {
return responseObject2;
}
}
})).isEqualTo(responseObject1);
try {
responseReader.readConditional(classCastExceptionField, new ResponseReader.ConditionalTypeReader<Object>() {
@Override
public Object read(String conditionalType, ResponseReader reader) {
return null;
}
});
fail("expected ClassCastException");
} catch (ClassCastException expected) {
// expected
}
}
Aggregations