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

Commit

Permalink
Merge pull request #55 from CJSCommonPlatform/report-schema-file-fail…
Browse files Browse the repository at this point in the history
…ed-parse

Report file that caused schema parse exception
  • Loading branch information
allanmckenzie committed Nov 1, 2019
2 parents b2c2fb8 + 72b4154 commit 0f502a5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## Unreleased

### Changed
- SchemaIdParser will now include which file caused a Json parsing exception

## [1.7.5] - 2019-10-23
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.stream.JsonParsingException;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -60,6 +61,8 @@ public Optional<URI> parse(final URL schemaFile) {

} catch (final IOException e) {
throw new CatalogGenerationException(format("Failed to extract id from schema file '%s'", schemaFile), e);
} catch (final JsonParsingException e) {
throw new CatalogGenerationException(format("Failed to parse schema file '%s'", schemaFile), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.argThat;
Expand All @@ -17,6 +18,8 @@
import java.net.URL;
import java.util.Optional;

import javax.json.stream.JsonParsingException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -73,4 +76,19 @@ public void shouldFailIfLoadingTheSchemaFileThrowsAnIOException() throws Excepti
assertThat(expected.getMessage(), is("Failed to extract id from schema file 'file:/this/file/does/not/exist.json'"));
}
}

@Test
public void shouldFailIfTheSchemaDoesNotParse() throws Exception {

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

try {
schemaIdParser.parse(schemaFile);
fail();
} catch (final CatalogGenerationException expected) {
assertThat(expected.getCause(), is(instanceOf(JsonParsingException.class)));
assertThat(expected.getMessage(), containsString("Failed to parse schema file '"));
assertThat(expected.getMessage(), containsString("dodgy-schemas/schema-with-missing-curly-brace.json'"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://justice.gov.uk/context/person.json",
"type": "object",
"properties": {
"nino": {
"type": "string"
},
"name": {
"type": "string"
},
"correspondence_address":
{"$ref" : "http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2"}

,
"required": [
"nino",
"name",
"correspondence_address"
]
}

0 comments on commit 0f502a5

Please sign in to comment.