Skip to content

Commit

Permalink
Allow customization of resource locations
Browse files Browse the repository at this point in the history
  • Loading branch information
trickl committed Feb 28, 2019
1 parent cea6f55 commit 43e20bf
Showing 1 changed file with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public abstract class AbstractObjectAssert<S extends AbstractObjectAssert<S>>
extends AbstractAssert<S, Object> {

private ObjectMapper objectMapper = new ObjectMapper();

private Path serializationResourcePath = null;

private URL deserializationResourceUrl = null;

public AbstractObjectAssert(Object actual, Class<?> selfType) {
super(actual, selfType);
Expand All @@ -33,11 +37,16 @@ public AbstractObjectAssert(Object actual, Class<?> selfType) {
* @throws IOException If any file errors occur
*/
public S jsonSerializationAsExpected() throws IOException {
if (serializationResourcePath == null) {
serializationResourcePath = classAsResourcePathConvention(
actual.getClass(), ".json");
}

String jsonString = serialize(actual);
com.trickl.assertj.core.api.Assertions.assertThat(json(jsonString))
.allowingAnyArrayOrdering()
.writeActualToFileOnFailure()
.isSameJsonAs(json(classAsResourcePathConvention(actual.getClass(), ".json")));
.isSameJsonAs(json(serializationResourcePath));
return myself;
}

Expand All @@ -47,18 +56,38 @@ public S jsonSerializationAsExpected() throws IOException {
* @throws IOException If any file errors occur
*/
public S jsonDeserializationAsExpected() throws IOException {
assertThat(
deserialize(
classAsResourceUrlConvention(actual.getClass(), ".json"), actual.getClass()))
if (deserializationResourceUrl == null) {
deserializationResourceUrl = classAsResourceUrlConvention(actual.getClass(), ".json");
}

assertThat(deserialize(deserializationResourceUrl, actual.getClass()))
.isEqualTo(actual);
return myself;
}

public S usingSerializationResourcePath(Path path) {
serializationResourcePath = path;
return myself;
}

public S usingDeserializationResourceUrl(URL url) {
deserializationResourceUrl = url;
return myself;
}

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

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);
}

private URL classAsResourceUrlConvention(Class clazz, String extension) {
String resourceName = clazz.getSimpleName() + extension;
return clazz.getResource(resourceName);
Expand All @@ -73,12 +102,4 @@ private Path classAsResourcePathConvention(Class clazz, String extension) {
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);
}
}

0 comments on commit 43e20bf

Please sign in to comment.