Search in sources :

Example 1 with DefaultMessage

use of org.springframework.data.redis.connection.DefaultMessage in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method onMessageCreated.

@Test
public void onMessageCreated() throws Exception {
    MapSession session = this.cached;
    byte[] pattern = "".getBytes("UTF-8");
    String channel = "spring:session:event:created:" + session.getId();
    JdkSerializationRedisSerializer defaultSerailizer = new JdkSerializationRedisSerializer();
    this.redisRepository.setDefaultSerializer(defaultSerailizer);
    byte[] body = defaultSerailizer.serialize(new HashMap());
    DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), body);
    this.redisRepository.setApplicationEventPublisher(this.publisher);
    this.redisRepository.onMessage(message, pattern);
    verify(this.publisher).publishEvent(this.event.capture());
    assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId());
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) HashMap(java.util.HashMap) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) MapSession(org.springframework.session.MapSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with DefaultMessage

use of org.springframework.data.redis.connection.DefaultMessage in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findBySecurityPrincipalNameExpireRemovesIndex.

@Test
public void findBySecurityPrincipalNameExpireRemovesIndex() throws Exception {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, this.context);
    this.repository.save(toSave);
    String body = "RedisOperationsSessionRepositoryITests:sessions:expires:" + toSave.getId();
    String channel = ":expired";
    DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), body.getBytes("UTF-8"));
    byte[] pattern = new byte[] {};
    this.repository.onMessage(message, pattern);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, getSecurityName());
    assertThat(findByPrincipalName).hasSize(0);
    assertThat(findByPrincipalName.keySet()).doesNotContain(toSave.getId());
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 3 with DefaultMessage

use of org.springframework.data.redis.connection.DefaultMessage in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByPrincipalNameExpireRemovesIndex.

@Test
public void findByPrincipalNameExpireRemovesIndex() throws Exception {
    String principalName = "findByPrincipalNameExpireRemovesIndex" + UUID.randomUUID();
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(INDEX_NAME, principalName);
    this.repository.save(toSave);
    String body = "RedisOperationsSessionRepositoryITests:sessions:expires:" + toSave.getId();
    String channel = ":expired";
    DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), body.getBytes("UTF-8"));
    byte[] pattern = new byte[] {};
    this.repository.onMessage(message, pattern);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).hasSize(0);
    assertThat(findByPrincipalName.keySet()).doesNotContain(toSave.getId());
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 4 with DefaultMessage

use of org.springframework.data.redis.connection.DefaultMessage in project camel by apache.

the class RedisConsumerTest method consumerReceivesMessages.

@Test
public void consumerReceivesMessages() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(2);
    ArgumentCaptor<MessageListener> messageListenerCaptor = ArgumentCaptor.forClass(MessageListener.class);
    verify(listenerContainer).addMessageListener(messageListenerCaptor.capture(), any(Collection.class));
    MessageListener messageListener = messageListenerCaptor.getValue();
    messageListener.onMessage(new DefaultMessage(null, null), null);
    messageListener.onMessage(new DefaultMessage(null, null), null);
    mock.assertIsSatisfied();
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MessageListener(org.springframework.data.redis.connection.MessageListener) Collection(java.util.Collection) Test(org.junit.Test)

Example 5 with DefaultMessage

use of org.springframework.data.redis.connection.DefaultMessage in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method onMessageCreatedCustomSerializer.

// gh-309
@Test
public void onMessageCreatedCustomSerializer() throws Exception {
    MapSession session = this.cached;
    byte[] pattern = "".getBytes("UTF-8");
    byte[] body = new byte[0];
    String channel = "spring:session:event:created:" + session.getId();
    given(this.defaultSerializer.deserialize(body)).willReturn(new HashMap<String, Object>());
    DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), body);
    this.redisRepository.setApplicationEventPublisher(this.publisher);
    this.redisRepository.onMessage(message, pattern);
    verify(this.publisher).publishEvent(this.event.capture());
    assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId());
    verify(this.defaultSerializer).deserialize(body);
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) MapSession(org.springframework.session.MapSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 MapSession (org.springframework.session.MapSession)2 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 MessageListener (org.springframework.data.redis.connection.MessageListener)1 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)1