Skip to content

Commit

Permalink
added redis reusable support
Browse files Browse the repository at this point in the history
  • Loading branch information
lpandzic committed Aug 7, 2023
1 parent b91c93e commit 8e6a676
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ public class RedisContainerInitializer extends InitializerBase<RedisContainerWra

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
Environment environment = applicationContext.getEnvironment();
String redisUrl = Objects.requireNonNull(environment.getProperty("spring.data.redis.url"));
RedisContainerWrapper container = Optional.ofNullable(
environment.getProperty("testcontainers.redis.docker.image"))
.map(RedisContainerWrapper::new)
.orElseGet(() -> new RedisContainerWrapper("redis:6.2.6-alpine"));
var environment = applicationContext.getEnvironment();
var redisUrl = Objects.requireNonNull(environment.getProperty("spring.data.redis.url"));
var wrapper = Optional.ofNullable(environment.getProperty("testcontainers.redis.docker.image"))
.map(RedisContainerWrapper::new)
.orElseGet(() -> new RedisContainerWrapper("redis:6.2.6-alpine"));
var container = handleReusable(environment, wrapper);
resolveStaticPort(redisUrl, GENERIC_URL_WITH_PORT_GROUP_PATTERN)
.ifPresent(staticPort -> bindPort(container, staticPort, RedisContainerWrapper.PORT));
.ifPresent(staticPort -> bindPort(container, staticPort, RedisContainerWrapper.PORT));

start(container);

String url = replaceHostAndPortPlaceholders(redisUrl, container, RedisContainerWrapper.PORT);
TestPropertyValues values = TestPropertyValues.of("spring.data.redis.url=" + url);
var url = replaceHostAndPortPlaceholders(redisUrl, container, RedisContainerWrapper.PORT);
var values = TestPropertyValues.of("spring.data.redis.url=" + url);
values.applyTo(applicationContext);
}

registerContainerAsBean(applicationContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.infobip.testcontainers.spring.redis;

import lombok.AllArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

import static org.assertj.core.api.BDDAssertions.then;

@AllArgsConstructor
@ActiveProfiles("reusable")
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@SpringBootTest(classes = Main.class)
class RedisContainerInitializerWithReusableTest {

private final RedisContainerWrapper wrapper;
private final int port = RedisContainerWrapper.PORT;

@Test
void shouldReuseContainer() {
// given
var givenContainer = new RedisContainerWrapper();
givenContainer.withReuse(true);

// when
givenContainer.start();

// then
then(givenContainer.getMappedPort(port)).isEqualTo(wrapper.getMappedPort(port));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
data:
redis:
url: redis://<host>:<port>

testcontainers.reuse.enable: true

0 comments on commit 8e6a676

Please sign in to comment.