Skip to content

Add Redis integration tests #4892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
<nashorn.version>15.6</nashorn.version>
<beanshell.version>2.0b6</beanshell.version>
<jruby.version>9.4.12.0</jruby.version>
<lettuce.version>6.6.0.RELEASE</lettuce.version>
<testcontainers-redis.version>2.2.4</testcontainers-redis.version>

<!-- samples dependencies -->
<spring-rabbit.version>${spring-amqp.version}</spring-rabbit.version>
Expand Down
12 changes: 12 additions & 0 deletions spring-batch-infrastructure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,18 @@
<version>${jruby.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>${lettuce.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.redis</groupId>
<artifactId>testcontainers-redis</artifactId>
<version>${testcontainers-redis.version}</version>
<scope>test</scope>
</dependency>

<!-- provided dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Person> reader;

private RedisTemplate<String, Person> 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<String, Person> redisTemplate = setUpRedisTemplate();
ScanOptions scanOptions = ScanOptions.scanOptions().match("person:*").count(10).build();
this.reader = new RedisItemReader<>(redisTemplate, scanOptions);

this.reader.open(new ExecutionContext());

List<Person> 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<String, Person> setUpRedisTemplate() {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(
new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort()));
connectionFactory.afterPropertiesSet();

RedisTemplate<String, Person> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();

return redisTemplate;
}

}
Original file line number Diff line number Diff line change
@@ -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<String, Person> writer;

private RedisTemplate<String, Person> template;

@BeforeEach
void setUp() {
this.template = setUpRedisTemplate();
this.template.getConnectionFactory().getConnection().serverCommands().flushAll();
}

@Test
void testWrite() throws Exception {
RedisTemplate<String, Person> redisTemplate = setUpRedisTemplate();
this.writer = new RedisItemWriter<>();
this.writer.setRedisTemplate(redisTemplate);
this.writer.setItemKeyMapper(p -> "person:" + p.getId());
this.writer.setDelete(false);

Chunk<Person> 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<String, Person> redisTemplate = setUpRedisTemplate();
this.writer = new RedisItemWriter<>();
this.writer.setRedisTemplate(redisTemplate);
this.writer.setItemKeyMapper(p -> "person:" + p.getId());
this.writer.setDelete(true);

Chunk<Person> 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<String, Person> setUpRedisTemplate() {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(
new RedisStandaloneConfiguration(redis.getRedisHost(), redis.getRedisPort()));
connectionFactory.afterPropertiesSet();

RedisTemplate<String, Person> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();

return redisTemplate;
}

}
Original file line number Diff line number Diff line change
@@ -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 + "}";
}

}