-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from infobip/reuse-container
Reusable testcontainers
- Loading branch information
Showing
54 changed files
with
585 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 19 additions & 18 deletions
37
...ain/java/com/infobip/testcontainers/spring/clickhouse/ClickhouseContainerInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
package com.infobip.testcontainers.spring.clickhouse; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import com.infobip.testcontainers.InitializerBase; | ||
import org.springframework.boot.test.util.TestPropertyValues; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class ClickhouseContainerInitializer extends InitializerBase<ClickhouseContainerWrapper> { | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
Environment environment = applicationContext.getEnvironment(); | ||
Optional<String> customPropertyPath = Optional.ofNullable(environment.getProperty("testcontainers.clickhouse.custom-path")); | ||
String jdbcUrlPropertyPath = customPropertyPath.orElse("spring.datasource") + ".jdbc-url"; | ||
String jdbcUrlValue = Objects.requireNonNull(environment.getProperty(jdbcUrlPropertyPath)); | ||
ClickhouseContainerWrapper container = Optional.ofNullable( | ||
environment.getProperty("testcontainers.clickhouse.docker.image.version")) | ||
.map(ClickhouseContainerWrapper::new) | ||
.orElseGet(ClickhouseContainerWrapper::new); | ||
|
||
Optional.ofNullable(environment.getProperty("testcontainers.clickhouse.init-script")).ifPresent(container::withInitScript); | ||
var environment = applicationContext.getEnvironment(); | ||
var customPropertyPath = Optional.ofNullable(environment.getProperty("testcontainers.clickhouse.custom-path")); | ||
var jdbcUrlPropertyPath = customPropertyPath.orElse("spring.datasource") + ".jdbc-url"; | ||
var jdbcUrlValue = Objects.requireNonNull(environment.getProperty(jdbcUrlPropertyPath)); | ||
var wrapper = Optional.ofNullable(environment.getProperty("testcontainers.clickhouse.docker.image.version")) | ||
.map(ClickhouseContainerWrapper::new) | ||
.orElseGet(ClickhouseContainerWrapper::new); | ||
var container = handleReusable(wrapper); | ||
|
||
Optional.ofNullable(environment.getProperty("testcontainers.clickhouse.init-script")) | ||
.ifPresent(container::withInitScript); | ||
|
||
resolveStaticPort(jdbcUrlValue, GENERIC_URL_WITH_PORT_GROUP_PATTERN) | ||
.ifPresent(staticPort -> bindPort(container, staticPort, ClickhouseContainerWrapper.HTTP_PORT)); | ||
.ifPresent(staticPort -> bindPort(container, staticPort, ClickhouseContainerWrapper.HTTP_PORT)); | ||
|
||
start(container); | ||
|
||
String url = replaceHostAndPortPlaceholders(jdbcUrlValue, container, ClickhouseContainerWrapper.HTTP_PORT); | ||
var url = replaceHostAndPortPlaceholders(jdbcUrlValue, container, ClickhouseContainerWrapper.HTTP_PORT); | ||
|
||
TestPropertyValues values = TestPropertyValues.of(String.format("%s=%s", jdbcUrlPropertyPath, url)); | ||
var values = TestPropertyValues.of(String.format("%s=%s", jdbcUrlPropertyPath, url)); | ||
values.applyTo(applicationContext); | ||
} | ||
|
||
registerContainerAsBean(applicationContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
...ip/testcontainers/spring/clickhouse/ClickhouseContainerInitializerWithInitScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...obip/testcontainers/spring/clickhouse/ClickhouseContainerInitializerWithReusableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.infobip.testcontainers.spring.clickhouse; | ||
|
||
import com.infobip.testcontainers.ReusableTestBase; | ||
import lombok.AllArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.testcontainers.containers.ClickHouseContainer; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
|
||
@AllArgsConstructor | ||
@ActiveProfiles("reusable") | ||
class ClickhouseContainerInitializerWithReusableTest extends ReusableTestBase { | ||
|
||
private final ClickhouseContainerWrapper wrapper; | ||
private final int port = ClickHouseContainer.NATIVE_PORT; | ||
|
||
@Test | ||
void shouldReuseContainer() { | ||
// given | ||
var givenContainer = new ClickhouseContainerWrapper(); | ||
givenContainer.withReuse(true); | ||
|
||
// when | ||
givenContainer.start(); | ||
|
||
// then | ||
then(givenContainer.getMappedPort(port)).isEqualTo(wrapper.getMappedPort(port)); | ||
} | ||
|
||
} |
17 changes: 7 additions & 10 deletions
17
...ip/testcontainers/spring/clickhouse/ClickhouseContainerInitializerWithStaticPortTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
...-spring-boot-starter/src/test/java/com/infobip/testcontainers/spring/clickhouse/Main.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...lickhouse-testcontainers-spring-boot-starter/src/test/resources/application-reusable.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
spring: | ||
datasource: | ||
clickhouse: | ||
jdbc-url: jdbc:clickhouse://<host>:<port> | ||
|
||
testcontainers: | ||
clickhouse: | ||
custom-path: "spring.datasource.clickhouse" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 54 additions & 36 deletions
90
...rter/src/main/java/com/infobip/testcontainers/spring/kafka/KafkaContainerInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,84 @@ | ||
package com.infobip.testcontainers.spring.kafka; | ||
|
||
import static java.lang.Integer.parseInt; | ||
import static java.lang.Short.parseShort; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.infobip.testcontainers.InitializerBase; | ||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.springframework.boot.test.util.TestPropertyValues; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.*; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.lang.Integer.parseInt; | ||
import static java.lang.Short.parseShort; | ||
|
||
public class KafkaContainerInitializer extends InitializerBase<KafkaContainerWrapper> { | ||
|
||
private static final Pattern KAFKA_SERVER_PATTERN = Pattern.compile("^.*:(\\d+).*"); | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
Environment environment = applicationContext.getEnvironment(); | ||
String bootstrapServers = Objects.requireNonNull(environment.getProperty("spring.kafka.bootstrap-servers")); | ||
KafkaContainerWrapper container = Optional.ofNullable( | ||
environment.getProperty("testcontainers.kafka.docker.image.version")) | ||
.map(KafkaContainerWrapper::new) | ||
.orElseGet(KafkaContainerWrapper::new); | ||
var environment = applicationContext.getEnvironment(); | ||
var bootstrapServers = Objects.requireNonNull(environment.getProperty("spring.kafka.bootstrap-servers")); | ||
var wrapper = Optional.ofNullable(environment.getProperty("testcontainers.kafka.docker.image.version")) | ||
.map(KafkaContainerWrapper::new) | ||
.orElseGet(KafkaContainerWrapper::new); | ||
var container = handleReusable(wrapper); | ||
|
||
resolveStaticPort(bootstrapServers, KAFKA_SERVER_PATTERN) | ||
.ifPresent(staticPort -> bindPort(container, staticPort, KafkaContainerWrapper.KAFKA_PORT)); | ||
.ifPresent(staticPort -> bindPort(container, staticPort, KafkaContainerWrapper.KAFKA_PORT)); | ||
|
||
start(container); | ||
String url = replaceHostAndPortPlaceholders(bootstrapServers, container, KafkaContainerWrapper.KAFKA_PORT); | ||
var url = replaceHostAndPortPlaceholders(bootstrapServers, container, KafkaContainerWrapper.KAFKA_PORT); | ||
|
||
Optional.ofNullable(environment.getProperty("testcontainers.kafka.topics", String[].class)) | ||
.ifPresent(topics -> createTestKafkaTopics(url, topics)); | ||
TestPropertyValues values = TestPropertyValues.of( | ||
"spring.kafka.bootstrap-servers=" + url); | ||
.ifPresent(topics -> createTestKafkaTopics(container, url, topics)); | ||
var values = TestPropertyValues.of( | ||
"spring.kafka.bootstrap-servers=" + url); | ||
values.applyTo(applicationContext); | ||
} | ||
|
||
private static void createTestKafkaTopics(String bootstrapServers, String[] topics) { | ||
registerContainerAsBean(applicationContext); | ||
} | ||
|
||
List<NewTopic> newTopics = Stream.of(topics) | ||
.map(topic -> topic.split(":")) | ||
.map(topicParts -> new NewTopic(topicParts[0], parseInt(topicParts[1]), | ||
parseShort(topicParts[2]))) | ||
.collect(Collectors.toList()); | ||
private static void createTestKafkaTopics(KafkaContainerWrapper container, String bootstrapServers, String[] topics) { | ||
|
||
try { | ||
AdminClient.create(Collections.singletonMap("bootstrap.servers", bootstrapServers)) | ||
.createTopics(newTopics) | ||
.all() | ||
.get(60, TimeUnit.SECONDS); | ||
try (var client = AdminClient.create(Collections.singletonMap("bootstrap.servers", bootstrapServers))) { | ||
if(container.isShouldBeReused()) { | ||
deleteTopics(client, topics); | ||
} | ||
createTopics(client, topics); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static void deleteTopics(AdminClient client, String[] topics) throws ExecutionException, | ||
InterruptedException, TimeoutException { | ||
var existingTopics = client.listTopics().names().get(60, TimeUnit.SECONDS); | ||
var deleteTopics = Stream.of(topics) | ||
.map(topic -> topic.split(":")) | ||
.filter(topic -> !existingTopics.contains(topic[0])) | ||
.map(topicParts -> topicParts[0]) | ||
.toList(); | ||
|
||
client.deleteTopics(deleteTopics); | ||
} | ||
|
||
private static void createTopics(AdminClient client, String[] topics) throws InterruptedException, | ||
ExecutionException, TimeoutException { | ||
var existingTopics = client.listTopics().names().get(60, TimeUnit.SECONDS); | ||
var newTopics = Stream.of(topics) | ||
.map(topic -> topic.split(":")) | ||
.filter(topic -> !existingTopics.contains(topic[0])) | ||
.map(topicParts -> new NewTopic(topicParts[0], parseInt(topicParts[1]), | ||
parseShort(topicParts[2]))) | ||
.toList(); | ||
|
||
client.createTopics(newTopics) | ||
.all() | ||
.get(60, TimeUnit.SECONDS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.