@ -25,7 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.boot.autoconfigure.AutoConfigurations ;
import org.springframework.boot.autoconfigure.AutoConfigurations ;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration ;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration ;
import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration. SpringBootRedisHttpSessionConfiguration;
import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration. IndexedRedisSessionConfiguration. SpringBootRedisIndexed HttpSessionConfiguration;
import org.springframework.boot.autoconfigure.web.ServerProperties ;
import org.springframework.boot.autoconfigure.web.ServerProperties ;
import org.springframework.boot.test.context.FilteredClassLoader ;
import org.springframework.boot.test.context.FilteredClassLoader ;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext ;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext ;
@ -38,6 +38,7 @@ import org.springframework.session.FlushMode;
import org.springframework.session.SaveMode ;
import org.springframework.session.SaveMode ;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository ;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository ;
import org.springframework.session.data.redis.RedisIndexedSessionRepository ;
import org.springframework.session.data.redis.RedisIndexedSessionRepository ;
import org.springframework.session.data.redis.RedisSessionRepository ;
import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction ;
import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction ;
import org.springframework.session.data.redis.config.ConfigureRedisAction ;
import org.springframework.session.data.redis.config.ConfigureRedisAction ;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository ;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository ;
@ -70,8 +71,8 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. run ( validateSpringSessionUses Redis( "spring:session :event:0:created :", FlushMode . ON_SAVE ,
. run ( validateSpringSessionUses Default Redis( "spring:session :", FlushMode . ON_SAVE ,
SaveMode . ON_SET_ATTRIBUTE , "0 * * * * *" )) ;
SaveMode . ON_SET_ATTRIBUTE )) ;
}
}
@Test
@Test
@ -79,8 +80,8 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. run ( validateSpringSessionUses Redis( "spring:session :event:0:created :", FlushMode . ON_SAVE ,
. run ( validateSpringSessionUses Default Redis( "spring:session :", FlushMode . ON_SAVE ,
SaveMode . ON_SET_ATTRIBUTE , "0 * * * * *" )) ;
SaveMode . ON_SET_ATTRIBUTE )) ;
}
}
@Test
@Test
@ -89,64 +90,97 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) , "spring.session.timeout=1m" )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) , "spring.session.timeout=1m" )
. withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) ) . run ( ( context ) - > {
. withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) ) . run ( ( context ) - > {
RedisIndexedSessionRepository repository = validateSessionRepository ( context ,
RedisSessionRepository repository = validateSessionRepository ( context ,
RedisIndexedSessionRepository . class ) ;
RedisSessionRepository . class ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "defaultMaxInactiveInterval" , 60 ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "defaultMaxInactiveInterval" ,
Duration . ofMinutes ( 1 ) ) ;
} ) ;
} ) ;
}
}
@Test
@Test
void r edisSessionStoreWithCustomizations( ) {
void defaultR edisSessionStoreWithCustomizations( ) {
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withPropertyValues ( "spring.session.redis.namespace=foo" , "spring.session.redis.flush-mode=immediate" ,
. withPropertyValues ( "spring.session.redis.namespace=foo" , "spring.session.redis.flush-mode=immediate" ,
"spring.session.redis.save-mode=on-get-attribute" ,
"spring.session.redis.save-mode=on-get-attribute" , "spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. run ( validateSpringSessionUsesDefaultRedis ( "foo:" , FlushMode . IMMEDIATE , SaveMode . ON_GET_ATTRIBUTE ) ) ;
}
@Test
void indexedRedisSessionDefaultConfig ( ) {
this . contextRunner . withPropertyValues ( "spring.session.redis.repository=indexed" ,
"spring.data.redis.host=" + redis . getHost ( ) , "spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. run ( validateSpringSessionUsesIndexedRedis ( "spring:session:" , FlushMode . ON_SAVE ,
SaveMode . ON_SET_ATTRIBUTE , "0 * * * * *" ) ) ;
}
@Test
void indexedRedisSessionStoreWithCustomizations ( ) {
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withPropertyValues ( "spring.session.redis.repository=indexed" , "spring.session.redis.namespace=foo" ,
"spring.session.redis.flush-mode=immediate" , "spring.session.redis.save-mode=on-get-attribute" ,
"spring.session.redis.cleanup-cron=0 0 12 * * *" , "spring.data.redis.host=" + redis . getHost ( ) ,
"spring.session.redis.cleanup-cron=0 0 12 * * *" , "spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. run ( validateSpringSessionUsesRedis ( "foo:event:0:created:" , FlushMode . IMMEDIATE ,
. run ( validateSpringSessionUses Indexed Redis( "foo: ", FlushMode . IMMEDIATE , SaveMode . ON_GET_ATTRIBUTE ,
SaveMode . ON_GET_ATTRIBUTE , "0 0 12 * * *" ) ) ;
"0 0 12 * * *" ) ) ;
}
}
@Test
@Test
void redisSessionWithConfigureActionNone ( ) {
void indexedR edisSessionWithConfigureActionNone( ) {
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withPropertyValues ( "spring.session.redis.configure-action=none" ,
. withPropertyValues ( "spring.session.redis. repository=indexed ",
"spring.data.redis.host=" + redis . getHost ( ) ,
"spring. session.redis.configure-action=none", "spring. data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. run ( validateStrategy ( ConfigureRedisAction . NO_OP . getClass ( ) ) ) ;
. run ( validateStrategy ( ConfigureRedisAction . NO_OP . getClass ( ) ) ) ;
}
}
@Test
@Test
void redisSessionWithDefaultConfigureActionNone ( ) {
void indexedR edisSessionWithDefaultConfigureActionNone( ) {
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
. withPropertyValues ( "spring.session.redis.repository=indexed" ,
"spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. run ( validateStrategy ( ConfigureNotifyKeyspaceEventsAction . class ,
. run ( validateStrategy ( ConfigureNotifyKeyspaceEventsAction . class ,
entry ( "notify-keyspace-events" , "gxE" ) ) ) ;
entry ( "notify-keyspace-events" , "gxE" ) ) ) ;
}
}
@Test
@Test
void r edisSessionWithCustomConfigureRedisActionBean( ) {
void indexedR edisSessionWithCustomConfigureRedisActionBean( ) {
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
this . contextRunner . withConfiguration ( AutoConfigurations . of ( RedisAutoConfiguration . class ) )
. withUserConfiguration ( MaxEntriesRedisAction . class )
. withUserConfiguration ( MaxEntriesRedisAction . class )
. withPropertyValues ( "spring.data.redis.host=" + redis . getHost ( ) ,
. withPropertyValues ( "spring.session.redis.repository=indexed" ,
"spring.data.redis.host=" + redis . getHost ( ) ,
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
"spring.data.redis.port=" + redis . getFirstMappedPort ( ) )
. run ( validateStrategy ( MaxEntriesRedisAction . class , entry ( "set-max-intset-entries" , "1024" ) ) ) ;
. run ( validateStrategy ( MaxEntriesRedisAction . class , entry ( "set-max-intset-entries" , "1024" ) ) ) ;
}
}
private ContextConsumer < AssertableWebApplicationContext > validateSpringSessionUsesRedis (
private ContextConsumer < AssertableWebApplicationContext > validateSpringSessionUsesDefaultRedis ( String keyNamespace ,
String sessionCreatedChannelPrefix , FlushMode flushMode , SaveMode saveMode , String cleanupCron ) {
FlushMode flushMode , SaveMode saveMode ) {
return ( context ) - > {
RedisSessionRepository repository = validateSessionRepository ( context , RedisSessionRepository . class ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "defaultMaxInactiveInterval" ,
new ServerProperties ( ) . getServlet ( ) . getSession ( ) . getTimeout ( ) ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "keyNamespace" , keyNamespace ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "flushMode" , flushMode ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "saveMode" , saveMode ) ;
} ;
}
private ContextConsumer < AssertableWebApplicationContext > validateSpringSessionUsesIndexedRedis ( String keyNamespace ,
FlushMode flushMode , SaveMode saveMode , String cleanupCron ) {
return ( context ) - > {
return ( context ) - > {
RedisIndexedSessionRepository repository = validateSessionRepository ( context ,
RedisIndexedSessionRepository repository = validateSessionRepository ( context ,
RedisIndexedSessionRepository . class ) ;
RedisIndexedSessionRepository . class ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "defaultMaxInactiveInterval" ,
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "defaultMaxInactiveInterval" ,
( int ) new ServerProperties ( ) . getServlet ( ) . getSession ( ) . getTimeout ( ) . getSeconds ( ) ) ;
( int ) new ServerProperties ( ) . getServlet ( ) . getSession ( ) . getTimeout ( ) . getSeconds ( ) ) ;
assertThat ( repository . getSessionCreatedChannelPrefix ( ) ) . isEqualTo ( sessionCreatedChannelPrefix ) ;
assertThat ( repository ). hasFieldOrPropertyWithValue ( "namespace" , keyNamespace ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "flushMode" , flushMode ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "flushMode" , flushMode ) ;
SpringBootRedisHttpSessionConfiguration configuration = context
. getBean ( SpringBootRedisHttpSessionConfiguration . class ) ;
assertThat ( configuration ) . hasFieldOrPropertyWithValue ( "cleanupCron" , cleanupCron ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "saveMode" , saveMode ) ;
assertThat ( repository ) . hasFieldOrPropertyWithValue ( "saveMode" , saveMode ) ;
SpringBootRedisIndexedHttpSessionConfiguration configuration = context
. getBean ( SpringBootRedisIndexedHttpSessionConfiguration . class ) ;
assertThat ( configuration ) . hasFieldOrPropertyWithValue ( "cleanupCron" , cleanupCron ) ;
} ;
} ;
}
}