I found a way to unit test the Deserializers, was quite some trouble to figure out. Have a look at my repo https://bitbucket.org/arbeitsgruppedenktmit/de.denktmit.rest.hal
The unit test class is here:
de.denktmit.rest.hal / src / test / java / de / denktmit / rest / hal / jackson / RelationDeserializerTest.java
An integration test, for testing the Deserializer in context can be found here:
de.denktmit.rest.hal / src / test / java / de / denktmit / rest / hal / OrderResourceIntegrationTest.java
EDIT due to comment
Base class to setup mapper for easy unit testing
public abstract class AbstractJackson2MarshallingTest {
protected ObjectMapper mapper;
@Before
public void setUp() {
mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
protected String write(Object object) throws Exception {
Writer writer = new StringWriter();
mapper.writeValue(writer, object);
return writer.toString();
}
protected <T> T read(String source, Class<T> targetType) throws Exception {
return mapper.readValue(source, targetType);
}
protected String getPackagePath() {
return "/" + this.getClass().getPackage().getName().replace('.', '/');
}
}
The test class containing the unit tests
public class RelationDeserializerTest extends AbstractJackson2MarshallingIntegrationTest {
@Rule public ResourceFile resourceFile = new ResourceFile(getPackagePath() + "/single_valued_relation.json");
@Rule public ResourceFile resourceFile2 = new ResourceFile(getPackagePath() + "/multi_valued_relation.json");
private RelationDeserializer deserializer = new RelationDeserializer();
@Test
public void testDeserializeSingleValuedRelation() throws IOException {
resourceFile = new ResourceFile(getPackagePath() + "/single_valued_relation.json");
JsonParser parser = mapper.getFactory().createParser(resourceFile.getContent());
DeserializationContext ctxt = mapper.getDeserializationContext();
SingleValuedRelation rel = (SingleValuedRelation) deserializer.deserialize(parser, ctxt);
assertEquals(rel.getName(), "invoiceAddress");
assertEquals("invoiceAddressURL", rel.getLink().getHref());
assertEquals("linkName", rel.getLink().getName());
assertEquals("de", rel.getLink().getHreflang());
assertNull(parser.nextToken());
}
@Test
public void testDeserializeMultiValuedRelation() throws IOException {
resourceFile = new ResourceFile(getPackagePath() + "/multi_valued_relation.json");
JsonParser parser = mapper.getFactory().createParser(resourceFile.getContent());
DeserializationContext ctxt = mapper.getDeserializationContext();
MultiValuedRelation rel = (MultiValuedRelation) deserializer.deserialize(parser, ctxt);
assertEquals(rel.getName(), "images");
Iterator<Link> linkIterator = rel.getLinks().iterator();
Link link = linkIterator.next();
assertEquals("imageUrl1", link.getHref());
link = linkIterator.next();
assertEquals("imageUrl2", link.getHref());
assertNull(parser.nextToken());
}
}
The class under test
public class RelationDeserializer extends StdDeserializer<Relation> {
public RelationDeserializer() {
super(Relation.class);
}
@Override
public Relation deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
if (p.getCurrentToken() == null && p.nextToken() == null) {
String msg = getClass().getCanonicalName()
+ ": Can not deserialize without token";
throw new IOException(msg);
}
if (p.getCurrentToken() != JsonToken.START_OBJECT
&& p.getCurrentToken() != JsonToken.START_ARRAY) {
String msg = getClass().getCanonicalName()
+ ": Expected data to start with an Relation object or an array of Relation objects";
throw new IOException(msg);
}
if (p.nextToken() != JsonToken.FIELD_NAME) {
String msg = getClass().getCanonicalName()
+ ": Expected relation to be started by a field name";
throw new IOException(msg);
}
String relationName = p.getText();
JsonToken tok = p.nextToken();
Relation rel;
switch (tok) {
case START_ARRAY:
rel = createMultiValuedRelation(relationName, p);
break;
case START_OBJECT:
rel = createSingleValuedRelation(relationName, p);
break;
default:
String msg = getClass().getCanonicalName() + "Expected relation content is a single link or array of links";
throw new IOException(msg);
}
p.nextToken();
return rel;
}
private Relation createMultiValuedRelation(String relationName, JsonParser p)
throws JsonParseException, IOException {
List<Link> links = new ArrayList<Link>();
if (p.nextToken() == JsonToken.START_OBJECT) {
Iterator<DefaultLink> linkIterator = p.readValuesAs(DefaultLink.class);
while (linkIterator.hasNext()) {
links.add(linkIterator.next());
}
}
if (p.getCurrentToken() != JsonToken.END_ARRAY) {
String msg = getClass().getCanonicalName() + "Expected relation content is a single link or (possibly empty) array of links";
throw new IOException(msg);
}
return RelationFactory.createRelation(relationName, links);
}
private Relation createSingleValuedRelation(String relationName,
JsonParser p) throws JsonParseException, IOException {
return RelationFactory.createRelation(relationName, p.readValueAs(DefaultLink.class));
}
}
Hope that helps and best regards