Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Merge 54fce47 into bde20cf
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Jul 4, 2018
2 parents bde20cf + 54fce47 commit ab06b9e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## Unreleased

## [1.3.1] - 2018-08-04
### Added
- updateCatalogSchemaCache method to SchemaCatalogResolver for easier updating of the RawCatalog
using resources that are not on the classpath

## [1.3.0] - 2018-08-04
### Added
- Schema catalog resolver to enable loading schemas from JSON objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Update a {@link RawCatalog} cache, to add resource paths that are not on the classpath.
*/
public class CatalogUpdater {

private static final Logger LOGGER = LoggerFactory.getLogger(CatalogUpdater.class);

/**
* Updates the cache with raw json schemas that are not on the classpath
*
* @param basePath the base directory to load the resources from
* @param paths the resources to parse
*/
public void updateRawCatalog(final Map<String, String> schemaIdsToRawJsonSchemaCache, final Path basePath, final Collection<Path> paths) {

paths.forEach(path ->{
final Path updatedPath = Paths.get(format("%s/%s",basePath.toString(),path.toString()));
paths.forEach(path -> {
final Path updatedPath = Paths.get(format("%s/%s", basePath.toString(), path.toString()));

try {
final String schema = IOUtils.toString(updatedPath.toUri().toURL(), UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
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}
* 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;


private final CatalogUpdater catalogUpdater;

private Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();

public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader,
Expand All @@ -27,30 +24,30 @@ public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader,
this.catalogUpdater = catalogUpdater;
}


/**
* Initializes the cache of raw json schemas by scanning the classpath and
* loading all json schemas it finds.
* 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.
* @return An {@link Optional} containing the raw json schema of the schema file with the
* specified id, if it exists.
*/
public Optional<String> getRawJsonSchema(final String schemaId) {

return ofNullable(schemaIdsToRawJsonSchemaCache.get(schemaId));
}

/**
* Updates the cache with raw json schemas required during pojo generation
* @param basePath
* @param paths
* Updates the cache with raw json schemas that are not on the classpath
*
* @param basePath the base directory to load the resources from
* @param paths the resources to parse
*/
public void updateCatalogSchemaCache(final Path basePath, final Collection<Path> paths) {
catalogUpdater.updateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import uk.gov.justice.schema.catalog.client.SchemaClientFactory;

import java.nio.file.Path;
import java.util.Collection;

import org.everit.json.schema.Schema;
import org.json.JSONObject;

Expand All @@ -24,4 +27,14 @@ public Schema loadSchema(final JSONObject jsonSchema) {
jsonSchema,
schemaClientFactory.create(rawCatalog));
}

/**
* Updates the raw catalogue cache with raw json schemas that are not on the classpath
*
* @param basePath the base directory to load the resources from
* @param paths the resources to parse
*/
public void updateCatalogSchemaCache(final Path basePath, final Collection<Path> paths) {
this.rawCatalog.updateCatalogSchemaCache(basePath, paths);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.schema.catalog;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.hamcrest.CoreMatchers.is;
Expand All @@ -10,15 +11,9 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -110,33 +105,17 @@ public void shouldReturnEmptyIfTheCacheIsNotInitialized() throws Exception {
}

@Test
public void testShouldUpdateCatalogSchemaWithPaths(){
final Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();
public void shouldUpdateCatalogSchemaWithPaths() throws Exception {
final Path basePath = Paths.get((""));
final Path aliasJsonPath = Paths.get(this.getClass().getClassLoader().getResource("json/schema/standards/example.events.alias.json").toURI());
final Path personJson = Paths.get(this.getClass().getClassLoader().getResource("json/schema/standards/example.events.person-updated.json").toURI());
final List<Path> paths = asList(aliasJsonPath, personJson);

final String expectedSchema = IOUtils.toString(personJson.toUri(), UTF_8);
final String schemaId = "http://justice.gov.uk/standards/example.events.person-updated.json";

schemaIdsToRawJsonSchemaCache.put("http://justice.gov.uk/standards/address.json", "json schema" );
rawCatalog.updateCatalogSchemaCache(basePath, paths);

rawCatalog.initialize();
Collection<Path> paths = new ArrayList<>();

try {
final File aliasJson = new File(this.getClass().getClassLoader().getResource("json/schema/standards/example.events.alias.json").toURI());
final File personJson = new File(this.getClass().getClassLoader().getResource("json/schema/standards/example.events.person-updated.json").toURI());
paths.add(Paths.get(aliasJson.toURI()));
paths.add(Paths.get(personJson.toURI()));
rawCatalog.updateCatalogSchemaCache(basePath, paths);

final String schema = IOUtils.toString(personJson.toURI().toURL(), UTF_8);
assertThat(rawCatalog.getRawJsonSchema(schemaId), is(of(schema)));

} catch (URISyntaxException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
assertThat(rawCatalog.getRawJsonSchema(schemaId), is(of(expectedSchema)));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package uk.gov.justice.schema.catalog;

import static java.util.Collections.singletonList;
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.schema.catalog.client.SchemaClientFactory;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaClient;
import org.json.JSONObject;
Expand Down Expand Up @@ -44,4 +50,14 @@ public void shouldResolveSchema() {

assertThat(resultSchema, is(schema));
}

@Test
public void shouldUpdateRawCatalogWithPaths() {
final Path basePath = Paths.get("");
final List<Path> paths = singletonList(Paths.get("test/path"));

schemaCatalogResolver.updateCatalogSchemaCache(basePath, paths);

verify(rawCatalog).updateCatalogSchemaCache(basePath, paths);
}
}

0 comments on commit ab06b9e

Please sign in to comment.