Rework the delete everything in Redis example

This commit is contained in:
Ashley Frieze 2020-05-22 12:42:39 +01:00
parent 8c987e89c9
commit 8a771a8b46
6 changed files with 94 additions and 107 deletions

View File

@ -4,3 +4,4 @@
- [Introduction to Lettuce the Java Redis Client](https://www.baeldung.com/java-redis-lettuce)
- [List All Available Redis Keys](https://www.baeldung.com/redis-list-available-keys)
- [Spring Data Rediss Property-Based Configuration](https://www.baeldung.com/spring-data-redis-properties)
- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data)

View File

@ -33,7 +33,8 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>

View File

@ -0,0 +1,91 @@
package com.baeldung.redis.deleteeverything;
import org.junit.*;
import redis.clients.jedis.Jedis;
import redis.embedded.RedisServer;
import java.io.IOException;
import java.net.ServerSocket;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class DeleteEverythingInRedisIntegrationTest {
private Jedis jedis;
private RedisServer redisServer;
private int port;
@Before
public void setUp() throws IOException {
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
redisServer = new RedisServer(port);
redisServer.start();
// Configure JEDIS
jedis = new Jedis("localhost", port);
}
@After
public void destroy() {
redisServer.stop();
}
@Test
public void whenPutDataIntoRedis_thenCanBeFound() {
String key = "key";
String value = "value";
jedis.set(key, value);
String received = jedis.get(key);
assertEquals(value, received);
}
@Test
public void whenPutDataIntoRedisAndThenFlush_thenCannotBeFound() {
String key = "key";
String value = "value";
jedis.set(key, value);
jedis.flushDB();
String received = jedis.get(key);
assertNull(received);
}
@Test
public void whenPutDataIntoMultipleDatabases_thenFlushAllRemovesAll() {
// add keys in different databases
jedis.select(0);
jedis.set("key1", "value1");
jedis.select(1);
jedis.set("key2", "value2");
// we'll find the correct keys in the correct dbs
jedis.select(0);
assertEquals("value1", jedis.get("key1"));
assertNull(jedis.get("key2"));
jedis.select(1);
assertEquals("value2", jedis.get("key2"));
assertNull(jedis.get("key1"));
// then, when we flush
jedis.flushAll();
// the keys will have gone
jedis.select(0);
assertNull(jedis.get("key1"));
assertNull(jedis.get("key2"));
jedis.select(1);
assertNull(jedis.get("key1"));
assertNull(jedis.get("key2"));
}
}

View File

@ -4,7 +4,6 @@
- [Introduction to Spring Data Redis](https://www.baeldung.com/spring-data-redis-tutorial)
- [PubSub Messaging with Spring Data Redis](https://www.baeldung.com/spring-data-redis-pub-sub)
- [An Introduction to Spring Data Redis Reactive](https://www.baeldung.com/spring-data-redis-reactive)
- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data)
### Build the Project with Tests Running
```
@ -15,4 +14,3 @@ mvn clean install
```
mvn test
```

View File

@ -35,18 +35,6 @@ public class RedisConfig {
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory();
}
@Bean(name = "flushRedisTemplate")
public RedisTemplate<String, String> flushRedisTemplate() {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(lettuceConnectionFactory());
return template;
}
@Bean
MessageListenerAdapter messageListener() {

View File

@ -1,92 +0,0 @@
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<String, String> 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<String, String> simpleValues = flushRedisTemplate.opsForValue();
String key = "key";
String value = "value";
simpleValues.set(key, value);
assertThat(simpleValues.get(key)).isEqualTo(value);
flushRedisTemplate.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return null;
}
});
assertThat(simpleValues.get(key)).isNull();
}
@Test
public void whenFlushAll_thenAllKeysInDatabasesAreCleared() {
ValueOperations<String, String> simpleValues = flushRedisTemplate.opsForValue();
String key = "key";
String value = "value";
simpleValues.set(key, value);
assertThat(simpleValues.get(key)).isEqualTo(value);
flushRedisTemplate.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushAll();
return null;
}
});
assertThat(simpleValues.get(key)).isNull();
}
}