Skip to content

Commit

Permalink
Add assertions for Json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
trickl committed Feb 28, 2019
1 parent 2efb173 commit cea6f55
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/trickl/assertj/core/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.trickl.assertj.core.api.json.JsonAssert;
import com.trickl.assertj.core.api.json.JsonContainer;
import com.trickl.assertj.core.api.json.ObjectAssert;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -19,6 +20,10 @@ public static JsonAssert assertThat(JsonElement json) {
return new JsonAssert(new JsonContainer(json.toString()));
}

public static ObjectAssert assertThat(Object object) {
return new ObjectAssert(object);
}

public static JsonContainer json(String str) {
return new JsonContainer(str);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public abstract class AbstractJsonAssert<S extends AbstractJsonAssert<S>>
extends AbstractAssert<S, JsonContainer> {

@VisibleForTesting Json json = Json.instance();

@VisibleForTesting JSONComparator defaultComparator = new DefaultComparator(NON_EXTENSIBLE);

private boolean extensible = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.trickl.assertj.core.api.json;

import static com.trickl.assertj.core.api.Assertions.json;
import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.assertj.core.api.AbstractAssert;

/**
* Base class for all json related assertions on objects.
*
* @param <S> the "self" type of this assertion class. Please read &quot;<a
* href="http://bit.ly/1IZIRcY" target="_blank">Emulating 'self types' using Java Generics to
* simplify fluent API implementation</a>&quot; for more details.
*/
public abstract class AbstractObjectAssert<S extends AbstractObjectAssert<S>>
extends AbstractAssert<S, Object> {

private ObjectMapper objectMapper = new ObjectMapper();

public AbstractObjectAssert(Object actual, Class<?> selfType) {
super(actual, selfType);
}

/**
* Check the json serialization of the object matches the expected output.
* @return A new assertion object
* @throws IOException If any file errors occur
*/
public S jsonSerializationAsExpected() throws IOException {
String jsonString = serialize(actual);
com.trickl.assertj.core.api.Assertions.assertThat(json(jsonString))
.allowingAnyArrayOrdering()
.writeActualToFileOnFailure()
.isSameJsonAs(json(classAsResourcePathConvention(actual.getClass(), ".json")));
return myself;
}

/**
* Check the json deserialization of the object matches the expected output.
* @return A new assertion object
* @throws IOException If any file errors occur
*/
public S jsonDeserializationAsExpected() throws IOException {
assertThat(
deserialize(
classAsResourceUrlConvention(actual.getClass(), ".json"), actual.getClass()))
.isEqualTo(actual);
return myself;
}

public S usingObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return myself;
}

private URL classAsResourceUrlConvention(Class clazz, String extension) {
String resourceName = clazz.getSimpleName() + extension;
return clazz.getResource(resourceName);
}

private Path classAsResourcePathConvention(Class clazz, String extension) {
String resourcePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
String projectDir = resourcePath.substring(0, resourcePath.indexOf("target"));
return Paths.get(
projectDir,
"src/test/resources/",
clazz.getPackage().getName().replaceAll("\\.", "/"),
clazz.getSimpleName() + extension);
}

private <T> T deserialize(URL value, Class<T> clazz) throws JsonProcessingException, IOException {
return objectMapper.readValue(value, (Class<T>) clazz);
}

private String serialize(Object obj) throws JsonProcessingException {
return objectMapper.writeValueAsString(obj);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/trickl/assertj/core/api/json/ObjectAssert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.trickl.assertj.core.api.json;

/*
* Assertions for directories {@link File}s.
*/
public class ObjectAssert extends AbstractObjectAssert<ObjectAssert> {
public ObjectAssert(Object object) {
super(object, ObjectAssert.class);
}
}

0 comments on commit cea6f55

Please sign in to comment.