Search in sources :

Example 36 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method saveNewSession.

@Test
public void saveNewSession() {
    RedisSession session = this.redisRepository.createSession();
    given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
    this.redisRepository.save(session);
    Map<String, Object> delta = getDelta();
    assertThat(delta.size()).isEqualTo(3);
    Object creationTime = delta.get(RedisOperationsSessionRepository.CREATION_TIME_ATTR);
    assertThat(creationTime).isEqualTo(session.getCreationTime().toEpochMilli());
    assertThat(delta.get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR)).isEqualTo((int) Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS).getSeconds());
    assertThat(delta.get(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR)).isEqualTo(session.getCreationTime().toEpochMilli());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 37 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method flushModeSetMaxInactiveIntervalInSeconds.

@Test
public void flushModeSetMaxInactiveIntervalInSeconds() {
    given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
    this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE);
    RedisSession session = this.redisRepository.createSession();
    reset(this.boundHashOperations);
    session.setMaxInactiveInterval(Duration.ofSeconds(1));
    verify(this.boundHashOperations).expire(anyLong(), any(TimeUnit.class));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Example 38 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByChangedPrincipalName.

@Test
public void findByChangedPrincipalName() throws Exception {
    String principalName = "findByChangedPrincipalName" + UUID.randomUUID();
    String principalNameChanged = "findByChangedPrincipalName" + UUID.randomUUID();
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(INDEX_NAME, principalName);
    this.repository.save(toSave);
    toSave.setAttribute(INDEX_NAME, principalNameChanged);
    this.repository.save(toSave);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).isEmpty();
    findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalNameChanged);
    assertThat(findByPrincipalName).hasSize(1);
    assertThat(findByPrincipalName.keySet()).containsOnly(toSave.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 39 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method putAllOnSingleAttrDoesNotRemoveOld.

@Test
public void putAllOnSingleAttrDoesNotRemoveOld() {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute("a", "b");
    this.repository.save(toSave);
    toSave = this.repository.findById(toSave.getId());
    toSave.setAttribute("1", "2");
    this.repository.save(toSave);
    toSave = this.repository.findById(toSave.getId());
    Session session = this.repository.findById(toSave.getId());
    assertThat(session.getAttributeNames().size()).isEqualTo(2);
    assertThat(session.<String>getAttribute("a")).isEqualTo("b");
    assertThat(session.<String>getAttribute("1")).isEqualTo("2");
    this.repository.deleteById(toSave.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) EnableRedisHttpSession(org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession) Session(org.springframework.session.Session) Test(org.junit.Test)

Example 40 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByPrincipalNameNoSecurityPrincipalNameChangeReload.

@Test
public void findByPrincipalNameNoSecurityPrincipalNameChangeReload() throws Exception {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, this.context);
    this.repository.save(toSave);
    toSave = this.repository.findById(toSave.getId());
    toSave.setAttribute("other", "value");
    this.repository.save(toSave);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, getSecurityName());
    assertThat(findByPrincipalName).hasSize(1);
    assertThat(findByPrincipalName.keySet()).containsOnly(toSave.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)47 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)47 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MapSession (org.springframework.session.MapSession)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 Session (org.springframework.session.Session)2 PrincipalNameResolver (org.springframework.session.data.redis.RedisOperationsSessionRepository.PrincipalNameResolver)2 EnableRedisHttpSession (org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 TimeUnit (java.util.concurrent.TimeUnit)1 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)1 SessionCreatedEvent (org.springframework.session.events.SessionCreatedEvent)1 SessionDestroyedEvent (org.springframework.session.events.SessionDestroyedEvent)1