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.
- Loading branch information
amckenzie
committed
Aug 23, 2017
1 parent
fab8996
commit 4e3930e
Showing
5 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/ArrayIT.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,112 @@ | ||
package uk.gov.justice.generation.pojo.integration.test; | ||
|
||
import static com.jayway.jsonassert.JsonAssert.with; | ||
import static java.util.Arrays.asList; | ||
import static org.apache.commons.io.FileUtils.cleanDirectory; | ||
import static org.hamcrest.CoreMatchers.is; | ||
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.NameGenerator; | ||
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 ArrayIT { | ||
|
||
private final SourceWriter sourceWriter = new SourceWriter(); | ||
private final ClassCompiler classCompiler = new ClassCompiler(); | ||
|
||
private final JavaGeneratorFactory javaGeneratorFactory = new JavaGeneratorFactory(); | ||
private final NameGenerator rootFieldNameGenerator = new NameGenerator(); | ||
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/arrays"); | ||
classesOutputDirectory = new File("./target/test-classes"); | ||
|
||
sourceOutputDirectory.mkdirs(); | ||
classesOutputDirectory.mkdirs(); | ||
|
||
if (sourceOutputDirectory.exists()) { | ||
cleanDirectory(sourceOutputDirectory); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldAnArraySchemaDocumentWithAnArrayOfItemSchemas() throws Exception { | ||
|
||
final File jsonSchemaFile = new File("src/test/resources/schemas/example.recipe-added.json"); | ||
final Schema schema = schemaLoader.loadFrom(jsonSchemaFile); | ||
final String fieldName = rootFieldNameGenerator.rootFieldNameFrom(jsonSchemaFile); | ||
|
||
final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor("uk.gov.justice.pojo.arrays"); | ||
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(2)); | ||
assertThat(newClasses.get(0).getSimpleName(), is("Ingredients")); | ||
assertThat(newClasses.get(1).getSimpleName(), is("RecipeAdded")); | ||
|
||
final Constructor<?> ingredientsConstructor = newClasses.get(0).getConstructor( | ||
Integer.class, | ||
String.class); | ||
final Constructor<?> recipeAddedConstructor = newClasses.get(1).getConstructor( | ||
String.class, | ||
Boolean.class, | ||
List.class, | ||
String.class); | ||
|
||
final Object ingredient_1 = ingredientsConstructor.newInstance(1, "Eye of Newt"); | ||
final Object ingredient_2 = ingredientsConstructor.newInstance(3, "Toe of Frog"); | ||
|
||
final Object regicidePie = recipeAddedConstructor.newInstance( | ||
"Regicide Pie", | ||
false, | ||
asList(ingredient_1, ingredient_2), | ||
"13" | ||
); | ||
|
||
final String json = objectMapper.writeValueAsString(regicidePie); | ||
|
||
with(json) | ||
.assertThat("$.name", is("Regicide Pie")) | ||
.assertThat("$.recipeId", is("13")) | ||
.assertThat("$.glutenFree", is(false)) | ||
.assertThat("$.ingredientsList[0].name", is("Eye of Newt")) | ||
.assertThat("$.ingredientsList[0].quantity", is(1)) | ||
.assertThat("$.ingredientsList[1].name", is("Toe of Frog")) | ||
.assertThat("$.ingredientsList[1].quantity", is(3)) | ||
; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
integration-test/src/test/resources/schemas/example.recipe-added.json
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,51 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"id": "", | ||
"type": "object", | ||
"properties": { | ||
"recipeId": { | ||
"id": "/recipeId", | ||
"type": "string", | ||
"description": "Uniquely identifies the recipe", | ||
"name": "id of recipe", | ||
"title": "id of recipe" | ||
}, | ||
"name": { | ||
"id": "/name", | ||
"type": "string", | ||
"description": "Name of the recipe", | ||
"name": "Name of Cake", | ||
"title": "Name of Cake" | ||
}, | ||
"glutenFree": { | ||
"id": "/glutenFree", | ||
"type": "boolean" | ||
}, | ||
"ingredients": { | ||
"id": "/ingredients", | ||
"type": "array", | ||
"items": [ | ||
{ | ||
"id": "ingredient", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"id": "name", | ||
"type": "string" | ||
}, | ||
"quantity": { | ||
"id": "quantity", | ||
"type": "integer" | ||
} | ||
} | ||
} | ||
], | ||
"minItems": 1, | ||
"description": "List ingredients and quantities for recipe" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"ingredients" | ||
] | ||
} |
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