Search in sources :

Example 21 with JPackage

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

the class SchemaMapperTest method generateCreatesSchemaFromSchemaAsStringInput.

@Test
public void generateCreatesSchemaFromSchemaAsStringInput() throws IOException {
    String schemaContent = IOUtils.toString(this.getClass().getResourceAsStream("/schema/address.json"));
    final SchemaRule mockSchemaRule = mock(SchemaRule.class);
    final RuleFactory mockRuleFactory = mock(RuleFactory.class);
    when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
    when(mockRuleFactory.getGenerationConfig()).thenReturn(new DefaultGenerationConfig());
    new SchemaMapper(mockRuleFactory, new SchemaGenerator()).generate(new JCodeModel(), "Address", "com.example.package", schemaContent);
    ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
    ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class);
    verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), Mockito.isA(Schema.class));
    assertThat(capturePackage.getValue().name(), is("com.example.package"));
    assertThat(captureNode.getValue(), is(notNullValue()));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) Test(org.junit.Test)

Example 22 with JPackage

use of com.sun.codemodel.JPackage 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), 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 23 with JPackage

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

the class SchemaMapperTest method generateCreatesSchemaFromExampleJsonWhenInJsonMode.

@Test
public void generateCreatesSchemaFromExampleJsonWhenInJsonMode() throws IOException {
    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), 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 24 with JPackage

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

the class ArrayRuleTest method arrayOfPrimitivesProducesCollectionOfWrapperTypes.

@Test
public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "number");
    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.set("uniqueItems", BooleanNode.FALSE);
    propertyNode.set("items", itemsNode);
    Schema schema = mock(Schema.class);
    when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
    when(config.isUsePrimitives()).thenReturn(true);
    when(config.isUseDoubleNumbers()).thenReturn(true);
    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
    assertThat(propertyType, notNullValue());
    assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
    assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(org.jsonschema2pojo.Schema) JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 25 with JPackage

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

the class ArrayRuleTest method arrayDefaultsToNonUnique.

@Test
public void arrayDefaultsToNonUnique() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "boolean");
    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.set("uniqueItems", BooleanNode.FALSE);
    propertyNode.set("items", itemsNode);
    Schema schema = mock(Schema.class);
    when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));
    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
    assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(org.jsonschema2pojo.Schema) JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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