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.
- Loading branch information
Showing
11 changed files
with
355 additions
and
2 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
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
47 changes: 47 additions & 0 deletions
47
catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogUpdater.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,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()); | ||
} | ||
}); | ||
} | ||
} |
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
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
113 changes: 113 additions & 0 deletions
113
catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/CatalogUpdaterTest.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,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'")); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
catalog-core/src/test/resources/json/schema/standards/defendant.json
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
25 changes: 25 additions & 0 deletions
25
catalog-core/src/test/resources/json/schema/standards/example.events.alias.json
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 @@ | ||
{ | ||
"$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" | ||
] | ||
} |
24 changes: 24 additions & 0 deletions
24
catalog-core/src/test/resources/json/schema/standards/example.events.alias.missing.id.json
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 @@ | ||
{ | ||
"$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" | ||
] | ||
} |
Oops, something went wrong.