Search in sources :

Example 36 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class ReactiveRedisOperationsSessionRepositoryTests method getSessionFound.

@Test
@SuppressWarnings("unchecked")
public void getSessionFound() {
    given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
    String attrName = "attrName";
    MapSession expected = new MapSession();
    expected.setLastAccessedTime(Instant.now().minusSeconds(60));
    expected.setAttribute(attrName, "attrValue");
    Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), expected.getAttribute(attrName), RedisOperationsSessionRepository.CREATION_TIME_ATTR, expected.getCreationTime().toEpochMilli(), RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, (int) expected.getMaxInactiveInterval().getSeconds(), RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, expected.getLastAccessedTime().toEpochMilli());
    given(this.hashOperations.entries(anyString())).willReturn(Flux.fromIterable(map.entrySet()));
    Mono<ReactiveRedisOperationsSessionRepository.RedisSession> session = this.repository.findById("test");
    StepVerifier.create(session).expectNextMatches(predicate -> {
        assertThat(predicate.getId()).isEqualTo(expected.getId());
        assertThat(predicate.getAttributeNames()).isEqualTo(expected.getAttributeNames());
        assertThat(predicate.<String>getAttribute(attrName)).isEqualTo(expected.getAttribute(attrName));
        assertThat(predicate.getCreationTime()).isEqualTo(expected.getCreationTime());
        assertThat(predicate.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval());
        assertThat(predicate.getLastAccessedTime()).isEqualTo(expected.getLastAccessedTime());
        return true;
    });
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 37 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class RedisSessionExpirationPolicyTests method setup.

@Before
public void setup() {
    RedisOperationsSessionRepository repository = new RedisOperationsSessionRepository(this.sessionRedisOperations);
    this.policy = new RedisSessionExpirationPolicy(this.sessionRedisOperations, repository::getExpirationsKey, repository::getSessionKey);
    this.session = new MapSession();
    this.session.setLastAccessedTime(Instant.ofEpochMilli(1429116694675L));
    this.session.setId("12345");
    given(this.sessionRedisOperations.boundSetOps(anyString())).willReturn(this.setOperations);
    given(this.sessionRedisOperations.boundHashOps(anyString())).willReturn(this.hashOperations);
    given(this.sessionRedisOperations.boundValueOps(anyString())).willReturn(this.valueOperations);
}
Also used : MapSession(org.springframework.session.MapSession) Before(org.junit.Before)

Example 38 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class HazelcastSessionRepositoryTests method getSessionFound.

@Test
public void getSessionFound() {
    verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
    MapSession saved = new MapSession();
    saved.setAttribute("savedName", "savedValue");
    given(this.sessions.get(eq(saved.getId()))).willReturn(saved);
    HazelcastSession session = this.repository.findById(saved.getId());
    assertThat(session.getId()).isEqualTo(saved.getId());
    assertThat(session.<String>getAttribute("savedName")).isEqualTo("savedValue");
    verify(this.sessions, times(1)).get(eq(saved.getId()));
    verifyZeroInteractions(this.sessions);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) MapSession(org.springframework.session.MapSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HazelcastSession(org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession) Test(org.junit.Test)

Example 39 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class HazelcastSessionRepositoryTests method getSessionExpired.

@Test
public void getSessionExpired() {
    verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
    MapSession expired = new MapSession();
    expired.setLastAccessedTime(Instant.now().minusSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS + 1));
    given(this.sessions.get(eq(expired.getId()))).willReturn(expired);
    HazelcastSession session = this.repository.findById(expired.getId());
    assertThat(session).isNull();
    verify(this.sessions, times(1)).get(eq(expired.getId()));
    verify(this.sessions, times(1)).remove(eq(expired.getId()));
    verifyZeroInteractions(this.sessions);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) MapSession(org.springframework.session.MapSession) HazelcastSession(org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession) Test(org.junit.Test)

Example 40 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class IndexDocTests method repositoryDemo.

@Test
public void repositoryDemo() {
    RepositoryDemo<MapSession> demo = new RepositoryDemo<>();
    demo.repository = new MapSessionRepository(new ConcurrentHashMap<>());
    demo.demo();
}
Also used : MapSessionRepository(org.springframework.session.MapSessionRepository) MapSession(org.springframework.session.MapSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

MapSession (org.springframework.session.MapSession)47 Test (org.junit.Test)37 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)7 HashMap (java.util.HashMap)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 MapSessionRepository (org.springframework.session.MapSessionRepository)6 IOException (java.io.IOException)5 Before (org.junit.Before)5 Session (org.springframework.session.Session)5 HazelcastSession (org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession)5 MapListener (com.hazelcast.map.listener.MapListener)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Map (java.util.Map)3 BatchPreparedStatementSetter (org.springframework.jdbc.core.BatchPreparedStatementSetter)3 PreparedStatementSetter (org.springframework.jdbc.core.PreparedStatementSetter)3 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)2