From cf1827a56d4616cfae70a36a58189f02074de21a Mon Sep 17 00:00:00 2001 From: Marcus Da Coregio Date: Wed, 18 Oct 2023 13:31:13 -0300 Subject: [PATCH] Move tests to dynamic configuration tests (cherry picked from commit bf5f04f40c606e540821b4bd43d553ceced7e595) --- ...IndexedSessionRepositoryDynamicITests.java | 103 ++++++++++++++++++ .../RedisIndexedSessionRepositoryITests.java | 34 ------ 2 files changed, 103 insertions(+), 34 deletions(-) create mode 100644 spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryDynamicITests.java diff --git a/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryDynamicITests.java b/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryDynamicITests.java new file mode 100644 index 00000000..893c6d48 --- /dev/null +++ b/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryDynamicITests.java @@ -0,0 +1,103 @@ +/* + * Copyright 2014-2023 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.Instant; + +import io.lettuce.core.RedisCommandExecutionException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.RedisSystemException; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.session.data.redis.RedisIndexedSessionRepository.RedisSession; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisIndexedHttpSession; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willThrow; +import static org.mockito.Mockito.spy; + +/** + * Key miss error tests for {@link RedisIndexedSessionRepository} + * + * @author Marcus da Coregio + * @see Related + * GitHub Issue + */ +@ExtendWith(SpringExtension.class) +class RedisIndexedSessionRepositoryDynamicITests extends AbstractRedisITests { + + private RedisIndexedSessionRepository sessionRepository; + + private RedisOperations spyOperations; + + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + + // gh-1743 + @Test + @SuppressWarnings("unchecked") + void saveChangeSessionIdWhenFailedRenameOperationExceptionContainsMoreDetailsThenIgnoreError() { + this.context.register(Config.class); + refreshAndPrepareFields(); + + RedisSession toSave = this.sessionRepository.createSession(); + String sessionId = toSave.getId(); + + this.sessionRepository.save(toSave); + RedisSession session = this.sessionRepository.findById(sessionId); + this.sessionRepository.deleteById(sessionId); + String newSessionId = session.changeSessionId(); + + RedisSystemException redisSystemException = new RedisSystemException(null, + new RedisCommandExecutionException("ERR no such key. channel: [id: 0xec125091,...")); + willThrow(redisSystemException).given(this.spyOperations).rename(any(), any()); + + this.sessionRepository.save(session); + assertThat(this.sessionRepository.findById(sessionId)).isNull(); + assertThat(this.sessionRepository.findById(newSessionId)).isNull(); + } + + @SuppressWarnings("unchecked") + private void refreshAndPrepareFields() { + this.context.refresh(); + this.sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class); + RedisOperations redisOperations = (RedisOperations) ReflectionTestUtils + .getField(this.sessionRepository, "sessionRedisOperations"); + this.spyOperations = spy(redisOperations); + ReflectionTestUtils.setField(this.sessionRepository, "sessionRedisOperations", this.spyOperations); + } + + private RedisSession createAndSaveSession(Instant lastAccessedTime) { + RedisSession session = this.sessionRepository.createSession(); + session.setLastAccessedTime(lastAccessedTime); + session.setAttribute("attribute1", "value1"); + this.sessionRepository.save(session); + return this.sessionRepository.findById(session.getId()); + } + + @Configuration + @EnableRedisIndexedHttpSession + static class Config extends BaseConfig { + + } + +} diff --git a/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryITests.java b/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryITests.java index 089d3204..5eca6598 100644 --- a/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryITests.java +++ b/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryITests.java @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.UUID; -import io.lettuce.core.RedisCommandExecutionException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -28,7 +27,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.RedisSystemException; import org.springframework.data.redis.connection.DefaultMessage; import org.springframework.data.redis.core.RedisOperations; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -47,13 +45,8 @@ import org.springframework.session.events.SessionDestroyedEvent; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.willThrow; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.spy; /** * Integration tests for {@link RedisIndexedSessionRepository}. @@ -689,33 +682,6 @@ class RedisIndexedSessionRepositoryITests extends AbstractRedisITests { assertThat(this.repository.findById(copy2.getId())).isNull(); } - // gh-1743 - @Test - @SuppressWarnings("unchecked") - void saveChangeSessionIdWhenFailedRenameOperationExceptionContainsMoreDetailsThenIgnoreError() { - RedisOperations sessionRedisOperations = (RedisOperations) ReflectionTestUtils - .getField(this.repository, "sessionRedisOperations"); - RedisOperations spyOperations = spy(sessionRedisOperations); - ReflectionTestUtils.setField(this.repository, "sessionRedisOperations", spyOperations); - - RedisSession toSave = this.repository.createSession(); - String sessionId = toSave.getId(); - - this.repository.save(toSave); - RedisIndexedSessionRepository.RedisSession session = this.repository.findById(sessionId); - this.repository.deleteById(sessionId); - String newSessionId = session.changeSessionId(); - - RedisSystemException redisSystemException = new RedisSystemException(null, - new RedisCommandExecutionException("ERR no such key. channel: [id: 0xec125091,...")); - willThrow(redisSystemException).given(spyOperations).rename(any(), any()); - - this.repository.save(session); - assertThat(this.repository.findById(sessionId)).isNull(); - assertThat(this.repository.findById(newSessionId)).isNull(); - reset(spyOperations); - } - private String getSecurityName() { return this.context.getAuthentication().getName(); }