Search in sources :

Example 1 with JDefinedClass

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

the class TypeRuleTest method applyGeneratesCustomObject.

@Test
public void applyGeneratesCustomObject() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "object");
    JDefinedClass mockObjectType = mock(JDefinedClass.class);
    ObjectRule mockObjectRule = mock(ObjectRule.class);
    when(mockObjectRule.apply("fooBar", objectNode, jpackage, null)).thenReturn(mockObjectType);
    when(ruleFactory.getObjectRule()).thenReturn(mockObjectRule);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result, is((JType) mockObjectType));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JDefinedClass(com.sun.codemodel.JDefinedClass) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 2 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 3 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 4 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 5 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)

Aggregations

JDefinedClass (com.sun.codemodel.JDefinedClass)42 JClass (com.sun.codemodel.JClass)23 JMethod (com.sun.codemodel.JMethod)16 JVar (com.sun.codemodel.JVar)13 JCodeModel (com.sun.codemodel.JCodeModel)11 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