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

Commit

Permalink
TP-960: Surfacing the file name and location responsible for breaking…
Browse files Browse the repository at this point in the history
… the POJO generation process
  • Loading branch information
Mahesh Subramanian committed Feb 15, 2019
1 parent 4b1ea24 commit 6b9513f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.json.Json.createReader;

import uk.gov.justice.schema.catalog.exception.InvalidJsonFileException;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path;
Expand All @@ -13,6 +15,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 All @@ -26,6 +29,7 @@ public class CatalogUpdater {
private static final Logger LOGGER = LoggerFactory.getLogger(CatalogUpdater.class);

private static final String CLASSPATH = "CLASSPATH";

/**
* Updates the cache with raw json schemas that are not on the classpath
*
Expand All @@ -38,14 +42,20 @@ public void updateRawCatalog(final Map<String, String> schemaIdsToRawJsonSchemaC
final Path updatedPath = Paths.get(format("%s/%s", basePath.toString(), path.toString()));

try {
if(!updatedPath.toString().contains(CLASSPATH)){
if (!updatedPath.toString().contains(CLASSPATH)) {
final String schema = IOUtils.toString(updatedPath.toUri().toURL(), UTF_8);
try (final JsonReader reader = createReader(new StringReader(schema))) {
final JsonObject jsonObject = reader.readObject();
try {
final JsonObject jsonObject = reader.readObject();

if (jsonObject.containsKey("id")) {
final String id = jsonObject.getString("id");
schemaIdsToRawJsonSchemaCache.put(id, schema);
if (jsonObject.containsKey("id")) {
final String id = jsonObject.getString("id");
schemaIdsToRawJsonSchemaCache.put(id, schema);
}
} catch (final JsonParsingException e) {
final String errorMessage = format("Unable to parse Json file: %s", updatedPath);
LOGGER.error(errorMessage);
throw new InvalidJsonFileException(errorMessage, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package uk.gov.justice.schema.catalog.exception;

public class InvalidJsonFileException extends RuntimeException {

public InvalidJsonFileException(final String errorMessage, final Throwable cause) {
super(errorMessage, cause);
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package uk.gov.justice.schema.catalog.util;

import static org.hamcrest.CoreMatchers.containsString;
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 uk.gov.justice.schema.catalog.exception.InvalidJsonFileException;

import java.io.File;
import java.io.IOException;
Expand All @@ -24,6 +24,8 @@
import java.util.HashMap;
import java.util.Map;

import javax.json.stream.JsonParsingException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -43,12 +45,12 @@ public class CatalogUpdaterTest {
private CatalogUpdater catalogUpdater;

@Test
public void shouldCacheFileWithReferenceToAnotherSchema(){
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" );
schemaIdsToRawJsonSchemaCache.put("http://justice.gov.uk/standards/address.json", "json schema");

rawCatalog.initialize();

Expand All @@ -73,7 +75,7 @@ public void shouldFailIfFileWithReferenceToAnotherSchemaHasNoId() throws Excepti
final Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();
final Path basePath = Paths.get((""));

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

rawCatalog.initialize();

Expand Down Expand Up @@ -110,4 +112,28 @@ public void shouldFailIfLoadingFileWithReferenceToAnotherSchemaThrowsAnIOExcepti
assertThat(expected.getMessage(), is("Failed to extract id from schema file 'file:/this/file/does/not/exist.json'"));
}
}

@Test
public void shouldThrowExceptionWhenParsingInvalidJsonFile() throws URISyntaxException {

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 incompleteJson = new File(this.getClass().getClassLoader().getResource("json/dodgy-schemas/incomplete-schema-missing-closing-brace.json").toURI());
paths.add(Paths.get(incompleteJson.toURI()));
try {
catalogUpdater.updateRawCatalog(schemaIdsToRawJsonSchemaCache, basePath, paths);
fail("Should have thrown an exception for parsing invalid json file");
} catch (final InvalidJsonFileException e) {
assertThat(e.getCause(), instanceOf(JsonParsingException.class));
assertThat(e.getMessage(), containsString("json/dodgy-schemas/incomplete-schema-missing-closing-brace.json"));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"id": "http://justice.gov.uk/context/person.json",
"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 6b9513f

Please sign in to comment.