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

Commit

Permalink
Add support to generate referenced schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Benster Nzewi committed Jun 29, 2018
1 parent 79a5a66 commit 90e7bed
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;

Expand All @@ -23,22 +24,24 @@ public CatalogUpdater(final Logger logger) {
this.logger = logger;
}

public void updtateRawCatalog(Map<String, String> schemaIdsToRawJsonSchemaCache, Collection<Path> paths) {
public void updtateRawCatalog(Map<String, String> schemaIdsToRawJsonSchemaCache, Path basePath, Collection<Path> paths) {

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

final String schema;
try {
schema = IOUtils.toString(path.toUri().toURL(), UTF_8);
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);
}
else
logger.warn(schema.format("Failed to generate catalog. Schema '%s' has no id", path.toUri().toURL()));
}
//logger.warn(schema.format());
} catch (IOException e) {
logger.error(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader,
}



public void updtateCatalog(Collection<Path> paths) {
catalogUpdater.updtateRawCatalog(schemaIdsToRawJsonSchemaCache, paths);

}

/**
* Initializes the cache of raw json schemas by scanning the classpath and
* loading all json schemas it finds.
Expand All @@ -65,20 +59,10 @@ public void initialize() {
*/
public Optional<String> getRawJsonSchema(final String schemaId) {

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

/* private void updateCatalogSchemaCache(final String schemaId) {
if(schemaIdsToRawJsonSchemaCache.get(schemaId) == null){
try {
final Optional<Pair<String, String>> schemaDetails = schemaIdAndUriParser.parse(new URL(schemaId));
if (schemaDetails.isPresent()) {
schemaIdsToRawJsonSchemaCache.put(schemaDetails.get().getKey(), schemaDetails.get().getValue());
}
} catch (MalformedURLException e) {
logger.error(e.getMessage());
}
}
}*/
public void updateCatalogSchemaCache(final Path basePath, final Collection<Path> paths) {
catalogUpdater.updtateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package uk.gov.justice.schema.catalog.util;


import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;

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

import java.io.File;
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.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class CatalogUpdaterTest {

@Mock Logger logger;
@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();
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.updtateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
assertThat(schemaIdsToRawJsonSchemaCache.size(), greaterThan(1));

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


}

}
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,56 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"id": "http://justice.gov.uk/standards/example.events.person-updated.json",
"properties": {
"personId": {
"$ref": "http://justice.gov.uk/standards/example.standard.uuid.json"
},
"title": {
"type": "string",
"enum": [
"",
"Mr",
"Mrs",
"Ms"
]
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"required": {
"type": "boolean"
},
"signedInCount": {
"type": "integer"
},
"ratio": {
"type": "number"
},
"startDate": {
"type": "string",
"format": "date-time"
},
"aliases": {
"type": "array",
"items": {
"$ref": "http://justice.gov.uk/standards/alias.json"
}
}
},
"additionalProperties": false,
"required": [
"personId",
"title",
"firstName",
"lastName",
"required",
"signedInCount",
"ratio",
"startDate",
"alias"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://justice.gov.uk/standards/example.standard.uuid.json",
"$ref": "#/definitions/uuid",
"definitions": {
"uuid": {
"type": "string",
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
}
}
}

0 comments on commit 90e7bed

Please sign in to comment.