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

Commit

Permalink
Add soft handling of missing schema ids in generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo authored and amckenzie committed Dec 11, 2017
1 parent 81ba9a8 commit 91a50c1
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

public class CatalogGenerationException extends RuntimeException {

public CatalogGenerationException(final String message) {
super(message);
}

public CatalogGenerationException(final String message, final Throwable cause) {
super(message, cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Generates a json {@link Catalog} from a list of json schema files
Expand All @@ -35,6 +36,8 @@ public Catalog generate(final String catalogName, final List<URL> schemaFiles, f

final List<SchemaDef> schemaDefs = schemaFiles.stream()
.map(schemaFile -> schemaDefParser.parse(schemaFile, jsonSchemaPath))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList());

final List<Group> groups = asGroups(schemaDefs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.justice.schema.catalog.generation;

import static org.slf4j.LoggerFactory.getLogger;

import uk.gov.justice.schema.catalog.CatalogObjectFactory;

/**
Expand All @@ -26,7 +28,7 @@ public CatalogWriter catalogWriter() {
* @return a new instance of {@link SchemaIdParser}
*/
public SchemaIdParser schemaIdParser() {
return new SchemaIdParser(urlConverter());
return new SchemaIdParser(urlConverter(), getLogger(SchemaIdParser.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package uk.gov.justice.schema.catalog.generation;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static uk.gov.justice.schema.catalog.generation.CatalogGenerationContext.AN_EMPTY_STRING;

import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;

/**
* Parses an absolute {@link URL} to a json schema file into a {@link SchemaDef} containing:
Expand Down Expand Up @@ -50,21 +53,28 @@ public SchemaDefParser(final SchemaIdParser schemaIdParser) {
* @param jsonSchemaPath a path to the schema root
* @return The parsed {@link SchemaDef}
*/
public SchemaDef parse(final URL schemaFile, final Path jsonSchemaPath) {
public Optional<SchemaDef> parse(final URL schemaFile, final Path jsonSchemaPath) {

final URI id = schemaIdParser.parse(schemaFile);
final String fileUrl = schemaFile.toString();
final Optional<URI> id = schemaIdParser.parse(schemaFile);

final String jsonSchemaPathString = jsonSchemaPath.toString();
final String relativeUri = fileUrl.substring(fileUrl.indexOf(jsonSchemaPathString) + jsonSchemaPathString.length() + 1);
if (id.isPresent()) {
final String fileUrl = schemaFile.toString();

final int firstSlashIndex = relativeUri.indexOf('/');
final String jsonSchemaPathString = jsonSchemaPath.toString();
final String relativeUri = fileUrl.substring(fileUrl.indexOf(jsonSchemaPathString) + jsonSchemaPathString.length() + 1);

final String groupName = getGroup(relativeUri, firstSlashIndex);
final String baseLocation = getBaseLocation(groupName, firstSlashIndex);
final String location = relativeUri.substring(firstSlashIndex + 1);
final int firstSlashIndex = relativeUri.indexOf('/');

return new SchemaDef(schemaFile, id, groupName, baseLocation, location);
final String groupName = getGroup(relativeUri, firstSlashIndex);
final String baseLocation = getBaseLocation(groupName, firstSlashIndex);
final String location = relativeUri.substring(firstSlashIndex + 1);

final SchemaDef schemaDef = new SchemaDef(schemaFile, id.get(), groupName, baseLocation, location);

return of(schemaDef);
}

return empty();
}

private String getGroup(final String relativeUri, final int firstSlashIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
public class SchemaFinder {

// TODO: Un-hard-code me
private static final String SCHEMA_PACKAGE = "raml.json.schema";

private final UrlConverter urlConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import uk.gov.justice.schema.catalog.util.UrlConverter;
Expand All @@ -10,11 +12,13 @@
import java.io.StringReader;
import java.net.URI;
import java.net.URL;
import java.util.Optional;

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

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

/**
* Loads a json schema file and extracts the schema id. A {@link CatalogGenerationException} is
Expand All @@ -23,9 +27,11 @@
public class SchemaIdParser {

private final UrlConverter urlConverter;
private final Logger logger;

public SchemaIdParser(final UrlConverter urlConverter) {
public SchemaIdParser(final UrlConverter urlConverter, final Logger logger) {
this.urlConverter = urlConverter;
this.logger = logger;
}

/**
Expand All @@ -35,7 +41,7 @@ public SchemaIdParser(final UrlConverter urlConverter) {
* @param schemaFile A {@link URL} to the json schema file
* @return The id contained in the json schema file.
*/
public URI parse(final URL schemaFile) {
public Optional<URI> parse(final URL schemaFile) {

try {
final String schema = IOUtils.toString(schemaFile, UTF_8);
Expand All @@ -44,11 +50,13 @@ public URI parse(final URL schemaFile) {

if (jsonObject.containsKey("id")) {
final String id = jsonObject.getString("id");
return urlConverter.toUri(id);
return of(urlConverter.toUri(id));
}
}

throw new CatalogGenerationException(format("Failed to generate catalog. Schema '%s' has no id", schemaFile));
logger.warn(format("Failed to generate catalog. Schema '%s' has no id", schemaFile));

return empty();

} catch (final IOException e) {
throw new CatalogGenerationException(format("Failed to extract id from schema file '%s'", schemaFile), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.Assert.assertThat;

import uk.gov.justice.schema.catalog.domain.Catalog;
import uk.gov.justice.schema.catalog.util.UrlConverter;

import java.net.URL;
import java.nio.file.Path;
Expand All @@ -16,11 +15,7 @@

public class CatalogObjectGeneratorTest {

private final UrlConverter urlConverter = new UrlConverter();
private final SchemaIdParser schemaIdParser = new SchemaIdParser(urlConverter);
private final SchemaDefParser schemaDefParser = new SchemaDefParser(schemaIdParser);

private final CatalogObjectGenerator catalogObjectGenerator = new CatalogObjectGenerator(schemaDefParser);
private final CatalogObjectGenerator catalogObjectGenerator = new GenerationObjectFactory().catalogObjectGenerator();

@Test
public void shouldParseAListOfSchemaFilesIntoACatalogObject() throws Exception {
Expand All @@ -40,7 +35,6 @@ public void shouldParseAListOfSchemaFilesIntoACatalogObject() throws Exception {
complexAddressSchemaFile,
defendantSchemaFile);


final Catalog catalog = catalogObjectGenerator.generate(catalogName, schemaFiles, jsonSchemaPath);

assertThat(catalog.getName(), is(catalogName));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package uk.gov.justice.schema.catalog.generation;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static uk.gov.justice.schema.catalog.generation.CatalogGenerationContext.AN_EMPTY_STRING;

import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -36,15 +40,20 @@ public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocation() throws E
final URL schemaFile = new URL("file:/path/to/raml/json/schema/group/some/path/some-schema-or-other.json");

final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json";
when(schemaIdParser.parse(schemaFile)).thenReturn(new URL(schemaId).toURI());

final SchemaDef schemaDef = schemaDefParser.parse(schemaFile, jsonSchemaPath);

assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is("group"));
assertThat(schemaDef.getBaseLocation(), is("group/"));
assertThat(schemaDef.getLocation(), is("some/path/some-schema-or-other.json"));
when(schemaIdParser.parse(schemaFile)).thenReturn(of(new URL(schemaId).toURI()));

final Optional<SchemaDef> schemaDefOptional = schemaDefParser.parse(schemaFile, jsonSchemaPath);

if (schemaDefOptional.isPresent()) {
final SchemaDef schemaDef = schemaDefOptional.get();
assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is("group"));
assertThat(schemaDef.getBaseLocation(), is("group/"));
assertThat(schemaDef.getLocation(), is("some/path/some-schema-or-other.json"));
} else {
fail();
}
}

@Test
Expand All @@ -54,15 +63,20 @@ public void shouldHandleSchemasAtTheRootOfTheJsonSchemaDirectory() throws Except
final URL schemaFile = new URL("file:/path/to/raml/json/schema/some-schema-or-other.json");

final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json";
when(schemaIdParser.parse(schemaFile)).thenReturn(new URL(schemaId).toURI());

final SchemaDef schemaDef = schemaDefParser.parse(schemaFile, jsonSchemaPath);

assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is(AN_EMPTY_STRING));
assertThat(schemaDef.getBaseLocation(), is(AN_EMPTY_STRING));
assertThat(schemaDef.getLocation(), is("some-schema-or-other.json"));
when(schemaIdParser.parse(schemaFile)).thenReturn(of(new URL(schemaId).toURI()));

final Optional<SchemaDef> schemaDefOptional = schemaDefParser.parse(schemaFile, jsonSchemaPath);

if (schemaDefOptional.isPresent()) {
final SchemaDef schemaDef = schemaDefOptional.get();
assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is(AN_EMPTY_STRING));
assertThat(schemaDef.getBaseLocation(), is(AN_EMPTY_STRING));
assertThat(schemaDef.getLocation(), is("some-schema-or-other.json"));
} else {
fail();
}
}

@Test
Expand All @@ -72,14 +86,32 @@ public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocationWithSubDire
final URL schemaFile = new URL("file:/path/to/raml/json/schema/a-sub-directory/another-sub-directory/some-schema-or-other.json");

final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json";
when(schemaIdParser.parse(schemaFile)).thenReturn(new URL(schemaId).toURI());
when(schemaIdParser.parse(schemaFile)).thenReturn(of(new URL(schemaId).toURI()));

final Optional<SchemaDef> schemaDefOptional = schemaDefParser.parse(schemaFile, jsonSchemaPath);

if (schemaDefOptional.isPresent()) {
final SchemaDef schemaDef = schemaDefOptional.get();

assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is("a-sub-directory"));
assertThat(schemaDef.getBaseLocation(), is("a-sub-directory/"));
assertThat(schemaDef.getLocation(), is("another-sub-directory/some-schema-or-other.json"));
} else {
fail();
}
}

@Test
public void shouldReturnEmptyIfTheSchemaHasNoId() throws Exception {
final Path jsonSchemaPath = Paths.get("json/schema/");
final URL schemaFile = new URL("file:/path/to/raml/json/schema/a-sub-directory/another-sub-directory/some-schema-or-other.json");

when(schemaIdParser.parse(schemaFile)).thenReturn(empty());

final SchemaDef schemaDef = schemaDefParser.parse(schemaFile, jsonSchemaPath);
final Optional<SchemaDef> schemaDefOptional = schemaDefParser.parse(schemaFile, jsonSchemaPath);

assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is("a-sub-directory"));
assertThat(schemaDef.getBaseLocation(), is("a-sub-directory/"));
assertThat(schemaDef.getLocation(), is("another-sub-directory/some-schema-or-other.json"));
assertThat(schemaDefOptional.isPresent(), is(false));
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,63 @@
package uk.gov.justice.schema.catalog.generation;

import static java.util.Optional.of;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.verify;

import uk.gov.justice.schema.catalog.util.UrlConverter;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Optional;

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;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class SchemaIdParserTest {

@Mock
private Logger logger;

@Spy
@SuppressWarnings("unused")
private final UrlConverter urlConverter = new UrlConverter();

@InjectMocks
private SchemaIdParser schemaIdParser;


@Test
public void shouldParseTheSchemaFileAndExtractItsId() throws Exception {

final URL schemaFile = getClass().getClassLoader().getResource("raml/json/schema/context/person.json");

final URI id = schemaIdParser.parse(schemaFile);
final Optional<URI> id = schemaIdParser.parse(schemaFile);

assertThat(id.toString(), is("http://justice.gov.uk/context/person.json"));
assertThat(id, is(of(new URI("http://justice.gov.uk/context/person.json"))));
}

@Test
public void shouldFailIfTheSchemaHasNoId() throws Exception {

final URL schemaFile = getClass().getClassLoader().getResource("dodgy-schemas/schema-with-missing-id.json");

try {
schemaIdParser.parse(schemaFile);
fail();
} catch (final CatalogGenerationException expected) {
assertThat(expected.getMessage(), startsWith("Failed to generate catalog. Schema 'file:"));
assertThat(expected.getMessage(), endsWith("/dodgy-schemas/schema-with-missing-id.json' has no id"));
}
assertThat(schemaIdParser.parse(schemaFile).isPresent(), is(false));

verify(logger).warn(argThat(startsWith("Failed to generate catalog. Schema 'file:")));
verify(logger).warn(argThat(endsWith("/dodgy-schemas/schema-with-missing-id.json' has no id")));
}

@Test
Expand Down

0 comments on commit 91a50c1

Please sign in to comment.