|
| 1 | +package org.finos.gitproxy.dashboard.session; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.time.Duration; |
| 6 | +import org.junit.jupiter.api.*; |
| 7 | +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
| 8 | +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; |
| 9 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 10 | +import org.springframework.data.redis.core.RedisTemplate; |
| 11 | +import org.springframework.data.redis.serializer.RedisSerializer; |
| 12 | +import org.springframework.session.data.redis.RedisIndexedSessionRepository; |
| 13 | +import org.testcontainers.containers.GenericContainer; |
| 14 | +import org.testcontainers.junit.jupiter.Container; |
| 15 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 16 | +import org.testcontainers.utility.DockerImageName; |
| 17 | + |
| 18 | +@Testcontainers |
| 19 | +@Tag("integration") |
| 20 | +class RedisSessionRepositoryIntegrationTest { |
| 21 | + |
| 22 | + @SuppressWarnings("resource") |
| 23 | + @Container |
| 24 | + static final GenericContainer<?> VALKEY = |
| 25 | + new GenericContainer<>(DockerImageName.parse("docker.io/valkey/valkey:8")).withExposedPorts(6379); |
| 26 | + |
| 27 | + private LettuceConnectionFactory factory; |
| 28 | + private RedisIndexedSessionRepository repo; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + void setUp() { |
| 32 | + var standalone = new RedisStandaloneConfiguration(VALKEY.getHost(), VALKEY.getMappedPort(6379)); |
| 33 | + factory = new LettuceConnectionFactory(standalone, LettuceClientConfiguration.defaultConfiguration()); |
| 34 | + factory.afterPropertiesSet(); |
| 35 | + |
| 36 | + var template = new RedisTemplate<String, Object>(); |
| 37 | + template.setConnectionFactory(factory); |
| 38 | + template.setKeySerializer(RedisSerializer.string()); |
| 39 | + template.setHashKeySerializer(RedisSerializer.string()); |
| 40 | + template.afterPropertiesSet(); |
| 41 | + |
| 42 | + repo = new RedisIndexedSessionRepository(template); |
| 43 | + repo.setDefaultMaxInactiveInterval(Duration.ofMinutes(30)); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + void tearDown() { |
| 48 | + factory.destroy(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void createSession_hasConfiguredTimeout() { |
| 53 | + var s = repo.createSession(); |
| 54 | + assertEquals(Duration.ofMinutes(30), s.getMaxInactiveInterval()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void save_then_findById_roundTripsAttributes() { |
| 59 | + var s = repo.createSession(); |
| 60 | + s.setAttribute("username", "alice"); |
| 61 | + s.setAttribute("role-count", 3); |
| 62 | + repo.save(s); |
| 63 | + |
| 64 | + var loaded = repo.findById(s.getId()); |
| 65 | + assertNotNull(loaded); |
| 66 | + assertEquals(s.getId(), loaded.getId()); |
| 67 | + assertEquals("alice", loaded.getAttribute("username")); |
| 68 | + assertEquals(Integer.valueOf(3), loaded.getAttribute("role-count")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void findById_unknownId_returnsNull() { |
| 73 | + assertNull(repo.findById("does-not-exist")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void deleteById_removesSession() { |
| 78 | + var s = repo.createSession(); |
| 79 | + s.setAttribute("k", "v"); |
| 80 | + repo.save(s); |
| 81 | + assertNotNull(repo.findById(s.getId())); |
| 82 | + |
| 83 | + repo.deleteById(s.getId()); |
| 84 | + assertNull(repo.findById(s.getId())); |
| 85 | + } |
| 86 | +} |
0 commit comments