@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 } }
@Test public void optionalFieldsIOException() throws Exception { RealResponseReader<Map<String, Object>> responseReader = responseReader(Collections.<String, Object>emptyMap()); responseReader.readString(ResponseField.forString("stringField", "stringField", null, true, NO_CONDITIONS)); responseReader.readInt(ResponseField.forInt("intField", "intField", null, true, NO_CONDITIONS)); responseReader.readLong(ResponseField.forLong("longField", "longField", null, true, NO_CONDITIONS)); responseReader.readDouble(ResponseField.forDouble("doubleField", "doubleField", null, true, NO_CONDITIONS)); responseReader.readBoolean(ResponseField.forBoolean("booleanField", "booleanField", null, true, NO_CONDITIONS)); responseReader.readObject(ResponseField.forObject("objectField", "objectField", null, true, NO_CONDITIONS), new ResponseReader.ObjectReader<Object>() { @Override public Object read(ResponseReader reader) { return null; } }); responseReader.readList(ResponseField.forList("scalarListField", "scalarListField", null, true, NO_CONDITIONS), new ResponseReader.ListReader() { @Override public Object read(ResponseReader.ListItemReader reader) { return null; } }); responseReader.readCustomType((ResponseField.CustomTypeField) ResponseField.forCustomType("customTypeField", "customTypeField", null, true, DATE_CUSTOM_TYPE, NO_CONDITIONS)); }
responseReader.readObject(ResponseField.forObject("objectField", "objectField", null, false, NO_CONDITIONS), new ResponseReader.ObjectReader<Object>() { @Override public Object read(ResponseReader reader) { return null;