use of org.jsonschema2pojo.rules.RuleFactory 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"));
}
use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.
the class Jsonschema2Pojo method generate.
/**
* Reads the contents of the given source and initiates schema generation.
*
* @param config
* the configuration options (including source and target paths,
* and other behavioural options) that will control code
* generation
* @throws FileNotFoundException
* if the source path is not found
* @throws IOException
* if the application is unable to read data from the source
*/
public static void generate(GenerationConfig config) throws IOException {
Annotator annotator = getAnnotator(config);
RuleFactory ruleFactory = createRuleFactory(config);
ruleFactory.setAnnotator(annotator);
ruleFactory.setGenerationConfig(config);
SchemaMapper mapper = new SchemaMapper(ruleFactory, new SchemaGenerator());
JCodeModel codeModel = new JCodeModel();
if (config.isRemoveOldOutput()) {
removeOldOutput(config.getTargetDirectory());
}
for (Iterator<URL> sources = config.getSource(); sources.hasNext(); ) {
URL source = sources.next();
if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
generateRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
} else {
mapper.generate(codeModel, getNodeName(source, config), defaultString(config.getTargetPackage()), source);
}
}
if (config.getTargetDirectory().exists() || config.getTargetDirectory().mkdirs()) {
CodeWriter sourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
CodeWriter resourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
codeModel.build(sourcesWriter, resourcesWriter);
} else {
throw new GenerationException("Could not create or access target directory " + config.getTargetDirectory().getAbsolutePath());
}
}
use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.
the class FragmentRefIT method refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile.
@Test
public void refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile() throws IOException, ClassNotFoundException {
JCodeModel codeModel = new JCodeModel();
JsonNode schema = new ObjectMapper().readTree("{\n" + " \"type\": \"object\",\n" + " \"definitions\": {\n" + " \"location\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"cat\": {\n" + " \"$ref\": \"#/definitions/cat\"\n" + " }\n" + " }\n" + " },\n" + " \"cat\": {\n" + " \"type\": \"number\"\n" + " }\n" + " },\n" + " \"properties\": {\n" + " \"location\": {\n" + " \"$ref\": \"#/definitions/location\"\n" + " }\n" + " }\n" + "}");
JPackage p = codeModel._package("com.example");
new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema, schema));
}
use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.
the class FragmentRefIT method selfRefWithoutParentFile.
@Test
public void selfRefWithoutParentFile() throws IOException {
JCodeModel codeModel = new JCodeModel();
JsonNode schema = new ObjectMapper().readTree("{\"type\":\"object\", \"properties\":{\"a\":{\"$ref\":\"#/b\"}}, \"b\":\"string\"}");
JPackage p = codeModel._package("com.example");
new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema, schema));
}
Aggregations