From e121dd76f53d402429ed091832543c41587f0ea9 Mon Sep 17 00:00:00 2001 From: Hyunwoo Jung Date: Sat, 21 Jun 2025 23:36:34 +0900 Subject: [PATCH] Add Redis integration tests Signed-off-by: Hyunwoo Jung --- pom.xml | 2 + spring-batch-infrastructure/pom.xml | 12 ++ .../RedisItemReaderIntegrationTests.java | 101 +++++++++++++++ .../RedisItemWriterIntegrationTests.java | 117 ++++++++++++++++++ .../batch/item/redis/example/Person.java | 66 ++++++++++ 5 files changed, 298 insertions(+) create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemReaderIntegrationTests.java create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemWriterIntegrationTests.java create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/example/Person.java diff --git a/pom.xml b/pom.xml index 8f353cc805..a644d22107 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,8 @@ 15.6 2.0b6 9.4.12.0 + 6.6.0.RELEASE + 2.2.4 ${spring-amqp.version} diff --git a/spring-batch-infrastructure/pom.xml b/spring-batch-infrastructure/pom.xml index e283ca5c8f..e7d43f1445 100644 --- a/spring-batch-infrastructure/pom.xml +++ b/spring-batch-infrastructure/pom.xml @@ -559,6 +559,18 @@ ${jruby.version} test + + io.lettuce + lettuce-core + ${lettuce.version} + test + + + com.redis + testcontainers-redis + ${testcontainers-redis.version} + test + diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemReaderIntegrationTests.java new file mode 100644 index 0000000000..4d8670fb2a --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemReaderIntegrationTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.redis; + +import com.redis.testcontainers.RedisContainer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.redis.example.Person; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ScanOptions; +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + +/** + * @author Hyunwoo Jung + */ +@Testcontainers(disabledWithoutDocker = true) +@ExtendWith(SpringExtension.class) +class RedisItemReaderIntegrationTests { + + private static final DockerImageName REDIS_IMAGE = DockerImageName.parse("redis:8.0.2"); + + @Container + public static RedisContainer redis = new RedisContainer(REDIS_IMAGE); + + private RedisItemReader reader; + + private RedisTemplate template; + + @BeforeEach + void setUp() { + this.template = setUpRedisTemplate(); + this.template.getConnectionFactory().getConnection().serverCommands().flushAll(); + } + + @Test + void testRead() throws Exception { + this.template.opsForValue().set("person:1", new Person(1, "foo")); + this.template.opsForValue().set("person:2", new Person(2, "bar")); + this.template.opsForValue().set("person:3", new Person(3, "baz")); + this.template.opsForValue().set("person:4", new Person(4, "qux")); + this.template.opsForValue().set("person:5", new Person(5, "quux")); + + RedisTemplate redisTemplate = setUpRedisTemplate(); + ScanOptions scanOptions = ScanOptions.scanOptions().match("person:*").count(10).build(); + this.reader = new RedisItemReader<>(redisTemplate, scanOptions); + + this.reader.open(new ExecutionContext()); + + List items = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + items.add(this.reader.read()); + } + + assertThat(items, containsInAnyOrder(new Person(1, "foo"), new Person(2, "bar"), new Person(3, "baz"), + new Person(4, "qux"), new Person(5, "quux"))); + } + + private RedisTemplate setUpRedisTemplate() { + LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory( + new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort())); + connectionFactory.afterPropertiesSet(); + + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(connectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); + redisTemplate.afterPropertiesSet(); + + return redisTemplate; + } + +} \ No newline at end of file diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemWriterIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemWriterIntegrationTests.java new file mode 100644 index 0000000000..82b4d6e8cc --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemWriterIntegrationTests.java @@ -0,0 +1,117 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.redis; + +import com.redis.testcontainers.RedisContainer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.batch.item.Chunk; +import org.springframework.batch.item.redis.example.Person; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Hyunwoo Jung + */ +@Testcontainers(disabledWithoutDocker = true) +@ExtendWith(SpringExtension.class) +class RedisItemWriterIntegrationTests { + + private static final DockerImageName REDIS_IMAGE = DockerImageName.parse("redis:8.0.2"); + + @Container + public static RedisContainer redis = new RedisContainer(REDIS_IMAGE); + + private RedisItemWriter writer; + + private RedisTemplate template; + + @BeforeEach + void setUp() { + this.template = setUpRedisTemplate(); + this.template.getConnectionFactory().getConnection().serverCommands().flushAll(); + } + + @Test + void testWrite() throws Exception { + RedisTemplate redisTemplate = setUpRedisTemplate(); + this.writer = new RedisItemWriter<>(); + this.writer.setRedisTemplate(redisTemplate); + this.writer.setItemKeyMapper(p -> "person:" + p.getId()); + this.writer.setDelete(false); + + Chunk items = new Chunk<>(new Person(1, "foo"), new Person(2, "bar"), new Person(3, "baz"), + new Person(4, "qux"), new Person(5, "quux")); + this.writer.write(items); + + assertEquals(new Person(1, "foo"), this.template.opsForValue().get("person:1")); + assertEquals(new Person(2, "bar"), this.template.opsForValue().get("person:2")); + assertEquals(new Person(3, "baz"), this.template.opsForValue().get("person:3")); + assertEquals(new Person(4, "qux"), this.template.opsForValue().get("person:4")); + assertEquals(new Person(5, "quux"), this.template.opsForValue().get("person:5")); + } + + @Test + void testDelete() throws Exception { + this.template.opsForValue().set("person:1", new Person(1, "foo")); + this.template.opsForValue().set("person:2", new Person(2, "bar")); + this.template.opsForValue().set("person:3", new Person(3, "baz")); + this.template.opsForValue().set("person:4", new Person(4, "qux")); + this.template.opsForValue().set("person:5", new Person(5, "quux")); + + RedisTemplate redisTemplate = setUpRedisTemplate(); + this.writer = new RedisItemWriter<>(); + this.writer.setRedisTemplate(redisTemplate); + this.writer.setItemKeyMapper(p -> "person:" + p.getId()); + this.writer.setDelete(true); + + Chunk items = new Chunk<>(new Person(1, "foo"), new Person(2, "bar"), new Person(3, "baz"), + new Person(4, "qux"), new Person(5, "quux")); + this.writer.write(items); + + assertFalse(this.template.hasKey("person:1")); + assertFalse(this.template.hasKey("person:2")); + assertFalse(this.template.hasKey("person:3")); + assertFalse(this.template.hasKey("person:4")); + assertFalse(this.template.hasKey("person:5")); + } + + private RedisTemplate setUpRedisTemplate() { + LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory( + new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort())); + connectionFactory.afterPropertiesSet(); + + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(connectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); + redisTemplate.afterPropertiesSet(); + + return redisTemplate; + } + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/example/Person.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/example/Person.java new file mode 100644 index 0000000000..2cc819ab99 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/example/Person.java @@ -0,0 +1,66 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.redis.example; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Objects; + +/** + * @author Hyunwoo Jung + */ +public class Person implements Serializable { + + @Serial + private static final long serialVersionUID = 2396556853218591048L; + + private long id; + + private String name; + + public Person(long id, String name) { + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + Person person = (Person) o; + return id == person.id && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public String toString() { + return "Person{id=" + id + ", name=" + name + "}"; + } + +}