diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java index ff4871a..77be3fe 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java @@ -2,6 +2,10 @@ public class SchemaCatalogException extends RuntimeException { + public SchemaCatalogException(final String message) { + super(message); + } + public SchemaCatalogException(final String message, final Throwable cause) { super(message, cause); } diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java index 10a38a9..e26e7a1 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java @@ -26,4 +26,12 @@ public URL toUrl(final URI uri) { throw new SchemaCatalogException(format("Failed to convert URI '%s' to URL", uri), e); } } + + public URI toUri(final String uri) { + try { + return new URI(uri); + } catch (URISyntaxException e) { + throw new SchemaCatalogException(format("Failed to convert URI '%s' to URL", uri), e); + } + } } diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java index 02a5d53..9655978 100644 --- a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java @@ -57,7 +57,7 @@ public void shouldLoadCatalogsFromTheClasspath() throws Exception { assertThat(catalog.getGroup().size(), is(2)); assertThat(catalog.getGroup().get(0).getName(), is("standards")); - assertThat(catalog.getGroup().get(0).getBaseLocation(), is("/json/schema/standards")); + assertThat(catalog.getGroup().get(0).getBaseLocation(), is("standards/")); assertThat(catalog.getGroup().get(0).getSchema().size(), is(2)); assertThat(catalog.getGroup().get(0).getSchema().get(0).getId(), is("http://justice.gov.uk/standards/complex_address.json")); @@ -70,6 +70,6 @@ public void shouldLoadCatalogsFromTheClasspath() throws Exception { assertThat(catalog.getGroup().get(1).getSchema().size(), is(1)); assertThat(catalog.getGroup().get(1).getSchema().get(0).getId(), is("http://justice.gov.uk/context/person.json")); - assertThat(catalog.getGroup().get(1).getSchema().get(0).getLocation(), is("/json/schema/context/person.json")); + assertThat(catalog.getGroup().get(1).getSchema().get(0).getLocation(), is("context/person.json")); } } diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java index f86e4e6..76801a6 100644 --- a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java @@ -1,9 +1,14 @@ package uk.gov.justice.schema.catalog.util; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import uk.gov.justice.schema.catalog.SchemaCatalogException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import org.junit.Test; @@ -35,4 +40,25 @@ public void shouldConvertAnUrlToAnUri() throws Exception { assertThat(url.toString(), is("file://src/main/fred.txt")); } + + @Test + public void shouldConvertAUriStringToAUriObject() throws Exception { + + final String uri = "file://src/main/fred.txt"; + + assertThat(urlConverter.toUri(uri).toString(), is(uri)); + } + + @Test + public void shouldFailIfConvertingAStringToAUriThrowsAURISyntaxException() throws Exception { + final String uri = "this is not a uri"; + + try { + urlConverter.toUri(uri); + fail(); + } catch (final SchemaCatalogException expected) { + assertThat(expected.getCause(), is(instanceOf(URISyntaxException.class))); + assertThat(expected.getMessage(), is("Failed to convert URI 'this is not a uri' to URL")); + } + } } diff --git a/catalog-core/src/test/resources/json/schema/schema_catalog.json b/catalog-core/src/test/resources/json/schema/schema_catalog.json index a16272a..53acf52 100644 --- a/catalog-core/src/test/resources/json/schema/schema_catalog.json +++ b/catalog-core/src/test/resources/json/schema/schema_catalog.json @@ -4,7 +4,7 @@ "group": [ { "name": "standards", - "baseLocation": "/json/schema/standards", + "baseLocation": "standards/", "schema": [ { "id": "http://justice.gov.uk/standards/complex_address.json", @@ -21,7 +21,7 @@ "schema": [ { "id": "http://justice.gov.uk/context/person.json", - "location": "/json/schema/context/person.json" + "location": "context/person.json" } ] } diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java index 10f86bc..fbcdebc 100644 --- a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java @@ -7,7 +7,7 @@ public class Catalog { private final List group; private final String name; - public Catalog(final List group, final String name) { + public Catalog(final String name, final List group) { this.group = group; this.name = name; } @@ -20,4 +20,11 @@ public String getName() { return name; } + @Override + public String toString() { + return "Catalog{" + + "group=" + group + + ", name='" + name + '\'' + + '}'; + } } diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java index 953e956..126e06c 100644 --- a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java @@ -2,13 +2,14 @@ public class CatalogWrapper { - private Catalog catalog; + private final Catalog catalog; + + public CatalogWrapper(final Catalog catalog) { + this.catalog = catalog; + } public Catalog getCatalog() { return catalog; } - public void setCatalog(Catalog catalog) { - this.catalog = catalog; - } } diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java index e937345..c0fa18f 100644 --- a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java @@ -25,4 +25,13 @@ public String getName() { public List getSchema() { return schema; } + + @Override + public String toString() { + return "Group{" + + "baseLocation='" + baseLocation + '\'' + + ", name='" + name + '\'' + + ", schema=" + schema + + '}'; + } } diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java index 9003b86..4533acf 100644 --- a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java @@ -17,4 +17,12 @@ public String getId() { public String getLocation() { return location; } + + @Override + public String toString() { + return "Schema{" + + "id='" + id + '\'' + + ", location='" + location + '\'' + + '}'; + } } diff --git a/everit-schema-client/src/main/resources/json/schema/json_catalog.schema.json b/catalog-domain/src/main/resources/domain/json_catalog.schema.json similarity index 100% rename from everit-schema-client/src/main/resources/json/schema/json_catalog.schema.json rename to catalog-domain/src/main/resources/domain/json_catalog.schema.json diff --git a/catalog-generation-plugin-it/pom.xml b/catalog-generation-plugin-it/pom.xml new file mode 100644 index 0000000..1b66ed6 --- /dev/null +++ b/catalog-generation-plugin-it/pom.xml @@ -0,0 +1,62 @@ + + + + json-schema-catalog + uk.gov.justice.schema + 1.0.0-SNAPSHOT + + 4.0.0 + + catalog-generation-plugin-it + + + + + generator-plugin + uk.gov.justice.maven.generator + ${generator-maven-plugin.version} + + + internal-jsons + + + uk.gov.justice.schema.catalog.generation.MavenCatalogGenerator + + + uk.gov.justice.schema.catalog.generation.io.parser.ListOfUriParser + + uk.gov.justice.events.pojo + ${basedir}/src/raml/json/schema + + ${project.build.directory}/generated-sources + + + **/*.json + + + + + ${project.artifactId} + + + + generate + + generate-sources + + + + + uk.gov.justice.schema + catalog-generation + ${project.version} + + + + + + + diff --git a/catalog-generation-plugin-it/src/raml/json/schema/context/person.json b/catalog-generation-plugin-it/src/raml/json/schema/context/person.json new file mode 100644 index 0000000..c27c64b --- /dev/null +++ b/catalog-generation-plugin-it/src/raml/json/schema/context/person.json @@ -0,0 +1,21 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/context/person.json", + "type": "object", + "properties": { + "nino": { + "type": "string" + }, + "name": { + "type": "string" + }, + "correspondence_address": + {"$ref" : "http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2"} + + }, + "required": [ + "nino", + "name", + "correspondence_address" + ] +} diff --git a/catalog-generation-plugin-it/src/raml/json/schema/standards/address.json b/catalog-generation-plugin-it/src/raml/json/schema/standards/address.json new file mode 100644 index 0000000..eea5825 --- /dev/null +++ b/catalog-generation-plugin-it/src/raml/json/schema/standards/address.json @@ -0,0 +1,25 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/address.json", + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode" + ] + +} \ No newline at end of file diff --git a/catalog-generation-plugin-it/src/raml/json/schema/standards/complex_address.json b/catalog-generation-plugin-it/src/raml/json/schema/standards/complex_address.json new file mode 100644 index 0000000..290bf06 --- /dev/null +++ b/catalog-generation-plugin-it/src/raml/json/schema/standards/complex_address.json @@ -0,0 +1,65 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/complex_address.json", + "type": "object", + "definitions": { + "complex_address": { + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + }, + "country": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode", + "country" + ] + }, + "complex_address2": { + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + }, + "ss": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode", + "ss" + ] + } + }, + "allOf": [ + { + "$ref": "#/definitions/complex_address" + } + ] +} + diff --git a/catalog-generation-plugin-it/src/raml/json/schema/standards/defendant.json b/catalog-generation-plugin-it/src/raml/json/schema/standards/defendant.json new file mode 100644 index 0000000..9a29411 --- /dev/null +++ b/catalog-generation-plugin-it/src/raml/json/schema/standards/defendant.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/defendant.json", + "type": "object", + "properties": { + "personalInfo": { + "type": "string" + }, + "currentAddress": { + "type": "string" + }, + "correspondenceAddress": { + "type": "string" + }, + "courtCentreId": { + "type": "string" + } + }, + "required": [ + "personalInfo", + "currentAddress" + ] +} diff --git a/catalog-generation/paths.txt b/catalog-generation/paths.txt new file mode 100644 index 0000000..f68ead7 --- /dev/null +++ b/catalog-generation/paths.txt @@ -0,0 +1,20 @@ + + +Schema file location: + file:/Users/dev/workspace/json-schema-catalog/catalog-generation/target/test-classes/raml/json/schema/context/person.json + +Id (taken from the schema file): + http://justice.gov.uk/context/person.json + +Location where catalog will be generated (on classpath): + raml/json/schema/schema_catalog.json + +Group: + context + +Base location: + context/ + +Location + person.json + diff --git a/catalog-generation/pom.xml b/catalog-generation/pom.xml new file mode 100644 index 0000000..272f99b --- /dev/null +++ b/catalog-generation/pom.xml @@ -0,0 +1,73 @@ + + + + json-schema-catalog + uk.gov.justice.schema + 1.0.0-SNAPSHOT + + 4.0.0 + + catalog-generation + + + + uk.gov.justice.schema + catalog-core + ${project.version} + + + uk.gov.justice.maven.generator + generator-core + ${generator-maven-plugin.version} + + + uk.gov.justice.maven.generator + parser-common + ${generator-maven-plugin.version} + + + org.reflections + reflections + + + uk.gov.justice.maven.generator + generator-core + ${generator-maven-plugin.version} + + + uk.gov.justice.maven.generator + parser-common + ${generator-maven-plugin.version} + + + org.apache.maven.plugin-tools + maven-plugin-annotations + provided + + + org.apache.maven + maven-artifact + + + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + com.jayway.jsonpath + json-path-assert + test + + + diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationContext.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationContext.java new file mode 100644 index 0000000..f241a8d --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationContext.java @@ -0,0 +1,26 @@ +package uk.gov.justice.schema.catalog.generation; + +public class CatalogGenerationContext { + + private static final String CATALOG_GENERATION_ROOT = "target/generated-catalogs"; + private static final String CATALOG_PATH = "json/schema/catalog"; + private static final String CATALOG_FILENAME = "schema_catalog.json"; + private static final String JSON_SCHEMA_PATH = "raml/json/schema/"; + public static final String AN_EMPTY_STRING = ""; + + public String getCatalogGenerationRoot() { + return CATALOG_GENERATION_ROOT; + } + + public String getCatalogPath() { + return CATALOG_PATH; + } + + public String getCatalogFilename() { + return CATALOG_FILENAME; + } + + public String getJsonSchemaPath() { + return JSON_SCHEMA_PATH; + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationException.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationException.java new file mode 100644 index 0000000..1a5c44b --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationException.java @@ -0,0 +1,14 @@ +package uk.gov.justice.schema.catalog.generation; + +import uk.gov.justice.schema.catalog.SchemaCatalogException; + +public class CatalogGenerationException extends SchemaCatalogException { + + public CatalogGenerationException(final String message) { + super(message); + } + + public CatalogGenerationException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationRunner.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationRunner.java new file mode 100644 index 0000000..065e180 --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationRunner.java @@ -0,0 +1,41 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.util.stream.Collectors.toList; + +import uk.gov.justice.schema.catalog.domain.Catalog; +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URI; +import java.net.URL; +import java.util.List; + +public class CatalogGenerationRunner { + + private final CatalogObjectGenerator catalogObjectGenerator; + private final CatalogWriter catalogWriter; + private final UrlConverter urlConverter; + + public CatalogGenerationRunner( + final CatalogObjectGenerator catalogObjectGenerator, + final CatalogWriter catalogWriter, + final UrlConverter urlConverter) { + this.catalogObjectGenerator = catalogObjectGenerator; + this.catalogWriter = catalogWriter; + this.urlConverter = urlConverter; + } + + public void generateCatalog(final String catalogName, final List schemaFiles) { + + final Catalog catalog = catalogObjectGenerator.generate( + catalogName, + asUrls(schemaFiles)); + + catalogWriter.write(catalog); + } + + private List asUrls(final List schemaFiles) { + return schemaFiles.stream() + .map(urlConverter::toUrl) + .collect(toList()); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogObjectGenerator.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogObjectGenerator.java new file mode 100644 index 0000000..d6fdb0b --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogObjectGenerator.java @@ -0,0 +1,62 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.util.stream.Collectors.toList; + +import uk.gov.justice.schema.catalog.domain.Catalog; +import uk.gov.justice.schema.catalog.domain.Group; +import uk.gov.justice.schema.catalog.domain.Schema; + +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CatalogObjectGenerator { + + private final SchemaDefParser schemaDefParser; + + public CatalogObjectGenerator(final SchemaDefParser schemaDefParser) { + this.schemaDefParser = schemaDefParser; + } + + public Catalog generate(final String catalogName, final List schemaFiles) { + + final List schemaDefs = schemaFiles.stream() + .map(schemaDefParser::parse) + .collect(toList()); + + final List groups = asGroups(schemaDefs); + + return new Catalog(catalogName, groups); + } + + private List asGroups(final List schemaDefs) { + + final Map groups = new HashMap<>(); + + schemaDefs.forEach(schemaDef -> createGroups(schemaDef, groups)); + + return new ArrayList<>(groups.values()); + } + + private void createGroups(final SchemaDef schemaDef, final Map groups) { + + final String groupName = schemaDef.getGroupName(); + if (!groups.containsKey(groupName)) { + + final Group group = new Group( + schemaDef.getBaseLocation(), + groupName, + new ArrayList<>()); + + groups.put(groupName, group); + } + + final Group group = groups.get(groupName); + + final List schemas = group.getSchema(); + + schemas.add(new Schema(schemaDef.getId().toString(), schemaDef.getLocation())); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogWriter.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogWriter.java new file mode 100644 index 0000000..09a3aeb --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/CatalogWriter.java @@ -0,0 +1,50 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.lang.String.format; + +import uk.gov.justice.schema.catalog.domain.Catalog; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.IOUtils; + +public class CatalogWriter { + + private final ObjectMapper objectMapper; + private final CatalogGenerationContext catalogGenerationContext; + + public CatalogWriter(final ObjectMapper objectMapper, final CatalogGenerationContext catalogGenerationContext) { + this.objectMapper = objectMapper; + this.catalogGenerationContext = catalogGenerationContext; + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + public void write(final Catalog catalog) { + + final String catalogJson = writeAsString(catalog); + + final String catalogGenerationRoot = catalogGenerationContext.getCatalogGenerationRoot(); + final String catalogPath = catalogGenerationContext.getCatalogPath(); + final File outputDirectory = new File(catalogGenerationRoot, catalogPath); + outputDirectory.mkdirs(); + + final File outputFile = new File(outputDirectory, catalogGenerationContext.getCatalogFilename()); + try(final FileWriter fileWriter = new FileWriter(outputFile)) { + IOUtils.write(catalogJson, fileWriter); + } catch (final IOException e) { + throw new CatalogGenerationException(format("Failed to write catalog to '%s'", outputFile.getAbsolutePath()), e); + } + } + + private String writeAsString(final Catalog catalog) { + try { + return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(catalog); + } catch (final JsonProcessingException e) { + throw new CatalogGenerationException("Failed to write Catalog as json string", e); + } + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/MavenCatalogGenerator.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/MavenCatalogGenerator.java new file mode 100644 index 0000000..c0dcf79 --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/MavenCatalogGenerator.java @@ -0,0 +1,36 @@ +package uk.gov.justice.schema.catalog.generation; + +import uk.gov.justice.maven.generator.io.files.parser.core.Generator; +import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig; +import uk.gov.justice.schema.catalog.generation.maven.CatalogGeneratorProperties; + +import java.net.URI; +import java.util.List; + +import com.google.common.annotations.VisibleForTesting; + +public class MavenCatalogGenerator implements Generator> { + + private final ObjectFactory objectFactory; + + public MavenCatalogGenerator() { + this(new ObjectFactory()); + } + + @VisibleForTesting + public MavenCatalogGenerator(final ObjectFactory objectFactory) { + this.objectFactory = objectFactory; + } + + @Override + public void run(final List schemaFiles, final GeneratorConfig generatorConfig) { + + final CatalogGenerationRunner catalogGenerationRunner = objectFactory.catalogGenerationRunner(); + + final CatalogGeneratorProperties generatorProperties = + (CatalogGeneratorProperties) generatorConfig.getGeneratorProperties(); + final String catalogName = generatorProperties.getCatalogName(); + + catalogGenerationRunner.generateCatalog(catalogName, schemaFiles); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/ObjectFactory.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/ObjectFactory.java new file mode 100644 index 0000000..7768766 --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/ObjectFactory.java @@ -0,0 +1,44 @@ +package uk.gov.justice.schema.catalog.generation; + +import uk.gov.justice.schema.catalog.util.UrlConverter; +import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class ObjectFactory { + + public CatalogGenerationContext catalogGenerationConstants() { + return new CatalogGenerationContext(); + } + + public UrlConverter urlConverter() { + return new UrlConverter(); + } + + public ObjectMapper objectMapper() { + return new ObjectMapperProducer().objectMapper(); + } + + public CatalogWriter catalogWriter() { + return new CatalogWriter(objectMapper(), catalogGenerationConstants()); + } + + public SchemaIdParser schemaIdParser() { + return new SchemaIdParser(urlConverter()); + } + + public CatalogObjectGenerator catalogObjectGenerator() { + return new CatalogObjectGenerator(schemaDefParser()); + } + + public SchemaDefParser schemaDefParser() { + return new SchemaDefParser(schemaIdParser(), catalogGenerationConstants()); + } + + public CatalogGenerationRunner catalogGenerationRunner() { + return new CatalogGenerationRunner( + catalogObjectGenerator(), + catalogWriter(), + urlConverter()); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaDef.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaDef.java new file mode 100644 index 0000000..84e7cc7 --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaDef.java @@ -0,0 +1,46 @@ +package uk.gov.justice.schema.catalog.generation; + +import java.net.URI; +import java.net.URL; + +public class SchemaDef { + + private final URL schemaFile; + private final URI id; + private final String groupName; + private final String baseLocation; + private final String location; + + public SchemaDef( + final URL schemaFile, + final URI id, + final String groupName, + final String baseLocation, + final String location) { + this.schemaFile = schemaFile; + this.id = id; + this.groupName = groupName; + this.baseLocation = baseLocation; + this.location = location; + } + + public URL getSchemaFile() { + return schemaFile; + } + + public URI getId() { + return id; + } + + public String getGroupName() { + return groupName; + } + + public String getBaseLocation() { + return baseLocation; + } + + public String getLocation() { + return location; + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaDefParser.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaDefParser.java new file mode 100644 index 0000000..70a729d --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaDefParser.java @@ -0,0 +1,54 @@ +package uk.gov.justice.schema.catalog.generation; + +import static uk.gov.justice.schema.catalog.generation.CatalogGenerationContext.AN_EMPTY_STRING; + +import java.net.URI; +import java.net.URL; + +public class SchemaDefParser { + + private final SchemaIdParser schemaIdParser; + private final CatalogGenerationContext catalogGenerationContext; + + public SchemaDefParser( + final SchemaIdParser schemaIdParser, + final CatalogGenerationContext catalogGenerationContext) { + this.schemaIdParser = schemaIdParser; + this.catalogGenerationContext = catalogGenerationContext; + } + + public SchemaDef parse(final URL schemaFile) { + + final URI id = schemaIdParser.parse(schemaFile); + final String fileUrl = schemaFile.toString(); + + final String jsonSchemaPath = catalogGenerationContext.getJsonSchemaPath(); + final String relativeUri = fileUrl.substring(fileUrl.indexOf(jsonSchemaPath) + jsonSchemaPath.length()); + + final int firstSlashIndex = relativeUri.indexOf('/'); + + final String groupName = getGroup(relativeUri, firstSlashIndex); + final String baseLocation = getBaseLocation(groupName, firstSlashIndex); + final String location = relativeUri.substring(firstSlashIndex + 1); + + return new SchemaDef(schemaFile, id, groupName, baseLocation, location); + } + + private String getGroup(final String relativeUri, final int firstSlashIndex) { + + if(firstSlashIndex == -1) { + return AN_EMPTY_STRING; + } + + return relativeUri.substring(0, firstSlashIndex); + } + + private String getBaseLocation(final String groupName, final int firstSlashIndex) { + + if(firstSlashIndex == -1) { + return AN_EMPTY_STRING; + } + + return groupName + "/"; + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaFinder.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaFinder.java new file mode 100644 index 0000000..9699bc6 --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaFinder.java @@ -0,0 +1,43 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.util.Collections.sort; +import static java.util.stream.Collectors.toList; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +import org.reflections.Reflections; +import org.reflections.scanners.ResourcesScanner; + +public class SchemaFinder { + + private static final String SCHEMA_PACKAGE = "raml.json.schema"; + + private final UrlConverter urlConverter; + + public SchemaFinder(final UrlConverter urlConverter) { + this.urlConverter = urlConverter; + } + + public List listSchemas() { + + final Reflections reflections = new Reflections(SCHEMA_PACKAGE, new ResourcesScanner()); + final List fileNames = new ArrayList<>(reflections.getResources(Pattern.compile(".*\\.json"))); + + sort(fileNames); + + return fileNames.stream() + .map(this::asUri) + .collect(toList()); + } + + private URI asUri(final String filename) { + final URL resource = getClass().getClassLoader().getResource(filename); + return urlConverter.toUri(resource); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaIdParser.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaIdParser.java new file mode 100644 index 0000000..056cf84 --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/SchemaIdParser.java @@ -0,0 +1,46 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; +import static javax.json.Json.createReader; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.io.IOException; +import java.io.StringReader; +import java.net.URI; +import java.net.URL; + +import javax.json.JsonObject; +import javax.json.JsonReader; + +import org.apache.commons.io.IOUtils; + +public class SchemaIdParser { + + private final UrlConverter urlConverter; + + public SchemaIdParser(final UrlConverter urlConverter) { + this.urlConverter = urlConverter; + } + + public URI parse(final URL schemaFile) { + + try { + final String schema = IOUtils.toString(schemaFile, UTF_8); + try(final JsonReader reader = createReader(new StringReader(schema))) { + final JsonObject jsonObject = reader.readObject(); + + if (jsonObject.containsKey("id")) { + final String id = jsonObject.getString("id"); + return urlConverter.toUri(id); + } + } + + throw new CatalogGenerationException(format("Failed to generate catalog. Schema '%s' has no id", schemaFile)); + + } catch (final IOException e) { + throw new CatalogGenerationException(format("Failed to extract id from schema file '%s'", schemaFile), e); + } + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/io/parser/ListOfUriParser.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/io/parser/ListOfUriParser.java new file mode 100644 index 0000000..10af93f --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/io/parser/ListOfUriParser.java @@ -0,0 +1,42 @@ +package uk.gov.justice.schema.catalog.generation.io.parser; + +import static java.util.Collections.singletonList; +import static java.util.stream.Collectors.toList; + +import uk.gov.justice.maven.generator.io.files.parser.FileParser; + +import java.net.URI; +import java.nio.file.Path; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; + +/** + * Used by the Maven Generator Plugin to process the file paths set in the Maven configuration. This + * parser transforms all the paths in to absolute URIs and returns as a Collection of List of URI. + * The Maven Generator will be supplied with a single call with a List of URIs. + */ +public class ListOfUriParser implements FileParser> { + + /** + * Returns all the files as URIs, under the given base path specified by the Collection + * + * Used by the Maven Generator Plugin + * + * @param basePath The base path of the source directories + * @param paths Appended to the base path to create the source directories + * @return A list of source URIs + */ + @Override + public Collection> parse(final Path basePath, final Collection paths) { + final Function toUri = uriFunctionWith(basePath); + + return singletonList(paths.stream() + .map(toUri) + .collect(toList())); + } + + private Function uriFunctionWith(final Path basePath) { + return path -> basePath.resolve(path).toAbsolutePath().toUri(); + } +} diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/CatalogGeneratorProperties.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/CatalogGeneratorProperties.java new file mode 100644 index 0000000..caba1ec --- /dev/null +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/CatalogGeneratorProperties.java @@ -0,0 +1,15 @@ +package uk.gov.justice.schema.catalog.generation.maven; + +import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorProperties; + +import org.apache.maven.plugins.annotations.Parameter; + +public class CatalogGeneratorProperties implements GeneratorProperties { + + @Parameter + private String catalogName; + + public String getCatalogName() { + return catalogName; + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationContextTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationContextTest.java new file mode 100644 index 0000000..0879fba --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationContextTest.java @@ -0,0 +1,36 @@ +package uk.gov.justice.schema.catalog.generation; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CatalogGenerationContextTest { + + @InjectMocks + private CatalogGenerationContext catalogGenerationContext; + + @Test + public void shouldGetCatalogGenerationRoot() throws Exception { + assertThat(catalogGenerationContext.getCatalogGenerationRoot(), is("target/generated-catalogs")); + } + + @Test + public void shouldGetCatalogPath() throws Exception { + assertThat(catalogGenerationContext.getCatalogPath(), is("json/schema/catalog")); + } + + @Test + public void shouldGetCatalogFilename() throws Exception { + assertThat(catalogGenerationContext.getCatalogFilename(), is("schema_catalog.json")); + } + + @Test + public void shouldGetJsonSchemaPath() throws Exception { + assertThat(catalogGenerationContext.getJsonSchemaPath(), is("raml/json/schema/")); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationRunnerTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationRunnerTest.java new file mode 100644 index 0000000..cb1f9b6 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogGenerationRunnerTest.java @@ -0,0 +1,53 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.util.Collections.singletonList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import uk.gov.justice.schema.catalog.domain.Catalog; +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URI; +import java.net.URL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CatalogGenerationRunnerTest { + + @Mock + private CatalogObjectGenerator catalogObjectGenerator; + + @Mock + private CatalogWriter catalogWriter; + + @Mock + private UrlConverter urlConverter; + + @InjectMocks + private CatalogGenerationRunner catalogGenerationRunner; + + @Test + public void shouldGenerateACatalogFile() throws Exception { + + final String catalogName = "catalog name"; + final URI schemaFile = new URI("file:/schemaFile.json"); + final URL schemaFileUrl = schemaFile.toURL(); + + final Catalog catalog = mock(Catalog.class); + + when(urlConverter.toUrl(schemaFile)).thenReturn(schemaFileUrl); + when(catalogObjectGenerator.generate( + catalogName, + singletonList(schemaFileUrl))).thenReturn(catalog); + + catalogGenerationRunner.generateCatalog(catalogName, singletonList(schemaFile)); + + verify(catalogWriter).write(catalog); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogObjectGeneratorTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogObjectGeneratorTest.java new file mode 100644 index 0000000..3643f66 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogObjectGeneratorTest.java @@ -0,0 +1,62 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.util.Arrays.asList; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.catalog.domain.Catalog; +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URL; +import java.util.List; + +import org.junit.Test; + +public class CatalogObjectGeneratorTest { + + private final UrlConverter urlConverter = new UrlConverter(); + private final SchemaIdParser schemaIdParser = new SchemaIdParser(urlConverter); + private final SchemaDefParser schemaDefParser = new SchemaDefParser(schemaIdParser, new CatalogGenerationContext()); + + private final CatalogObjectGenerator catalogObjectGenerator = new CatalogObjectGenerator(schemaDefParser); + + @Test + public void shouldParseAListOfSchemaFilesIntoACatalogObject() throws Exception { + + final String catalogName = "my catalog"; + + final ClassLoader classLoader = getClass().getClassLoader(); + final URL personSchemaFile = classLoader.getResource("raml/json/schema/context/person.json"); + final URL addressSchemaFile = classLoader.getResource("raml/json/schema/standards/address.json"); + final URL complexAddressSchemaFile = classLoader.getResource("raml/json/schema/standards/complex_address.json"); + final URL defendantSchemaFile = classLoader.getResource("raml/json/schema/standards/defendant.json"); + + final List schemaFiles = asList( + personSchemaFile, + addressSchemaFile, + complexAddressSchemaFile, + defendantSchemaFile); + + + final Catalog catalog = catalogObjectGenerator.generate(catalogName, schemaFiles); + + assertThat(catalog.getName(), is(catalogName)); + assertThat(catalog.getGroup().size(), is(2)); + + assertThat(catalog.getGroup().get(0).getName(), is("standards")); + assertThat(catalog.getGroup().get(0).getBaseLocation(), is("standards/")); + assertThat(catalog.getGroup().get(0).getSchema().size(), is(3)); + assertThat(catalog.getGroup().get(0).getSchema().get(0).getId(), is("http://justice.gov.uk/standards/address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(0).getLocation(), is("address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(1).getId(), is("http://justice.gov.uk/standards/complex_address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(1).getLocation(), is("complex_address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(2).getId(), is("http://justice.gov.uk/standards/defendant.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(2).getLocation(), is("defendant.json")); + + assertThat(catalog.getGroup().get(1).getName(), is("context")); + assertThat(catalog.getGroup().get(1).getBaseLocation(), is("context/")); + assertThat(catalog.getGroup().get(1).getSchema().size(), is(1)); + assertThat(catalog.getGroup().get(1).getSchema().get(0).getId(), is("http://justice.gov.uk/context/person.json")); + assertThat(catalog.getGroup().get(1).getSchema().get(0).getLocation(), is("person.json")); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogWriterTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogWriterTest.java new file mode 100644 index 0000000..1a7c2e3 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/CatalogWriterTest.java @@ -0,0 +1,78 @@ +package uk.gov.justice.schema.catalog.generation; + +import static com.jayway.jsonassert.JsonAssert.with; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.apache.commons.io.FileUtils.deleteQuietly; +import static org.apache.commons.io.FileUtils.readFileToString; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.catalog.domain.Catalog; +import uk.gov.justice.schema.catalog.domain.Group; +import uk.gov.justice.schema.catalog.domain.Schema; +import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; + +import java.io.File; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Before; +import org.junit.Test; + +public class CatalogWriterTest { + + private static final File GENERATED_CATALOG_FILE = new File(new File("target/generated-catalogs", "json/schema/catalog"), "schema_catalog.json"); + + private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); + + private CatalogWriter catalogWriter = new CatalogWriter(objectMapper, new CatalogGenerationContext()); + + @Before + public void cleanUpAnyPreviouslyGeneratedCatalog() { + deleteQuietly(GENERATED_CATALOG_FILE); + } + + @Test + public void shouldGenerateACatalogInTheCorrectLocationFromTheCatalogDomainObjects() throws Exception { + + assertThat(GENERATED_CATALOG_FILE.exists(), is(false)); + + final Catalog catalog = new Catalog( + "my catalog", + asList(new Group("/json/schema/standards", "standards", asList( + new Schema( + "http://justice.gov.uk/standards/complex_address.json", + "complex_address.json"), + new Schema( + "http://justice.gov.uk/standards/address.json", + "address.json") + )), + new Group(null, "staging interface", singletonList( + new Schema( + "http://justice.gov.uk/context/person.json", + "/raml/json/schema/context/person.json") + )) + ) + ); + + catalogWriter.write(catalog); + + assertThat(GENERATED_CATALOG_FILE.exists(), is(true)); + + final String catalogJson = readFileToString(GENERATED_CATALOG_FILE); + + with(catalogJson) + .assertThat("$.name", is(catalog.getName())) + .assertThat("$.group[0].name", is(catalog.getGroup().get(0).getName())) + .assertThat("$.group[0].baseLocation", is(catalog.getGroup().get(0).getBaseLocation())) + .assertThat("$.group[0].schema[0].id", is(catalog.getGroup().get(0).getSchema().get(0).getId())) + .assertThat("$.group[0].schema[0].location", is(catalog.getGroup().get(0).getSchema().get(0).getLocation())) + .assertThat("$.group[0].schema[1].id", is(catalog.getGroup().get(0).getSchema().get(1).getId())) + .assertThat("$.group[0].schema[1].location", is(catalog.getGroup().get(0).getSchema().get(1).getLocation())) + .assertThat("$.group[1].name", is(catalog.getGroup().get(1).getName())) + .assertNotDefined("$.group[1].baseLocation") + .assertThat("$.group[1].schema[0].id", is(catalog.getGroup().get(1).getSchema().get(0).getId())) + .assertThat("$.group[1].schema[0].location", is(catalog.getGroup().get(1).getSchema().get(0).getLocation())) + ; + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/MavenCatalogGeneratorTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/MavenCatalogGeneratorTest.java new file mode 100644 index 0000000..6a81b1d --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/MavenCatalogGeneratorTest.java @@ -0,0 +1,55 @@ +package uk.gov.justice.schema.catalog.generation; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig; +import uk.gov.justice.schema.catalog.generation.maven.CatalogGeneratorProperties; + +import java.net.URI; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class MavenCatalogGeneratorTest { + + @Mock + private ObjectFactory objectFactory; + + @InjectMocks + private MavenCatalogGenerator mavenCatalogGenerator; + + @Test + public void shouldInstantiateAndRunTheCatalogGeneration() throws Exception { + + final String catalogName = "my catalog"; + final List schemaFiles = singletonList(new URI("/a-schema-file.json")); + + final CatalogGenerationRunner catalogGenerationRunner = mock(CatalogGenerationRunner.class); + final GeneratorConfig generatorConfig = mock(GeneratorConfig.class); + final CatalogGeneratorProperties catalogGeneratorProperties = mock(CatalogGeneratorProperties.class); + + when(objectFactory.catalogGenerationRunner()).thenReturn(catalogGenerationRunner); + when(generatorConfig.getGeneratorProperties()).thenReturn(catalogGeneratorProperties); + when(catalogGeneratorProperties.getCatalogName()).thenReturn(catalogName); + + mavenCatalogGenerator.run(schemaFiles, generatorConfig); + + verify(catalogGenerationRunner).generateCatalog(catalogName, schemaFiles); + } + + @Test + public void shouldAddThisTestToStopCoverallsFromWhingeing() throws Exception { + assertThat(new MavenCatalogGenerator(), is(instanceOf(MavenCatalogGenerator.class))); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/ObjectFactoryTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/ObjectFactoryTest.java new file mode 100644 index 0000000..893bc99 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/ObjectFactoryTest.java @@ -0,0 +1,102 @@ +package uk.gov.justice.schema.catalog.generation; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.lang.reflect.Field; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class ObjectFactoryTest { + + @InjectMocks + private ObjectFactory objectFactory; + + @Test + public void shouldCreateACatalogGenerationContext() throws Exception { + assertThat(objectFactory.catalogGenerationConstants(), is(instanceOf(CatalogGenerationContext.class))); + } + + @Test + public void shouldCreateAUrlConverter() throws Exception { + assertThat(objectFactory.urlConverter(), is(instanceOf(UrlConverter.class))); + } + + @Test + public void shouldCreateAnObjectMapper() throws Exception { + assertThat(objectFactory.objectMapper(), is(instanceOf(ObjectMapper.class))); + } + + @Test + public void shouldCreateACatalogWriter() throws Exception { + + final CatalogWriter catalogWriter = objectFactory.catalogWriter(); + assertThat(catalogWriter, is(notNullValue())); + + final Object objectMapper = getPrivateField("objectMapper", catalogWriter); + assertThat(objectMapper, is(notNullValue())); + } + + @Test + public void shouldCreateASchemaIdParser() throws Exception { + + final SchemaIdParser schemaIdParser = objectFactory.schemaIdParser(); + assertThat(schemaIdParser, is(notNullValue())); + + final Object urlConverter = getPrivateField("urlConverter", schemaIdParser); + assertThat(urlConverter, is(notNullValue())); + } + + @Test + public void shouldCreateACatalogObjectGenerator() throws Exception { + + final CatalogObjectGenerator catalogObjectGenerator = objectFactory.catalogObjectGenerator(); + assertThat(catalogObjectGenerator, is(notNullValue())); + + final Object schemaDefParser = getPrivateField("schemaDefParser", catalogObjectGenerator); + assertThat(schemaDefParser, is(notNullValue())); + } + + @Test + public void shouldCreateASchemaDefParser() throws Exception { + + final SchemaDefParser schemaDefParser = objectFactory.schemaDefParser(); + assertThat(schemaDefParser, is(notNullValue())); + + final Object schemaIdParser = getPrivateField("schemaIdParser", schemaDefParser); + assertThat(schemaIdParser, is(notNullValue())); + } + + @Test + public void shouldCreateACatalogGenerationRunner() throws Exception { + + final CatalogGenerationRunner catalogGenerationRunner = objectFactory.catalogGenerationRunner(); + assertThat(catalogGenerationRunner, is(notNullValue())); + + final Object catalogObjectGenerator = getPrivateField("catalogObjectGenerator", catalogGenerationRunner); + assertThat(catalogObjectGenerator, is(notNullValue())); + + final Object catalogWriter = getPrivateField("catalogWriter", catalogGenerationRunner); + assertThat(catalogWriter, is(notNullValue())); + + final Object urlConverter = getPrivateField("urlConverter", catalogGenerationRunner); + assertThat(urlConverter, is(notNullValue())); + } + + private Object getPrivateField(final String fieldName, final Object fromThisObject) throws Exception { + + final Field field = fromThisObject.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + + return field.get(fromThisObject); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaDefParserTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaDefParserTest.java new file mode 100644 index 0000000..9df4d6c --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaDefParserTest.java @@ -0,0 +1,80 @@ +package uk.gov.justice.schema.catalog.generation; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; +import static uk.gov.justice.schema.catalog.generation.CatalogGenerationContext.AN_EMPTY_STRING; + +import java.net.URL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SchemaDefParserTest { + + @Spy + @SuppressWarnings("unused") + private final CatalogGenerationContext catalogGenerationContext = new CatalogGenerationContext(); + + @Mock + private SchemaIdParser schemaIdParser; + + @InjectMocks + private SchemaDefParser schemaDefParser; + + @Test + public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocation() throws Exception { + + final URL schemaFile = new URL("file:/path/to/raml/json/schema/a-sub-directory/some-schema-or-other.json"); + + final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json"; + when(schemaIdParser.parse(schemaFile)).thenReturn(new URL(schemaId).toURI()); + + final SchemaDef schemaDef = schemaDefParser.parse(schemaFile); + + assertThat(schemaDef.getSchemaFile(), is(schemaFile)); + assertThat(schemaDef.getId().toString(), is(schemaId)); + assertThat(schemaDef.getGroupName(), is("a-sub-directory")); + assertThat(schemaDef.getBaseLocation(), is("a-sub-directory/")); + assertThat(schemaDef.getLocation(), is("some-schema-or-other.json")); + } + + @Test + public void shouldHandleSchemasAtTheRootOfTheJsonSchemaDirectory() throws Exception { + + final URL schemaFile = new URL("file:/path/to/raml/json/schema/some-schema-or-other.json"); + + final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json"; + when(schemaIdParser.parse(schemaFile)).thenReturn(new URL(schemaId).toURI()); + + final SchemaDef schemaDef = schemaDefParser.parse(schemaFile); + + assertThat(schemaDef.getSchemaFile(), is(schemaFile)); + assertThat(schemaDef.getId().toString(), is(schemaId)); + assertThat(schemaDef.getGroupName(), is(AN_EMPTY_STRING)); + assertThat(schemaDef.getBaseLocation(), is(AN_EMPTY_STRING)); + assertThat(schemaDef.getLocation(), is("some-schema-or-other.json")); + } + + @Test + public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocationWithSubDirectories() throws Exception { + + final URL schemaFile = new URL("file:/path/to/raml/json/schema/a-sub-directory/another-sub-directory/some-schema-or-other.json"); + + final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json"; + when(schemaIdParser.parse(schemaFile)).thenReturn(new URL(schemaId).toURI()); + + final SchemaDef schemaDef = schemaDefParser.parse(schemaFile); + + assertThat(schemaDef.getSchemaFile(), is(schemaFile)); + assertThat(schemaDef.getId().toString(), is(schemaId)); + assertThat(schemaDef.getGroupName(), is("a-sub-directory")); + assertThat(schemaDef.getBaseLocation(), is("a-sub-directory/")); + assertThat(schemaDef.getLocation(), is("another-sub-directory/some-schema-or-other.json")); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaFinderTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaFinderTest.java new file mode 100644 index 0000000..0934f25 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaFinderTest.java @@ -0,0 +1,36 @@ +package uk.gov.justice.schema.catalog.generation; + +import static org.hamcrest.CoreMatchers.endsWith; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URI; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SchemaFinderTest { + + @Spy @SuppressWarnings("unused") + private final UrlConverter urlConverter = new UrlConverter(); + + @InjectMocks + private SchemaFinder schemaFinder; + + @Test + public void shouldFindAllJsonFilesOnTheClasspathAtTheCorrectLocation() throws Exception { + + final List schemas = schemaFinder.listSchemas(); + + assertThat(schemas.get(0).toString(), endsWith("raml/json/schema/context/person.json")); + assertThat(schemas.get(1).toString(), endsWith("raml/json/schema/standards/address.json")); + assertThat(schemas.get(2).toString(), endsWith("raml/json/schema/standards/complex_address.json")); + assertThat(schemas.get(3).toString(), endsWith("raml/json/schema/standards/defendant.json")); + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaIdParserTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaIdParserTest.java new file mode 100644 index 0000000..43df966 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/SchemaIdParserTest.java @@ -0,0 +1,69 @@ +package uk.gov.justice.schema.catalog.generation; + +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.io.IOException; +import java.net.URI; +import java.net.URL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SchemaIdParserTest { + + @Spy + @SuppressWarnings("unused") + private final UrlConverter urlConverter = new UrlConverter(); + + @InjectMocks + private SchemaIdParser schemaIdParser; + + @Test + public void shouldParseTheSchemaFileAndExtractItsId() throws Exception { + + final URL schemaFile = getClass().getClassLoader().getResource("raml/json/schema/context/person.json"); + + final URI id = schemaIdParser.parse(schemaFile); + + assertThat(id.toString(), is("http://justice.gov.uk/context/person.json")); + } + + @Test + public void shouldFailIfTheSchemaHasNoId() throws Exception { + + final URL schemaFile = getClass().getClassLoader().getResource("dodgy-schemas/schema-with-missing-id.json"); + + try { + schemaIdParser.parse(schemaFile); + fail(); + } catch (final CatalogGenerationException expected) { + assertThat(expected.getMessage(), startsWith("Failed to generate catalog. Schema 'file:")); + assertThat(expected.getMessage(), endsWith("/dodgy-schemas/schema-with-missing-id.json' has no id")); + } + } + + @Test + public void shouldFailIfLoadingTheSchemaFileThrowsAnIOException() throws Exception { + + final URL schemaFile = new URL("file:/this/file/does/not/exist.json"); + + try { + schemaIdParser.parse(schemaFile); + fail(); + } catch (final CatalogGenerationException expected) { + assertThat(expected.getCause(), is(instanceOf(IOException.class))); + assertThat(expected.getMessage(), is("Failed to extract id from schema file 'file:/this/file/does/not/exist.json'")); + } + } +} diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/io/parser/ListOfUriParserTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/io/parser/ListOfUriParserTest.java new file mode 100644 index 0000000..641b624 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/io/parser/ListOfUriParserTest.java @@ -0,0 +1,47 @@ +package uk.gov.justice.schema.catalog.generation.io.parser; + +import static java.nio.file.Paths.get; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.net.URI; +import java.nio.file.Path; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; + +public class ListOfUriParserTest { + + private final Path basePath = get("src/test/resources/parser/"); + + @Test + public void shouldReturnCollectionOfFilesForGivenPath() throws Exception { + final List paths = asList( + get("test1.txt"), + get("test2.txt")); + + final Collection> collection = new ListOfUriParser().parse(basePath, paths); + + assertThat(collection.size(), is(1)); + + final List uris = collection.iterator().next(); + assertThat(uris.size(), is(2)); + assertThat(uris, hasItems( + basePath.resolve("test1.txt").toAbsolutePath().toUri(), + basePath.resolve("test2.txt").toAbsolutePath().toUri())); + } + + @Test + public void shouldReturnEmoptyCollectionForNoPaths() throws Exception { + final Collection> collection = new ListOfUriParser().parse(basePath, emptyList()); + + assertThat(collection.size(), is(1)); + + final List uris = collection.iterator().next(); + assertThat(uris.size(), is(0)); + } +} \ No newline at end of file diff --git a/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/maven/CatalogGeneratorPropertiesTest.java b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/maven/CatalogGeneratorPropertiesTest.java new file mode 100644 index 0000000..60efb61 --- /dev/null +++ b/catalog-generation/src/test/java/uk/gov/justice/schema/catalog/generation/maven/CatalogGeneratorPropertiesTest.java @@ -0,0 +1,23 @@ +package uk.gov.justice.schema.catalog.generation.maven; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CatalogGeneratorPropertiesTest { + + @InjectMocks + private CatalogGeneratorProperties catalogGeneratorProperties; + + @Test + public void shouldAddAPointlessTestToStopCoverallsFromWhingeing() throws Exception { + + assertThat(catalogGeneratorProperties.getCatalogName(), is(nullValue())); + } +} diff --git a/catalog-generation/src/test/resources/dodgy-schemas/schema-with-missing-id.json b/catalog-generation/src/test/resources/dodgy-schemas/schema-with-missing-id.json new file mode 100644 index 0000000..8f97159 --- /dev/null +++ b/catalog-generation/src/test/resources/dodgy-schemas/schema-with-missing-id.json @@ -0,0 +1,20 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "nino": { + "type": "string" + }, + "name": { + "type": "string" + }, + "correspondence_address": + {"$ref" : "http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2"} + + }, + "required": [ + "nino", + "name", + "correspondence_address" + ] +} diff --git a/catalog-generation/src/test/resources/parser/test1.txt b/catalog-generation/src/test/resources/parser/test1.txt new file mode 100644 index 0000000..e69de29 diff --git a/catalog-generation/src/test/resources/parser/test2.txt b/catalog-generation/src/test/resources/parser/test2.txt new file mode 100644 index 0000000..e69de29 diff --git a/catalog-generation/src/test/resources/raml/json/schema/context/person.json b/catalog-generation/src/test/resources/raml/json/schema/context/person.json new file mode 100644 index 0000000..c27c64b --- /dev/null +++ b/catalog-generation/src/test/resources/raml/json/schema/context/person.json @@ -0,0 +1,21 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/context/person.json", + "type": "object", + "properties": { + "nino": { + "type": "string" + }, + "name": { + "type": "string" + }, + "correspondence_address": + {"$ref" : "http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2"} + + }, + "required": [ + "nino", + "name", + "correspondence_address" + ] +} diff --git a/catalog-generation/src/test/resources/raml/json/schema/standards/address.json b/catalog-generation/src/test/resources/raml/json/schema/standards/address.json new file mode 100644 index 0000000..eea5825 --- /dev/null +++ b/catalog-generation/src/test/resources/raml/json/schema/standards/address.json @@ -0,0 +1,25 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/address.json", + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode" + ] + +} \ No newline at end of file diff --git a/catalog-generation/src/test/resources/raml/json/schema/standards/complex_address.json b/catalog-generation/src/test/resources/raml/json/schema/standards/complex_address.json new file mode 100644 index 0000000..290bf06 --- /dev/null +++ b/catalog-generation/src/test/resources/raml/json/schema/standards/complex_address.json @@ -0,0 +1,65 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/complex_address.json", + "type": "object", + "definitions": { + "complex_address": { + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + }, + "country": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode", + "country" + ] + }, + "complex_address2": { + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + }, + "ss": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode", + "ss" + ] + } + }, + "allOf": [ + { + "$ref": "#/definitions/complex_address" + } + ] +} + diff --git a/catalog-generation/src/test/resources/raml/json/schema/standards/defendant.json b/catalog-generation/src/test/resources/raml/json/schema/standards/defendant.json new file mode 100644 index 0000000..9a29411 --- /dev/null +++ b/catalog-generation/src/test/resources/raml/json/schema/standards/defendant.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/defendant.json", + "type": "object", + "properties": { + "personalInfo": { + "type": "string" + }, + "currentAddress": { + "type": "string" + }, + "correspondenceAddress": { + "type": "string" + }, + "courtCentreId": { + "type": "string" + } + }, + "required": [ + "personalInfo", + "currentAddress" + ] +} diff --git a/pom.xml b/pom.xml index e89b22e..e17441b 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,8 @@ everit-schema-client integration-test catalog-core + catalog-generation + catalog-generation-plugin-it schema-service @@ -27,6 +29,7 @@ json-schema-catalog 1.21.0 1.10.0 + 2.2.0