diff --git a/pom.xml b/pom.xml index 667b992278..3ff160cc3c 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,9 @@ 15.6 2.0b6 9.4.12.0 + 6.6.0.RELEASE + 6.0.0 + 2.2.4 ${spring-amqp.version} diff --git a/spring-batch-infrastructure/pom.xml b/spring-batch-infrastructure/pom.xml index e283ca5c8f..2e7d1f4b58 100644 --- a/spring-batch-infrastructure/pom.xml +++ b/spring-batch-infrastructure/pom.xml @@ -559,6 +559,24 @@ ${jruby.version} test + + io.lettuce + lettuce-core + ${lettuce.version} + test + + + redis.clients + jedis + ${jedis.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..2e1b9fe9b4 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemReaderIntegrationTests.java @@ -0,0 +1,126 @@ +/* + * 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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.redis.example.Person; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +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.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 java.util.stream.Stream; + +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(lettuceConnectionFactory()); + } + + @AfterEach + void tearDown() { + this.template.getConnectionFactory().getConnection().serverCommands().flushAll(); + } + + @ParameterizedTest + @MethodSource("connectionFactories") + void testRead(RedisConnectionFactory connectionFactory) 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(connectionFactory); + 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(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); + redisTemplate.afterPropertiesSet(); + + return redisTemplate; + } + + private static Stream connectionFactories() { + return Stream.of(Arguments.of(lettuceConnectionFactory()), Arguments.of(jedisConnectionFactory())); + } + + private static RedisConnectionFactory lettuceConnectionFactory() { + LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory( + new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort())); + lettuceConnectionFactory.afterPropertiesSet(); + return lettuceConnectionFactory; + } + + private static JedisConnectionFactory jedisConnectionFactory() { + JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory( + new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort())); + jedisConnectionFactory.afterPropertiesSet(); + return jedisConnectionFactory; + } + +} 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..79e19126fa --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/redis/RedisItemWriterIntegrationTests.java @@ -0,0 +1,145 @@ +/* + * 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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.batch.item.Chunk; +import org.springframework.batch.item.redis.example.Person; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +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.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.stream.Stream; + +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(lettuceConnectionFactory()); + } + + @AfterEach + void tearDown() { + this.template.getConnectionFactory().getConnection().serverCommands().flushAll(); + } + + @ParameterizedTest + @MethodSource("connectionFactories") + void testWriteWithLettuce(RedisConnectionFactory connectionFactory) throws Exception { + RedisTemplate redisTemplate = setUpRedisTemplate(connectionFactory); + 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")); + } + + @ParameterizedTest + @MethodSource("connectionFactories") + void testDelete(RedisConnectionFactory connectionFactory) 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(connectionFactory); + 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(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); + redisTemplate.afterPropertiesSet(); + + return redisTemplate; + } + + private static Stream connectionFactories() { + return Stream.of(Arguments.of(lettuceConnectionFactory()), Arguments.of(jedisConnectionFactory())); + } + + private static RedisConnectionFactory lettuceConnectionFactory() { + LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory( + new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort())); + lettuceConnectionFactory.afterPropertiesSet(); + return lettuceConnectionFactory; + } + + private static JedisConnectionFactory jedisConnectionFactory() { + JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory( + new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort())); + jedisConnectionFactory.afterPropertiesSet(); + return jedisConnectionFactory; + } + +} 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 + "}"; + } + +}