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

Commit

Permalink
Merge 429bb8b into 7969180
Browse files Browse the repository at this point in the history
  • Loading branch information
BenNzewi committed Jul 3, 2018
2 parents 7969180 + 429bb8b commit ff95f0a
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## Unreleased

## [1.2.4] - 2018-07-03

### Changed
- Added fix to generate schema with reference to another schema in the same source directory

## [1.2.3] - 2018-06-21

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public JsonSchemaFileLoader jsonSchemaFileLoader() {
return new JsonSchemaFileLoader(fileContentsAsStringLoader(), catalogToSchemaResolver());
}

/**
* @return a new instance of {@link CatalogUpdater}
*/
public CatalogUpdater catalogUpdater() {
return new CatalogUpdater();
}

/**
* @return a new instance of {@link SchemaClientFactory}
*/
Expand All @@ -121,7 +128,7 @@ public Catalog catalog() {
* @return a new instance of {@link RawCatalog}
*/
public RawCatalog rawCatalog() {
final RawCatalog rawCatalog = new RawCatalog(jsonSchemaFileLoader());
final RawCatalog rawCatalog = new RawCatalog(jsonSchemaFileLoader(), catalogUpdater());
rawCatalog.initialize();

return rawCatalog;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.gov.justice.schema.catalog;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.json.Json.createReader;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Map;

import javax.json.JsonObject;
import javax.json.JsonReader;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CatalogUpdater {

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

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()));

try {
final String schema = IOUtils.toString(updatedPath.toUri().toURL(), 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");
schemaIdsToRawJsonSchemaCache.put(id, schema);
}
}
LOGGER.warn(format("Failed to generate catalog. Schema '%s' has no id", path.toUri().toURL()));

} catch (IOException e) {
LOGGER.error(e.getMessage());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static java.util.Optional.ofNullable;

import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -14,12 +16,18 @@ public class RawCatalog {

private final JsonSchemaFileLoader jsonSchemaFileLoader;


private final CatalogUpdater catalogUpdater;

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

public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader) {
public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader,
final CatalogUpdater catalogUpdater) {
this.jsonSchemaFileLoader = jsonSchemaFileLoader;
this.catalogUpdater = catalogUpdater;
}


/**
* Initializes the cache of raw json schemas by scanning the classpath and
* loading all json schemas it finds.
Expand All @@ -35,6 +43,16 @@ public void initialize() {
* 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
*/
public void updateCatalogSchemaCache(final Path basePath, final Collection<Path> paths) {
catalogUpdater.updateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.schema.catalog;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.hamcrest.CoreMatchers.is;
Expand All @@ -9,12 +10,23 @@
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.Map;

import org.apache.commons.io.IOUtils;
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)
Expand All @@ -26,6 +38,9 @@ public class RawCatalogTest {
@InjectMocks
private RawCatalog rawCatalog;

@Spy
private CatalogUpdater catalogUpdater;

@SuppressWarnings("unchecked")
@Test
public void shouldGetRawJsonSchemaFromTheLoadedSchemaCache() throws Exception {
Expand Down Expand Up @@ -93,4 +108,35 @@ public void shouldReturnEmptyIfTheCacheIsNotInitialized() throws Exception {

assertThat(rawCatalog.getRawJsonSchema(schemaId), is(empty()));
}

@Test
public void testShouldUpdateCatalogSchemaWithPaths(){
final Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();
final Path basePath = Paths.get((""));

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.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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package uk.gov.justice.schema.catalog.util;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;

import uk.gov.justice.schema.catalog.CatalogUpdater;
import uk.gov.justice.schema.catalog.JsonSchemaFileLoader;
import uk.gov.justice.schema.catalog.RawCatalog;
import uk.gov.justice.schema.catalog.SchemaCatalogException;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
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.Map;

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 CatalogUpdaterTest {

@Mock
private JsonSchemaFileLoader jsonSchemaFileLoader;

@InjectMocks
private RawCatalog rawCatalog;

@InjectMocks
private CatalogUpdater catalogUpdater;

@Test
public void shouldCacheFileWithReferenceToAnotherSchema(){

final Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();
final Path basePath = Paths.get((""));

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

rawCatalog.initialize();

final 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()));
catalogUpdater.updateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
assertThat(schemaIdsToRawJsonSchemaCache.size(), greaterThan(1));

} catch (URISyntaxException e) {
e.printStackTrace();
}
}

@Test
public void shouldFailIfFileWithReferenceToAnotherSchemaHasNoId() throws Exception {

final Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();
final Path basePath = Paths.get((""));

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

rawCatalog.initialize();

final Collection<Path> paths = new ArrayList<>();

final File aliasJson = new File(this.getClass().getClassLoader().getResource("json/schema/standards/example.events.alias.missing.id.json").toURI());

paths.add(Paths.get(aliasJson.toURI()));
catalogUpdater.updateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);

assertThat(schemaIdsToRawJsonSchemaCache.size(), is(1));
}

@Test
public void shouldFailIfLoadingFileWithReferenceToAnotherSchemaThrowsAnIOException() throws Exception {

final Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();
final Path basePath = Paths.get((""));

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

rawCatalog.initialize();

final Collection<Path> paths = new ArrayList<>();

final URL aliasJson = new URL("file:/this/file/does/not/exist.json");

paths.add(Paths.get(aliasJson.toURI()));

try {
catalogUpdater.updateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
} catch (final SchemaCatalogException 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'"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://justice.gov.uk/standards/defendant.json",
"type": "object",
"properties": {
"personalInfo": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"id": "http://justice.gov.uk/standards/alias.json",
"properties": {
"aliasForenames": {
"type": "string",
"maxLength": 20
},
"aliasInitials": {
"type": "string",
"maxLength": 2
},
"aliasSurname": {
"type": "string",
"maxLength": 30
}
},
"additionalProperties": false,
"required": [
"aliasForenames",
"aliasInitials",
"aliasSurname"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"aliasForenames": {
"type": "string",
"maxLength": 20
},
"aliasInitials": {
"type": "string",
"maxLength": 2
},
"aliasSurname": {
"type": "string",
"maxLength": 30
}
},
"additionalProperties": false,
"required": [
"aliasForenames",
"aliasInitials",
"aliasSurname"
]
}
Loading

0 comments on commit ff95f0a

Please sign in to comment.