This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from CJSCommonPlatform/enum-generator
Add Enum generator
- Loading branch information
Showing
27 changed files
with
599 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 27 additions & 20 deletions
47
...n-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/ComplexSchemaIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,66 @@ | ||
package uk.gov.justice.generation.pojo.integration.test; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.apache.commons.io.FileUtils.cleanDirectory; | ||
|
||
import uk.gov.justice.generation.io.files.loader.ObjectSchemaLoader; | ||
import uk.gov.justice.generation.pojo.core.DefinitionBuilderVisitor; | ||
import uk.gov.justice.generation.pojo.core.GenerationContext; | ||
import uk.gov.justice.generation.pojo.core.JsonSchemaWrapper; | ||
import uk.gov.justice.generation.pojo.core.RootFieldNameGenerator; | ||
import uk.gov.justice.generation.pojo.generators.ClassGeneratable; | ||
import uk.gov.justice.generation.pojo.generators.JavaGeneratorFactory; | ||
import uk.gov.justice.generation.pojo.integration.utils.ClassCompiler; | ||
import uk.gov.justice.generation.pojo.write.SourceWriter; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import org.everit.json.schema.ObjectSchema; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ComplexSchemaIT { | ||
|
||
private final SourceWriter sourceWriter = new SourceWriter(); | ||
private final ClassCompiler classCompiler = new ClassCompiler(); | ||
|
||
private final JavaGeneratorFactory javaGeneratorFactory = new JavaGeneratorFactory(); | ||
private final RootFieldNameGenerator rootFieldNameGenerator = new RootFieldNameGenerator(); | ||
private final ObjectSchemaLoader objectSchemaLoader = new ObjectSchemaLoader(); | ||
|
||
private File sourceOutputDirectory; | ||
private File classesOutputDirectory; | ||
|
||
@Before | ||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
public void setup() throws Exception { | ||
sourceOutputDirectory = new File("./target/test-generation"); | ||
classesOutputDirectory = new File("./target/test-classes"); | ||
|
||
sourceOutputDirectory.mkdirs(); | ||
classesOutputDirectory.mkdirs(); | ||
|
||
if (sourceOutputDirectory.exists()) { | ||
cleanDirectory(sourceOutputDirectory); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldParseAVeryComplexSchemaDocument() throws Exception { | ||
|
||
final File jsonSchemaFile = new File("src/test/resources/schemas/context.command.complex-data.json"); | ||
final ObjectSchema schema = objectSchemaLoader.loadFrom(jsonSchemaFile); | ||
final String fieldName = rootFieldNameGenerator.generateNameFrom(jsonSchemaFile); | ||
|
||
final GenerationContext generationContext = new GenerationContext(sourceOutputDirectory); | ||
final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor("uk.gov.justice.pojo"); | ||
final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(schema); | ||
|
||
jsonSchemaWrapper.accept(fieldName, definitionBuilderVisitor); | ||
|
||
final List<ClassGeneratable> classGeneratables = definitionBuilderVisitor.getDefinitions() | ||
.stream() | ||
.map(javaGeneratorFactory::createClassGeneratorFor) | ||
.collect(toList()); | ||
|
||
|
||
final File sourceOutputDirectory = new File("./target/test-generation"); | ||
final File classesOutputDirectory = new File("./target/test-classes"); | ||
|
||
sourceOutputDirectory.delete(); | ||
|
||
final GenerationContext generationContext = new GenerationContext(sourceOutputDirectory); | ||
|
||
classGeneratables.forEach(classGeneratable -> { | ||
sourceWriter.write(classGeneratable, generationContext); | ||
classCompiler.compile(classGeneratable, sourceOutputDirectory, classesOutputDirectory); | ||
}); | ||
javaGeneratorFactory | ||
.createClassGeneratorsFor(definitionBuilderVisitor.getDefinitions()) | ||
.forEach(classGeneratable -> { | ||
sourceWriter.write(classGeneratable, generationContext); | ||
classCompiler.compile(classGeneratable, sourceOutputDirectory, classesOutputDirectory); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
integration-test/src/test/resources/schemas/person-enum-schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"id": "person", | ||
"properties": { | ||
"firstName": { | ||
"type": "string" | ||
}, | ||
"lastName": { | ||
"type": "string" | ||
}, | ||
"title": { | ||
"type": "string", | ||
"enum": [ | ||
"Mr", | ||
"Mrs", | ||
"Ms" | ||
] | ||
}, | ||
"required": { | ||
"type": "boolean" | ||
}, | ||
"signedInCount": { | ||
"type": "integer" | ||
}, | ||
"ratio": { | ||
"type": "number" | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": [ | ||
"title", | ||
"firstName", | ||
"lastName", | ||
"required", | ||
"signedInCount", | ||
"ratio" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.