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

Commit

Permalink
Add convenience method to JsonToSchemaConverter API
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Jan 5, 2018
1 parent 3142baa commit 75988d7
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class Catalog {

private final RawCatalog rawCatalog;
private final SchemaClientFactory schemaClientFactory;
private final JsonStringToSchemaConverter jsonStringToSchemaConverter;
private final JsonToSchemaConverter jsonToSchemaConverter;

public Catalog(
final RawCatalog rawCatalog,
final SchemaClientFactory schemaClientFactory,
final JsonStringToSchemaConverter jsonStringToSchemaConverter) {
final JsonToSchemaConverter jsonToSchemaConverter) {
this.rawCatalog = rawCatalog;
this.schemaClientFactory = schemaClientFactory;
this.jsonStringToSchemaConverter = jsonStringToSchemaConverter;
this.jsonToSchemaConverter = jsonToSchemaConverter;
}

/**
Expand All @@ -58,7 +58,7 @@ public Optional<Schema> getSchema(final String schemaId) {
final Optional<String> rawJsonSchema = rawCatalog.getRawJsonSchema(schemaId);

if (rawJsonSchema.isPresent()) {
final Schema schema = jsonStringToSchemaConverter.convert(
final Schema schema = jsonToSchemaConverter.convert(
rawJsonSchema.get(),
schemaClientFactory.create(rawCatalog));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public UriResolver uriResolver() {
}

/**
* @return a new instance of {@link JsonStringToSchemaConverter}
* @return a new instance of {@link JsonToSchemaConverter}
*/
public JsonStringToSchemaConverter jsonStringToSchemaConverter() {
return new JsonStringToSchemaConverter();
public JsonToSchemaConverter jsonToSchemaConverter() {
return new JsonToSchemaConverter();
}

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ public Catalog catalog() {
return new Catalog(
rawCatalog(),
schemaClientFactory(),
jsonStringToSchemaConverter());
jsonToSchemaConverter());
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package uk.gov.justice.schema.catalog;

import static java.lang.String.format;

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
* Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas
* in the raw Json Schema to their location on the classpath
*/
public class JsonToSchemaConverter {

/**
* Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas
* in the raw Json Schema to their location on the classpath
*
* @param schemaJson The raw Json Schema as a {@link String}
* @param schemaClient An implementation of Everit's {@link SchemaClient} that
* rather than returning a schema found at the Schema id,
* returns a Schema found on the classpath mapped to that
* Schema id
* @return A fully resolved Everit {@link Schema}
*/
public Schema convert(final String schemaJson, final SchemaClient schemaClient) {
try {
final JSONObject schemaJsonObject = new JSONObject(new JSONTokener(schemaJson));
return convert(schemaJsonObject, schemaClient);
} catch (final JSONException e) {
throw new SchemaCatalogException(format("Failed to convert schema json to Schema Object. Schema json:%n%s", schemaJson), e);
}
}

/**
* Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas
* in the raw Json Schema to their location on the classpath
*
* @param schemaJsonObject The raw Json Schema as a {@link JSONObject}
* @param schemaClient An implementation of Everit's {@link SchemaClient} that
* rather than returning a schema found at the Schema id,
* returns a Schema found on the classpath mapped to that
* Schema id
* @return A fully resolved Everit {@link Schema}
*/
public Schema convert(final JSONObject schemaJsonObject, final SchemaClient schemaClient) {
return SchemaLoader.builder()
.schemaJson(schemaJsonObject)
.httpClient(schemaClient)
.build()
.load()
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void shouldCreateAUriResolver() throws Exception {
@Test
public void shouldCreateAJsonStringToSchemaConverter() throws Exception {

final JsonStringToSchemaConverter jsonStringToSchemaConverter = catalogObjectFactory.jsonStringToSchemaConverter();
assertThat(jsonStringToSchemaConverter, is(notNullValue()));
final JsonToSchemaConverter jsonToSchemaConverter = catalogObjectFactory.jsonToSchemaConverter();
assertThat(jsonToSchemaConverter, is(notNullValue()));
}

@Test
Expand Down Expand Up @@ -155,8 +155,8 @@ public void shouldCreateCatalog() throws Exception {
final SchemaClientFactory schemaClientFactory = getPrivateField("schemaClientFactory", catalog, SchemaClientFactory.class);
assertThat(schemaClientFactory, is(notNullValue()));

final JsonStringToSchemaConverter jsonStringToSchemaConverter = getPrivateField("jsonStringToSchemaConverter", catalog, JsonStringToSchemaConverter.class);
assertThat(jsonStringToSchemaConverter, is(notNullValue()));
final JsonToSchemaConverter jsonToSchemaConverter = getPrivateField("jsonToSchemaConverter", catalog, JsonToSchemaConverter.class);
assertThat(jsonToSchemaConverter, is(notNullValue()));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void shouldCreateTheCatalog() throws Exception {

assertThat(getPrivateField(catalog, "rawCatalog", RawCatalog.class), is(notNullValue()));
assertThat(getPrivateField(catalog, "schemaClientFactory", SchemaClientFactory.class), is(notNullValue()));
assertThat(getPrivateField(catalog, "jsonStringToSchemaConverter", JsonStringToSchemaConverter.class), is(notNullValue()));
assertThat(getPrivateField(catalog, "jsonToSchemaConverter", JsonToSchemaConverter.class), is(notNullValue()));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CatalogTest {
private SchemaClientFactory schemaClientFactory;

@Mock
private JsonStringToSchemaConverter jsonStringToSchemaConverter;
private JsonToSchemaConverter jsonToSchemaConverter;

@InjectMocks
private Catalog catalog;
Expand All @@ -44,7 +44,7 @@ public void shouldLoadTheRawJsonSchemaAndConvertToAFullSchemaObject() throws Exc

when(rawCatalog.getRawJsonSchema(schemaId)).thenReturn(of(rawJsonSchema));
when(schemaClientFactory.create(rawCatalog)).thenReturn(localFileSystemSchemaClient);
when(jsonStringToSchemaConverter.convert(rawJsonSchema, localFileSystemSchemaClient)).thenReturn(schema);
when(jsonToSchemaConverter.convert(rawJsonSchema, localFileSystemSchemaClient)).thenReturn(schema);

assertThat(catalog.getSchema(schemaId), is(of(schema)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class JsonStringToSchemaConverterTest {
public class JsonToSchemaConverterTest {

@InjectMocks
private JsonStringToSchemaConverter jsonStringToSchemaConverter;
private JsonToSchemaConverter jsonToSchemaConverter;

@Test
public void shouldLoadASchemaWhichIncludesALocallyStoredSchemaFragment() throws Exception {
public void shouldLoadASchemaWhichIncludesALocallyStoredSchemaFragmentFromJsonAsString() throws Exception {

final URL mainSchemaUrl = getClass().getClassLoader().getResource("json/schema/context/person.json");
final URL schemaFragmentUrl = getClass().getClassLoader().getResource("json/schema/standards/complex_address.json");
Expand All @@ -48,7 +48,35 @@ public void shouldLoadASchemaWhichIncludesALocallyStoredSchemaFragment() throws
when(localFileSystemSchemaClient.get("http://justice.gov.uk/standards/complex_address.json")).thenReturn(inputStream);

final String schemaJson = IOUtils.toString(mainSchemaUrl);
final Schema schema = jsonStringToSchemaConverter.convert(schemaJson, localFileSystemSchemaClient);
final Schema schema = jsonToSchemaConverter.convert(schemaJson, localFileSystemSchemaClient);

final String json = IOUtils.toString(jsonToVerifyUrl);

schema.validate(new JSONObject(json));
}
}

@Test
public void shouldLoadASchemaWhichIncludesALocallyStoredSchemaFragmentFromJsonObject() throws Exception {

final URL mainSchemaUrl = getClass().getClassLoader().getResource("json/schema/context/person.json");
final URL schemaFragmentUrl = getClass().getClassLoader().getResource("json/schema/standards/complex_address.json");

final URL jsonToVerifyUrl = getClass().getClassLoader().getResource("json/person.json");

assertThat(mainSchemaUrl, is(notNullValue()));
assertThat(schemaFragmentUrl, is(notNullValue()));
assertThat(jsonToVerifyUrl, is(notNullValue()));

final LocalFileSystemSchemaClient localFileSystemSchemaClient = mock(LocalFileSystemSchemaClient.class);

try(final InputStream inputStream = schemaFragmentUrl.openStream()) {

when(localFileSystemSchemaClient.get("http://justice.gov.uk/standards/complex_address.json")).thenReturn(inputStream);

final String schemaJson = IOUtils.toString(mainSchemaUrl);

final Schema schema = jsonToSchemaConverter.convert(new JSONObject(schemaJson), localFileSystemSchemaClient);

final String json = IOUtils.toString(jsonToVerifyUrl);

Expand All @@ -66,7 +94,7 @@ public void shouldFailWithErrorMessageIfParsingTheJsonStringFails() throws Excep
final String schemaJson = IOUtils.toString(url);

try {
jsonStringToSchemaConverter.convert(schemaJson, mock(LocalFileSystemSchemaClient.class));
jsonToSchemaConverter.convert(schemaJson, mock(LocalFileSystemSchemaClient.class));
fail();
} catch (final SchemaCatalogException expected) {
assertThat(expected.getCause(), is(instanceOf(JSONException.class)));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<properties>
<cpp.repo.name>json-schema-catalog</cpp.repo.name>
<common-bom.version>1.21.0</common-bom.version>
<utilities.version>1.10.0</utilities.version>
<utilities.version>1.11.0</utilities.version>
<generator-maven-plugin.version>2.3.0</generator-maven-plugin.version>
</properties>

Expand Down

0 comments on commit 75988d7

Please sign in to comment.