Skip to content

Instantly share code, notes, and snippets.

@martypitt
Last active December 20, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martypitt/6183822 to your computer and use it in GitHub Desktop.
Save martypitt/6183822 to your computer and use it in GitHub Desktop.
These tests fail, demonstrating either a bug somewhere in the Jackson / Spring-Data-Redis stack, or a configuration problem within my code. It requires Lombok to run
package com.mangofactory.example
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.Data;
import org.junit.Test;
import org.springframework.data.redis.hash.DecoratingStringHashMapper;
import org.springframework.data.redis.hash.JacksonHashMapper;
import com.google.common.collect.Lists;
public class JacksonSerializerTests {
@Test
public void nullLongIsHandledCorrectly()
{
DecoratingStringHashMapper<EntityWithId> mapper = mapperOf(EntityWithId.class);
EntityWithId entity = new EntityWithId();
// Encode & decode
Map<String, String> hash = mapper.toHash(entity);
EntityWithId decoded = mapper.fromHash(hash);
assertThat(decoded, notNullValue());
assertThat(decoded.getId(), nullValue());
}
@Test
public void listOfEnumsDecodedCorrectly()
{
DecoratingStringHashMapper<EntityWithListOfEnums> mapper = mapperOf(EntityWithListOfEnums.class);
EntityWithListOfEnums entity = new EntityWithListOfEnums();
// Encode & decode
Map<String, String> hash = mapper.toHash(entity);
EntityWithListOfEnums decoded = mapper.fromHash(hash);
assertThat(decoded, notNullValue());
assertThat(decoded.getRoles(), hasSize(1));
assertThat(decoded.getRoles(), contains(UserRole.USER));
}
@Test
public void emptyListDecodedCorreclty()
{
DecoratingStringHashMapper<EntityWithListOfEnums> mapper = mapperOf(EntityWithListOfEnums.class);
EntityWithListOfEnums entity = new EntityWithListOfEnums();
List<UserRole> roles = Collections.emptyList();
entity.setRoles(roles);
// Encode & decode
Map<String, String> hash = mapper.toHash(entity);
EntityWithListOfEnums decoded = mapper.fromHash(hash);
assertThat(decoded, notNullValue());
assertThat(decoded.getRoles(), notNullValue());
assertThat(decoded.getRoles(), hasSize(0));
}
@Test
public void listOfStringsDecodedCorrectly()
{
DecoratingStringHashMapper<EntityWithListOfStrings> mapper = mapperOf(EntityWithListOfStrings.class);
EntityWithListOfStrings entity = new EntityWithListOfStrings();
// Encode & decode
Map<String, String> hash = mapper.toHash(entity);
EntityWithListOfStrings decoded = mapper.fromHash(hash);
assertThat(decoded, notNullValue());
assertThat(decoded.getRoles(), hasSize(1));
assertThat(decoded.getRoles(), contains("Hello","World"));
}
@Test
public void emptyListOfStringsDecodedCorrectly()
{
DecoratingStringHashMapper<EntityWithListOfStrings> mapper = mapperOf(EntityWithListOfStrings.class);
EntityWithListOfStrings entity = new EntityWithListOfStrings();
List<String> roles = Collections.emptyList();
entity.setRoles(roles);
// Encode & decode
Map<String, String> hash = mapper.toHash(entity);
EntityWithListOfStrings decoded = mapper.fromHash(hash);
assertThat(decoded, notNullValue());
assertThat(decoded.getRoles(), hasSize(1));
assertThat(decoded.getRoles(), contains("Hello","World"));
}
@Data
public static class EntityWithId
{
Long id;
String otherField = "Hello"; // so there's something to serialize
}
@Data
public static class EntityWithListOfEnums
{
List<UserRole> roles = Lists.newArrayList(UserRole.USER);
}
@Data
public static class EntityWithListOfStrings
{
List<String> roles = Lists.newArrayList("Hello","World");
}
public enum UserRole
{
USER,ADMIN;
}
private <T> DecoratingStringHashMapper<T> mapperOf(Class<T> clazz)
{
return new DecoratingStringHashMapper<T>(new JacksonHashMapper<T>(clazz));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment