From 5acb0d3ec07381e69a885371d8e0377de391697a Mon Sep 17 00:00:00 2001 From: amckenzie Date: Wed, 6 Dec 2017 11:02:16 +0000 Subject: [PATCH] add javadoc --- .../gov/justice/schema/catalog/Catalog.java | 41 ++++++++++++++++-- .../schema/catalog/CatalogObjectFactory.java | 43 +++++++++++++++++++ .../schema/catalog/CatalogProducer.java | 6 +++ .../catalog/CatalogToSchemaResolver.java | 7 +++ .../catalog/ClasspathCatalogLoader.java | 10 +++++ .../catalog/FileContentsAsStringLoader.java | 10 +++++ .../schema/catalog/JsonSchemaFileLoader.java | 16 ++++++- .../catalog/JsonStringToSchemaConverter.java | 16 +++++++ .../justice/schema/catalog/RawCatalog.java | 14 ++++++ .../schema/catalog/SchemaResolver.java | 41 +++++++++++++++--- .../client/LocalFileSystemSchemaClient.java | 16 +++++++ .../catalog/client/SchemaClientFactory.java | 10 +++++ .../catalog/util/ClasspathResourceLoader.java | 14 +++++- .../schema/catalog/util/UriResolver.java | 13 ++++++ .../schema/catalog/util/UrlConverter.java | 19 ++++++++ .../justice/schema/catalog/CatalogTest.java | 8 ++-- .../JsonStringToSchemaConverterTest.java | 11 ++--- .../schema/catalog/SchemaResolverTest.java | 22 +++++----- .../schema/catalog/domain/Catalog.java | 9 ++++ .../justice/schema/catalog/domain/Group.java | 12 ++++++ .../justice/schema/catalog/domain/Schema.java | 9 ++++ .../generation/CatalogGenerationContext.java | 6 +++ .../generation/CatalogGenerationRunner.java | 10 +++++ .../generation/CatalogObjectGenerator.java | 10 +++++ .../catalog/generation/CatalogWriter.java | 9 ++++ .../generation/GenerationObjectFactory.java | 22 ++++++++++ .../schema/catalog/generation/SchemaDef.java | 21 +++++++++ .../catalog/generation/SchemaDefParser.java | 36 ++++++++++++++++ .../catalog/generation/SchemaFinder.java | 8 ++++ .../catalog/generation/SchemaIdParser.java | 11 +++++ .../maven/CatalogGeneratorProperties.java | 10 +++++ .../maven/MavenCatalogGenerator.java | 11 +++++ .../maven/MavenCatalogGeneratorFactory.java | 6 +++ .../generation/SchemaDefParserTest.java | 8 ++-- .../schema/service/SchemaCatalogService.java | 10 +++++ 35 files changed, 488 insertions(+), 37 deletions(-) diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/Catalog.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/Catalog.java index 20ab796..b34ef99 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/Catalog.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/Catalog.java @@ -8,27 +8,60 @@ import java.util.Optional; import org.everit.json.schema.Schema; -import org.everit.json.schema.loader.SchemaClient; +/** + * Main entry point into the application. Gets a fully resolved Schema by the Schema's id. + * + *

+ * Usage: + *

+ * + *
+ *     {@code
+ *
+ *       final String json = "{\"some\": \"json\"}";
+ *
+ *       final String schemaId = "http://justice.gov.uk/domain/schemas/some-json-schema.json";
+ *
+ *       final Catalog catalog = new CatalogObjectFactory().catalog();
+ *       final Optional schema = catalog.getSchema(schemaId);
+ *
+ *       if(schema.isPresent()) {
+ *           schema.get().validate(json);
+ *       }
+ *     }
+ *     
+ */ public class Catalog { private final RawCatalog rawCatalog; private final SchemaClientFactory schemaClientFactory; private final JsonStringToSchemaConverter jsonStringToSchemaConverter; - public Catalog(final RawCatalog rawCatalog, final SchemaClientFactory schemaClientFactory, final JsonStringToSchemaConverter jsonStringToSchemaConverter) { + public Catalog( + final RawCatalog rawCatalog, + final SchemaClientFactory schemaClientFactory, + final JsonStringToSchemaConverter jsonStringToSchemaConverter) { this.rawCatalog = rawCatalog; this.schemaClientFactory = schemaClientFactory; this.jsonStringToSchemaConverter = jsonStringToSchemaConverter; } + /** + * Gets a a fully resolved json {@link Schema} by its schema id + * + * @param schemaId The id of the required schema + * @return a fully resolved {@link Schema} + */ public Optional getSchema(final String schemaId) { final Optional rawJsonSchema = rawCatalog.getRawJsonSchema(schemaId); if (rawJsonSchema.isPresent()) { - final SchemaClient schemaClient = schemaClientFactory.create(rawCatalog); - final Schema schema = jsonStringToSchemaConverter.convert(rawJsonSchema.get(), schemaClient); + final Schema schema = jsonStringToSchemaConverter.convert( + rawJsonSchema.get(), + schemaClientFactory.create(rawCatalog)); + return of(schema); } diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogObjectFactory.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogObjectFactory.java index 074f1c5..17f6506 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogObjectFactory.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogObjectFactory.java @@ -10,28 +10,50 @@ import com.fasterxml.jackson.databind.ObjectMapper; +/** + * A simple cheap way of avoiding having to use a dependency injection framework in the project. + * Gets a instance of an object with the object's dependencies instantiated and injected. + */ public class CatalogObjectFactory { + /** + * @return a new instance of {@link UrlConverter} + */ public UrlConverter urlConverter() { return new UrlConverter(); } + /** + * @return a new instance of {@link ObjectMapper} + */ public ObjectMapper objectMapper() { return new ObjectMapperProducer().objectMapper(); } + /** + * @return a new instance of {@link UriResolver} + */ public UriResolver uriResolver() { return new UriResolver(); } + /** + * @return a new instance of {@link JsonStringToSchemaConverter} + */ public JsonStringToSchemaConverter jsonStringToSchemaConverter() { return new JsonStringToSchemaConverter(); } + /** + * @return a new instance of {@link ClasspathResourceLoader} + */ public ClasspathResourceLoader classpathResourceLoader() { return new ClasspathResourceLoader(); } + /** + * @return a new instance of {@link ClasspathCatalogLoader} + */ public ClasspathCatalogLoader classpathCatalogLoader() { return new ClasspathCatalogLoader( objectMapper(), @@ -39,10 +61,16 @@ public ClasspathCatalogLoader classpathCatalogLoader() { urlConverter()); } + /** + * @return a new instance of {@link SchemaResolver} + */ public SchemaResolver schemaResolver() { return new SchemaResolver(urlConverter(), uriResolver()); } + /** + * @return a new instance of {@link CatalogToSchemaResolver} + */ public CatalogToSchemaResolver catalogToSchemaResolver() { return new CatalogToSchemaResolver( classpathCatalogLoader(), @@ -50,18 +78,30 @@ public CatalogToSchemaResolver catalogToSchemaResolver() { getLogger(CatalogToSchemaResolver.class)); } + /** + * @return a new instance of {@link FileContentsAsStringLoader} + */ public FileContentsAsStringLoader fileContentsAsStringLoader() { return new FileContentsAsStringLoader(); } + /** + * @return a new instance of {@link JsonSchemaFileLoader} + */ public JsonSchemaFileLoader jsonSchemaFileLoader() { return new JsonSchemaFileLoader(fileContentsAsStringLoader(), catalogToSchemaResolver()); } + /** + * @return a new instance of {@link SchemaClientFactory} + */ public SchemaClientFactory schemaClientFactory() { return new SchemaClientFactory(); } + /** + * @return a new instance of {@link Catalog} + */ public Catalog catalog() { return new Catalog( rawCatalog(), @@ -69,6 +109,9 @@ public Catalog catalog() { jsonStringToSchemaConverter()); } + /** + * @return a new instance of {@link RawCatalog} + */ public RawCatalog rawCatalog() { final RawCatalog rawCatalog = new RawCatalog(jsonSchemaFileLoader()); rawCatalog.initialize(); diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogProducer.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogProducer.java index 147f91c..6181b15 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogProducer.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogProducer.java @@ -3,9 +3,15 @@ import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; +/** + * A Producer for the {@link Catalog} should you be running in a CDI container + */ @ApplicationScoped public class CatalogProducer { + /** + * @return a new instance of {@link Catalog} + */ @Produces public Catalog catalog() { return new CatalogObjectFactory().catalog(); diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java index ccab859..2cbc4c2 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java @@ -15,6 +15,10 @@ import org.slf4j.Logger; +/** + * Finds all catalog files on the classpath, then returns a mapping of the schema id to its + * location. + */ public class CatalogToSchemaResolver { private final ClasspathCatalogLoader classpathCatalogLoader; @@ -30,6 +34,9 @@ public CatalogToSchemaResolver(final ClasspathCatalogLoader classpathCatalogLoad } /** + * Find all catalog files on the classpath and return a mapping of the schema id to + * the location of the schema file + * * @return Mapping from schemaId to a schema location url */ public Map resolveSchemaLocations() { diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java index a205f26..4f4a20c 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java @@ -15,6 +15,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; +/** + * Loads all {@link Catalog}s from any part of the classpath, which have the path + * '/json/schema/schema_catalog.json' + */ public class ClasspathCatalogLoader { private static final String DEFAULT_JSON_CATALOG_LOCATION = "json/schema/schema_catalog.json"; @@ -31,6 +35,12 @@ public ClasspathCatalogLoader(final ObjectMapper objectMapper, this.urlConverter = urlConverter; } + /** + * Loads all {@link Catalog}s from any part of the classpath found on the path + * 'json/schema/schema_catalog.json' + * + * @return All found {@link Catalog}s mapped by their URIs + */ public Map getCatalogs() { return listAllCatalogsFromClasspath().stream() .collect(toMap(urlConverter::toUri, this::loadCatalog)); diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java index 1c850aa..a149705 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java @@ -8,8 +8,18 @@ import org.apache.commons.io.IOUtils; +/** + * Gets the contents of a file as a {@link String} + */ public class FileContentsAsStringLoader { + /** + * Gets the contents of a file as a {@link String} + * + * @param urlOfFile The {@link java.net.URI} of the file + * + * @return The File's contents as a UTF 8 String. + */ public String readFileContents(final URL urlOfFile) { try { diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaFileLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaFileLoader.java index 1701446..cc8f159 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaFileLoader.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaFileLoader.java @@ -4,16 +4,30 @@ import java.util.Map; +/** + * Loads all Json Schema files found on the classpath as a raw {@link String} mapped by the id + * of that Schema + */ public class JsonSchemaFileLoader { private final FileContentsAsStringLoader fileContentsAsStringLoader; private final CatalogToSchemaResolver catalogToSchemaResolver; - public JsonSchemaFileLoader(final FileContentsAsStringLoader fileContentsAsStringLoader, final CatalogToSchemaResolver catalogToSchemaResolver) { + public JsonSchemaFileLoader( + final FileContentsAsStringLoader fileContentsAsStringLoader, + final CatalogToSchemaResolver catalogToSchemaResolver) { + this.fileContentsAsStringLoader = fileContentsAsStringLoader; this.catalogToSchemaResolver = catalogToSchemaResolver; } + /** + * Loads all Json Schema files found on the classpath as a raw {@link String} mapped by the id + * of that Schema. + * + * @return A {@link Map} of Schemas as a raw {@link String} mapped to the + * id of each Schema + */ public Map loadSchemas() { return catalogToSchemaResolver.resolveSchemaLocations().entrySet() diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java index 8031090..3addaa1 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java @@ -9,9 +9,25 @@ import org.json.JSONObject; import org.json.JSONTokener; +/** + * Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas + * in the raw Json Schema to their location on the classpath + */ public class JsonStringToSchemaConverter { + /** + * Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas + * in the raw Json Schema to their location on the classpath + * + * @param schemaJson The raw Json Schema as a {@link String} + * @param schemaClient An implementation of Everit's {@link SchemaClient} that + * rather than returning a schema found at the Schema id, + * returns a Schema found on the classpath mapped to that + * Schema id + * @return A fully resolved Everit {@link Schema} + */ public Schema convert(final String schemaJson, final SchemaClient schemaClient) { + try { return SchemaLoader.builder() .schemaJson(new JSONObject(new JSONTokener(schemaJson))) diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/RawCatalog.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/RawCatalog.java index b6ea2a0..aca153d 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/RawCatalog.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/RawCatalog.java @@ -6,6 +6,10 @@ import java.util.Map; import java.util.Optional; +/** + * Main cache of all Json Schemas found on the classpath mapped by their Schema id. + * All Schemas are the raw unresolved contents of the Json Schema file as a {@link String} + */ public class RawCatalog { private final JsonSchemaFileLoader jsonSchemaFileLoader; @@ -16,10 +20,20 @@ public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader) { this.jsonSchemaFileLoader = jsonSchemaFileLoader; } + /** + * Initializes the cache of raw json schemas by scanning the classpath and + * loading all json schemas it finds. + */ public void initialize() { schemaIdsToRawJsonSchemaCache = jsonSchemaFileLoader.loadSchemas(); } + /** + * Gets the raw unresolved json of a Schema file found on the classpath by its id. + * @param schemaId The id of the required json schema file + * @return An {@link Optional} containing the raw json schema of the schema file with + * the specified id, if it exists. + */ public Optional getRawJsonSchema(final String schemaId) { return ofNullable(schemaIdsToRawJsonSchemaCache.get(schemaId)); } diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java index e7e15e9..5ab42d3 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java @@ -10,6 +10,27 @@ import java.net.URL; import java.util.Optional; +/** + * Resolves the actual URI of a json schema file based on the absolute uri of + * the catalog file, an {@link Optional} base location relative to the catalog uri + * and the relative path of the schema file.
+ * + *

+ * For instance: given a catalog file
+ * 'file:/json/schemas/schema-catalog.json'
+ * + * A relative base location of
+ * 'domain/objects'
+ * + * And a schema location of
+ * definitions/address.json
+ * + * This would be resolved into the actual location of the json schema file
+ * file:/json/schemas/domain/objects/definitions/address.json + * + *

+ * + */ public class SchemaResolver { private static final String AN_EMPTY_STRING = ""; @@ -22,19 +43,27 @@ public SchemaResolver(final UrlConverter urlConverter, final UriResolver uriReso this.uriResolver = uriResolver; } + /** + * Resolves the absolute uri of a json schema relative to a catalog file + * + * @param uriOfCatalogFile An absolute URL of a catalog file + * @param relativeLocationOfSchema A relative path of a Schema file + * @param schemaBaseLocation An {@link Optional} base location of the schema file + * @return The absolute uri of a json schema file + */ public URL resolve( - final URI catalogUri, - final String fileLocation, - final Optional fileBaseLocation) { + final URI uriOfCatalogFile, + final String relativeLocationOfSchema, + final Optional schemaBaseLocation) { try { - final URI schemaUri = new URI(fileBaseLocation.orElse(AN_EMPTY_STRING)).resolve(fileLocation); - final URI resolvedUri = uriResolver.resolve(catalogUri, schemaUri); + final URI schemaUri = new URI(schemaBaseLocation.orElse(AN_EMPTY_STRING)).resolve(relativeLocationOfSchema); + final URI resolvedUri = uriResolver.resolve(uriOfCatalogFile, schemaUri); return urlConverter.toUrl(resolvedUri); } catch (final URISyntaxException e) { - throw new SchemaCatalogException(format("Failed to resolve '%s', to file location '%s', with base location '%s'", catalogUri, fileLocation, fileBaseLocation.orElse(null)), e); + throw new SchemaCatalogException(format("Failed to resolve '%s', to file location '%s', with base location '%s'", uriOfCatalogFile, relativeLocationOfSchema, schemaBaseLocation.orElse(null)), e); } } } diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/LocalFileSystemSchemaClient.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/LocalFileSystemSchemaClient.java index 9aa995a..4bfddee 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/LocalFileSystemSchemaClient.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/LocalFileSystemSchemaClient.java @@ -10,6 +10,14 @@ import org.everit.json.schema.loader.SchemaClient; +/** + * An implementation of the Everit Json Validator + * SchemaClient. + * + * Rather than returning a schema at the uri specified by its schema id. It returns a + * Schema mapped by the schema id on the local file system + * + */ public class LocalFileSystemSchemaClient implements SchemaClient { private final RawCatalog rawCatalog; @@ -18,6 +26,14 @@ public LocalFileSystemSchemaClient(final RawCatalog rawCatalog) { this.rawCatalog = rawCatalog; } + /** + * Returns an {@link InputStream} to a locally stored Schema file that has been + * mapped to its Schema id + * + * @param schemaId The id of the Schema + * + * @return An {@link InputStream} to the locally stored Schema + */ @Override public InputStream get(final String schemaId) { diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/SchemaClientFactory.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/SchemaClientFactory.java index 84bac40..4f30cf7 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/SchemaClientFactory.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/client/SchemaClientFactory.java @@ -4,8 +4,18 @@ import org.everit.json.schema.loader.SchemaClient; +/** + * Creates a new instance of {@link LocalFileSystemSchemaClient}. + */ public class SchemaClientFactory { + /** + * Creates a new instance of {@link LocalFileSystemSchemaClient} + * + * @param rawCatalog The {@link RawCatalog} containing the mapping of Schema ids + * to their location on the file system + * @return A new instance of {@link LocalFileSystemSchemaClient} + */ public SchemaClient create(final RawCatalog rawCatalog) { return new LocalFileSystemSchemaClient(rawCatalog); } diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java index d77f700..b8846d9 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java @@ -6,9 +6,19 @@ import java.net.URL; import java.util.List; +/** + * Finds all URIs to all resources on the classpath specified by the resource name + */ public class ClasspathResourceLoader { - public List getResources(final Class clazz, final String location) throws IOException { - return list(clazz.getClassLoader().getResources(location)); + + /** + * @param clazz A class from which to get its classloader + * @param resourceName The name of the resource on the classpath + * @return a {@link URL} to the resource + * @throws IOException If the get fails + */ + public List getResources(final Class clazz, final String resourceName) throws IOException { + return list(clazz.getClassLoader().getResources(resourceName)); } } diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UriResolver.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UriResolver.java index 5c89553..088c11f 100644 --- a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UriResolver.java +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UriResolver.java @@ -3,8 +3,21 @@ import java.net.URI; import java.net.URISyntaxException; +/** + * Resolves one {@link URI} against another {@link URI} handling any problems if + * the {@link URI} is opaque (as is the URI to a resource in a jar file) + */ public class UriResolver { + /** + * Resolve one {@link URI} against another {@link URI} handling any problems if + * the {@link URI} is opaque (as is the URI to a resource in a jar file) + * + * @param baseUri A URI + * @param otherUri Another URI to resolve + * @return The resolved URI + * @throws URISyntaxException If either uri's syntax is malformed + */ public URI resolve(final URI baseUri, final URI otherUri) throws URISyntaxException { if (baseUri.isOpaque()) { 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 e26e7a1..7f8dbe7 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 @@ -9,8 +9,17 @@ import java.net.URISyntaxException; import java.net.URL; +/** + * Utility class for converting {@link URL}s to {@link URI}s, that handles any exceptions + * thrown by the conversion. + */ public class UrlConverter { + /** + * Converts a {@link URI} to a {@link URL} + * @param url A {@link URL} to convert + * @return The converted {@link URI} + */ public URI toUri(final URL url) { try { return url.toURI(); @@ -19,6 +28,11 @@ public URI toUri(final URL url) { } } + /** + * Converts a {@link URI} to a {@link URL} + * @param uri A {@link URI} to convert + * @return The converted {@link URL} + */ public URL toUrl(final URI uri) { try { return uri.toURL(); @@ -27,6 +41,11 @@ public URL toUrl(final URI uri) { } } + /** + * Converts a {@link String} URI to a {@link URI} Object + * @param uri A URI in String form + * @return A {@link URI} Object of that String uri + */ public URI toUri(final String uri) { try { return new URI(uri); diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogTest.java index 11ff48d..a56db9a 100644 --- a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogTest.java +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogTest.java @@ -7,10 +7,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import uk.gov.justice.schema.catalog.client.LocalFileSystemSchemaClient; import uk.gov.justice.schema.catalog.client.SchemaClientFactory; import org.everit.json.schema.Schema; -import org.everit.json.schema.loader.SchemaClient; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -39,12 +39,12 @@ public void shouldLoadTheRawJsonSchemaAndConvertToAFullSchemaObject() throws Exc final String schemaId = "schemaId"; final String rawJsonSchema = "raw json schema"; - final SchemaClient schemaClient = mock(SchemaClient.class); + final LocalFileSystemSchemaClient localFileSystemSchemaClient = mock(LocalFileSystemSchemaClient.class); final Schema schema = mock(Schema.class); when(rawCatalog.getRawJsonSchema(schemaId)).thenReturn(of(rawJsonSchema)); - when(schemaClientFactory.create(rawCatalog)).thenReturn(schemaClient); - when(jsonStringToSchemaConverter.convert(rawJsonSchema, schemaClient)).thenReturn(schema); + when(schemaClientFactory.create(rawCatalog)).thenReturn(localFileSystemSchemaClient); + when(jsonStringToSchemaConverter.convert(rawJsonSchema, localFileSystemSchemaClient)).thenReturn(schema); assertThat(catalog.getSchema(schemaId), is(of(schema))); } diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java index 089d12e..56e511d 100644 --- a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java @@ -9,12 +9,13 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import uk.gov.justice.schema.catalog.client.LocalFileSystemSchemaClient; + import java.io.InputStream; import java.net.URL; import org.apache.commons.io.IOUtils; import org.everit.json.schema.Schema; -import org.everit.json.schema.loader.SchemaClient; import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; @@ -40,14 +41,14 @@ public void shouldLoadASchemaWhichIncludesALocallyStoredSchemaFragment() throws assertThat(schemaFragmentUrl, is(notNullValue())); assertThat(jsonToVerifyUrl, is(notNullValue())); - final SchemaClient schemaClient = mock(SchemaClient.class); + final LocalFileSystemSchemaClient localFileSystemSchemaClient = mock(LocalFileSystemSchemaClient.class); try(final InputStream inputStream = schemaFragmentUrl.openStream()) { - when(schemaClient.get("http://justice.gov.uk/standards/complex_address.json")).thenReturn(inputStream); + when(localFileSystemSchemaClient.get("http://justice.gov.uk/standards/complex_address.json")).thenReturn(inputStream); final String schemaJson = IOUtils.toString(mainSchemaUrl); - final Schema schema = jsonStringToSchemaConverter.convert(schemaJson, schemaClient); + final Schema schema = jsonStringToSchemaConverter.convert(schemaJson, localFileSystemSchemaClient); final String json = IOUtils.toString(jsonToVerifyUrl); @@ -65,7 +66,7 @@ public void shouldFailWithErrorMessageIfParsingTheJsonStringFails() throws Excep final String schemaJson = IOUtils.toString(url); try { - jsonStringToSchemaConverter.convert(schemaJson, mock(SchemaClient.class)); + jsonStringToSchemaConverter.convert(schemaJson, mock(LocalFileSystemSchemaClient.class)); fail(); } catch (final SchemaCatalogException expected) { assertThat(expected.getCause(), is(instanceOf(JSONException.class))); diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java index 60b3725..72bf530 100644 --- a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java @@ -38,35 +38,35 @@ public class SchemaResolverTest { @Test public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndBaseLocation() throws Exception { - final URI catalogUri = new URI("file:/src/main/schema.json"); + final URI absoluteCatalogUri = new URI("file:/src/main/catalog-file.json"); - final String fileLocation = "some/path/to.json"; + final String schemaFileLocation = "some/path/to/schema.json"; final Optional baseLocation = of("base/location/"); - final URL resolvedUri = schemaResolver.resolve(catalogUri, fileLocation, baseLocation); + final URL resolvedUri = schemaResolver.resolve(absoluteCatalogUri, schemaFileLocation, baseLocation); - assertThat(resolvedUri.toString(), is("file:/src/main/base/location/some/path/to.json")); + assertThat(resolvedUri.toString(), is("file:/src/main/base/location/some/path/to/schema.json")); } @Test public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndAnEmptyBaseLocation() throws Exception { - final URI catalogUri = new URI("file:/src/main/schema.json"); + final URI absoluteCatalogUri = new URI("file:/src/main/catalog-file.json"); - final String fileLocation = "some/path/to.json"; + final String schemaFileLocation = "some/path/to/schema.json"; final Optional baseLocation = empty(); - final URL resolvedUri = schemaResolver.resolve(catalogUri, fileLocation, baseLocation); + final URL resolvedUri = schemaResolver.resolve(absoluteCatalogUri, schemaFileLocation, baseLocation); - assertThat(resolvedUri.toString(), is("file:/src/main/some/path/to.json")); + assertThat(resolvedUri.toString(), is("file:/src/main/some/path/to/schema.json")); } @Test public void shouldFailIfResolvingTheUrlThrowsAURISyntaxException() throws Exception { - final URI catalogUri = new URI("file:/src/main/schema.json"); + final URI catalogUri = new URI("file:/src/main/catalog-file.json"); - final String fileLocation = "some/path/to.json"; + final String fileLocation = "some/path/to/schema.json"; final Optional baseLocation = of("this path is silly"); try { @@ -74,7 +74,7 @@ public void shouldFailIfResolvingTheUrlThrowsAURISyntaxException() throws Except fail(); } catch (final SchemaCatalogException expected) { assertThat(expected.getCause(), is(instanceOf(URISyntaxException.class))); - assertThat(expected.getMessage(), is("Failed to resolve 'file:/src/main/schema.json', to file location 'some/path/to.json', with base location 'this path is silly'")); + assertThat(expected.getMessage(), is("Failed to resolve 'file:/src/main/catalog-file.json', to file location 'some/path/to/schema.json', with base location 'this path is silly'")); } } } 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 c4ee50d..06813c6 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 @@ -2,6 +2,9 @@ import java.util.List; +/** + * An Object representation of a json Catalog file + */ public class Catalog { private final List groups; @@ -12,10 +15,16 @@ public Catalog(final String name, final List groups) { this.name = name; } + /** + * @return The list of all {@link Group}s in the Catalog + */ public List getGroups() { return groups; } + /** + * @return The name of the Catalog + */ public String getName() { return name; } 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 fffcc7c..e94715f 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 @@ -2,6 +2,9 @@ import java.util.List; +/** + * An Object representation of a group in a json catalog file + */ public class Group { private final String baseLocation; @@ -14,14 +17,23 @@ public Group(final String baseLocation, final String name, final List sc this.schemas = schemas; } + /** + * @return The base location of the group + */ public String getBaseLocation() { return baseLocation; } + /** + * @return The name of the group + */ public String getName() { return name; } + /** + * @return A {@link List} of all {@link Schema}s in the group + */ public List getSchemas() { return schemas; } 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 4533acf..4e81a75 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 @@ -1,5 +1,8 @@ package uk.gov.justice.schema.catalog.domain; +/** + * Object representation of a Schema in a json catalog file + */ public class Schema { private final String id; @@ -10,10 +13,16 @@ public Schema(final String id, final String location) { this.location = location; } + /** + * @return The id of the Schema + */ public String getId() { return id; } + /** + * @return The Schema's location + */ public String getLocation() { return location; } 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 index f7e8d02..31cb25f 100644 --- 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 @@ -1,10 +1,16 @@ package uk.gov.justice.schema.catalog.generation; +/** + * Constants used in the Catalog generation + */ public class CatalogGenerationContext { private static final String CATALOG_FILENAME = "schema_catalog.json"; public static final String AN_EMPTY_STRING = ""; + /** + * @return The file name of the json catalog. + */ public String getCatalogFilename() { return CATALOG_FILENAME; } 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 index 0a89c36..adb1d37 100644 --- 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 @@ -10,6 +10,9 @@ import java.nio.file.Path; import java.util.List; +/** + * Class for running the generation of a json catalog file from within a Maven plugin + */ public class CatalogGenerationRunner { private final CatalogObjectGenerator catalogObjectGenerator; @@ -25,6 +28,13 @@ public CatalogGenerationRunner( this.urlConverter = urlConverter; } + /** + * Generates a json catalog + * @param catalogName The name of the catalog + * @param schemaFiles A list of json schema files + * @param catalogGenerationPath The path to where the catalog should be generated + * @param jsonSchemaPath A path where json schema files can be found + */ public void generateCatalog(final String catalogName, final List schemaFiles, final Path catalogGenerationPath, final Path jsonSchemaPath) { final Catalog catalog = catalogObjectGenerator.generate( 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 index c5b6c06..ec97f02 100644 --- 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 @@ -13,6 +13,9 @@ import java.util.List; import java.util.Map; +/** + * Generates a json {@link Catalog} from a list of json schema files + */ public class CatalogObjectGenerator { private final SchemaDefParser schemaDefParser; @@ -21,6 +24,13 @@ public CatalogObjectGenerator(final SchemaDefParser schemaDefParser) { this.schemaDefParser = schemaDefParser; } + /** + * Generate a new json {@link Catalog} from a list of json schema files + * @param catalogName The name of the catalog + * @param schemaFiles A list of json schema files + * @param jsonSchemaPath A default root location in the schemas path + * @return A {@link Catalog} of the json schema files + */ public Catalog generate(final String catalogName, final List schemaFiles, final Path jsonSchemaPath) { final List schemaDefs = schemaFiles.stream() 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 index 919d36d..195a176 100644 --- 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 @@ -13,6 +13,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.io.IOUtils; +/** + * Writes a {@link Catalog} Object as a json document + */ public class CatalogWriter { private static final String CATALOG_SUB_DIRECTORY = "json/schema"; @@ -25,6 +28,12 @@ public CatalogWriter(final ObjectMapper objectMapper, final CatalogGenerationCon this.catalogGenerationContext = catalogGenerationContext; } + /** + * Write a {@link Catalog} Object as a json document + * + * @param catalog The {@link Catalog} + * @param catalogGenerationPath The location of where the new json document should be written + */ @SuppressWarnings("ResultOfMethodCallIgnored") public void write(final Catalog catalog, final Path catalogGenerationPath) { diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/GenerationObjectFactory.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/GenerationObjectFactory.java index e62833d..42fea3b 100644 --- a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/GenerationObjectFactory.java +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/GenerationObjectFactory.java @@ -2,28 +2,50 @@ import uk.gov.justice.schema.catalog.CatalogObjectFactory; +/** + * A simple cheap way of avoiding having to use a dependency injection framework in the project. + * Gets a instance of an object with the object's dependencies instantiated and injected. + */ public class GenerationObjectFactory extends CatalogObjectFactory { + /** + * @return a new instance of {@link CatalogGenerationContext} + */ public CatalogGenerationContext catalogGenerationContext() { return new CatalogGenerationContext(); } + /** + * @return a new instance of {@link CatalogWriter} + */ public CatalogWriter catalogWriter() { return new CatalogWriter(objectMapper(), catalogGenerationContext()); } + /** + * @return a new instance of {@link SchemaIdParser} + */ public SchemaIdParser schemaIdParser() { return new SchemaIdParser(urlConverter()); } + /** + * @return a new instance of {@link CatalogObjectGenerator} + */ public CatalogObjectGenerator catalogObjectGenerator() { return new CatalogObjectGenerator(schemaDefParser()); } + /** + * @return a new instance of {@link SchemaDefParser} + */ public SchemaDefParser schemaDefParser() { return new SchemaDefParser(schemaIdParser()); } + /** + * @return a new instance of {@link CatalogGenerationRunner} + */ public CatalogGenerationRunner catalogGenerationRunner() { return new CatalogGenerationRunner( catalogObjectGenerator(), 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 index d1d77c7..da46511 100644 --- 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 @@ -3,6 +3,12 @@ import java.net.URI; import java.net.URL; +/** + * The file location, id, group name, base location and location of a json schema file. + * + * See {@link SchemaDefParser} for an explanation of the terms + * + */ public class SchemaDef { private final URL schemaFile; @@ -24,22 +30,37 @@ public SchemaDef( this.location = location; } + /** + * @return The {@link URL} of the json schema file + */ public URL getSchemaFile() { return schemaFile; } + /** + * @return The id of the json schema file + */ public URI getId() { return id; } + /** + * @return The group name + */ public String getGroupName() { return groupName; } + /** + * @return The base location of the schema files + */ public String getBaseLocation() { return baseLocation; } + /** + * @return The location of the schema file + */ 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 index 34141e9..9fc0af5 100644 --- 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 @@ -6,6 +6,32 @@ import java.net.URL; import java.nio.file.Path; +/** + * Parses an absolute {@link URL} to a json schema file into a {@link SchemaDef} containing: + *
    + *
  • The id of the schema file
  • + *
  • A group name of the scheme file (basically the path from the root of the schema locations)
  • + *
  • The base location (a common path from to schema location root to the schema file)
  • + *
  • Schema location (the relative path from base location and group to the schema file)
  • + *
  • Schema file (The original unparsed absolute {@link URL} of the json schema file)
  • + *
+ * + * For example:
+ * Given an absolute {@link URL} of a json file + *
file:/path/to/raml/json/schema/group/some/path/some-schema-or-other.json
+ * and a path to the json files (a sub path to locate the root of the json schemas) + *
json/schema/
+ * would give us the remaining sub path: + *
group/some/path/some-schema-or-other.json
+ * which would give use the group name + *
group
+ * and the base location + *
group/
+ * and the schema location: + *
some/path/some-schema-or-other.json
+ * + * + */ public class SchemaDefParser { private final SchemaIdParser schemaIdParser; @@ -14,6 +40,16 @@ public SchemaDefParser(final SchemaIdParser schemaIdParser) { this.schemaIdParser = schemaIdParser; } + /** + * Parse an absolute {@link URL} to a json schema file into a {@link SchemaDef} using a path to + * find the root location of the schema files + * + * See above for definitions of the {@link SchemaDef} + * + * @param schemaFile The absolute {@link URL} of the json schema file + * @param jsonSchemaPath a path to the schema root + * @return The parsed {@link SchemaDef} + */ public SchemaDef parse(final URL schemaFile, final Path jsonSchemaPath) { final URI id = schemaIdParser.parse(schemaFile); 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 index 9699bc6..af2724c 100644 --- 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 @@ -14,6 +14,10 @@ import org.reflections.Reflections; import org.reflections.scanners.ResourcesScanner; + +/** + * Lists all *.json files found on the classpath under a known package + */ public class SchemaFinder { private static final String SCHEMA_PACKAGE = "raml.json.schema"; @@ -24,6 +28,10 @@ public SchemaFinder(final UrlConverter urlConverter) { this.urlConverter = urlConverter; } + /** + * @return A list of all *.json files found under the raml.json.schema package on the + * classpath + */ public List listSchemas() { final Reflections reflections = new Reflections(SCHEMA_PACKAGE, new ResourcesScanner()); 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 index 056cf84..d4e8408 100644 --- 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 @@ -16,6 +16,10 @@ import org.apache.commons.io.IOUtils; +/** + * Loads a json schema file and extracts the schema id. A {@link CatalogGenerationException} is + * thrown if no id is found in the schema file. + */ public class SchemaIdParser { private final UrlConverter urlConverter; @@ -24,6 +28,13 @@ public SchemaIdParser(final UrlConverter urlConverter) { this.urlConverter = urlConverter; } + /** + * Extracts the id from a json schema file. A {@link CatalogGenerationException} is + * thrown if no id is found in the schema file, or if there is a problem reading the file. + * + * @param schemaFile A {@link URL} to the json schema file + * @return The id contained in the json schema file. + */ public URI parse(final URL schemaFile) { try { 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 index 150996a..70b42ad 100644 --- 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 @@ -7,18 +7,28 @@ import org.apache.maven.plugins.annotations.Parameter; +/** + * Object that gives the properties specified in the maven plugin xml + */ public class CatalogGeneratorProperties implements GeneratorProperties { @Parameter private String catalogName; + @Parameter(defaultValue = "json/schema/") private String jsonSchemaPath = "json/schema/"; + /** + * @return The name of the catalog specified in the maven plugin xml + */ public String getCatalogName() { return catalogName; } + /** + * @return The path (on the classpath) to where the json schemas are located. Defaults to 'json/schema' + */ public Path getJsonSchemaPath() { return Paths.get(jsonSchemaPath); } diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGenerator.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGenerator.java index eb0ea1a..4a5fb98 100644 --- a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGenerator.java +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGenerator.java @@ -9,6 +9,11 @@ import java.nio.file.Path; import java.util.List; +/** + * Implementation of a maven generator which generates a catalog of json schema + * files in the spirit of + * XML Catalog + */ public class MavenCatalogGenerator implements Generator> { private final GenerationObjectFactory generationObjectFactory; @@ -17,6 +22,12 @@ public MavenCatalogGenerator(final GenerationObjectFactory generationObjectFacto this.generationObjectFactory = generationObjectFactory; } + /** + * Main entry point of the Maven Plugin + * + * @param schemaFiles A {@link List} of json schema files + * @param generatorConfig The configuration of the Maven Plugin + */ @Override public void run(final List schemaFiles, final GeneratorConfig generatorConfig) { diff --git a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGeneratorFactory.java b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGeneratorFactory.java index aefc2fb..b6ae41c 100644 --- a/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGeneratorFactory.java +++ b/catalog-generation/src/main/java/uk/gov/justice/schema/catalog/generation/maven/MavenCatalogGeneratorFactory.java @@ -7,8 +7,14 @@ import java.net.URI; import java.util.List; +/** + * Factory for creating a {@link MavenCatalogGenerator} + */ public class MavenCatalogGeneratorFactory implements GeneratorFactory> { + /** + * @return A new instance of {@link MavenCatalogGenerator} + */ @Override public Generator> create() { return new MavenCatalogGenerator(new GenerationObjectFactory()); 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 index 12f9aeb..5c934bd 100644 --- 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 @@ -33,7 +33,7 @@ public class SchemaDefParserTest { public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocation() throws Exception { final Path jsonSchemaPath = Paths.get("json/schema/"); - final URL schemaFile = new URL("file:/path/to/raml/json/schema/a-sub-directory/some-schema-or-other.json"); + final URL schemaFile = new URL("file:/path/to/raml/json/schema/group/some/path/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()); @@ -42,9 +42,9 @@ public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocation() throws E 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")); + assertThat(schemaDef.getGroupName(), is("group")); + assertThat(schemaDef.getBaseLocation(), is("group/")); + assertThat(schemaDef.getLocation(), is("some/path/some-schema-or-other.json")); } @Test diff --git a/schema-service/src/main/java/uk/gov/justice/schema/service/SchemaCatalogService.java b/schema-service/src/main/java/uk/gov/justice/schema/service/SchemaCatalogService.java index 9adc0d3..bd9ae0b 100644 --- a/schema-service/src/main/java/uk/gov/justice/schema/service/SchemaCatalogService.java +++ b/schema-service/src/main/java/uk/gov/justice/schema/service/SchemaCatalogService.java @@ -11,6 +11,11 @@ import org.everit.json.schema.Schema; +/** + * A service that finds a fully resolved json schema by its schema id. Allows a schema catalog + * to be used in an application that uses a dependency injection framework. All schemas are + * looked up on the fly, then cached. + */ @ApplicationScoped public class SchemaCatalogService { @@ -19,6 +24,11 @@ public class SchemaCatalogService { @Inject private Catalog catalog; + /** + * Finds a json schema file on the classpath by its schema id + * @param schemaId The id of the schema + * @return An {@link Optional} containing the fully resolved schema or empty if not found + */ public Optional findSchema(final String schemaId) { if(schemaMap.containsKey(schemaId)) {