This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
45 changes: 45 additions & 0 deletions
45
...t-utils/src/main/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ils/src/test/java/uk/gov/justice/schema/catalog/test/utils/SchemaCatalogResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
catalog-test-utils/src/test/resources/json/add-recipe.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "Eton Mess", | ||
"glutenFree": false, | ||
"ingredients": [ | ||
{ | ||
"name": "custard", | ||
"quantity": "incorrect" | ||
} | ||
] | ||
} |
31 changes: 31 additions & 0 deletions
31
catalog-test-utils/src/test/resources/json/schema/example.add-recipe.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters