Skip to content

Commit

Permalink
Add json schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
trickl committed Feb 28, 2019
1 parent 43e20bf commit ed071a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +25,8 @@ public abstract class AbstractObjectAssert<S extends AbstractObjectAssert<S>>

private ObjectMapper objectMapper = new ObjectMapper();

private Path jsonSchemaResourcePath = null;

private Path serializationResourcePath = null;

private URL deserializationResourceUrl = null;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit ed071a6

Please sign in to comment.