diff --git a/catalog-test-utils/pom.xml b/catalog-test-utils/pom.xml new file mode 100644 index 0000000..4840d7e --- /dev/null +++ b/catalog-test-utils/pom.xml @@ -0,0 +1,35 @@ + + + + json-schema-catalog + uk.gov.justice.schema + 1.3.0-SNAPSHOT + + + 4.0.0 + + catalog-test-utils + + + + uk.gov.justice.schema + catalog-core + ${project.version} + + + + + junit + junit + test + + + uk.gov.justice.schema + schema-example-standards + ${project.version} + test + + + \ No newline at end of file diff --git a/catalog-test-utils/src/main/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolver.java b/catalog-test-utils/src/main/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolver.java new file mode 100644 index 0000000..af709e6 --- /dev/null +++ b/catalog-test-utils/src/main/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolver.java @@ -0,0 +1,45 @@ +package uk.gov.justice.schema.catalog.test.utils; + +import uk.gov.justice.schema.catalog.CatalogObjectFactory; +import uk.gov.justice.schema.catalog.JsonToSchemaConverter; +import uk.gov.justice.schema.catalog.RawCatalog; +import uk.gov.justice.schema.catalog.client.SchemaClientFactory; + +import org.everit.json.schema.Schema; +import org.json.JSONObject; + +public class SchemaCatalogResolver { + + private final RawCatalog rawCatalog; + private final SchemaClientFactory schemaClientFactory; + private final JsonToSchemaConverter jsonStringToSchemaConverter; + + public SchemaCatalogResolver(final RawCatalog rawCatalog, + final SchemaClientFactory schemaClientFactory, + final JsonToSchemaConverter jsonStringToSchemaConverter) { + this.rawCatalog = rawCatalog; + this.schemaClientFactory = schemaClientFactory; + this.jsonStringToSchemaConverter = jsonStringToSchemaConverter; + } + + public Schema loadSchema(final JSONObject jsonSchema) { + return jsonStringToSchemaConverter.convert( + jsonSchema, + schemaClientFactory.create(rawCatalog)); + } + + public Schema loadSchema(final String jsonSchemaAsString) { + return jsonStringToSchemaConverter.convert( + jsonSchemaAsString, + schemaClientFactory.create(rawCatalog)); + } + + public static SchemaCatalogResolver schemaCatalogResolver() { + final CatalogObjectFactory catalogObjectFactory = new CatalogObjectFactory(); + + return new SchemaCatalogResolver( + catalogObjectFactory.rawCatalog(), + catalogObjectFactory.schemaClientFactory(), + catalogObjectFactory.jsonToSchemaConverter()); + } +} diff --git a/catalog-test-utils/src/test/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolverTest.java b/catalog-test-utils/src/test/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolverTest.java new file mode 100644 index 0000000..bda02ed --- /dev/null +++ b/catalog-test-utils/src/test/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolverTest.java @@ -0,0 +1,62 @@ +package uk.gov.justice.schema.catalog.test.utils; + +import static com.google.common.base.Charsets.UTF_8; +import static com.google.common.io.Resources.getResource; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static uk.gov.justice.schema.catalog.test.utils.SchemaCatalogResolver.schemaCatalogResolver; + +import java.io.IOException; +import java.util.List; + +import com.google.common.io.Resources; +import org.everit.json.schema.Schema; +import org.everit.json.schema.ValidationException; +import org.json.JSONObject; +import org.junit.Test; + +public class SchemaCatalogResolverTest { + + @Test + public void shouldResolveSchemaFromJsonObjectWithReferences() throws Exception { + try { + final Schema schema = schemaCatalogResolver() + .loadSchema(loadJsonObject("json/schema/example.add-recipe.json")); + + schema.validate(loadJsonObject("json/add-recipe.json")); + + fail(); + } catch (final ValidationException e) { + final List allMessages = e.getAllMessages(); + + assertThat(allMessages.size(), is(1)); + assertThat(allMessages.get(0), is("#/ingredients/0/quantity: expected type: Number, found: String")); + } + } + + @Test + public void shouldResolveSchemaFromJsonStringWithReferences() throws Exception { + try { + final Schema schema = schemaCatalogResolver() + .loadSchema(loadStringResource("json/schema/example.add-recipe.json")); + + schema.validate(loadJsonObject("json/add-recipe.json")); + + fail(); + } catch (final ValidationException e) { + final List allMessages = e.getAllMessages(); + + assertThat(allMessages.size(), is(1)); + assertThat(allMessages.get(0), is("#/ingredients/0/quantity: expected type: Number, found: String")); + } + } + + private JSONObject loadJsonObject(final String resourcePath) throws IOException { + return new JSONObject(loadStringResource(resourcePath)); + } + + private String loadStringResource(final String resourcePath) throws IOException { + return Resources.toString(getResource(resourcePath), UTF_8); + } +} \ No newline at end of file diff --git a/catalog-test-utils/src/test/resources/json/add-recipe.json b/catalog-test-utils/src/test/resources/json/add-recipe.json new file mode 100644 index 0000000..369eea4 --- /dev/null +++ b/catalog-test-utils/src/test/resources/json/add-recipe.json @@ -0,0 +1,10 @@ +{ + "name": "Eton Mess", + "glutenFree": false, + "ingredients": [ + { + "name": "custard", + "quantity": "incorrect" + } + ] +} diff --git a/catalog-test-utils/src/test/resources/json/schema/example.add-recipe.json b/catalog-test-utils/src/test/resources/json/schema/example.add-recipe.json new file mode 100644 index 0000000..59175d3 --- /dev/null +++ b/catalog-test-utils/src/test/resources/json/schema/example.add-recipe.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/example/cakeshop/example.add-recipe.json", + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Uniquely identifies the cake to be produced from the recipe", + "name": "Name of Cake", + "title": "Name of Cake" + }, + "glutenFree": { + "type": "boolean" + }, + "ingredients": { + "type": "array", + "items": [ + { + "$ref": "http://justice.gov.uk/example/standard/ingredient.json" + } + ], + "minItems": 1, + "description": "List ingredients and quantities for recipe" + } + }, + "required": [ + "name", + "ingredients", + "glutenFree" + ] +} diff --git a/pom.xml b/pom.xml index b6f4ded..abd0936 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ catalog-generation-plugin-it schema-service schema-example-context + catalog-test-utils diff --git a/schema-example-context/schema-example-command-api/src/raml/json/schema/example.add-recipe.json b/schema-example-context/schema-example-command-api/src/raml/json/schema/example.add-recipe.json index ab34659..59175d3 100644 --- a/schema-example-context/schema-example-command-api/src/raml/json/schema/example.add-recipe.json +++ b/schema-example-context/schema-example-command-api/src/raml/json/schema/example.add-recipe.json @@ -16,7 +16,7 @@ "type": "array", "items": [ { - "id": "http://justice.gov.uk/example/standard/ingredient.json" + "$ref": "http://justice.gov.uk/example/standard/ingredient.json" } ], "minItems": 1,