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 #13 from CJSCommonPlatform/implement-combined-schemas
implement combined schemas
- Loading branch information
Showing
20 changed files
with
619 additions
and
203 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
121 changes: 121 additions & 0 deletions
121
...-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/CombinedSchemaIT.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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package uk.gov.justice.generation.pojo.integration.test; | ||
|
||
import static com.jayway.jsonassert.JsonAssert.with; | ||
import static org.apache.commons.io.FileUtils.cleanDirectory; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import uk.gov.justice.generation.io.files.loader.SchemaLoader; | ||
import uk.gov.justice.generation.pojo.core.DefinitionBuilderVisitor; | ||
import uk.gov.justice.generation.pojo.core.JsonSchemaWrapper; | ||
import uk.gov.justice.generation.pojo.core.RootFieldNameGenerator; | ||
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 uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Constructor; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.everit.json.schema.Schema; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CombinedSchemaIT { | ||
|
||
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 SchemaLoader schemaLoader = new SchemaLoader(); | ||
private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); | ||
|
||
private File sourceOutputDirectory; | ||
private File classesOutputDirectory; | ||
|
||
@Before | ||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
public void setup() throws Exception { | ||
sourceOutputDirectory = new File("./target/test-generation/combined-schema"); | ||
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/address.json"); | ||
final Schema schema = schemaLoader.loadFrom(jsonSchemaFile); | ||
final String fieldName = rootFieldNameGenerator.generateNameFrom(jsonSchemaFile); | ||
|
||
final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor("uk.gov.justice.pojo.combined.schema"); | ||
final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(schema); | ||
|
||
jsonSchemaWrapper.accept(fieldName, definitionBuilderVisitor); | ||
|
||
final List<Class<?>> newClasses = new ArrayList<>(); | ||
|
||
javaGeneratorFactory | ||
.createClassGeneratorsFor(definitionBuilderVisitor.getDefinitions()) | ||
.forEach(classGeneratable -> { | ||
sourceWriter.write(classGeneratable, sourceOutputDirectory.toPath()); | ||
final Class<?> newClass = classCompiler.compile(classGeneratable, sourceOutputDirectory, classesOutputDirectory); | ||
newClasses.add(newClass); | ||
}); | ||
|
||
assertThat(newClasses.size(), is(1)); | ||
assertThat(newClasses.get(0).getSimpleName(), is("Address")); | ||
|
||
final Constructor<?> constructor = newClasses.get(0).getConstructor( | ||
String.class, | ||
String.class, | ||
String.class, | ||
String.class, | ||
String.class, | ||
String.class, | ||
String.class); | ||
|
||
assertThat(constructor, is(notNullValue())); | ||
|
||
final String zipCode = "zipCode"; | ||
final String city = "city"; | ||
final String county = "county"; | ||
final String addressLine1 = "addressLine1"; | ||
final String addressLine2 = "addressLine2"; | ||
final String postCode = "postCode"; | ||
final String state = "state"; | ||
|
||
final Object addressObject = constructor.newInstance( | ||
zipCode, | ||
city, | ||
county, | ||
addressLine1, | ||
addressLine2, | ||
postCode, | ||
state | ||
); | ||
|
||
final String json = objectMapper.writeValueAsString(addressObject); | ||
|
||
with(json) | ||
.assertThat("$.addressLine1", is("addressLine1")) | ||
.assertThat("$.addressLine2", is("addressLine2")) | ||
.assertThat("$.city", is("city")) | ||
.assertThat("$.county", is("county")) | ||
.assertThat("$.postCode", is("postCode")) | ||
.assertThat("$.state", is("state")) | ||
.assertThat("$.zipCode", is("zipCode")) | ||
; | ||
} | ||
} |
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
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
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,54 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"title": "Address", | ||
"additionalProperties": true, | ||
"properties": { | ||
"addressLine1": { | ||
"type": "string" | ||
}, | ||
"addressLine2": { | ||
"type": "string" | ||
}, | ||
"city": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"addressLine1", | ||
"addressLine2", | ||
"city" | ||
], | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"state": { | ||
"type": "string" | ||
}, | ||
"zipCode": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"state", | ||
"zipCode" | ||
] | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"county": { | ||
"type": "string" | ||
}, | ||
"postCode": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"county", | ||
"postCode" | ||
] | ||
} | ||
] | ||
} |
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.