Add test for data-redis Jackson2 backed hash mapper

Data Redis HashMapper relies on internal Jackson2 node types.

Related to: spring-projects/spring-data-redis#2838

See gh-204
This commit is contained in:
Christoph Strobl
2024-02-02 12:03:57 +01:00
committed by Moritz Halbritter
parent a2260c6e89
commit c19dbb2584
2 changed files with 38 additions and 0 deletions

View File

@@ -40,6 +40,19 @@ class DataRedisApplicationAotTests {
});
}
@Test
void jackson2hashMapper(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasLineMatching("hash-mapper-default-raw: .*firstname=hashed-fn.*");
assertThat(output).hasSingleLineContaining(
"hash-mapper-default-mapped: Person{firstname='hashed-fn', lastname='hashed-ln'}");
assertThat(output).hasLineMatching("hash-mapper-flat-raw: .*firstname=hashed-fn.*");
assertThat(output).hasSingleLineContaining(
"hash-mapper-flat-mapped: Person{firstname='hashed-fn', lastname='hashed-ln'}");
});
}
@Test
void jsonSerializer(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {

View File

@@ -1,6 +1,7 @@
package com.example.data.redis;
import java.util.Arrays;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
@@ -8,6 +9,7 @@ import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@@ -35,6 +37,8 @@ class CLR implements CommandLineRunner {
templateOperations();
keyBoundOperations();
redisBackedSet();
hashMapper(HashStructure.DEFAULT);
hashMapper(HashStructure.FLAT);
jsonSerializer();
pubSub();
@@ -67,6 +71,16 @@ class CLR implements CommandLineRunner {
System.out.printf("redis set: %s%n", redisSet.iterator().next());
}
private void hashMapper(HashStructure structure) {
Jackson2HashMapper hashMapper = new Jackson2HashMapper(HashStructure.FLAT.equals(structure));
template.opsForHash().putAll("hash", hashMapper.toHash(new Person("hashed-fn", "hashed-ln")));
Map<String, Object> hashedEntry = template.<String, Object>opsForHash().entries("hash");
System.out.printf("hash-mapper-%s-raw: %s%n", structure, hashedEntry);
System.out.printf("hash-mapper-%s-mapped: %s%n", structure, hashMapper.fromHash(hashedEntry));
}
private void keyBoundOperations() {
BoundValueOperations<String, String> keyBoundOps = template.boundValueOps("bound-key");
keyBoundOps.set("OK");
@@ -107,4 +121,15 @@ class CLR implements CommandLineRunner {
System.out.printf("pub/sub: %s%n", messageHandler.receivedMessages());
}
enum HashStructure {
DEFAULT, FLAT;
@Override
public String toString() {
return name().toLowerCase();
}
}
}