diff --git a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/ComplexSchemaIT.java b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/ComplexSchemaIT.java new file mode 100644 index 0000000..f5e7874 --- /dev/null +++ b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/ComplexSchemaIT.java @@ -0,0 +1,58 @@ +package uk.gov.justice.generation.pojo.integration.test; + +import static java.util.stream.Collectors.toList; + +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.integration.utils.JsonSchemaLoader; +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.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(); + + @Test + public void shouldParseAVeryComplexSchemaDocument() throws Exception { + + final File jsonSchemaFile = new File("src/test/resources/schemas/context.command.complex-data.json"); + final ObjectSchema schema = JsonSchemaLoader.loadSchema(jsonSchemaFile, ObjectSchema.class); + final String fieldName = rootFieldNameGenerator.generateNameFrom(jsonSchemaFile); + + final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor("uk.gov.justice.pojo"); + final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(schema); + + jsonSchemaWrapper.accept(fieldName, definitionBuilderVisitor); + + final List 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); + }); + } +} diff --git a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/DefinitionBuilderIT.java b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/DefinitionBuilderIT.java index a801b63..a93bda2 100644 --- a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/DefinitionBuilderIT.java +++ b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/DefinitionBuilderIT.java @@ -7,6 +7,7 @@ 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.dom.ClassDefinition; import uk.gov.justice.generation.pojo.generators.ClassGeneratable; import uk.gov.justice.generation.pojo.generators.JavaGeneratorFactory; @@ -28,16 +29,19 @@ public class DefinitionBuilderIT { private final SourceWriter sourceWriter = new SourceWriter(); private final ClassCompiler classCompiler = new ClassCompiler(); private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); + private final RootFieldNameGenerator rootFieldNameGenerator = new RootFieldNameGenerator(); @Test public void shouldBuildTypeSpecFromSchema() throws Exception { + final File schemaFile = new File("src/test/resources/schemas/person-schema.json"); final ObjectSchema schema = JsonSchemaLoader - .loadSchema("src/test/resources/schemas/person-schema.json", ObjectSchema.class); + .loadSchema(schemaFile, ObjectSchema.class); + final String fieldName = rootFieldNameGenerator.generateNameFrom(schemaFile); final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor("uk.gov.justice.pojo"); - final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(schema); - jsonSchemaWrapper.accept(definitionBuilderVisitor); + final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(schema); + jsonSchemaWrapper.accept(fieldName, definitionBuilderVisitor); final ClassDefinition personClassDefinition = definitionBuilderVisitor.getDefinitions().get(0); final ClassGeneratable personClassGenerator = new JavaGeneratorFactory().createClassGeneratorFor(personClassDefinition); diff --git a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/SourceWriterIT.java b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/SourceWriterIT.java index bca5c41..8dfadfb 100644 --- a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/SourceWriterIT.java +++ b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/SourceWriterIT.java @@ -77,18 +77,4 @@ private ClassDefinition addressDefinition(final String packageName) { return addressDefinition; } - - private ClassDefinition employeeDefinition(final String packageName, final ClassDefinition addressDefinition) { - final ClassDefinition employeeDefinition = new ClassDefinition("employee", new ClassName(packageName, "Employee")); - employeeDefinition.addFieldDefinition(new FieldDefinition("firstName", new ClassName(String.class))); - employeeDefinition.addFieldDefinition(new FieldDefinition("lastName", new ClassName(String.class))); - employeeDefinition.addFieldDefinition(new FieldDefinition("poundsPerHour", new ClassName(BigDecimal.class))); - employeeDefinition.addFieldDefinition(new FieldDefinition("startDate", new ClassName(ZonedDateTime.class))); - employeeDefinition.addFieldDefinition(new FieldDefinition("favouriteColours", new ClassName(List.class), new ClassName(String.class))); - employeeDefinition.addFieldDefinition(addressDefinition); - - return employeeDefinition; - } - - } diff --git a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/FileLoader.java b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/FileLoader.java index 04c891d..747829c 100644 --- a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/FileLoader.java +++ b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/FileLoader.java @@ -10,8 +10,11 @@ public class FileLoader { - public String loadAsJsonSting(final String pathname) { - final File file = new File(pathname); + public String loadAsJsonString(final String pathname) { + return loadAsJsonString(new File(pathname)); + } + + public String loadAsJsonString(final File file) { try(final FileInputStream input = new FileInputStream(file)) { return IOUtils.toString(input, defaultCharset()); } catch (IOException e) { diff --git a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/JsonSchemaLoader.java b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/JsonSchemaLoader.java index f7e8239..1233faf 100644 --- a/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/JsonSchemaLoader.java +++ b/integration-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/JsonSchemaLoader.java @@ -1,5 +1,7 @@ package uk.gov.justice.generation.pojo.integration.utils; +import java.io.File; + import javax.inject.Inject; import org.everit.json.schema.ArraySchema; @@ -23,14 +25,10 @@ public class JsonSchemaLoader { * @return the schema */ @SuppressWarnings("unchecked") - public static T loadSchema(final String path, @SuppressWarnings("unused") final Class clazz) { + public static T loadSchema(final File path, @SuppressWarnings("unused") final Class clazz) { // TODO: load from classpath rather than working directory - final JSONObject schemaJsonObject = new JSONObject(new FileLoader().loadAsJsonSting(path)); + final JSONObject schemaJsonObject = new JSONObject(new FileLoader().loadAsJsonString(path)); return (T) SchemaLoader.load(schemaJsonObject); } - - public static ArraySchema loadArraySchema(final String path) { - return loadSchema(path, ArraySchema.class); - } } diff --git a/integration-test/src/test/resources/schemas/context.command.complex-data.json b/integration-test/src/test/resources/schemas/context.command.complex-data.json new file mode 100644 index 0000000..3512b8b --- /dev/null +++ b/integration-test/src/test/resources/schemas/context.command.complex-data.json @@ -0,0 +1,404 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "recordId": { + "$ref": "#/definitions/uuid" + }, + "person": { + "type": "object", + "properties": { + "title": { + "$ref": "#/definitions/nameTitle" + }, + "surname": { + "type": "string", + "maxLength": 30 + }, + "forenames": { + "type": "string", + "maxLength": 20 + }, + "initials": { + "type": "string", + "maxLength": 2 + }, + "dateOfBirth": { + "$ref": "#/definitions/dateString" + }, + "companyName": { + "type": "string", + "maxLength": 50 + }, + "age": { + "type": "integer", + "minimum": 0, + "maximum": 999 + }, + "addressLine1": { + "type": "string", + "maxLength": 50 + }, + "addressLine2": { + "type": "string", + "maxLength": 50 + }, + "addressLine3": { + "type": "string", + "maxLength": 50 + }, + "addressLine4": { + "type": "string", + "maxLength": 50 + }, + "addressLine5": { + "type": "string", + "maxLength": 50 + }, + "postCode": { + "type": "string", + "maxLength": 8 + }, + "alias": { + "type": "array", + "items": { + "type": "object", + "properties": { + "aliasForenames": { + "type": "string", + "maxLength": 20 + }, + "aliasInitials": { + "type": "string", + "maxLength": 2 + }, + "aliasSurname": { + "type": "string", + "maxLength": 30 + } + } + }, + "additionalProperties": false + }, + "nationalInsuranceNumber": { + "type": "string", + "maxLength": 9 + }, + "telephoneNumberHome": { + "$ref": "#/definitions/phoneNumber" + }, + "telephoneNumberBusiness": { + "$ref": "#/definitions/phoneNumber" + }, + "telephoneNumberMobile": { + "$ref": "#/definitions/phoneNumber" + }, + "emailAddress1": { + "type": "string", + "maxLength": 76 + }, + "emailAddress2": { + "type": "string", + "maxLength": 76 + }, + "vehicleMake": { + "type": "string", + "maxLength": 30 + }, + "vehicleRegistrationMark": { + "type": "string", + "maxLength": 11 + }, + "documentLanguage": { + "type": "string", + "enum": [ + "", + "W" + ] + }, + "accountComment": { + "type": "string", + "maxLength": 30 + } + } + }, + "result": { + "type": "array", + "items": { + "sequenceNumber": { + "type": "integer", + "minimum": 0, + "maximum": 999 + }, + "resultCode": { + "type": "string", + "enum": [ + "GF", + "ABHY", + "SW", + "COPI", + "FFF", + "FCOMP", + "BBC" + ] + }, + "majorCreditorCode": { + "type": "string", + "maxLength": 4 + }, + "minorCreditorSequenceNumber": { + "type": "integer", + "minimum": 0, + "maximum": 999 + } + } + }, + "parentGuardian": { + "type": "object", + "properties": { + "name": { + "type": "string", + "maxLength": 30 + } + }, + "addressLine1": { + "type": "string", + "maxLength": 50 + }, + "addressLine2": { + "type": "string", + "maxLength": 50 + }, + "addressLine3": { + "type": "string", + "maxLength": 50 + }, + "addressLine4": { + "type": "string", + "maxLength": 50 + }, + "addressLine5": { + "type": "string", + "maxLength": 50 + } + }, + "paymentTerms": { + "type": "object", + "properties": { + "paymentType": { + "type": "string", + "enum": [ + "BD", + "WK", + "FN", + "MO", + "WL", + "FL", + "ML", + "PD" + ] + }, + "payByDate": { + "$ref": "#/definitions/dateString" + }, + "lumpSumAmount": { + "$ref": "#/definitions/money" + }, + "instalmentAmount": { + "$ref": "#/definitions/money" + }, + "instalmentStartDate": { + "$ref": "#/definitions/dateString" + }, + "parentGuardianToPay": { + "type": "string", + "enum": [ + "", + "Y" + ] + } + } + }, + "minorCreditor": { + "type": "array", + "items": { + "sequenceNumber": { + "type": "integer", + "minimum": 0, + "maximum": 999 + }, + "minorCreditorTitle": { + "$ref": "#/definitions/nameTitle" + }, + "minorCreditorSurname": { + "type": "string", + "maxLength": 30 + }, + "minorCreditorInitials": { + "type": "string", + "maxLength": 2 + }, + "minorCreditorForenames": { + "type": "string", + "maxLength": 20 + }, + "companyName": { + "type": "string", + "maxLength": 50 + }, + "minorCreditorAddressLine1": { + "type": "string", + "maxLength": 50 + }, + "minorCreditorAddressLine2": { + "type": "string", + "maxLength": 50 + }, + "minorCreditorAddressLine3": { + "type": "string", + "maxLength": 50 + }, + "minorCreditorAddressLine4": { + "type": "string", + "maxLength": 50 + }, + "minorCreditorAddressLine5": { + "type": "string", + "maxLength": 50 + }, + "minorCreditorPostcode": { + "type": "string", + "maxLength": 8 + }, + "minorCreditorPayByBACS": { + "type": "string", + "enum": { + "items": [ + "Y", + "N" + ] + } + }, + "bankAccountType": { + "type": "integer", + "minimum": 0, + "maximum": 9 + }, + "bankSortCode": { + "type": "integer", + "minimum": 0, + "maximum": 999999 + }, + "BankAccountNumber": { + "type": "integer", + "minimum": 0, + "maximum": 99999999 + }, + "accountName": { + "type": "string", + "maxLength": 18 + }, + "bankAccountReference": { + "type": "string", + "maxLength": 18 + } + } + }, + "accountNote": { + "type": "array", + "items": { + "sequenceNumber": { + "type": "integer", + "minimum": 0, + "maximum": 999 + }, + "accountNoteText": { + "type": "String", + "maxLength": 28 + } + } + }, + "employer": { + "type": "object", + "properties": { + "employerReference": { + "type": "string", + "maxLength": 20 + }, + "employerCompanyName": { + "type": "string", + "maxLength": 35 + }, + "employerAddressLine1": { + "type": "string", + "maxLength": 32 + }, + "employerAddressLine2": { + "type": "string", + "maxLength": 32 + }, + "employerAddressLine3": { + "type": "string", + "maxLength": 32 + }, + "employerAddressLine4": { + "type": "string", + "maxLength": 32 + }, + "employerAddressLine5": { + "type": "string", + "maxLength": 32 + }, + "employerPostcode": { + "type": "string", + "maxLength": 8 + }, + "employerTelephoneNumber": { + "$ref": "#/definitions/phoneNumber" + }, + "employerEmailAddress": { + "type": "string", + "maxLength": 32 + } + } + } + }, + "additionalProperties": false, + "required": [ + "enterpriseId", + "personId", + "person", + "result", + "paymentTerms", + "accountNote" + ], + "definitions": { + "uuid": { + "type": "string", + "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" + }, + "dateString": { + "type": "string", + "pattern": "^[1|2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$" + }, + "money": { + "type": "number", + "multipleOf": 0.01 + }, + "nameTitle": { + "type": "string", + "enum": [ + "Mr", + "Mrs", + "Ms", + "Miss", + "Co" + ] + }, + "phoneNumber": { + "type": "string", + "maxLength": 35, + "pattern": "^[\\d -]*$" + } + } +} diff --git a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaParseException.java b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaParseException.java new file mode 100644 index 0000000..8d421bf --- /dev/null +++ b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaParseException.java @@ -0,0 +1,8 @@ +package uk.gov.justice.generation.pojo.core; + +public class JsonSchemaParseException extends RuntimeException { + + public JsonSchemaParseException(final String message) { + super(message); + } +} diff --git a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapper.java b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapper.java index 62a5338..8ed5def 100644 --- a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapper.java +++ b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapper.java @@ -23,12 +23,7 @@ public JsonSchemaWrapper(final Schema schema) { } @Override - public void accept(final Visitor visitor) { - final String fieldName = schema.getId(); - accept(fieldName, visitor); - } - - private void accept(final String fieldName, final Visitor visitor) { + public void accept(final String fieldName, final Visitor visitor) { switch (schema.getClass().getSimpleName()) { case "ObjectSchema": diff --git a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/RootFieldNameGenerator.java b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/RootFieldNameGenerator.java new file mode 100644 index 0000000..162bd08 --- /dev/null +++ b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/RootFieldNameGenerator.java @@ -0,0 +1,43 @@ +package uk.gov.justice.generation.pojo.core; + +import static java.lang.String.format; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.lang3.StringUtils.uncapitalize; + +import java.io.File; +import java.util.stream.Stream; + +import org.apache.commons.lang3.StringUtils; + +public class RootFieldNameGenerator { + + public String generateNameFrom(final File jsonSchemaFile) { + + final String fileName = jsonSchemaFile.getName(); + + if (!fileName.endsWith(".json")) { + throw new JsonSchemaParseException(format("Failed to load json schema file '%s'. File does not have a '.json' extension", jsonSchemaFile.getAbsolutePath())); + } + + final String name = getNameFrom(fileName); + + if (name.contains(".") || name.isEmpty()) { + throw new JsonSchemaParseException(format("Failed to load json schema file '%s'. File name is invalid", jsonSchemaFile.getAbsolutePath())); + } + + final String className = Stream.of(name.split("-")).map(StringUtils::capitalize).collect(joining()); + + return uncapitalize(className); + } + + private String getNameFrom(final String fileName) { + final String name = fileName.substring(0, fileName.lastIndexOf(".")); + + final int index = name.lastIndexOf('.'); + if (index > -1) { + return name.substring(index + 1); + } + + return name; + } +} diff --git a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/Visitable.java b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/Visitable.java index 12edeb8..0f9e20f 100644 --- a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/Visitable.java +++ b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/Visitable.java @@ -2,6 +2,5 @@ public interface Visitable { - void accept(final Visitor visitor); - + void accept(String fieldName, Visitor visitor); } diff --git a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/generators/ClassGenerator.java b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/generators/ClassGenerator.java index 34bc981..d3c9004 100644 --- a/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/generators/ClassGenerator.java +++ b/pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/generators/ClassGenerator.java @@ -24,7 +24,7 @@ public class ClassGenerator implements ClassGeneratable { private final JavaGeneratorFactory javaGeneratorFactory; private final DefinitionToTypeNameConverter definitionToTypeNameConverter = new DefinitionToTypeNameConverter(); - ClassGenerator(final ClassDefinition classDefinition, final JavaGeneratorFactory javaGeneratorFactory) { + public ClassGenerator(final ClassDefinition classDefinition, final JavaGeneratorFactory javaGeneratorFactory) { this.classDefinition = classDefinition; this.javaGeneratorFactory = javaGeneratorFactory; } diff --git a/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapperTest.java b/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapperTest.java index d57c346..e89144b 100644 --- a/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapperTest.java +++ b/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/JsonSchemaWrapperTest.java @@ -38,7 +38,7 @@ public void shouldWrapObjectSchema() { final ObjectSchema objectSchema = ObjectSchema.builder().id(fieldName).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); } @@ -49,10 +49,10 @@ public void shouldVisitObjectAndStringSchema() { final StringSchema stringSchema = StringSchema.builder().build(); final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, stringSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, stringSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, stringSchema); @@ -64,10 +64,10 @@ public void shouldVisitObjectAndBooleanSchema() { final BooleanSchema booleanSchema = BooleanSchema.INSTANCE; final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, booleanSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, booleanSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, booleanSchema); @@ -79,10 +79,10 @@ public void shouldVisitObjectAndNumberSchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final NumberSchema numberSchema = NumberSchema.builder().build(); - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, numberSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, numberSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, numberSchema); @@ -94,10 +94,10 @@ public void shouldVisitObjectAndEnumSchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final EnumSchema enumSchema = EnumSchema.builder().build(); - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, enumSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, enumSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, enumSchema); @@ -109,10 +109,10 @@ public void shouldVisitObjectAndNullSchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final NullSchema nullSchema = NullSchema.INSTANCE; - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, nullSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, nullSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, nullSchema); @@ -124,10 +124,10 @@ public void shouldVisitObjectAndEmptySchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final EmptySchema emptySchema = EmptySchema.INSTANCE; - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, emptySchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, emptySchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, emptySchema); @@ -139,10 +139,10 @@ public void shouldVisitObjectAndReferenceSchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final ReferenceSchema referenceSchema = ReferenceSchema.builder().build(); - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, referenceSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, referenceSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, referenceSchema); @@ -154,10 +154,10 @@ public void shouldVisitObjectAndArraySchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final ArraySchema arraySchema = ArraySchema.builder().build(); - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, arraySchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, arraySchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, arraySchema); @@ -169,10 +169,10 @@ public void shouldVisitObjectAndCombinedSchema() { final String fieldName = "fieldName"; final String childFieldName = "childFieldName"; final CombinedSchema combinedSchema = CombinedSchema.builder().criterion(ANY_CRITERION).build(); - final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, combinedSchema).id(fieldName).build(); + final ObjectSchema objectSchema = ObjectSchema.builder().addPropertySchema(childFieldName, combinedSchema).build(); final JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(objectSchema); - jsonSchemaWrapper.accept(visitor); + jsonSchemaWrapper.accept(fieldName, visitor); verify(visitor).enter(fieldName, objectSchema); verify(visitor).visit(childFieldName, combinedSchema); @@ -185,20 +185,29 @@ public void shouldThrowExceptionIfUnknownSchemaProcessed() throws Exception { expectedException.expectMessage("Schema of type: DummySchema is not supported."); final Schema.Builder builder = mock(Schema.Builder.class); - final DummySchema dummySchema = new DummySchema(builder); + final String fieldName = "myDummy"; + final DummySchema dummySchema = new DummySchema(builder, fieldName); - new JsonSchemaWrapper(dummySchema).accept(mock(Visitor.class)); + new JsonSchemaWrapper(dummySchema).accept(fieldName, mock(Visitor.class)); } private class DummySchema extends Schema { - DummySchema(final Builder builder) { + private final String id; + + DummySchema(final Builder builder, final String id) { super(builder); + this.id = id; } @Override public void validate(final Object o) { //do nothing } + + @Override + public String getId() { + return id; + } } } diff --git a/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/RootFieldNameGeneratorTest.java b/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/RootFieldNameGeneratorTest.java new file mode 100644 index 0000000..4c61b4a --- /dev/null +++ b/pojo-generator-core/src/test/java/uk/gov/justice/generation/pojo/core/RootFieldNameGeneratorTest.java @@ -0,0 +1,82 @@ +package uk.gov.justice.generation.pojo.core; + +import static org.junit.Assert.*; + +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.mockito.InjectMocks; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.File; + + +@RunWith(MockitoJUnitRunner.class) +public class RootFieldNameGeneratorTest { + + @InjectMocks + private RootFieldNameGenerator rootFieldNameGenerator; + + @Test + public void shouldParseTheFileNameIntoAValidJavaClassName() throws Exception { + + final File schemaFile = new File("src/test/resources/schemas/object-property-schema.json"); + + assertThat(schemaFile.exists(), is(true)); + + assertThat(rootFieldNameGenerator.generateNameFrom(schemaFile), is("objectPropertySchema")); + } + + @Test + public void shouldRmoveAnyPrependingDotsFromTheFileName() throws Exception { + + final File schemaFile = new File("src/test/resources/schemas/context.command.do-something-or-other.json"); + + assertThat(schemaFile.exists(), is(true)); + + assertThat(rootFieldNameGenerator.generateNameFrom(schemaFile), is("doSomethingOrOther")); + } + + @Test + public void shouldFailIfTheFileNameDoesNotHaveTheExtensionJson() throws Exception { + + final File schemaFile = new File("src/test/resources/schemas/object-property-schema"); + assertThat(schemaFile.exists(), is(true)); + try { + rootFieldNameGenerator.generateNameFrom(schemaFile); + fail(); + } catch (final JsonSchemaParseException expected) { + assertThat(expected.getMessage(), is("Failed to load json schema file '" + schemaFile.getAbsolutePath() + "'. File does not have a '.json' extension")); + } + } + + @Test + public void shouldFailIfTheFileNameIsOnlyTheExtendion() throws Exception { + + final File schemaFile = new File("src/test/resources/schemas/.json"); + try { + rootFieldNameGenerator.generateNameFrom(schemaFile); + fail(); + } catch (final JsonSchemaParseException expected) { + assertThat(expected.getMessage(), is("Failed to load json schema file '" + schemaFile.getAbsolutePath() + "'. File name is invalid")); + } + } + + @Test + public void shouldFailIfTheFileNameIsOnlyADot() throws Exception { + + final File schemaFile = new File("src/test/resources/schemas/..json"); + try { + rootFieldNameGenerator.generateNameFrom(schemaFile); + fail(); + } catch (final JsonSchemaParseException expected) { + assertThat(expected.getMessage(), is("Failed to load json schema file '" + schemaFile.getAbsolutePath() + "'. File name is invalid")); + } + } +} diff --git a/pojo-generator-core/src/test/resources/schemas/context.command.do-something-or-other.json b/pojo-generator-core/src/test/resources/schemas/context.command.do-something-or-other.json new file mode 100644 index 0000000..3db2b13 --- /dev/null +++ b/pojo-generator-core/src/test/resources/schemas/context.command.do-something-or-other.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "filename": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "filename" + ] +} diff --git a/pojo-generator-core/src/test/resources/schemas/object-property-schema b/pojo-generator-core/src/test/resources/schemas/object-property-schema new file mode 100644 index 0000000..094493c --- /dev/null +++ b/pojo-generator-core/src/test/resources/schemas/object-property-schema @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "objectProperty": { + "type": "object", + "properties": { + "stringProperty": { + "type": "string" + } + } + } + } +} diff --git a/pojo-generator-core/src/test/resources/schemas/object-property-schema.json b/pojo-generator-core/src/test/resources/schemas/object-property-schema.json index 59f7e93..094493c 100644 --- a/pojo-generator-core/src/test/resources/schemas/object-property-schema.json +++ b/pojo-generator-core/src/test/resources/schemas/object-property-schema.json @@ -4,7 +4,6 @@ "properties": { "objectProperty": { "type": "object", - "id": "objectProperty", "properties": { "stringProperty": { "type": "string" diff --git a/pojo-generator-core/src/test/resources/schemas/person-schema.json b/pojo-generator-core/src/test/resources/schemas/person-schema.json index 1e68c8b..0e6a254 100644 --- a/pojo-generator-core/src/test/resources/schemas/person-schema.json +++ b/pojo-generator-core/src/test/resources/schemas/person-schema.json @@ -1,7 +1,6 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", - "id": "person", "properties": { "firstName": { "type": "string" diff --git a/pojo-generator-core/src/test/resources/schemas/simple-property-schema.json b/pojo-generator-core/src/test/resources/schemas/simple-property-schema.json index 0d5fb49..f8350e0 100644 --- a/pojo-generator-core/src/test/resources/schemas/simple-property-schema.json +++ b/pojo-generator-core/src/test/resources/schemas/simple-property-schema.json @@ -1,6 +1,5 @@ { "$schema": "http://json-schema.org/draft-04/schema#", - "id": "simpleProperty", "type": "object", "properties": { "stringProperty": { diff --git a/pojo-generator-core/src/test/resources/schemas/strings-schema.json b/pojo-generator-core/src/test/resources/schemas/strings-schema.json index 661debe..b202eb8 100644 --- a/pojo-generator-core/src/test/resources/schemas/strings-schema.json +++ b/pojo-generator-core/src/test/resources/schemas/strings-schema.json @@ -1,6 +1,5 @@ { "$schema": "http://json-schema.org/draft-04/schema#", - "id": "stringsExample", "type": "array", "items": { "type": "string"