From 74608cd8cbaaf81203f2a082e7ffe68599f626e9 Mon Sep 17 00:00:00 2001 From: amckenzie Date: Fri, 1 Dec 2017 10:34:26 +0000 Subject: [PATCH] add a test which loads a complex schema --- .../JsonStringToSchemaConverterTest.java | 32 ++++++++++++++++++- .../test/resources/catalog_test_valid.json | 8 ++--- .../src/test/resources/json/person.json | 12 +++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 catalog-core/src/test/resources/json/person.json diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java index a46822c..089d12e 100644 --- a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverterTest.java @@ -7,24 +7,54 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.InputStream; import java.net.URL; import org.apache.commons.io.IOUtils; +import org.everit.json.schema.Schema; import org.everit.json.schema.loader.SchemaClient; import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.runners.MockitoJUnitRunner; - @RunWith(MockitoJUnitRunner.class) public class JsonStringToSchemaConverterTest { @InjectMocks private JsonStringToSchemaConverter jsonStringToSchemaConverter; + @Test + public void shouldLoadASchemaWhichIncludesALocallyStoredSchemaFragment() 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 SchemaClient schemaClient = mock(SchemaClient.class); + + try(final InputStream inputStream = schemaFragmentUrl.openStream()) { + + when(schemaClient.get("http://justice.gov.uk/standards/complex_address.json")).thenReturn(inputStream); + + final String schemaJson = IOUtils.toString(mainSchemaUrl); + final Schema schema = jsonStringToSchemaConverter.convert(schemaJson, schemaClient); + + final String json = IOUtils.toString(jsonToVerifyUrl); + + schema.validate(new JSONObject(json)); + } + } + @Test public void shouldFailWithErrorMessageIfParsingTheJsonStringFails() throws Exception { diff --git a/catalog-core/src/test/resources/catalog_test_valid.json b/catalog-core/src/test/resources/catalog_test_valid.json index 2ad8039..b8bec69 100644 --- a/catalog-core/src/test/resources/catalog_test_valid.json +++ b/catalog-core/src/test/resources/catalog_test_valid.json @@ -1,11 +1,11 @@ { "catalog": { "name": "my catalog", - "group": [ + "groups": [ { "name": "standards", "baseLocation": "jar://CommonSchemas.jar!//context/schemas", - "schema": [ + "schemas": [ { "id": "http://justive.gov.uk/standards/complex_addressV1.json", "location": "standards/complex_addressV1.json" @@ -19,7 +19,7 @@ { "name": "staging interface", "baseLocation": "file:///context/schemas", - "schema": [ + "schemas": [ { "id": "http://justive.gov.uk/context/exhibitV1.json", "location": "context/eh.json" @@ -28,4 +28,4 @@ } ] } -} \ No newline at end of file +} diff --git a/catalog-core/src/test/resources/json/person.json b/catalog-core/src/test/resources/json/person.json new file mode 100644 index 0000000..b6546eb --- /dev/null +++ b/catalog-core/src/test/resources/json/person.json @@ -0,0 +1,12 @@ +{ + "nino": "the nino", + "name": "the name", + "correspondence_address": { + "addressline1": "21 Gasworks Lane", + "addressline2": "Croydon", + "city": "London", + "postcode": "EC3 2AB", + "country": "UK", + "ss": "fred" + } +}