Fix attribute mapping in ReactiveRedisOperationsSessionRepository

This commit ensures that attributes with null values are correctly mapped to session on retrieval from Redis.

Closes gh-1035
This commit is contained in:
Vedran Pavic
2018-04-19 15:31:08 +02:00
parent 6f8359ba16
commit 91b4efc5bd
3 changed files with 25 additions and 15 deletions

View File

@@ -22,7 +22,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@@ -149,8 +148,7 @@ public class ReactiveRedisOperationsSessionRepository implements
String sessionKey = getSessionKey(id); String sessionKey = getSessionKey(id);
return this.sessionRedisOperations.opsForHash().entries(sessionKey) return this.sessionRedisOperations.opsForHash().entries(sessionKey)
.collect( .collectMap(e -> e.getKey().toString(), Map.Entry::getValue)
Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue))
.filter(map -> !map.isEmpty()).map(new SessionMapper(id)) .filter(map -> !map.isEmpty()).map(new SessionMapper(id))
.filter(session -> !session.isExpired()).map(RedisSession::new) .filter(session -> !session.isExpired()).map(RedisSession::new)
.switchIfEmpty(Mono.defer(() -> deleteById(id).then(Mono.empty()))); .switchIfEmpty(Mono.defer(() -> deleteById(id).then(Mono.empty())));

View File

@@ -305,13 +305,17 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void getSessionFound() { public void getSessionFound() {
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
String attrName = "attrName"; String attribute1 = "attribute1";
String attribute2 = "attribute2";
MapSession expected = new MapSession("test"); MapSession expected = new MapSession("test");
expected.setLastAccessedTime(Instant.now().minusSeconds(60)); expected.setLastAccessedTime(Instant.now().minusSeconds(60));
expected.setAttribute(attrName, "attrValue"); expected.setAttribute(attribute1, "test");
expected.setAttribute(attribute2, null);
Map map = map( Map map = map(
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attrName, ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attribute1,
expected.getAttribute(attrName), expected.getAttribute(attribute1),
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attribute2,
expected.getAttribute(attribute2),
ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY, ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY,
expected.getCreationTime().toEpochMilli(), expected.getCreationTime().toEpochMilli(),
ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY, ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY,
@@ -330,8 +334,10 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
assertThat(session.getId()).isEqualTo(expected.getId()); assertThat(session.getId()).isEqualTo(expected.getId());
assertThat(session.getAttributeNames()) assertThat(session.getAttributeNames())
.isEqualTo(expected.getAttributeNames()); .isEqualTo(expected.getAttributeNames());
assertThat(session.<String>getAttribute(attrName)) assertThat(session.<String>getAttribute(attribute1))
.isEqualTo(expected.getAttribute(attrName)); .isEqualTo(expected.getAttribute(attribute1));
assertThat(session.<String>getAttribute(attribute2))
.isEqualTo(expected.getAttribute(attribute2));
assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime()); assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
assertThat(session.getMaxInactiveInterval()) assertThat(session.getMaxInactiveInterval())
.isEqualTo(expected.getMaxInactiveInterval()); .isEqualTo(expected.getMaxInactiveInterval());

View File

@@ -370,14 +370,18 @@ public class RedisOperationsSessionRepositoryTests {
@Test @Test
public void getSessionFound() { public void getSessionFound() {
String attrName = "attrName"; String attribute1 = "attribute1";
String attribute2 = "attribute2";
MapSession expected = new MapSession(); MapSession expected = new MapSession();
expected.setLastAccessedTime(Instant.now().minusSeconds(60)); expected.setLastAccessedTime(Instant.now().minusSeconds(60));
expected.setAttribute(attrName, "attrValue"); expected.setAttribute(attribute1, "test");
expected.setAttribute(attribute2, null);
given(this.redisOperations.boundHashOps(getKey(expected.getId()))) given(this.redisOperations.boundHashOps(getKey(expected.getId())))
.willReturn(this.boundHashOperations); .willReturn(this.boundHashOperations);
Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attribute1),
expected.getAttribute(attrName), expected.getAttribute(attribute1),
RedisOperationsSessionRepository.getSessionAttrNameKey(attribute2),
expected.getAttribute(attribute2),
RedisOperationsSessionRepository.CREATION_TIME_ATTR, RedisOperationsSessionRepository.CREATION_TIME_ATTR,
expected.getCreationTime().toEpochMilli(), expected.getCreationTime().toEpochMilli(),
RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, RedisOperationsSessionRepository.MAX_INACTIVE_ATTR,
@@ -389,8 +393,10 @@ public class RedisOperationsSessionRepositoryTests {
RedisSession session = this.redisRepository.findById(expected.getId()); RedisSession session = this.redisRepository.findById(expected.getId());
assertThat(session.getId()).isEqualTo(expected.getId()); assertThat(session.getId()).isEqualTo(expected.getId());
assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames()); assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
assertThat(session.<String>getAttribute(attrName)) assertThat(session.<String>getAttribute(attribute1))
.isEqualTo(expected.getAttribute(attrName)); .isEqualTo(expected.getAttribute(attribute1));
assertThat(session.<String>getAttribute(attribute2))
.isEqualTo(expected.getAttribute(attribute2));
assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime()); assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
assertThat(session.getMaxInactiveInterval()) assertThat(session.getMaxInactiveInterval())
.isEqualTo(expected.getMaxInactiveInterval()); .isEqualTo(expected.getMaxInactiveInterval());