Search in sources :

Example 6 with DefaultRedisZSet

use of org.springframework.data.redis.support.collections.DefaultRedisZSet in project spring-integration by spring-projects.

the class RedisStoreWritingMessageHandlerTests method testZsetWithListPayloadParsedAndProvidedKeyDefault.

@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyDefault() {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.deleteKey(jcf, "foo");
    String key = "foo";
    RedisZSet<String> redisZset = new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
    assertEquals(0, redisZset.size());
    RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf);
    handler.setKey(key);
    handler.setCollectionType(CollectionType.ZSET);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    List<String> list = new ArrayList<String>();
    list.add("Manny");
    list.add("Moe");
    list.add("Jack");
    Message<List<String>> message = MessageBuilder.withPayload(list).setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, true).build();
    handler.handleMessage(message);
    assertEquals(3, redisZset.size());
    Set<TypedTuple<String>> pepboys = redisZset.rangeByScoreWithScores(1, 1);
    for (TypedTuple<String> pepboy : pepboys) {
        assertTrue(pepboy.getScore() == 1);
    }
    handler.handleMessage(message);
    assertEquals(3, redisZset.size());
    pepboys = redisZset.rangeByScoreWithScores(1, 2);
    // should have incremented by 1
    for (TypedTuple<String> pepboy : pepboys) {
        assertEquals(Double.valueOf(2), pepboy.getScore());
    }
    this.deleteKey(jcf, "foo");
}
Also used : DefaultRedisZSet(org.springframework.data.redis.support.collections.DefaultRedisZSet) ArrayList(java.util.ArrayList) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) ArrayList(java.util.ArrayList) DefaultRedisList(org.springframework.data.redis.support.collections.DefaultRedisList) List(java.util.List) RedisList(org.springframework.data.redis.support.collections.RedisList) TypedTuple(org.springframework.data.redis.core.ZSetOperations.TypedTuple) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 7 with DefaultRedisZSet

use of org.springframework.data.redis.support.collections.DefaultRedisZSet in project spring-integration by spring-projects.

the class RedisStoreWritingMessageHandlerTests method testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement.

@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement() {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.deleteKey(jcf, "foo");
    String key = "foo";
    RedisZSet<String> redisZset = new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
    assertEquals(0, redisZset.size());
    RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf);
    handler.setKey(key);
    handler.setCollectionType(CollectionType.ZSET);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    List<String> list = new ArrayList<String>();
    list.add("Manny");
    list.add("Moe");
    list.add("Jack");
    Message<List<String>> message = MessageBuilder.withPayload(list).setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, Boolean.TRUE).build();
    handler.handleMessage(message);
    assertEquals(3, redisZset.size());
    Set<TypedTuple<String>> pepboys = redisZset.rangeByScoreWithScores(1, 1);
    for (TypedTuple<String> pepboy : pepboys) {
        assertTrue(pepboy.getScore() == 1);
    }
    handler.handleMessage(message);
    assertEquals(3, redisZset.size());
    pepboys = redisZset.rangeByScoreWithScores(1, 2);
    // should have incremented
    for (TypedTuple<String> pepboy : pepboys) {
        assertTrue(pepboy.getScore() == 2);
    }
    this.deleteKey(jcf, "foo");
}
Also used : DefaultRedisZSet(org.springframework.data.redis.support.collections.DefaultRedisZSet) ArrayList(java.util.ArrayList) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) ArrayList(java.util.ArrayList) DefaultRedisList(org.springframework.data.redis.support.collections.DefaultRedisList) List(java.util.List) RedisList(org.springframework.data.redis.support.collections.RedisList) TypedTuple(org.springframework.data.redis.core.ZSetOperations.TypedTuple) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 BeanFactory (org.springframework.beans.factory.BeanFactory)7 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)7 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)7 DefaultRedisZSet (org.springframework.data.redis.support.collections.DefaultRedisZSet)7 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)7 TypedTuple (org.springframework.data.redis.core.ZSetOperations.TypedTuple)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DefaultRedisList (org.springframework.data.redis.support.collections.DefaultRedisList)4 RedisList (org.springframework.data.redis.support.collections.RedisList)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)3