use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class SchemaMapperTest method generateCreatesSchemaFromExampleJsonWhenInJsonMode.
@Test
public void generateCreatesSchemaFromExampleJsonWhenInJsonMode() {
URL schemaContent = this.getClass().getResource("/schema/address.json");
ObjectNode schemaNode = JsonNodeFactory.instance.objectNode();
final SchemaRule mockSchemaRule = mock(SchemaRule.class);
final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class);
when(mockGenerationConfig.getSourceType()).thenReturn(SourceType.JSON);
final SchemaGenerator mockSchemaGenerator = mock(SchemaGenerator.class);
when(mockSchemaGenerator.schemaFromExample(schemaContent)).thenReturn(schemaNode);
final RuleFactory mockRuleFactory = mock(RuleFactory.class);
when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig);
new SchemaMapper(mockRuleFactory, mockSchemaGenerator).generate(new JCodeModel(), "Address", "com.example.package", schemaContent);
ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
verify(mockSchemaRule).apply(eq("Address"), eq(schemaNode), eq(null), capturePackage.capture(), Mockito.isA(Schema.class));
assertThat(capturePackage.getValue().name(), is("com.example.package"));
}
use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class SchemaMapperTest method generateCreatesSchemaFromExampleJSONAsStringInput.
@Test
public void generateCreatesSchemaFromExampleJSONAsStringInput() throws IOException {
String jsonContent = IOUtils.toString(this.getClass().getResourceAsStream("/example-json/user.json"));
ObjectNode schemaNode = JsonNodeFactory.instance.objectNode();
final SchemaRule mockSchemaRule = mock(SchemaRule.class);
final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class);
when(mockGenerationConfig.getSourceType()).thenReturn(SourceType.JSON);
final SchemaGenerator mockSchemaGenerator = mock(SchemaGenerator.class);
when(mockSchemaGenerator.schemaFromExample(new ObjectMapper().readTree(jsonContent))).thenReturn(schemaNode);
final RuleFactory mockRuleFactory = mock(RuleFactory.class);
when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig);
new SchemaMapper(mockRuleFactory, mockSchemaGenerator).generate(new JCodeModel(), "User", "com.example.package", jsonContent);
ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
verify(mockSchemaRule).apply(eq("User"), eq(schemaNode), eq(null), capturePackage.capture(), Mockito.isA(Schema.class));
assertThat(capturePackage.getValue().name(), is("com.example.package"));
}
use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class DescriptionRuleTest method applyAddsDescriptionToJavadoc.
@Test
public void applyAddsDescriptionToJavadoc() throws JClassAlreadyExistsException {
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
ObjectMapper mapper = new ObjectMapper();
TextNode descriptionNode = mapper.createObjectNode().textNode("some description");
JDocComment result = rule.apply("fooBar", descriptionNode, null, jclass, null);
assertThat(result, sameInstance(jclass.javadoc()));
assertThat(result.size(), is(1));
assertThat((String) result.get(0), is("some description"));
}
use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class TitleRuleTest method applyAddsDescriptionToJavadoc.
@Test
public void applyAddsDescriptionToJavadoc() throws JClassAlreadyExistsException {
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
ObjectMapper mapper = new ObjectMapper();
TextNode titleNode = mapper.createObjectNode().textNode("some title");
JDocComment result = rule.apply("fooBar", titleNode, null, jclass, null);
assertThat(result, sameInstance(jclass.javadoc()));
assertThat(result.size(), is(1));
assertThat(result.get(0), is("some title\n<p>\n"));
}
use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.
the class DefaultRuleTest method whenIsInitializeCollections_false_applyDoesNotInitializeField.
@Test
public void whenIsInitializeCollections_false_applyDoesNotInitializeField() throws JClassAlreadyExistsException {
final String fieldName = "fieldName";
when(config.isInitializeCollections()).thenReturn(false);
JDefinedClass jclass = new JCodeModel()._class("org.jsonschema2pojo.rules.ExampleClass");
JFieldVar field = jclass.field(JMod.NONE, jclass.owner().ref(fieldTypeClass).narrow(Object.class), fieldName);
ArrayNode node = new ObjectMapper().createArrayNode();
StringWriter sw = new StringWriter();
rule.apply("fooBar", node, null, field, null).bind(new JFormatter(sw));
assertThat(sw.toString(), is(String.format("%s<%s> %s", fieldTypeClass.getName(), Object.class.getName(), fieldName)));
}
Aggregations