Do not use mock for Rename Operation Exception Test

Issue gh-2645
This commit is contained in:
Marcus Hert Da Coregio
2023-12-12 10:17:29 -03:00
parent ebeb5b3a7d
commit bde989f543
2 changed files with 15 additions and 103 deletions

View File

@@ -1,103 +0,0 @@
/*
* 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.Mockito.doThrow;
import static org.mockito.Mockito.spy;
/**
* Key miss error tests for {@link RedisIndexedSessionRepository}
*
* @author Marcus da Coregio
* @see <a href="https://github.com/spring-projects/spring-session/issues/2021">Related
* GitHub Issue</a>
*/
@ExtendWith(SpringExtension.class)
class RedisIndexedSessionRepositoryDynamicITests extends AbstractRedisITests {
private RedisIndexedSessionRepository sessionRepository;
private RedisOperations<String, Object> 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,..."));
doThrow(redisSystemException).when(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<String, Object> redisOperations = (RedisOperations<String, Object>) 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 {
}
}

View File

@@ -47,6 +47,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Integration tests for {@link RedisIndexedSessionRepository}.
@@ -682,6 +683,20 @@ class RedisIndexedSessionRepositoryITests extends AbstractRedisITests {
assertThat(this.repository.findById(copy2.getId())).isNull();
}
// gh-1743
@Test
void saveChangeSessionIdWhenFailedRenameOperationExceptionThenIgnoreError() {
RedisSession toSave = this.repository.createSession();
String sessionId = toSave.getId();
this.repository.save(toSave);
RedisSession session = this.repository.findById(sessionId);
this.repository.deleteById(sessionId);
session.changeSessionId();
assertThatNoException().isThrownBy(() -> this.repository.save(session));
}
private String getSecurityName() {
return this.context.getAuthentication().getName();
}