From 604cd9f37fdb0b1a4e957a3bceffb4a71750c8c8 Mon Sep 17 00:00:00 2001 From: Vivek Date: Wed, 11 Mar 2020 23:25:14 +0530 Subject: [PATCH] Delete everything in Redis (#8823) --- .../spring/data/redis/config/RedisConfig.java | 20 +++- .../RedisFlushDatabaseIntegrationTest.java | 92 +++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java index 6fdb3c5bef..fdc279be42 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java @@ -1,13 +1,11 @@ package com.baeldung.spring.data.redis.config; -import com.baeldung.spring.data.redis.queue.MessagePublisher; -import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; -import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; @@ -15,6 +13,10 @@ import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.GenericToStringSerializer; +import com.baeldung.spring.data.redis.queue.MessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; + @Configuration @ComponentScan("com.baeldung.spring.data.redis") @EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo") @@ -33,6 +35,18 @@ public class RedisConfig { template.setValueSerializer(new GenericToStringSerializer(Object.class)); return template; } + + @Bean + public LettuceConnectionFactory lettuceConnectionFactory() { + return new LettuceConnectionFactory(); + } + + @Bean(name = "flushRedisTemplate") + public RedisTemplate flushRedisTemplate() { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(lettuceConnectionFactory()); + return template; + } @Bean MessageListenerAdapter messageListener() { diff --git a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java new file mode 100644 index 0000000000..1f56cbb25d --- /dev/null +++ b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java @@ -0,0 +1,92 @@ +package com.baeldung.spring.data.redis.delete; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.dao.DataAccessException; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.core.RedisCallback; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.data.redis.config.RedisConfig; + +import redis.embedded.RedisServer; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { RedisConfig.class }) +@DirtiesContext(classMode = ClassMode.BEFORE_CLASS) +public class RedisFlushDatabaseIntegrationTest { + + private RedisServer redisServer; + + @Autowired + @Qualifier("flushRedisTemplate") + private RedisTemplate flushRedisTemplate; + + @Before + public void setup() throws IOException { + redisServer = new RedisServer(6390); + redisServer.start(); + } + + @After + public void tearDown() { + redisServer.stop(); + } + + @Test + public void whenFlushDB_thenAllKeysInDatabaseAreCleared() { + + ValueOperations simpleValues = flushRedisTemplate.opsForValue(); + String key = "key"; + String value = "value"; + simpleValues.set(key, value); + assertThat(simpleValues.get(key)).isEqualTo(value); + + flushRedisTemplate.execute(new RedisCallback() { + + @Override + public Void doInRedis(RedisConnection connection) throws DataAccessException { + connection.flushDb(); + return null; + } + }); + + assertThat(simpleValues.get(key)).isNull(); + + } + + @Test + public void whenFlushAll_thenAllKeysInDatabasesAreCleared() { + + ValueOperations simpleValues = flushRedisTemplate.opsForValue(); + String key = "key"; + String value = "value"; + simpleValues.set(key, value); + assertThat(simpleValues.get(key)).isEqualTo(value); + + flushRedisTemplate.execute(new RedisCallback() { + + @Override + public Void doInRedis(RedisConnection connection) throws DataAccessException { + connection.flushAll(); + return null; + } + }); + + assertThat(simpleValues.get(key)).isNull(); + + } +} \ No newline at end of file