use of com.fasterxml.jackson.annotation.JsonFormat in project jsonschema2pojo by joelittlejohn.
the class CustomDateTimeFormatIT method testDefaultWhenFormatDateTimesConfigIsTrue.
/**
* This tests the class generated when formatDateTimes config option is set to TRUE
* The field should have @JsonFormat annotation with iso8601 date time pattern and UTC timezone
* It also tests the serialization and deserialization process
*
* @throws Exception
*/
@Test
public void testDefaultWhenFormatDateTimesConfigIsTrue() throws Exception {
Field field = classWhenConfigIsTrue.getDeclaredField("defaultFormat");
JsonFormat annotation = field.getAnnotation(JsonFormat.class);
assertThat(annotation, notNullValue());
// Assert that the patterns match
assertEquals("yyyy-MM-dd'T'HH:mm:ss.SSS", annotation.pattern());
// Assert that the timezones match
assertEquals("UTC", annotation.timezone());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setTimeZone(TimeZone.getTimeZone("UTC"));
ObjectNode node = objectMapper.createObjectNode();
node.put("defaultFormat", "2016-11-06T00:00:00.000");
Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue);
Method getter = new PropertyDescriptor("defaultFormat", classWhenConfigIsTrue).getReadMethod();
// Assert that the Date object in the deserialized class is as expected
assertEquals(dateTimeMilliSecFormatter.parse("2016-11-06T00:00:00.000").toString(), getter.invoke(pojo).toString());
JsonNode jsonVersion = objectMapper.valueToTree(pojo);
// Assert that when the class is serialized, the date object is serialized as expected
assertEquals("2016-11-06T00:00:00.000", jsonVersion.get("defaultFormat").asText());
}
Aggregations