This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean and refactor spike into well tested code
- Loading branch information
Showing
62 changed files
with
1,461 additions
and
888 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Json Schema Catalog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>json-schema-catalog</artifactId> | ||
<groupId>uk.gov.justice.schema</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>catalog-core</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>javax</groupId> | ||
<artifactId>javaee-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish</groupId> | ||
<artifactId>javax.json</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.schema</groupId> | ||
<artifactId>everit-schema-client</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.schema</groupId> | ||
<artifactId>catalog-domain</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.utils</groupId> | ||
<artifactId>utilities-core</artifactId> | ||
<version>${utilities.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.everit-org.json-schema</groupId> | ||
<artifactId>org.everit.json.schema</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.raml</groupId> | ||
<artifactId>raml-parser</artifactId> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.jayway.jsonpath</groupId> | ||
<artifactId>json-path-assert</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
37 changes: 37 additions & 0 deletions
37
catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import uk.gov.justice.schema.client.SchemaClientFactory; | ||
|
||
import java.util.Map; | ||
|
||
import org.everit.json.schema.Schema; | ||
|
||
public class CatalogLoader { | ||
|
||
private final SchemaResolverAndLoader schemaResolverAndLoader; | ||
private final CatalogToSchemaResolver catalogToSchemaResolver; | ||
private final JsonSchemaLoader jsonSchemaLoader; | ||
private final SchemaClientFactory schemaClientFactory; | ||
|
||
public CatalogLoader( | ||
final SchemaResolverAndLoader schemaResolverAndLoader, | ||
final CatalogToSchemaResolver catalogToSchemaResolver, | ||
final JsonSchemaLoader jsonSchemaLoader, | ||
final SchemaClientFactory schemaClientFactory) { | ||
this.schemaResolverAndLoader = schemaResolverAndLoader; | ||
this.catalogToSchemaResolver = catalogToSchemaResolver; | ||
this.jsonSchemaLoader = jsonSchemaLoader; | ||
this.schemaClientFactory = schemaClientFactory; | ||
} | ||
|
||
public Map<String, Schema> loadCatalogsFromClasspath() { | ||
|
||
final Map<String, String> urlsToJson = jsonSchemaLoader.loadJsonFrom( | ||
catalogToSchemaResolver.resolveSchemaLocations() | ||
); | ||
|
||
return schemaResolverAndLoader.loadSchemas( | ||
urlsToJson, | ||
schemaClientFactory.create(urlsToJson)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
import uk.gov.justice.schema.catalog.domain.Group; | ||
import uk.gov.justice.schema.catalog.domain.Schema; | ||
|
||
import java.net.URL; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
|
||
public class CatalogToSchemaResolver { | ||
|
||
private final ClasspathCatalogLoader classpathCatalogLoader; | ||
private final SchemaResolver schemaResolver; | ||
|
||
public CatalogToSchemaResolver(final ClasspathCatalogLoader classpathCatalogLoader, final SchemaResolver schemaResolver) { | ||
this.classpathCatalogLoader = classpathCatalogLoader; | ||
this.schemaResolver = schemaResolver; | ||
} | ||
|
||
public Map<String, URL> resolveSchemaLocations() { | ||
|
||
return classpathCatalogLoader.getCatalogs().entrySet().stream() | ||
.flatMap(catalogEntry -> catalogEntry.getValue().getGroup().stream() | ||
.flatMap(group -> group.getSchema().stream() | ||
.map(schema -> resolveToUrl(group, schema, catalogEntry.getKey())))) | ||
.collect(toMap(Entry::getKey, Entry::getValue)); | ||
} | ||
|
||
private Entry<String, URL> resolveToUrl(final Group group, final Schema schema, final URL catalogUrl) { | ||
final String location = schema.getLocation(); | ||
final Optional<String> baseLocation = ofNullable(group.getBaseLocation()); | ||
final URL url = schemaResolver.resolve(catalogUrl, location, baseLocation); | ||
|
||
return new SimpleEntry<>(schema.getId(), url); | ||
} | ||
|
||
|
||
} |
53 changes: 53 additions & 0 deletions
53
catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import static java.lang.String.format; | ||
|
||
import uk.gov.justice.schema.catalog.domain.Catalog; | ||
import uk.gov.justice.schema.catalog.domain.CatalogWrapper; | ||
import uk.gov.justice.schema.catalog.util.ClasspathResourceLoader; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class ClasspathCatalogLoader { | ||
|
||
private static final String DEFAULT_JSON_CATALOG_LOCATION = "json/schema/schema_catalog.json"; | ||
|
||
private final ObjectMapper objectMapper; | ||
private final ClasspathResourceLoader classpathResourceLoader; | ||
|
||
public ClasspathCatalogLoader(final ObjectMapper objectMapper, final ClasspathResourceLoader classpathResourceLoader) { | ||
this.objectMapper = objectMapper; | ||
this.classpathResourceLoader = classpathResourceLoader; | ||
} | ||
|
||
public Map<URL, Catalog> getCatalogs() { | ||
|
||
final Map<URL, Catalog> map = new HashMap<>(); | ||
|
||
for (final URI uri : listAllCatalogsFromClasspath()) { | ||
try { | ||
final Catalog catalog = objectMapper.readValue(uri.toURL(), CatalogWrapper.class).getCatalog(); | ||
map.put(uri.toURL(), catalog); | ||
} catch (IOException e) { | ||
throw new SchemaCatalogException(format("Failed to convert to json loaded from '%s' to a Catalog pojo", uri.toString()), e); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
|
||
private List<URI> listAllCatalogsFromClasspath() { | ||
try { | ||
return classpathResourceLoader.getResources(getClass(), DEFAULT_JSON_CATALOG_LOCATION); | ||
} catch (final IOException e) { | ||
throw new SchemaCatalogException(format("Failed to load the catalogs from the classpath for location '%s'", DEFAULT_JSON_CATALOG_LOCATION), e); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import static java.lang.String.format; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
public class FileContentsAsStringLoader { | ||
|
||
public String readFileContents(final URL urlOfFile) { | ||
|
||
try { | ||
return IOUtils.toString(urlOfFile, UTF_8); | ||
} catch (final IOException e) { | ||
throw new SchemaCatalogException(format("Failed to read file contents from '%s'", urlOfFile), e); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
public class JsonSchemaLoader { | ||
|
||
private final FileContentsAsStringLoader fileContentsAsStringLoader; | ||
|
||
public JsonSchemaLoader(final FileContentsAsStringLoader fileContentsAsStringLoader) { | ||
this.fileContentsAsStringLoader = fileContentsAsStringLoader; | ||
} | ||
|
||
public Map<String, String> loadJsonFrom(final Map<String, URL> schemaLocationMap) { | ||
return schemaLocationMap.entrySet() | ||
.stream() | ||
.collect(toMap(Map.Entry::getKey, entry -> fileContentsAsStringLoader.readFileContents(entry.getValue()))); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import org.everit.json.schema.Schema; | ||
import org.everit.json.schema.loader.SchemaClient; | ||
import org.everit.json.schema.loader.SchemaLoader; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
|
||
public class JsonStringToSchemaConverter { | ||
|
||
public Schema convert(final String schemaJson, final SchemaClient schemaClient) { | ||
return SchemaLoader.builder() | ||
.schemaJson(new JSONObject(new JSONTokener(schemaJson))) | ||
.httpClient(schemaClient) | ||
.build() | ||
.load() | ||
.build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
public class SchemaCatalogException extends RuntimeException { | ||
|
||
public SchemaCatalogException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import uk.gov.justice.schema.catalog.util.UrlConverter; | ||
|
||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
public class SchemaResolver { | ||
|
||
private static final String AN_EMPTY_STRING = ""; | ||
|
||
private final UrlConverter urlConverter; | ||
|
||
public SchemaResolver(final UrlConverter urlConverter) { | ||
this.urlConverter = urlConverter; | ||
} | ||
|
||
public URL resolve( | ||
final URL catalogUrl, | ||
final String fileLocation, | ||
final Optional<String> fileBaseLocation) { | ||
|
||
final URI schemaUri = URI.create(fileBaseLocation.orElse(AN_EMPTY_STRING)).resolve(fileLocation); | ||
final URI uri = urlConverter.toUri(catalogUrl).resolve(schemaUri); | ||
|
||
return urlConverter.toUrl(uri); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.util.Map; | ||
|
||
import org.everit.json.schema.Schema; | ||
import org.everit.json.schema.loader.SchemaClient; | ||
|
||
public class SchemaResolverAndLoader { | ||
|
||
private final JsonStringToSchemaConverter jsonStringToSchemaConverter; | ||
|
||
public SchemaResolverAndLoader(final JsonStringToSchemaConverter jsonStringToSchemaConverter) { | ||
this.jsonStringToSchemaConverter = jsonStringToSchemaConverter; | ||
} | ||
|
||
public Map<String, Schema> loadSchemas(final Map<String, String> urlsToJson, final SchemaClient schemaClient) { | ||
|
||
return urlsToJson.entrySet() | ||
.stream() | ||
.collect(toMap(Map.Entry::getKey, entry -> jsonStringToSchemaConverter.convert(entry.getValue(), schemaClient))); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package uk.gov.justice.schema.catalog.util; | ||
|
||
import static java.util.Collections.list; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
public class ClasspathResourceLoader { | ||
|
||
public ClasspathResourceLoader(final UrlConverter urlConverter) { | ||
this.urlConverter = urlConverter; | ||
} | ||
|
||
private final UrlConverter urlConverter; | ||
|
||
|
||
public List<URI> getResources(final Class<?> clazz, final String location) throws IOException { | ||
final List<URL> urls = list(clazz.getClassLoader().getResources(location)); | ||
return urls.stream().map(urlConverter::toUri).collect(toList()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uk.gov.justice.schema.catalog.util; | ||
|
||
import static java.lang.String.format; | ||
|
||
import uk.gov.justice.schema.catalog.SchemaCatalogException; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
|
||
public class UrlConverter { | ||
|
||
public URI toUri(final URL url) { | ||
try { | ||
return url.toURI(); | ||
} catch (URISyntaxException e) { | ||
throw new SchemaCatalogException(format("Failed to convert URL '%s' to URI", url), e); | ||
} | ||
} | ||
|
||
public URL toUrl(final URI uri) { | ||
try { | ||
return uri.toURL(); | ||
} catch (MalformedURLException e) { | ||
throw new SchemaCatalogException(format("Failed to convert URI '%s' to URL", uri), e); | ||
} | ||
} | ||
} |
Oops, something went wrong.