@Test public void readCustomObjectList() throws Exception { ResponseField successField = ResponseField.forCustomType("successFieldResponseName", "successFieldName", null, false, OBJECT_CUSTOM_TYPE, NO_CONDITIONS); Map<String, Object> objectMap = new HashMap<>(); objectMap.put("string", "string"); objectMap.put("boolean", true); List<?> objectList = asList(objectMap, objectMap); Map<String, Object> recordSet = new HashMap<>(); recordSet.put("successFieldResponseName", objectList); recordSet.put("successFieldName", objectList); RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet); assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) successField)) .isEqualTo("[{\"boolean\":true,\"string\":\"string\"},{\"boolean\":true,\"string\":\"string\"}]"); }
@Test public void readCustom() throws Exception { ResponseField successField = ResponseField.forCustomType("successFieldResponseName", "successFieldName", null, false, DATE_CUSTOM_TYPE, NO_CONDITIONS); ResponseField classCastExceptionField = ResponseField.forCustomType("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, DATE_CUSTOM_TYPE, NO_CONDITIONS); Map<String, Object> recordSet = new HashMap<>(); recordSet.put("successFieldResponseName", "2017-04-16"); recordSet.put("successFieldName", "2018-04-16"); recordSet.put("classCastExceptionFieldResponseName", 0); RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet); assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) successField)).isEqualTo(DATE_TIME_FORMAT.parse("2017-04-16")); try { responseReader.readCustomType((ResponseField.CustomTypeField) classCastExceptionField); fail("expected ClassCastException"); } catch (ClassCastException expected) { // expected } }
@Test public void readCustomWithDecodedNullValue() throws Exception { Map<String, Object> recordSet = new HashMap<>(); recordSet.put("responseName", "http:://"); RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet); ResponseField field = ResponseField.forCustomType("responseName", "fieldName", null, false, URL_CUSTOM_TYPE, NO_CONDITIONS); try { responseReader.readCustomType((ResponseField.CustomTypeField) field); fail("expected NullPointerException"); } catch (NullPointerException e) { // expected } field = ResponseField.forCustomType("responseName", "fieldName", null, true, URL_CUSTOM_TYPE, NO_CONDITIONS); Truth.assertThat(responseReader.readCustomType((ResponseField.CustomTypeField) field)).isNull(); }
@Test public void readCustomObjectMap() throws Exception { ResponseField successField = ResponseField.forCustomType("successFieldResponseName", "successFieldName", null, false, OBJECT_CUSTOM_TYPE, NO_CONDITIONS);
@Test public void readCustomWithDefaultAdapter() throws Exception { ResponseField.CustomTypeField stringField = ResponseField.forCustomType("stringField", "stringField", null, false, scalarTypeFor(String.class), NO_CONDITIONS); ResponseField.CustomTypeField booleanField = ResponseField.forCustomType("booleanField", "booleanField", null, false, scalarTypeFor(Boolean.class), NO_CONDITIONS); ResponseField.CustomTypeField integerField = ResponseField.forCustomType("integerField", "integerField", null, false, scalarTypeFor(Integer.class), NO_CONDITIONS); ResponseField.CustomTypeField longField = ResponseField.forCustomType("longField", "longField", null, false, scalarTypeFor(Long.class), NO_CONDITIONS); ResponseField.CustomTypeField floatField = ResponseField.forCustomType("floatField", "floatField", null, false, scalarTypeFor(Float.class), NO_CONDITIONS); ResponseField.CustomTypeField doubleField = ResponseField.forCustomType("doubleField", "doubleField", null, false, scalarTypeFor(Double.class), NO_CONDITIONS); ResponseField.CustomTypeField unsupportedField = ResponseField.forCustomType("unsupportedField", "unsupportedField", null, false, scalarTypeFor(RuntimeException.class), NO_CONDITIONS);
@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.readCustomType((ResponseField.CustomTypeField) ResponseField.forCustomType("customTypeField", "customTypeField", null, false, DATE_CUSTOM_TYPE, NO_CONDITIONS)); fail("expected NullPointerException");