Search in sources :

Example 16 with MapSession

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

the class ReactiveRedisOperationsSessionRepositoryTests method saveLastAccessChanged.

@Test
public void saveLastAccessChanged() {
    given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
    given(this.hashOperations.putAll(anyString(), this.delta.capture())).willReturn(Mono.just(true));
    given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
    ReactiveRedisOperationsSessionRepository.RedisSession session = this.repository.new RedisSession(new MapSession());
    session.setLastAccessedTime(Instant.ofEpochMilli(12345678L));
    Mono<Void> result = this.repository.save(session);
    StepVerifier.create(result).expectNextMatches(predicate -> {
        assertThat(this.delta.getAllValues().get(0)).isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli()));
        return true;
    });
}
Also used : MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 17 with MapSession

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

the class ReactiveRedisOperationsSessionRepositoryTests method saveRemoveAttribute.

@Test
public void saveRemoveAttribute() {
    given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
    given(this.hashOperations.putAll(anyString(), this.delta.capture())).willReturn(Mono.just(true));
    given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true));
    String attrName = "attrName";
    ReactiveRedisOperationsSessionRepository.RedisSession session = this.repository.new RedisSession(new MapSession());
    session.removeAttribute(attrName);
    Mono<Void> result = this.repository.save(session);
    StepVerifier.create(result).expectNextMatches(predicate -> {
        assertThat(this.delta.getAllValues().get(0)).isEqualTo(map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), null));
        return true;
    });
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 18 with MapSession

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

the class HazelcastSessionRepositoryTests method createSessionDefaultMaxInactiveInterval.

@Test
public void createSessionDefaultMaxInactiveInterval() throws Exception {
    verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
    HazelcastSession session = this.repository.createSession();
    assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval());
    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 19 with MapSession

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

the class HazelcastSessionRepositoryTests method findByIndexNameAndIndexValuePrincipalIndexNameFound.

@Test
public void findByIndexNameAndIndexValuePrincipalIndexNameFound() {
    verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
    String principal = "username";
    Authentication authentication = new UsernamePasswordAuthenticationToken(principal, "notused", AuthorityUtils.createAuthorityList("ROLE_USER"));
    List<MapSession> saved = new ArrayList<>(2);
    MapSession saved1 = new MapSession();
    saved1.setAttribute(SPRING_SECURITY_CONTEXT, authentication);
    saved.add(saved1);
    MapSession saved2 = new MapSession();
    saved2.setAttribute(SPRING_SECURITY_CONTEXT, authentication);
    saved.add(saved2);
    given(this.sessions.values(isA(EqualPredicate.class))).willReturn(saved);
    Map<String, HazelcastSession> sessions = this.repository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, principal);
    assertThat(sessions).hasSize(2);
    verify(this.sessions, times(1)).values(isA(EqualPredicate.class));
    verifyZeroInteractions(this.sessions);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) Authentication(org.springframework.security.core.Authentication) ArrayList(java.util.ArrayList) EqualPredicate(com.hazelcast.query.impl.predicates.EqualPredicate) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) HazelcastSession(org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession) Test(org.junit.Test)

Example 20 with MapSession

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

the class HazelcastSessionRepository method findByIndexNameAndIndexValue.

@Override
public Map<String, HazelcastSession> findByIndexNameAndIndexValue(String indexName, String indexValue) {
    if (!PRINCIPAL_NAME_INDEX_NAME.equals(indexName)) {
        return Collections.emptyMap();
    }
    Collection<MapSession> sessions = this.sessions.values(Predicates.equal(PRINCIPAL_NAME_ATTRIBUTE, indexValue));
    Map<String, HazelcastSession> sessionMap = new HashMap<>(sessions.size());
    for (MapSession session : sessions) {
        sessionMap.put(session.getId(), new HazelcastSession(session));
    }
    return sessionMap;
}
Also used : HashMap(java.util.HashMap) MapSession(org.springframework.session.MapSession)

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