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

Add schema resolver test utility #32

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions catalog-test-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>json-schema-catalog</artifactId>
<groupId>uk.gov.justice.schema</groupId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>catalog-test-utils</artifactId>

<dependencies>
<dependency>
<groupId>uk.gov.justice.schema</groupId>
<artifactId>catalog-core</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.schema</groupId>
<artifactId>schema-example-standards</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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);
}
}
10 changes: 10 additions & 0 deletions catalog-test-utils/src/test/resources/json/add-recipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Eton Mess",
"glutenFree": false,
"ingredients": [
{
"name": "custard",
"quantity": "incorrect"
}
]
}
Original file line number Diff line number Diff line change
@@ -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"
]
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>catalog-generation-plugin-it</module>
<module>schema-service</module>
<module>schema-example-context</module>
<module>catalog-test-utils</module>
</modules>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down