Search in sources :

Example 81 with JCodeModel

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"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JCodeModel(com.sun.codemodel.JCodeModel) URL(java.net.URL) Test(org.junit.Test)

Example 82 with JCodeModel

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"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 83 with JCodeModel

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"));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) TextNode(com.fasterxml.jackson.databind.node.TextNode) JCodeModel(com.sun.codemodel.JCodeModel) JDocComment(com.sun.codemodel.JDocComment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 84 with JCodeModel

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"));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) TextNode(com.fasterxml.jackson.databind.node.TextNode) JCodeModel(com.sun.codemodel.JCodeModel) JDocComment(com.sun.codemodel.JDocComment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 85 with JCodeModel

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)));
}
Also used : StringWriter(java.io.StringWriter) JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JFormatter(com.sun.codemodel.JFormatter) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)89 Test (org.junit.Test)70 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)58 JPackage (com.sun.codemodel.JPackage)50 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)45 JType (com.sun.codemodel.JType)43 JDefinedClass (com.sun.codemodel.JDefinedClass)18 JClass (com.sun.codemodel.JClass)14 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)14 Schema (org.jsonschema2pojo.Schema)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)5 File (java.io.File)5 IOException (java.io.IOException)5 URL (java.net.URL)5 JBlock (com.sun.codemodel.JBlock)4 JCatchBlock (com.sun.codemodel.JCatchBlock)4 JDocComment (com.sun.codemodel.JDocComment)4 JMethod (com.sun.codemodel.JMethod)4