Skip to content

Commit c19dbb2

Browse files
christophstroblmhalbritter
authored andcommitted
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
1 parent a2260c6 commit c19dbb2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: data/data-redis/src/appTest/java/com/example/data/redis/DataRedisApplicationAotTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ void redisDataStructure(AssertableOutput output) {
4040
});
4141
}
4242

43+
@Test
44+
void jackson2hashMapper(AssertableOutput output) {
45+
46+
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
47+
assertThat(output).hasLineMatching("hash-mapper-default-raw: .*firstname=hashed-fn.*");
48+
assertThat(output).hasSingleLineContaining(
49+
"hash-mapper-default-mapped: Person{firstname='hashed-fn', lastname='hashed-ln'}");
50+
assertThat(output).hasLineMatching("hash-mapper-flat-raw: .*firstname=hashed-fn.*");
51+
assertThat(output).hasSingleLineContaining(
52+
"hash-mapper-flat-mapped: Person{firstname='hashed-fn', lastname='hashed-ln'}");
53+
});
54+
}
55+
4356
@Test
4457
void jsonSerializer(AssertableOutput output) {
4558
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {

Diff for: data/data-redis/src/main/java/com/example/data/redis/CLR.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.example.data.redis;
22

33
import java.util.Arrays;
4+
import java.util.Map;
45

56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.CommandLineRunner;
78
import org.springframework.data.redis.core.BoundValueOperations;
89
import org.springframework.data.redis.core.RedisCallback;
910
import org.springframework.data.redis.core.RedisTemplate;
1011
import org.springframework.data.redis.core.StringRedisTemplate;
12+
import org.springframework.data.redis.hash.Jackson2HashMapper;
1113
import org.springframework.data.redis.listener.ChannelTopic;
1214
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
1315
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@@ -35,6 +37,8 @@ public void run(String... args) throws Exception {
3537
templateOperations();
3638
keyBoundOperations();
3739
redisBackedSet();
40+
hashMapper(HashStructure.DEFAULT);
41+
hashMapper(HashStructure.FLAT);
3842
jsonSerializer();
3943
pubSub();
4044

@@ -67,6 +71,16 @@ private void redisBackedSet() {
6771
System.out.printf("redis set: %s%n", redisSet.iterator().next());
6872
}
6973

74+
private void hashMapper(HashStructure structure) {
75+
76+
Jackson2HashMapper hashMapper = new Jackson2HashMapper(HashStructure.FLAT.equals(structure));
77+
template.opsForHash().putAll("hash", hashMapper.toHash(new Person("hashed-fn", "hashed-ln")));
78+
79+
Map<String, Object> hashedEntry = template.<String, Object>opsForHash().entries("hash");
80+
System.out.printf("hash-mapper-%s-raw: %s%n", structure, hashedEntry);
81+
System.out.printf("hash-mapper-%s-mapped: %s%n", structure, hashMapper.fromHash(hashedEntry));
82+
}
83+
7084
private void keyBoundOperations() {
7185
BoundValueOperations<String, String> keyBoundOps = template.boundValueOps("bound-key");
7286
keyBoundOps.set("OK");
@@ -107,4 +121,15 @@ private void pubSub() throws InterruptedException {
107121
System.out.printf("pub/sub: %s%n", messageHandler.receivedMessages());
108122
}
109123

124+
enum HashStructure {
125+
126+
DEFAULT, FLAT;
127+
128+
@Override
129+
public String toString() {
130+
return name().toLowerCase();
131+
}
132+
133+
}
134+
110135
}

0 commit comments

Comments
 (0)