|
| 1 | +/* |
| 2 | + * Copyright 2014-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.data.redis; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.data.redis.core.HashOperations; |
| 28 | +import org.springframework.data.redis.core.RedisOperations; |
| 29 | +import org.springframework.session.data.redis.RedisSessionRepository.RedisSession; |
| 30 | +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 33 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 34 | +import org.springframework.test.util.ReflectionTestUtils; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.mockito.ArgumentMatchers.any; |
| 38 | +import static org.mockito.BDDMockito.given; |
| 39 | +import static org.mockito.BDDMockito.willAnswer; |
| 40 | +import static org.mockito.Mockito.spy; |
| 41 | + |
| 42 | +/** |
| 43 | + * Integration tests for {@link RedisSessionRepository}. |
| 44 | + * |
| 45 | + * @author Vedran Pavic |
| 46 | + */ |
| 47 | +@ExtendWith(SpringExtension.class) |
| 48 | +@ContextConfiguration |
| 49 | +@WebAppConfiguration |
| 50 | +class RedisSessionRepositoryKeyMissITests extends AbstractRedisITests { |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private RedisSessionRepository sessionRepository; |
| 54 | + |
| 55 | + private RedisOperations<String, Object> spyOperations; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + void setup() { |
| 60 | + RedisOperations<String, Object> redisOperations = (RedisOperations<String, Object>) ReflectionTestUtils |
| 61 | + .getField(this.sessionRepository, "sessionRedisOperations"); |
| 62 | + this.spyOperations = spy(redisOperations); |
| 63 | + ReflectionTestUtils.setField(this.sessionRepository, "sessionRedisOperations", this.spyOperations); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void keyMiss() { |
| 68 | + RedisSession session = createAndSaveSession(Instant.now()); |
| 69 | + session.setAttribute("new", "value"); |
| 70 | + |
| 71 | + HashOperations<String, Object, Object> opsForHash = spy(this.spyOperations.opsForHash()); |
| 72 | + given(this.spyOperations.opsForHash()).willReturn(opsForHash); |
| 73 | + willAnswer((invocation) -> { |
| 74 | + this.sessionRepository.deleteById(session.getId()); |
| 75 | + return invocation.callRealMethod(); |
| 76 | + }).given(opsForHash).putAll(any(), any()); |
| 77 | + |
| 78 | + this.sessionRepository.save(session); |
| 79 | + this.sessionRepository.findById(session.getId()); |
| 80 | + |
| 81 | + // TODO maybe we can make RedisSessionMapper public and allow users to customize the handleMissingKey method |
| 82 | + // TODO or maybe we should provide the SafeDeserializingRepository https://github.com/spring-projects/spring-session/issues/529 |
| 83 | + } |
| 84 | + |
| 85 | + private RedisSession createAndSaveSession(Instant lastAccessedTime) { |
| 86 | + RedisSession session = this.sessionRepository.createSession(); |
| 87 | + session.setLastAccessedTime(lastAccessedTime); |
| 88 | + session.setAttribute("attribute1", "value1"); |
| 89 | + this.sessionRepository.save(session); |
| 90 | + return this.sessionRepository.findById(session.getId()); |
| 91 | + } |
| 92 | + |
| 93 | + @Configuration |
| 94 | + @EnableRedisHttpSession |
| 95 | + static class Config extends BaseConfig { |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments