-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assertions for Json serialization
- Loading branch information
Showing
5 changed files
with
104 additions
and
2 deletions.
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
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
84 changes: 84 additions & 0 deletions
84
src/main/java/com/trickl/assertj/core/api/json/AbstractObjectAssert.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,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 "<a | ||
* href="http://bit.ly/1IZIRcY" target="_blank">Emulating 'self types' using Java Generics to | ||
* simplify fluent API implementation</a>" 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
10
src/main/java/com/trickl/assertj/core/api/json/ObjectAssert.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,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); | ||
} | ||
} |