Search in sources :

Example 31 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method flushModeImmediateSetAttribute.

@Test
public void flushModeImmediateSetAttribute() {
    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();
    String attrName = "someAttribute";
    session.setAttribute(attrName, "someValue");
    Map<String, Object> delta = getDelta(2);
    assertThat(delta.size()).isEqualTo(1);
    assertThat(delta).isEqualTo(map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName)));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 32 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveSessionNothingChanged.

// gh-467
@Test
public void saveSessionNothingChanged() {
    RedisSession session = this.redisRepository.new RedisSession(this.cached);
    this.redisRepository.save(session);
    verifyZeroInteractions(this.redisOperations);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 33 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method flushModeSetLastAccessedTime.

@Test
public void flushModeSetLastAccessedTime() {
    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();
    session.setLastAccessedTime(Instant.now());
    Map<String, Object> delta = getDelta(2);
    assertThat(delta.size()).isEqualTo(1);
    assertThat(delta).isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli()));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 34 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveSetAttribute.

@Test
public void saveSetAttribute() {
    String attrName = "attrName";
    RedisSession session = this.redisRepository.new RedisSession(new MapSession());
    session.setAttribute(attrName, "attrValue");
    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);
    assertThat(getDelta()).isEqualTo(map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName)));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 35 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method flushModeImmediateCreate.

@Test
public void flushModeImmediateCreate() {
    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();
    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)

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