|
15 | 15 | */
|
16 | 16 | package org.springframework.data.redis.cache;
|
17 | 17 |
|
18 |
| -import org.junit.jupiter.api.Test; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.isNull; |
| 21 | +import static org.mockito.Mockito.doReturn; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 26 | + |
| 27 | +import java.time.Duration; |
19 | 28 |
|
20 |
| -import static org.assertj.core.api.Assertions.*; |
| 29 | +import org.junit.jupiter.api.Test; |
21 | 30 |
|
22 | 31 | import org.springframework.beans.DirectFieldAccessor;
|
23 | 32 | import org.springframework.core.convert.converter.Converter;
|
|
28 | 37 | * Unit tests for {@link RedisCacheConfiguration}.
|
29 | 38 | *
|
30 | 39 | * @author Mark Paluch
|
| 40 | + * @author John Blum |
31 | 41 | */
|
32 | 42 | class RedisCacheConfigurationUnitTests {
|
33 | 43 |
|
@@ -56,6 +66,42 @@ void shouldAllowConverterRegistration() {
|
56 | 66 | assertThat(config.getConversionService().canConvert(DomainType.class, String.class)).isTrue();
|
57 | 67 | }
|
58 | 68 |
|
| 69 | + @Test // GH-2628 |
| 70 | + @SuppressWarnings("deprecation") |
| 71 | + void getTtlReturnsFixedDuration() { |
| 72 | + |
| 73 | + Duration sixtySeconds = Duration.ofSeconds(60); |
| 74 | + |
| 75 | + RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() |
| 76 | + .entryTtl(sixtySeconds); |
| 77 | + |
| 78 | + |
| 79 | + assertThat(cacheConfiguration).isNotNull(); |
| 80 | + assertThat(cacheConfiguration.getTtl()).isEqualByComparingTo(sixtySeconds); |
| 81 | + assertThat(cacheConfiguration.getTtl()).isEqualByComparingTo(sixtySeconds); // does not change! |
| 82 | + } |
| 83 | + |
| 84 | + @Test // GH-2628 |
| 85 | + @SuppressWarnings("deprecation") |
| 86 | + public void getTtlCanReturnDynamicDuration() { |
| 87 | + |
| 88 | + Duration thirtyMinutes = Duration.ofMinutes(30); |
| 89 | + Duration twoHours = Duration.ofHours(2); |
| 90 | + |
| 91 | + RedisCacheWriter.TtlFunction mockTtlFunction = mock(RedisCacheWriter.TtlFunction.class); |
| 92 | + |
| 93 | + doReturn(thirtyMinutes).doReturn(twoHours).when(mockTtlFunction).getTimeToLive(any(), any()); |
| 94 | + |
| 95 | + RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() |
| 96 | + .entryTtl(mockTtlFunction); |
| 97 | + |
| 98 | + assertThat(cacheConfiguration.getTtl()).isEqualTo(thirtyMinutes); |
| 99 | + assertThat(cacheConfiguration.getTtl()).isEqualTo(twoHours); |
| 100 | + |
| 101 | + verify(mockTtlFunction, times(2)).getTimeToLive(isNull(), isNull()); |
| 102 | + verifyNoMoreInteractions(mockTtlFunction); |
| 103 | + } |
| 104 | + |
59 | 105 | private static class DomainType {
|
60 | 106 |
|
61 | 107 | }
|
|
0 commit comments