Search in sources :

Example 26 with JPackage

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

the class EnumRuleTest method applyGeneratesUniqueEnumNamesForMultipleEnumNodesWithSameName.

@Test
public void applyGeneratesUniqueEnumNamesForMultipleEnumNodesWithSameName() {
    Answer<String> firstArgAnswer = new FirstArgAnswer<String>();
    when(nameHelper.getFieldName(anyString(), any(JsonNode.class))).thenAnswer(firstArgAnswer);
    when(nameHelper.replaceIllegalCharacters(anyString())).thenAnswer(firstArgAnswer);
    when(nameHelper.normalizeName(anyString())).thenAnswer(firstArgAnswer);
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectMapper objectMapper = new ObjectMapper();
    ArrayNode arrayNode = objectMapper.createArrayNode();
    arrayNode.add("open");
    arrayNode.add("closed");
    ObjectNode enumNode = objectMapper.createObjectNode();
    enumNode.put("type", "string");
    enumNode.set("enum", arrayNode);
    // We're always a string for the purposes of this test
    when(typeRule.apply("status", enumNode, jpackage, schema)).thenReturn(jpackage.owner()._ref(String.class));
    JType result1 = rule.apply("status", enumNode, jpackage, schema);
    JType result2 = rule.apply("status", enumNode, jpackage, schema);
    assertThat(result1.fullName(), is("org.jsonschema2pojo.rules.Status"));
    assertThat(result2.fullName(), is("org.jsonschema2pojo.rules.Status_"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 27 with JPackage

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

the class TypeRuleTest method applyGeneratesDate.

@Test
public void applyGeneratesDate() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "string");
    TextNode formatNode = TextNode.valueOf("date-time");
    objectNode.set("format", formatNode);
    JType mockDateType = mock(JType.class);
    FormatRule mockFormatRule = mock(FormatRule.class);
    when(mockFormatRule.apply(eq("fooBar"), eq(formatNode), Mockito.isA(JType.class), isNull(Schema.class))).thenReturn(mockDateType);
    when(ruleFactory.getFormatRule()).thenReturn(mockFormatRule);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result, equalTo(mockDateType));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) TextNode(com.fasterxml.jackson.databind.node.TextNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 28 with JPackage

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

the class TypeRuleTest method applyGeneratesNumberUsingJavaTypeFloat.

@Test
public void applyGeneratesNumberUsingJavaTypeFloat() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "number");
    objectNode.put("javaType", "java.lang.Float");
    when(config.isUsePrimitives()).thenReturn(true);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is("java.lang.Float"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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 29 with JPackage

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

the class TypeRuleTest method applyGeneratesString.

@Test
public void applyGeneratesString() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "string");
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is(String.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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 30 with JPackage

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

the class TypeRuleTest method applyGeneratesNumberUsingJavaTypeFloatPrimitive.

@Test
public void applyGeneratesNumberUsingJavaTypeFloatPrimitive() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "number");
    objectNode.put("javaType", "float");
    when(config.isUsePrimitives()).thenReturn(false);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is("float"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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)

Aggregations

JPackage (com.sun.codemodel.JPackage)53 JCodeModel (com.sun.codemodel.JCodeModel)47 Test (org.junit.Test)47 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)44 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)44 JType (com.sun.codemodel.JType)37 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 JClass (com.sun.codemodel.JClass)7 Schema (org.jsonschema2pojo.Schema)7 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)6 SchemaRule (org.jsonschema2pojo.rules.SchemaRule)4 JDefinedClass (com.sun.codemodel.JDefinedClass)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1