use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.
the class SchemaRuleTest method enumAsRootIsGeneratedCorrectly.
@Test
public void enumAsRootIsGeneratedCorrectly() throws URISyntaxException, JClassAlreadyExistsException {
ObjectNode schemaContent = new ObjectMapper().createObjectNode();
ObjectNode enumNode = schemaContent.objectNode();
enumNode.put("type", "string");
schemaContent.set("enum", enumNode);
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
Schema schema = mock(Schema.class);
when(schema.getContent()).thenReturn(schemaContent);
schema.setJavaTypeIfEmpty(jclass);
EnumRule enumRule = mock(EnumRule.class);
when(mockRuleFactory.getEnumRule()).thenReturn(enumRule);
when(enumRule.apply(NODE_NAME, enumNode, jclass, schema)).thenReturn(jclass);
rule.apply(NODE_NAME, schemaContent, jclass, schema);
verify(enumRule).apply(NODE_NAME, schemaContent, jclass, schema);
verify(schema, atLeastOnce()).setJavaTypeIfEmpty(jclass);
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.
the class CustomDateTimeFormatIT method testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue.
/**
* This tests the class generated when formatDateTimes config option is set to TRUE
* The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone
* It also tests the serialization and deserialization process
*
* @throws Exception
*/
@Test
public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception {
Field field = classWhenConfigIsTrue.getDeclaredField("customFormatDefaultTZ");
JsonFormat annotation = field.getAnnotation(JsonFormat.class);
assertThat(annotation, notNullValue());
// Assert that the patterns match
assertEquals("yyyy-MM-dd'T'HH:mm:ss", 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("customFormatDefaultTZ", "2016-11-06T00:00:00");
Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue);
Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsTrue).getReadMethod();
// Assert that the Date object in the deserialized class is as expected
assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").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", jsonVersion.get("customFormatDefaultTZ").asText());
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.
the class TypeRuleTest method applyGeneratesNumberUsingJavaTypeBigDecimal.
@Test
public void applyGeneratesNumberUsingJavaTypeBigDecimal() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "number");
objectNode.put("javaType", "java.math.BigDecimal");
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result.fullName(), is("java.math.BigDecimal"));
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.
the class TypeRuleTest method applyGeneratesIntegerUsingJavaTypeLong.
@Test
public void applyGeneratesIntegerUsingJavaTypeLong() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("javaType", "java.lang.Long");
when(config.isUsePrimitives()).thenReturn(true);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result.fullName(), is("java.lang.Long"));
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.
the class TypeRuleTest method applyGeneratesIntegerPrimitive.
@Test
public void applyGeneratesIntegerPrimitive() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
when(config.isUsePrimitives()).thenReturn(true);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result.fullName(), is("int"));
}
Aggregations