Add support for session save mode
This commit introduces SaveMode enum that individual SessionRepository implementations use to allow customization of the way they handle save operation in terms of tracking delta of changes. Resolves: #1466
This commit is contained in:
@@ -32,6 +32,7 @@ import reactor.test.StepVerifier;
|
||||
import org.springframework.data.redis.core.ReactiveHashOperations;
|
||||
import org.springframework.data.redis.core.ReactiveRedisOperations;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.SaveMode;
|
||||
import org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepository.RedisSession;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
@@ -130,7 +131,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
|
||||
RedisSession newSession = this.repository.new RedisSession();
|
||||
RedisSession newSession = this.repository.new RedisSession(new MapSession(), true);
|
||||
StepVerifier.create(this.repository.save(newSession)).verifyComplete();
|
||||
|
||||
verify(this.redisOperations).opsForHash();
|
||||
@@ -154,7 +155,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
|
||||
RedisSession session = this.repository.new RedisSession(new MapSession(this.cached));
|
||||
RedisSession session = this.repository.new RedisSession(this.cached, false);
|
||||
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
|
||||
@@ -170,7 +171,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
|
||||
RedisSession session = this.repository.new RedisSession(this.cached);
|
||||
RedisSession session = this.repository.new RedisSession(this.cached, false);
|
||||
session.setLastAccessedTime(Instant.ofEpochMilli(12345678L));
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
|
||||
@@ -193,7 +194,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
|
||||
String attrName = "attrName";
|
||||
RedisSession session = this.repository.new RedisSession(this.cached);
|
||||
RedisSession session = this.repository.new RedisSession(this.cached, false);
|
||||
session.setAttribute(attrName, "attrValue");
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
|
||||
@@ -216,7 +217,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
|
||||
String attrName = "attrName";
|
||||
RedisSession session = this.repository.new RedisSession(new MapSession());
|
||||
RedisSession session = this.repository.new RedisSession(new MapSession(), false);
|
||||
session.removeAttribute(attrName);
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
|
||||
@@ -234,7 +235,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
@Test
|
||||
void redisSessionGetAttributes() {
|
||||
String attrName = "attrName";
|
||||
RedisSession session = this.repository.new RedisSession(this.cached);
|
||||
RedisSession session = this.repository.new RedisSession(this.cached, false);
|
||||
assertThat(session.getAttributeNames()).isEmpty();
|
||||
|
||||
session.setAttribute(attrName, "attrValue");
|
||||
@@ -325,7 +326,7 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
|
||||
@Test // gh-1120
|
||||
void getAttributeNamesAndRemove() {
|
||||
RedisSession session = this.repository.new RedisSession(this.cached);
|
||||
RedisSession session = this.repository.new RedisSession(this.cached, false);
|
||||
session.setAttribute("attribute1", "value1");
|
||||
session.setAttribute("attribute2", "value2");
|
||||
|
||||
@@ -336,6 +337,78 @@ class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
assertThat(session.getAttributeNames()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeOnSetAttribute() {
|
||||
given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
|
||||
given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
this.repository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE);
|
||||
MapSession delegate = new MapSession();
|
||||
delegate.setAttribute("attribute1", "value1");
|
||||
delegate.setAttribute("attribute2", "value2");
|
||||
delegate.setAttribute("attribute3", "value3");
|
||||
RedisSession session = this.repository.new RedisSession(delegate, false);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
verify(this.redisOperations).hasKey(anyString());
|
||||
verify(this.redisOperations).opsForHash();
|
||||
verify(this.hashOperations).putAll(anyString(), this.delta.capture());
|
||||
assertThat(this.delta.getValue()).hasSize(1);
|
||||
verify(this.redisOperations).expire(anyString(), any());
|
||||
verifyZeroInteractions(this.redisOperations);
|
||||
verifyZeroInteractions(this.hashOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeOnGetAttribute() {
|
||||
given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
|
||||
given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);
|
||||
MapSession delegate = new MapSession();
|
||||
delegate.setAttribute("attribute1", "value1");
|
||||
delegate.setAttribute("attribute2", "value2");
|
||||
delegate.setAttribute("attribute3", "value3");
|
||||
RedisSession session = this.repository.new RedisSession(delegate, false);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
verify(this.redisOperations).hasKey(anyString());
|
||||
verify(this.redisOperations).opsForHash();
|
||||
verify(this.hashOperations).putAll(anyString(), this.delta.capture());
|
||||
assertThat(this.delta.getValue()).hasSize(2);
|
||||
verify(this.redisOperations).expire(anyString(), any());
|
||||
verifyZeroInteractions(this.redisOperations);
|
||||
verifyZeroInteractions(this.hashOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeAlways() {
|
||||
given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
|
||||
given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true));
|
||||
given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
|
||||
this.repository.setSaveMode(SaveMode.ALWAYS);
|
||||
MapSession delegate = new MapSession();
|
||||
delegate.setAttribute("attribute1", "value1");
|
||||
delegate.setAttribute("attribute2", "value2");
|
||||
delegate.setAttribute("attribute3", "value3");
|
||||
RedisSession session = this.repository.new RedisSession(delegate, false);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
StepVerifier.create(this.repository.save(session)).verifyComplete();
|
||||
verify(this.redisOperations).hasKey(anyString());
|
||||
verify(this.redisOperations).opsForHash();
|
||||
verify(this.hashOperations).putAll(anyString(), this.delta.capture());
|
||||
assertThat(this.delta.getValue()).hasSize(3);
|
||||
verify(this.redisOperations).expire(anyString(), any());
|
||||
verifyZeroInteractions(this.redisOperations);
|
||||
verifyZeroInteractions(this.hashOperations);
|
||||
}
|
||||
|
||||
private Map<String, Object> map(Object... objects) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (objects == null) {
|
||||
|
||||
@@ -48,6 +48,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.FlushMode;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.SaveMode;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession;
|
||||
import org.springframework.session.events.AbstractSessionEvent;
|
||||
@@ -146,7 +147,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
|
||||
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached);
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached, false);
|
||||
session.setLastAccessedTime(session.getLastAccessedTime());
|
||||
String originalId = session.getId();
|
||||
String changeSessionId = session.changeSessionId();
|
||||
@@ -194,7 +195,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
// gh-467
|
||||
@Test
|
||||
void saveSessionNothingChanged() {
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached);
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached, false);
|
||||
|
||||
this.redisRepository.save(session);
|
||||
|
||||
@@ -230,7 +231,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
@Test
|
||||
void saveJavadoc() {
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached);
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached, false);
|
||||
session.setLastAccessedTime(session.getLastAccessedTime());
|
||||
|
||||
given(this.redisOperations.boundHashOps("spring:session:sessions:session-id"))
|
||||
@@ -252,7 +253,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
@Test
|
||||
void saveLastAccessChanged() {
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession(this.cached));
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached, false);
|
||||
session.setLastAccessedTime(Instant.ofEpochMilli(12345678L));
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
@@ -267,7 +268,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
@Test
|
||||
void saveSetAttribute() {
|
||||
String attrName = "attrName";
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession(), false);
|
||||
session.setAttribute(attrName, "attrValue");
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
@@ -282,7 +283,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
@Test
|
||||
void saveRemoveAttribute() {
|
||||
String attrName = "attrName";
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession(), false);
|
||||
session.removeAttribute(attrName);
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
@@ -295,7 +296,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
@Test
|
||||
void saveExpired() {
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession(), false);
|
||||
session.setMaxInactiveInterval(Duration.ZERO);
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
@@ -310,8 +311,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
@Test
|
||||
void redisSessionGetAttributes() {
|
||||
String attrName = "attrName";
|
||||
RedisSession session = this.redisRepository.new RedisSession(
|
||||
Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
|
||||
RedisSession session = this.redisRepository.createSession();
|
||||
assertThat(session.getAttributeNames()).isEmpty();
|
||||
session.setAttribute(attrName, "attrValue");
|
||||
assertThat(session.getAttributeNames()).containsOnly(attrName);
|
||||
@@ -746,7 +746,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
void changeRedisNamespace() {
|
||||
String namespace = "foo:bar";
|
||||
this.redisRepository.setRedisKeyNamespace(namespace);
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession(), false);
|
||||
session.setMaxInactiveInterval(Duration.ZERO);
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
@@ -772,7 +772,7 @@ class RedisOperationsSessionRepositoryTests {
|
||||
|
||||
@Test // gh-1120
|
||||
void getAttributeNamesAndRemove() {
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached);
|
||||
RedisSession session = this.redisRepository.new RedisSession(this.cached, false);
|
||||
session.setAttribute("attribute1", "value1");
|
||||
session.setAttribute("attribute2", "value2");
|
||||
|
||||
@@ -836,6 +836,57 @@ class RedisOperationsSessionRepositoryTests {
|
||||
verifyZeroInteractions(this.publisher);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeOnSetAttribute() {
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
|
||||
this.redisRepository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE);
|
||||
MapSession delegate = new MapSession();
|
||||
delegate.setAttribute("attribute1", "value1");
|
||||
delegate.setAttribute("attribute2", "value2");
|
||||
delegate.setAttribute("attribute3", "value3");
|
||||
RedisSession session = this.redisRepository.new RedisSession(delegate, false);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
this.redisRepository.save(session);
|
||||
assertThat(getDelta()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeOnGetAttribute() {
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
|
||||
this.redisRepository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);
|
||||
MapSession delegate = new MapSession();
|
||||
delegate.setAttribute("attribute1", "value1");
|
||||
delegate.setAttribute("attribute2", "value2");
|
||||
delegate.setAttribute("attribute3", "value3");
|
||||
RedisSession session = this.redisRepository.new RedisSession(delegate, false);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
this.redisRepository.save(session);
|
||||
assertThat(getDelta()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeAlways() {
|
||||
given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
|
||||
given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
|
||||
this.redisRepository.setSaveMode(SaveMode.ALWAYS);
|
||||
MapSession delegate = new MapSession();
|
||||
delegate.setAttribute("attribute1", "value1");
|
||||
delegate.setAttribute("attribute2", "value2");
|
||||
delegate.setAttribute("attribute3", "value3");
|
||||
RedisSession session = this.redisRepository.new RedisSession(delegate, false);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
this.redisRepository.save(session);
|
||||
assertThat(getDelta()).hasSize(3);
|
||||
}
|
||||
|
||||
private String getKey(String id) {
|
||||
return "spring:session:sessions:" + id;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.session.FlushMode;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.SaveMode;
|
||||
import org.springframework.session.data.redis.SimpleRedisOperationsSessionRepository.RedisSession;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
@@ -125,6 +126,19 @@ class SimpleRedisOperationsSessionRepositoryTests {
|
||||
.withMessage("flushMode must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void setSaveMode_ValidSaveMode_ShouldSetSaveMode() {
|
||||
this.sessionRepository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);
|
||||
assertThat(ReflectionTestUtils.getField(this.sessionRepository, "saveMode"))
|
||||
.isEqualTo(SaveMode.ON_GET_ATTRIBUTE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setSaveMode_NullSaveMode_ShouldThrowException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.sessionRepository.setSaveMode(null))
|
||||
.withMessage("saveMode must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSession_DefaultMaxInactiveInterval_ShouldCreateSession() {
|
||||
RedisSession redisSession = this.sessionRepository.createSession();
|
||||
@@ -223,6 +237,69 @@ class SimpleRedisOperationsSessionRepositoryTests {
|
||||
verifyNoMoreInteractions(this.sessionHashOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void save_WithSaveModeOnSetAttribute_SholdSaveSession() {
|
||||
given(this.sessionRedisOperations.hasKey(eq(TEST_SESSION_KEY))).willReturn(true);
|
||||
this.sessionRepository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE);
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
attributes.put("attribute1", "value1");
|
||||
attributes.put("attribute2", "value2");
|
||||
attributes.put("attribute3", "value3");
|
||||
RedisSession session = createTestSession(attributes);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
this.sessionRepository.save(session);
|
||||
verify(this.sessionRedisOperations).hasKey(eq(TEST_SESSION_KEY));
|
||||
verify(this.sessionRedisOperations).opsForHash();
|
||||
verify(this.sessionRedisOperations).expireAt(eq(TEST_SESSION_KEY), eq(getExpiry(session)));
|
||||
verify(this.sessionHashOperations).putAll(eq(TEST_SESSION_KEY), this.delta.capture());
|
||||
assertThat(this.delta.getValue()).hasSize(1);
|
||||
verifyNoMoreInteractions(this.sessionRedisOperations);
|
||||
verifyNoMoreInteractions(this.sessionHashOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeOnGetAttribute() {
|
||||
given(this.sessionRedisOperations.hasKey(eq(TEST_SESSION_KEY))).willReturn(true);
|
||||
this.sessionRepository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
attributes.put("attribute1", "value1");
|
||||
attributes.put("attribute2", "value2");
|
||||
attributes.put("attribute3", "value3");
|
||||
RedisSession session = createTestSession(attributes);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
this.sessionRepository.save(session);
|
||||
verify(this.sessionRedisOperations).hasKey(eq(TEST_SESSION_KEY));
|
||||
verify(this.sessionRedisOperations).opsForHash();
|
||||
verify(this.sessionRedisOperations).expireAt(eq(TEST_SESSION_KEY), eq(getExpiry(session)));
|
||||
verify(this.sessionHashOperations).putAll(eq(TEST_SESSION_KEY), this.delta.capture());
|
||||
assertThat(this.delta.getValue()).hasSize(2);
|
||||
verifyNoMoreInteractions(this.sessionRedisOperations);
|
||||
verifyNoMoreInteractions(this.sessionHashOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWithSaveModeAlways() {
|
||||
given(this.sessionRedisOperations.hasKey(eq(TEST_SESSION_KEY))).willReturn(true);
|
||||
this.sessionRepository.setSaveMode(SaveMode.ALWAYS);
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
attributes.put("attribute1", "value1");
|
||||
attributes.put("attribute2", "value2");
|
||||
attributes.put("attribute3", "value3");
|
||||
RedisSession session = createTestSession(attributes);
|
||||
session.getAttribute("attribute2");
|
||||
session.setAttribute("attribute3", "value4");
|
||||
this.sessionRepository.save(session);
|
||||
verify(this.sessionRedisOperations).hasKey(eq(TEST_SESSION_KEY));
|
||||
verify(this.sessionRedisOperations).opsForHash();
|
||||
verify(this.sessionRedisOperations).expireAt(eq(TEST_SESSION_KEY), eq(getExpiry(session)));
|
||||
verify(this.sessionHashOperations).putAll(eq(TEST_SESSION_KEY), this.delta.capture());
|
||||
assertThat(this.delta.getValue()).hasSize(3);
|
||||
verifyNoMoreInteractions(this.sessionRedisOperations);
|
||||
verifyNoMoreInteractions(this.sessionHashOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void save_SessionNotExists_ShouldThrowException() {
|
||||
RedisSession session = createTestSession();
|
||||
@@ -315,12 +392,16 @@ class SimpleRedisOperationsSessionRepositoryTests {
|
||||
return result;
|
||||
}
|
||||
|
||||
private RedisSession createTestSession() {
|
||||
private RedisSession createTestSession(Map<String, Object> attributes) {
|
||||
MapSession cached = new MapSession(TEST_SESSION_ID);
|
||||
cached.setCreationTime(Instant.EPOCH);
|
||||
cached.setLastAccessedTime(Instant.EPOCH);
|
||||
cached.setAttribute("attribute1", "value1");
|
||||
return this.sessionRepository.new RedisSession(cached);
|
||||
attributes.forEach(cached::setAttribute);
|
||||
return this.sessionRepository.new RedisSession(cached, false);
|
||||
}
|
||||
|
||||
private RedisSession createTestSession() {
|
||||
return createTestSession(Collections.singletonMap("attribute1", "value1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.session.FlushMode;
|
||||
import org.springframework.session.SaveMode;
|
||||
import org.springframework.session.data.redis.RedisFlushMode;
|
||||
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
|
||||
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;
|
||||
@@ -142,6 +143,20 @@ class RedisHttpSessionConfigurationTests {
|
||||
assertThat(ReflectionTestUtils.getField(configuration, "cleanupCron")).isEqualTo(CLEANUP_CRON_EXPRESSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customSaveModeAnnotation() {
|
||||
registerAndRefresh(RedisConfig.class, CustomSaveModeExpressionAnnotationConfiguration.class);
|
||||
assertThat(this.context.getBean(RedisOperationsSessionRepository.class)).hasFieldOrPropertyWithValue("saveMode",
|
||||
SaveMode.ALWAYS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customSaveModeSetter() {
|
||||
registerAndRefresh(RedisConfig.class, CustomSaveModeExpressionSetterConfiguration.class);
|
||||
assertThat(this.context.getBean(RedisOperationsSessionRepository.class)).hasFieldOrPropertyWithValue("saveMode",
|
||||
SaveMode.ALWAYS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void qualifiedConnectionFactoryRedisConfig() {
|
||||
registerAndRefresh(RedisConfig.class, QualifiedConnectionFactoryRedisConfig.class);
|
||||
@@ -299,6 +314,20 @@ class RedisHttpSessionConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@EnableRedisHttpSession(saveMode = SaveMode.ALWAYS)
|
||||
static class CustomSaveModeExpressionAnnotationConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomSaveModeExpressionSetterConfiguration extends RedisHttpSessionConfiguration {
|
||||
|
||||
CustomSaveModeExpressionSetterConfiguration() {
|
||||
setSaveMode(SaveMode.ALWAYS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
static class QualifiedConnectionFactoryRedisConfig {
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.ReactiveRedisOperations;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.session.SaveMode;
|
||||
import org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepository;
|
||||
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;
|
||||
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisOperations;
|
||||
@@ -107,6 +108,20 @@ class RedisWebSessionConfigurationTests {
|
||||
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customSaveModeAnnotation() {
|
||||
registerAndRefresh(RedisConfig.class, CustomSaveModeExpressionAnnotationConfiguration.class);
|
||||
assertThat(this.context.getBean(ReactiveRedisOperationsSessionRepository.class))
|
||||
.hasFieldOrPropertyWithValue("saveMode", SaveMode.ALWAYS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customSaveModeSetter() {
|
||||
registerAndRefresh(RedisConfig.class, CustomSaveModeExpressionSetterConfiguration.class);
|
||||
assertThat(this.context.getBean(ReactiveRedisOperationsSessionRepository.class))
|
||||
.hasFieldOrPropertyWithValue("saveMode", SaveMode.ALWAYS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void qualifiedConnectionFactoryRedisConfig() {
|
||||
registerAndRefresh(RedisConfig.class, QualifiedConnectionFactoryRedisConfig.class);
|
||||
@@ -249,6 +264,20 @@ class RedisWebSessionConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@EnableRedisWebSession(saveMode = SaveMode.ALWAYS)
|
||||
static class CustomSaveModeExpressionAnnotationConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomSaveModeExpressionSetterConfiguration extends RedisWebSessionConfiguration {
|
||||
|
||||
CustomSaveModeExpressionSetterConfiguration() {
|
||||
setSaveMode(SaveMode.ALWAYS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableRedisWebSession
|
||||
static class QualifiedConnectionFactoryRedisConfig {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user