@@ -167,15 +167,12 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
|
||||
Map<String, Object> delta = this.delta.getAllValues().get(0);
|
||||
assertThat(delta.size()).isEqualTo(3);
|
||||
assertThat(delta.get(ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY))
|
||||
assertThat(delta.get(RedisSessionMapper.CREATION_TIME_KEY))
|
||||
.isEqualTo(newSession.getCreationTime().toEpochMilli());
|
||||
assertThat(delta
|
||||
.get(ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY))
|
||||
.isEqualTo(
|
||||
(int) newSession.getMaxInactiveInterval().getSeconds());
|
||||
assertThat(delta
|
||||
.get(ReactiveRedisOperationsSessionRepository.LAST_ACCESSED_TIME_KEY))
|
||||
.isEqualTo(newSession.getLastAccessedTime().toEpochMilli());
|
||||
assertThat(delta.get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY))
|
||||
.isEqualTo((int) newSession.getMaxInactiveInterval().getSeconds());
|
||||
assertThat(delta.get(RedisSessionMapper.LAST_ACCESSED_TIME_KEY))
|
||||
.isEqualTo(newSession.getLastAccessedTime().toEpochMilli());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -214,7 +211,7 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
verifyZeroInteractions(this.hashOperations);
|
||||
|
||||
assertThat(this.delta.getAllValues().get(0))
|
||||
.isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
.isEqualTo(map(RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
session.getLastAccessedTime().toEpochMilli()));
|
||||
}
|
||||
|
||||
@@ -316,16 +313,14 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
expected.setLastAccessedTime(Instant.now().minusSeconds(60));
|
||||
expected.setAttribute(attribute1, "test");
|
||||
expected.setAttribute(attribute2, null);
|
||||
Map map = map(
|
||||
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attribute1,
|
||||
Map map = map(RedisSessionMapper.ATTRIBUTE_PREFIX + attribute1,
|
||||
expected.getAttribute(attribute1),
|
||||
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attribute2,
|
||||
expected.getAttribute(attribute2),
|
||||
ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY,
|
||||
RedisSessionMapper.ATTRIBUTE_PREFIX + attribute2,
|
||||
expected.getAttribute(attribute2), RedisSessionMapper.CREATION_TIME_KEY,
|
||||
expected.getCreationTime().toEpochMilli(),
|
||||
ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY,
|
||||
RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY,
|
||||
(int) expected.getMaxInactiveInterval().getSeconds(),
|
||||
ReactiveRedisOperationsSessionRepository.LAST_ACCESSED_TIME_KEY,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
expected.getLastAccessedTime().toEpochMilli());
|
||||
given(this.hashOperations.entries(anyString()))
|
||||
.willReturn(Flux.fromIterable(map.entrySet()));
|
||||
@@ -360,9 +355,9 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void getSessionExpired() {
|
||||
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
|
||||
Map map = map(ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY, 0L,
|
||||
ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY, 1,
|
||||
ReactiveRedisOperationsSessionRepository.LAST_ACCESSED_TIME_KEY,
|
||||
Map map = map(RedisSessionMapper.CREATION_TIME_KEY, 0L,
|
||||
RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli());
|
||||
given(this.hashOperations.entries(anyString()))
|
||||
.willReturn(Flux.fromIterable(map.entrySet()));
|
||||
|
||||
@@ -190,14 +190,12 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
Map<String, Object> delta = getDelta();
|
||||
assertThat(delta.size()).isEqualTo(3);
|
||||
Object creationTime = delta
|
||||
.get(RedisOperationsSessionRepository.CREATION_TIME_ATTR);
|
||||
Object creationTime = delta.get(RedisSessionMapper.CREATION_TIME_KEY);
|
||||
assertThat(creationTime).isEqualTo(session.getCreationTime().toEpochMilli());
|
||||
assertThat(delta.get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR))
|
||||
.isEqualTo((int) Duration
|
||||
.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)
|
||||
assertThat(delta.get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY)).isEqualTo(
|
||||
(int) Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)
|
||||
.getSeconds());
|
||||
assertThat(delta.get(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR))
|
||||
assertThat(delta.get(RedisSessionMapper.LAST_ACCESSED_TIME_KEY))
|
||||
.isEqualTo(session.getCreationTime().toEpochMilli());
|
||||
}
|
||||
|
||||
@@ -283,9 +281,8 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
this.redisRepository.save(session);
|
||||
|
||||
assertThat(getDelta())
|
||||
.isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
session.getLastAccessedTime().toEpochMilli()));
|
||||
assertThat(getDelta()).isEqualTo(map(RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
session.getLastAccessedTime().toEpochMilli()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -364,12 +361,11 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
given(this.redisOperations.boundSetOps(anyString()))
|
||||
.willReturn(this.boundSetOperations);
|
||||
Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName),
|
||||
expected.getAttribute(attrName),
|
||||
RedisOperationsSessionRepository.CREATION_TIME_ATTR,
|
||||
expected.getAttribute(attrName), RedisSessionMapper.CREATION_TIME_KEY,
|
||||
expected.getCreationTime().toEpochMilli(),
|
||||
RedisOperationsSessionRepository.MAX_INACTIVE_ATTR,
|
||||
RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY,
|
||||
(int) expected.getMaxInactiveInterval().getSeconds(),
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
expected.getLastAccessedTime().toEpochMilli());
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
given(this.redisOperations.boundSetOps(anyString()))
|
||||
@@ -378,7 +374,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
String id = expected.getId();
|
||||
this.redisRepository.deleteById(id);
|
||||
|
||||
assertThat(getDelta().get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR))
|
||||
assertThat(getDelta().get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY))
|
||||
.isEqualTo(0);
|
||||
verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id));
|
||||
verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id));
|
||||
@@ -418,12 +414,11 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attribute1),
|
||||
expected.getAttribute(attribute1),
|
||||
RedisOperationsSessionRepository.getSessionAttrNameKey(attribute2),
|
||||
expected.getAttribute(attribute2),
|
||||
RedisOperationsSessionRepository.CREATION_TIME_ATTR,
|
||||
expected.getAttribute(attribute2), RedisSessionMapper.CREATION_TIME_KEY,
|
||||
expected.getCreationTime().toEpochMilli(),
|
||||
RedisOperationsSessionRepository.MAX_INACTIVE_ATTR,
|
||||
RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY,
|
||||
(int) expected.getMaxInactiveInterval().getSeconds(),
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
expected.getLastAccessedTime().toEpochMilli());
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
|
||||
@@ -447,8 +442,8 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
String expiredId = "expired-id";
|
||||
given(this.redisOperations.boundHashOps(getKey(expiredId)))
|
||||
.willReturn(this.boundHashOperations);
|
||||
Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 1,
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
Map map = map(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli());
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
|
||||
@@ -464,8 +459,8 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
.willReturn(Collections.singleton(expiredId));
|
||||
given(this.redisOperations.boundHashOps(getKey(expiredId)))
|
||||
.willReturn(this.boundHashOperations);
|
||||
Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 1,
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
Map map = map(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli());
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
|
||||
@@ -486,11 +481,9 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
.willReturn(Collections.singleton(sessionId));
|
||||
given(this.redisOperations.boundHashOps(getKey(sessionId)))
|
||||
.willReturn(this.boundHashOperations);
|
||||
Map map = map(RedisOperationsSessionRepository.CREATION_TIME_ATTR,
|
||||
createdTime.toEpochMilli(),
|
||||
RedisOperationsSessionRepository.MAX_INACTIVE_ATTR,
|
||||
(int) maxInactive.getSeconds(),
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
Map map = map(RedisSessionMapper.CREATION_TIME_KEY, createdTime.toEpochMilli(),
|
||||
RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY,
|
||||
(int) maxInactive.getSeconds(), RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
lastAccessed.toEpochMilli());
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
|
||||
@@ -572,8 +565,8 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
String deletedId = "deleted-id";
|
||||
given(this.redisOperations.boundHashOps(getKey(deletedId)))
|
||||
.willReturn(this.boundHashOperations);
|
||||
Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 0,
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
Map map = map(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 0,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5));
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
|
||||
@@ -625,8 +618,8 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
String expiredId = "expired-id";
|
||||
given(this.redisOperations.boundHashOps(getKey(expiredId)))
|
||||
.willReturn(this.boundHashOperations);
|
||||
Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 1,
|
||||
RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
Map map = map(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1,
|
||||
RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5));
|
||||
given(this.boundHashOperations.entries()).willReturn(map);
|
||||
|
||||
@@ -753,14 +746,12 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
Map<String, Object> delta = getDelta();
|
||||
assertThat(delta.size()).isEqualTo(3);
|
||||
Object creationTime = delta
|
||||
.get(RedisOperationsSessionRepository.CREATION_TIME_ATTR);
|
||||
Object creationTime = delta.get(RedisSessionMapper.CREATION_TIME_KEY);
|
||||
assertThat(creationTime).isEqualTo(session.getCreationTime().toEpochMilli());
|
||||
assertThat(delta.get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR))
|
||||
.isEqualTo((int) Duration
|
||||
.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)
|
||||
assertThat(delta.get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY)).isEqualTo(
|
||||
(int) Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)
|
||||
.getSeconds());
|
||||
assertThat(delta.get(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR))
|
||||
assertThat(delta.get(RedisSessionMapper.LAST_ACCESSED_TIME_KEY))
|
||||
.isEqualTo(session.getCreationTime().toEpochMilli());
|
||||
}
|
||||
|
||||
@@ -777,8 +768,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
this.redisRepository.createSession();
|
||||
Map<String, Object> delta = getDelta();
|
||||
assertThat(delta.size()).isEqualTo(3);
|
||||
assertThat(delta.get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR))
|
||||
.isEqualTo(60);
|
||||
assertThat(delta.get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY)).isEqualTo(60);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -858,9 +848,8 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
Map<String, Object> delta = getDelta(2);
|
||||
assertThat(delta.size()).isEqualTo(1);
|
||||
assertThat(delta)
|
||||
.isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR,
|
||||
session.getLastAccessedTime().toEpochMilli()));
|
||||
assertThat(delta).isEqualTo(map(RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
|
||||
session.getLastAccessedTime().toEpochMilli()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.session.data.redis;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.session.MapSession;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link RedisSessionMapper}.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
class RedisSessionMapperTests {
|
||||
|
||||
private RedisSessionMapper mapper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.mapper = new RedisSessionMapper("id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_NullId_ShouldThrowException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new RedisSessionMapper(null))
|
||||
.withMessage("sessionId must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_EmptyId_ShouldThrowException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new RedisSessionMapper(" "))
|
||||
.withMessage("sessionId must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply_NullMap_ShouldThrowException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.mapper.apply(null))
|
||||
.withMessage("map must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply_EmptyMap_ShouldThrowException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> this.mapper.apply(Collections.emptyMap()))
|
||||
.withMessage("map must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply_MapWithoutCreationTime_ShouldThrowException() {
|
||||
Map<String, Object> sessionMap = new HashMap<>();
|
||||
sessionMap.put(RedisSessionMapper.LAST_ACCESSED_TIME_KEY, 0L);
|
||||
sessionMap.put(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1800);
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.mapper.apply(sessionMap))
|
||||
.withMessage(
|
||||
RedisSessionMapper.CREATION_TIME_KEY + " key must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply_MapWithoutLastAccessedTime_ShouldThrowException() {
|
||||
Map<String, Object> sessionMap = new HashMap<>();
|
||||
sessionMap.put(RedisSessionMapper.CREATION_TIME_KEY, 0L);
|
||||
sessionMap.put(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1800);
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.mapper.apply(sessionMap))
|
||||
.withMessage(RedisSessionMapper.LAST_ACCESSED_TIME_KEY
|
||||
+ " key must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply_MapWithoutMaxInactiveInterval_ShouldThrowException() {
|
||||
Map<String, Object> sessionMap = new HashMap<>();
|
||||
sessionMap.put(RedisSessionMapper.CREATION_TIME_KEY, 0L);
|
||||
sessionMap.put(RedisSessionMapper.LAST_ACCESSED_TIME_KEY, 0L);
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.mapper.apply(sessionMap))
|
||||
.withMessage(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY
|
||||
+ " key must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply_ValidMap_ShouldReturnSession() {
|
||||
Map<String, Object> sessionMap = new HashMap<>();
|
||||
sessionMap.put(RedisSessionMapper.CREATION_TIME_KEY, 0L);
|
||||
sessionMap.put(RedisSessionMapper.LAST_ACCESSED_TIME_KEY, 0L);
|
||||
sessionMap.put(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1800);
|
||||
sessionMap.put(RedisSessionMapper.ATTRIBUTE_PREFIX + "existing", "value");
|
||||
sessionMap.put(RedisSessionMapper.ATTRIBUTE_PREFIX + "missing", null);
|
||||
MapSession session = this.mapper.apply(sessionMap);
|
||||
assertThat(session.getId()).isEqualTo("id");
|
||||
assertThat(session.getCreationTime()).isEqualTo(Instant.ofEpochMilli(0));
|
||||
assertThat(session.getLastAccessedTime()).isEqualTo(Instant.ofEpochMilli(0));
|
||||
assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofMinutes(30));
|
||||
assertThat(session.getAttributeNames()).hasSize(1);
|
||||
assertThat((String) session.getAttribute("existing")).isEqualTo("value");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user