From ed071a62bcb69160311e6c271f3bd6b7f35967b7 Mon Sep 17 00:00:00 2001 From: Tim Gee Date: Thu, 28 Feb 2019 18:34:16 +0000 Subject: [PATCH] Add json schema validation --- pom.xml | 5 +++ .../core/api/json/AbstractObjectAssert.java | 37 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 12c9a0d..6096783 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,11 @@ jackson-databind 2.9.8 + + com.fasterxml.jackson.module + jackson-module-jsonSchema + 2.9.8 + junit junit diff --git a/src/main/java/com/trickl/assertj/core/api/json/AbstractObjectAssert.java b/src/main/java/com/trickl/assertj/core/api/json/AbstractObjectAssert.java index 50b3429..c0df12d 100644 --- a/src/main/java/com/trickl/assertj/core/api/json/AbstractObjectAssert.java +++ b/src/main/java/com/trickl/assertj/core/api/json/AbstractObjectAssert.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jsonSchema.JsonSchema; +import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; import java.io.IOException; import java.net.URL; import java.nio.file.Path; @@ -23,6 +25,8 @@ public abstract class AbstractObjectAssert> private ObjectMapper objectMapper = new ObjectMapper(); + private Path jsonSchemaResourcePath = null; + private Path serializationResourcePath = null; private URL deserializationResourceUrl = null; @@ -57,13 +61,39 @@ public S jsonSerializationAsExpected() throws IOException { */ public S jsonDeserializationAsExpected() throws IOException { if (deserializationResourceUrl == null) { - deserializationResourceUrl = classAsResourceUrlConvention(actual.getClass(), ".json"); + deserializationResourceUrl = classAsResourceUrlConvention( + actual.getClass(), ".example.json"); } assertThat(deserialize(deserializationResourceUrl, actual.getClass())) .isEqualTo(actual); return myself; } + + /** + * Check the json schema of the object matches the expected output. + * @return A new assertion object + * @throws IOException If any file errors occur + */ + public S jsonSchemaAsExpected() throws IOException { + if (jsonSchemaResourcePath == null) { + jsonSchemaResourcePath = classAsResourcePathConvention( + actual.getClass(), ".schema.json"); + } + + JsonSchema jsonSchema = jsonSchema(actual); + String jsonString = jsonSchema.toString(); + com.trickl.assertj.core.api.Assertions.assertThat(json(jsonString)) + .allowingAnyArrayOrdering() + .writeActualToFileOnFailure() + .isSameJsonAs(json(jsonSchemaResourcePath)); + return myself; + } + + public S usingJsonSchemaResourcePath(Path path) { + jsonSchemaResourcePath = path; + return myself; + } public S usingSerializationResourcePath(Path path) { serializationResourcePath = path; @@ -88,6 +118,11 @@ private String serialize(Object obj) throws JsonProcessingException { return objectMapper.writeValueAsString(obj); } + private JsonSchema jsonSchema(Object obj) throws JsonProcessingException { + JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(objectMapper); + return schemaGen.generateSchema(actual.getClass()); + } + private URL classAsResourceUrlConvention(Class clazz, String extension) { String resourceName = clazz.getSimpleName() + extension; return clazz.getResource(resourceName);