Skip to content

Commit

Permalink
Switch Redis samples to Testcontainers.
Browse files Browse the repository at this point in the history
  • Loading branch information
christophstrobl committed Aug 22, 2024
1 parent 7f3d39d commit dd846bd
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 87 deletions.
10 changes: 7 additions & 3 deletions redis/example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-redis-example-utils</artifactId>
<version>${project.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@
*/
package example.springdata.redis;

import jakarta.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import com.redis.testcontainers.RedisContainer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.utility.DockerImageName;

/**
* @author Christoph Strobl
*/
@SpringBootApplication
public class RedisTestConfiguration {

@Autowired RedisConnectionFactory factory;

/**
* Clear database before shut down.
*/
public @PreDestroy void flushTestDb() {
factory.getConnection().flushDb();
@Bean
@ServiceConnection(name = "redis")
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis:7"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
*/
package example.springdata.redis.commands;

import static org.assertj.core.api.Assertions.*;

import example.springdata.redis.test.condition.EnabledOnCommand;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Circle;
Expand All @@ -37,7 +34,6 @@
* @author Mark Paluch
*/
@SpringBootTest
@EnabledOnCommand("GEOADD")
class GeoOperationsTests {

@Autowired RedisOperations<String, String> operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
*/
package example.springdata.redis.commands;

import example.springdata.redis.test.condition.EnabledOnRedisAvailable;

import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;
import org.springframework.data.redis.connection.RedisConnection;
Expand All @@ -36,7 +33,6 @@
* @author Christoph Strobl
*/
@DataRedisTest
@EnabledOnRedisAvailable
class KeyOperationsTests {

private static final String PREFIX = KeyOperationsTests.class.getSimpleName();
Expand Down
24 changes: 12 additions & 12 deletions redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

</dependencies>

<dependencyManagement>

<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.redis</groupId>
<artifactId>testcontainers-redis</artifactId>
<version>2.2.2</version>
<scope>test</scope>
</dependency>

</dependencyManagement>
</dependencies>

</project>
10 changes: 7 additions & 3 deletions redis/pubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-redis-example-utils</artifactId>
<version>${project.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@
*/
package example.springdata.redis;

import com.redis.testcontainers.RedisContainer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.utility.DockerImageName;

/**
* @author Mark Paluch
* @author Christoph Strobl
*/
@SpringBootApplication
public class RedisTestConfiguration {}
public class RedisTestConfiguration {

@Bean
@ServiceConnection(name = "redis")
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis:7"));
}
}
10 changes: 7 additions & 3 deletions redis/reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-redis-example-utils</artifactId>
<version>${project.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,30 @@
*/
package example.springdata.redis;

import jakarta.annotation.PreDestroy;

import com.redis.testcontainers.RedisContainer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializationContext.RedisSerializationContextBuilder;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.testcontainers.utility.DockerImageName;

/**
* @author Mark Paluch
* @author Christoph Strobl
*/
@SpringBootApplication
public class RedisTestConfiguration {

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
@ServiceConnection(name = "redis")
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis:7"));
}

/**
Expand Down Expand Up @@ -72,10 +74,4 @@ public ReactiveRedisTemplate<String, Object> reactiveJsonObjectRedisTemplate(
return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}

/**
* Clear database before shut down.
*/
public @PreDestroy void flushTestDb() {
redisConnectionFactory().getConnection().flushDb();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@
*/
package example.springdata.redis.commands;

import example.springdata.redis.RedisTestConfiguration;
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Collections;
import java.util.UUID;

import example.springdata.redis.RedisTestConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
Expand All @@ -36,6 +31,8 @@
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.util.ByteUtils;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

/**
* Show usage of reactive operations on Redis keys using low level API provided by
Expand All @@ -44,7 +41,6 @@
* @author Mark Paluch
*/
@SpringBootTest(classes = RedisTestConfiguration.class)
@EnabledOnRedisAvailable
class KeyCommandsTests {

private static final String PREFIX = KeyCommandsTests.class.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import example.springdata.redis.EmailAddress;
import example.springdata.redis.Person;
import example.springdata.redis.RedisTestConfiguration;
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
import lombok.extern.slf4j.Slf4j;
import reactor.test.StepVerifier;

Expand All @@ -39,7 +38,6 @@
*/
@Slf4j
@SpringBootTest(classes = RedisTestConfiguration.class)
@EnabledOnRedisAvailable
class JacksonJsonTests {

@Autowired ReactiveRedisOperations<String, Person> typedOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package example.springdata.redis.operations;

import example.springdata.redis.RedisTestConfiguration;
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand All @@ -38,7 +37,6 @@
*/
@Slf4j
@SpringBootTest(classes = RedisTestConfiguration.class)
@EnabledOnRedisAvailable
class ListOperationsTests {

@Autowired ReactiveRedisOperations<String, String> operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.assertj.core.api.Assertions.*;

import example.springdata.redis.RedisTestConfiguration;
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand All @@ -39,7 +38,6 @@
*/
@Slf4j
@SpringBootTest(classes = RedisTestConfiguration.class)
@EnabledOnRedisAvailable
class ValueOperationsTests {

@Autowired ReactiveRedisOperations<String, String> operations;
Expand Down
10 changes: 4 additions & 6 deletions redis/repositories/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-redis-example-utils</artifactId>
<version>${project.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import static org.assertj.core.api.Assertions.*;

import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
import com.redis.testcontainers.RedisContainer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -29,6 +29,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -40,16 +41,23 @@
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.index.GeoIndexed;
import org.springframework.data.redis.core.index.Indexed;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

/**
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
*/
@Testcontainers
@DataRedisTest
@EnabledOnRedisAvailable
class PersonRepositoryTests {

@Container
@ServiceConnection
static RedisContainer redis = new RedisContainer(DockerImageName.parse("redis:7"));

/** {@link Charset} for String conversion **/
private static final Charset CHARSET = StandardCharsets.UTF_8;

Expand Down
10 changes: 7 additions & 3 deletions redis/streams/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-redis-example-utils</artifactId>
<version>${project.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Loading

0 comments on commit dd846bd

Please sign in to comment.