Search in sources :

Example 16 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.

the class RequiredArrayRuleTest method shouldUpdateJavaDoc.

@Test
public void shouldUpdateJavaDoc() throws JClassAlreadyExistsException {
    JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar");
    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo");
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode requiredNode = mapper.createArrayNode().add("fooBar");
    rule.apply("Class", requiredNode, jclass, new Schema(null, requiredNode, requiredNode));
    JDocComment fooBarJavaDoc = jclass.fields().get("fooBar").javadoc();
    JDocComment fooJavaDoc = jclass.fields().get("foo").javadoc();
    assertThat(fooBarJavaDoc.size(), is(1));
    assertThat((String) fooBarJavaDoc.get(0), is("\n(Required)"));
    assertThat(fooJavaDoc.size(), is(0));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) Schema(org.jsonschema2pojo.Schema) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) JDocComment(com.sun.codemodel.JDocComment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 17 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.

the class RequiredArrayRuleTest method shouldUpdateAnnotations.

@Test
public void shouldUpdateAnnotations() throws JClassAlreadyExistsException {
    setupRuleFactoryToIncludeJsr303();
    JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar");
    jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo");
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode requiredNode = mapper.createArrayNode().add("foo_bar");
    rule.apply("Class", requiredNode, jclass, new Schema(null, requiredNode, requiredNode));
    Collection<JAnnotationUse> fooBarAnnotations = jclass.fields().get("fooBar").annotations();
    Collection<JAnnotationUse> fooAnnotations = jclass.fields().get("foo").annotations();
    assertThat(fooBarAnnotations.size(), is(1));
    assertThat(fooBarAnnotations.iterator().next().getAnnotationClass().name(), is(NotNull.class.getSimpleName()));
    assertThat(fooAnnotations.size(), is(0));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) Schema(org.jsonschema2pojo.Schema) JAnnotationUse(com.sun.codemodel.JAnnotationUse) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 18 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.

the class RequiredRuleTest method applyAddsTextWhenRequired.

@Test
public void applyAddsTextWhenRequired() throws JClassAlreadyExistsException {
    JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
    ObjectMapper mapper = new ObjectMapper();
    BooleanNode descriptionNode = mapper.createObjectNode().booleanNode(true);
    JDocCommentable result = rule.apply("fooBar", descriptionNode, jclass, null);
    assertThat(result.javadoc(), sameInstance(jclass.javadoc()));
    assertThat(result.javadoc().size(), is(1));
    assertThat((String) result.javadoc().get(0), is("\n(Required)"));
}
Also used : JDocCommentable(com.sun.codemodel.JDocCommentable) JDefinedClass(com.sun.codemodel.JDefinedClass) JCodeModel(com.sun.codemodel.JCodeModel) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 19 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.

the class RequiredRuleTest method applySkipsTextWhenNotRequired.

@Test
public void applySkipsTextWhenNotRequired() throws JClassAlreadyExistsException {
    JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
    ObjectMapper mapper = new ObjectMapper();
    BooleanNode descriptionNode = mapper.createObjectNode().booleanNode(false);
    JDocCommentable result = rule.apply("fooBar", descriptionNode, jclass, null);
    assertThat(result.javadoc(), sameInstance(jclass.javadoc()));
    assertThat(result.javadoc().size(), is(0));
}
Also used : JDocCommentable(com.sun.codemodel.JDocCommentable) JDefinedClass(com.sun.codemodel.JDefinedClass) JCodeModel(com.sun.codemodel.JCodeModel) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 20 with JDefinedClass

use of com.sun.codemodel.JDefinedClass 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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JDefinedClass(com.sun.codemodel.JDefinedClass) Schema(org.jsonschema2pojo.Schema) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JDefinedClass (com.sun.codemodel.JDefinedClass)43 JClass (com.sun.codemodel.JClass)23 JMethod (com.sun.codemodel.JMethod)17 JVar (com.sun.codemodel.JVar)13 JCodeModel (com.sun.codemodel.JCodeModel)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 JType (com.sun.codemodel.JType)9 Test (org.junit.Test)9 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)8 Map (java.util.Map)8 JBlock (com.sun.codemodel.JBlock)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 JExpression (com.sun.codemodel.JExpression)5 JFieldVar (com.sun.codemodel.JFieldVar)5 HashMap (java.util.HashMap)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ArrayList (java.util.ArrayList)4 DataMap (com.linkedin.data.DataMap)3 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3