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

Commit

Permalink
add a test which loads a complex schema
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Dec 1, 2017
1 parent 5f244c5 commit 74608cd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
8 changes: 4 additions & 4 deletions catalog-core/src/test/resources/catalog_test_valid.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -28,4 +28,4 @@
}
]
}
}
}
12 changes: 12 additions & 0 deletions catalog-core/src/test/resources/json/person.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 74608cd

Please sign in to comment.