Search in sources :

Example 26 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisStoreWritingMessageHandlerTests method testMapWithMapKeyExpression.

@Test
@RedisAvailable
public void testMapWithMapKeyExpression() {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.deleteKey(jcf, "foo");
    String key = "foo";
    RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf);
    handler.setKey(key);
    handler.setCollectionType(CollectionType.MAP);
    handler.setMapKeyExpression(new LiteralExpression(key));
    handler.setBeanFactory(mock(BeanFactory.class));
    try {
        handler.afterPropertiesSet();
    } catch (Exception e) {
        fail("No exception expected:" + e.getMessage());
    }
    this.deleteKey(jcf, "foo");
}
Also used : LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) MessageHandlingException(org.springframework.messaging.MessageHandlingException) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 27 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisStoreWritingMessageHandlerTests method testZsetWithMapPayloadPojoAsSingleEntryHeaderKey.

@Test
@RedisAvailable
public void testZsetWithMapPayloadPojoAsSingleEntryHeaderKey() {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.deletePresidents(jcf);
    String key = "presidents";
    RedisZSet<Map<President, Double>> redisZset = new DefaultRedisZSet<Map<President, Double>>(key, this.initTemplate(jcf, new RedisTemplate<String, Map<President, Double>>()));
    assertEquals(0, redisZset.size());
    RedisTemplate<String, Map<President, Double>> template = this.initTemplate(jcf, new RedisTemplate<String, Map<President, Double>>());
    RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(template);
    handler.setKey(key);
    handler.setCollectionType(CollectionType.ZSET);
    handler.setExtractPayloadElements(false);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    Map<President, Double> presidents = new HashMap<President, Double>();
    presidents.put(new President("John Adams"), 18D);
    presidents.put(new President("Barack Obama"), 21D);
    presidents.put(new President("Thomas Jefferson"), 19D);
    Message<Map<President, Double>> message = MessageBuilder.withPayload(presidents).setHeader("redis_key", key).build();
    handler.handleMessage(message);
    assertEquals(1, redisZset.size());
    this.deletePresidents(jcf);
}
Also used : DefaultRedisZSet(org.springframework.data.redis.support.collections.DefaultRedisZSet) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) HashMap(java.util.HashMap) BeanFactory(org.springframework.beans.factory.BeanFactory) HashMap(java.util.HashMap) Map(java.util.Map) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 28 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testRemoveNonExistingMessageFromNonExistingTheGroup.

@Test
@RedisAvailable
public void testRemoveNonExistingMessageFromNonExistingTheGroup() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    store.removeMessagesFromGroup(this.groupId, new GenericMessage<>("2"));
}
Also used : RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 29 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testCompleteMessageGroup.

@Test
@RedisAvailable
public void testCompleteMessageGroup() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    Message<?> message = new GenericMessage<>("Hello");
    messageGroup = store.addMessageToGroup(messageGroup.getGroupId(), message);
    store.completeGroup(messageGroup.getGroupId());
    messageGroup = store.getMessageGroup(this.groupId);
    assertTrue(messageGroup.isComplete());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 30 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testJsonSerialization.

@Test
@RedisAvailable
public void testJsonSerialization() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    Message<?> genericMessage = new GenericMessage<>(new Date());
    Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
    Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
    ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
    store.addMessagesToGroup(this.groupId, genericMessage, mutableMessage, adviceMessage, errorMessage);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(4, messageGroup.size());
    List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
    assertEquals(genericMessage, messages.get(0));
    assertEquals(mutableMessage, messages.get(1));
    assertEquals(adviceMessage, messages.get(2));
    Message<?> errorMessageResult = messages.get(3);
    assertEquals(errorMessage.getHeaders(), errorMessageResult.getHeaders());
    assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
    assertEquals(errorMessage.getOriginalMessage(), ((ErrorMessage) errorMessageResult).getOriginalMessage());
    assertEquals(errorMessage.getPayload().getMessage(), ((ErrorMessage) errorMessageResult).getPayload().getMessage());
    Message<Foo> fooMessage = new GenericMessage<>(new Foo("foo"));
    try {
        store.addMessageToGroup(this.groupId, fooMessage).getMessages().iterator().next();
        fail("SerializationException expected");
    } catch (Exception e) {
        assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
        assertThat(e.getMessage(), containsString("The class with " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo and name of " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo " + "is not in the trusted packages:"));
    }
    mapper = JacksonJsonUtils.messagingAwareMapper(getClass().getPackage().getName());
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
    mapper = JacksonJsonUtils.messagingAwareMapper("*");
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
}
Also used : GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) AdviceMessage(org.springframework.integration.message.AdviceMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) AdviceMessage(org.springframework.integration.message.AdviceMessage) Date(java.util.Date) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)100 Test (org.junit.Test)83 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)62 GenericMessage (org.springframework.messaging.support.GenericMessage)22 BeanFactory (org.springframework.beans.factory.BeanFactory)19 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)17 MessageGroup (org.springframework.integration.store.MessageGroup)14 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)13 ArrayList (java.util.ArrayList)12 Properties (java.util.Properties)10 Message (org.springframework.messaging.Message)10 ApplicationContext (org.springframework.context.ApplicationContext)9 List (java.util.List)8 DefaultRedisList (org.springframework.data.redis.support.collections.DefaultRedisList)8 RedisList (org.springframework.data.redis.support.collections.RedisList)8 RedisConnection (org.springframework.data.redis.connection.RedisConnection)7 DefaultRedisZSet (org.springframework.data.redis.support.collections.DefaultRedisZSet)7 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 TypedTuple (org.springframework.data.redis.core.ZSetOperations.TypedTuple)6 LiteralExpression (org.springframework.expression.common.LiteralExpression)6