From 5f7268940c4d7bcc2a9572c1ac8572514b372b4c Mon Sep 17 00:00:00 2001 From: Lars Eckart Date: Thu, 26 Sep 2024 14:07:51 +0300 Subject: [PATCH] examples for testcontainers with redis, testing RedisMap --- .../java/lars/spielplatz/redis/RedisMap.java | 129 ++++++++++++++++++ ...{RedisTestsTest.java => RedisMapTest.java} | 40 +++++- .../java/lars/spielplatz/redis/RedisTest.java | 32 ----- ...sTest.java => LettuceExplorationTest.java} | 4 +- 4 files changed, 165 insertions(+), 40 deletions(-) create mode 100644 src/test/java/lars/spielplatz/redis/RedisMap.java rename src/test/java/lars/spielplatz/redis/{RedisTestsTest.java => RedisMapTest.java} (66%) delete mode 100644 src/test/java/lars/spielplatz/redis/RedisTest.java rename src/test/java/lars/spielplatz/tc/{RedisTest.java => LettuceExplorationTest.java} (97%) diff --git a/src/test/java/lars/spielplatz/redis/RedisMap.java b/src/test/java/lars/spielplatz/redis/RedisMap.java new file mode 100644 index 00000000..bc0b080b --- /dev/null +++ b/src/test/java/lars/spielplatz/redis/RedisMap.java @@ -0,0 +1,129 @@ +package lars.spielplatz.redis; + +import io.lettuce.core.api.StatefulRedisConnection; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.pool2.impl.GenericObjectPool; +import org.jetbrains.annotations.Nullable; + +public class RedisMap implements Map { + + // TODO: try out https://github.com/DanielYWoo/fast-object-pool + // TODO: try out https://github.com/chrisvest/stormpot + + private final GenericObjectPool> redisPool; + + public RedisMap(GenericObjectPool> redisPool) { + this.redisPool = redisPool; + } + + @Override + public int size() { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + return connection.sync().keys("*").size(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public boolean containsKey(Object key) { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + return connection.sync().exists((String) key) == 1; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean containsValue(Object value) { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + List keys = connection.sync().keys("*"); + for (String key : keys) { + if (connection.sync().get(key).equals(value)) { + return true; + } + } + return false; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public String get(Object key) { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + return connection.sync().get((String) key); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public @Nullable String put(String key, String value) { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + return connection.sync().set((String) key, value); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public String remove(Object key) { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + String value = connection.sync().get((String) key); + connection.sync().del((String) key); + return value; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void putAll(Map map) { + map.forEach(this::put); + } + + @Override + public void clear() { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + connection.sync().flushall(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public Set keySet() { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + return Set.copyOf(connection.sync().keys("*")); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public Collection values() { + return List.of(); + } + + @Override + public Set> entrySet() { + try (StatefulRedisConnection connection = redisPool.borrowObject()) { + List keys = connection.sync().keys("*"); + return keys.stream() + .map(key -> Map.entry(key, connection.sync().get(key))) + .collect(Collectors.toSet()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/test/java/lars/spielplatz/redis/RedisTestsTest.java b/src/test/java/lars/spielplatz/redis/RedisMapTest.java similarity index 66% rename from src/test/java/lars/spielplatz/redis/RedisTestsTest.java rename to src/test/java/lars/spielplatz/redis/RedisMapTest.java index f8c853dd..39056a91 100644 --- a/src/test/java/lars/spielplatz/redis/RedisTestsTest.java +++ b/src/test/java/lars/spielplatz/redis/RedisMapTest.java @@ -8,6 +8,7 @@ import io.lettuce.core.support.ConnectionPoolSupport; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -19,14 +20,14 @@ import org.testcontainers.utility.DockerImageName; @DisabledOnOs(OS.WINDOWS) // no docker on github actions windows -public class RedisTestsTest { +public class RedisMapTest { private static final GenericContainer redis = new GenericContainer<>(DockerImageName.parse("redis")).withReuse(true).withExposedPorts(6379); private static GenericObjectPool> pool; private static RedisClient redisClient; - private RedisTest redisKeyValueStorage; + private RedisMap redisMap; @BeforeAll static void setUpBeforeAll() { @@ -40,12 +41,39 @@ static void setUpBeforeAll() { @BeforeEach void setUp() { - redisKeyValueStorage = new RedisTest(pool); + redisMap = new RedisMap(pool); + } + + @AfterEach + void tearDown() { + redisClient.connect().sync().flushall(); + } + + @Test + void empty_map_is_empty() { + assertThat(redisMap.isEmpty()).isTrue(); + } + + @Test + void map_with_one_entry_is_not_empty() { + redisMap.put("key", "value"); + assertThat(redisMap.isEmpty()).isFalse(); + } + + @Test + void empty_map_has_size_0() { + assertThat(redisMap.size()).isZero(); + } + + @Test + void map_with_one_entry_has_size_1() { + redisMap.put("key", "value"); + assertThat(redisMap.size()).isEqualTo(1); } @Test - void update() { - redisKeyValueStorage.update(); - assertThat(redisKeyValueStorage.get()).isEqualTo("value"); + void map_contains_key() { + redisMap.put("key", "value"); + assertThat(redisMap.containsKey("key")).isTrue(); } } diff --git a/src/test/java/lars/spielplatz/redis/RedisTest.java b/src/test/java/lars/spielplatz/redis/RedisTest.java deleted file mode 100644 index 7894a375..00000000 --- a/src/test/java/lars/spielplatz/redis/RedisTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package lars.spielplatz.redis; - -import io.lettuce.core.api.StatefulRedisConnection; -import org.apache.commons.pool2.impl.GenericObjectPool; - -public class RedisTest { - - // TODO: try out https://github.com/DanielYWoo/fast-object-pool - // TODO: try out https://github.com/chrisvest/stormpot - - private final GenericObjectPool> redisPool; - - public RedisTest(GenericObjectPool> redisPool) { - this.redisPool = redisPool; - } - - public void update() { - try (StatefulRedisConnection connection = redisPool.borrowObject()) { - connection.sync().set("key", "value"); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public String get() { - try (StatefulRedisConnection connection = redisPool.borrowObject()) { - return connection.sync().get("key"); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/test/java/lars/spielplatz/tc/RedisTest.java b/src/test/java/lars/spielplatz/tc/LettuceExplorationTest.java similarity index 97% rename from src/test/java/lars/spielplatz/tc/RedisTest.java rename to src/test/java/lars/spielplatz/tc/LettuceExplorationTest.java index 28ca44c4..1ddd4197 100644 --- a/src/test/java/lars/spielplatz/tc/RedisTest.java +++ b/src/test/java/lars/spielplatz/tc/LettuceExplorationTest.java @@ -24,9 +24,9 @@ @Testcontainers @EnabledOnOs({OS.LINUX, OS.MAC}) -public class RedisTest { +public class LettuceExplorationTest { - private static final Logger log = getLogger(RedisTest.class); + private static final Logger log = getLogger(LettuceExplorationTest.class); @Container private GenericContainer redis =