Skip to content

Commit

Permalink
Generate test app for Kafka using KafkaContainer and apache/kafka-native
Browse files Browse the repository at this point in the history
Spring Boot 3.4.0-M2 supports org.testcontainers.kafka.KafkaContainer.
It also provides Testcontainers 1.20.1, which supports the
apache/kafka-native image.

See gh-1597
  • Loading branch information
eddumelendez authored and mhalbritter committed Sep 25, 2024
1 parent e073867 commit 9414aa4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public SimpleDockerServiceResolver() {
this.dockerServices.put("chroma", chroma());
this.dockerServices.put("elasticsearch", elasticsearch());
this.dockerServices.put("kafka", kafka());
this.dockerServices.put("kafka-native", kafkaNative());
this.dockerServices.put("mariaDb", mariaDb());
this.dockerServices.put("milvus", milvus());
this.dockerServices.put("mongoDb", mongoDb());
Expand Down Expand Up @@ -118,6 +119,13 @@ private static DockerService kafka() {
.build();
}

private static DockerService kafkaNative() {
return DockerService.withImageAndTag("apache/kafka-native")
.website("https://hub.docker.com/r/confluentinc/cp-kafka")
.ports(9092)
.build();
}

private static DockerService mariaDb() {
return DockerService.withImageAndTag("mariadb").website("https://hub.docker.com/_/mariadb").ports(3306).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
import io.spring.initializr.generator.version.VersionParser;
import io.spring.initializr.generator.version.VersionRange;
import io.spring.start.site.container.DockerServiceResolver;
import io.spring.start.site.container.ServiceConnections.ServiceConnection;
import io.spring.start.site.container.ServiceConnectionsCustomizer;
Expand All @@ -34,7 +37,17 @@
@ProjectGenerationConfiguration
class SpringKafkaProjectGenerationConfiguration {

private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.containers.KafkaContainer";
private static final String TESTCONTAINERS_CONFLUENT_CLASS_NAME = "org.testcontainers.containers.KafkaContainer";

private static final String TESTCONTAINERS_APACHE_CLASS_NAME = "org.testcontainers.kafka.KafkaContainer";

private static final VersionRange SPRING_BOOT_3_4_M2_OR_LATER = VersionParser.DEFAULT.parseRange("3.4.0-M2");

private final boolean isSpringBoot34OrLater;

SpringKafkaProjectGenerationConfiguration(ProjectDescription projectDescription) {
this.isSpringBoot34OrLater = SPRING_BOOT_3_4_M2_OR_LATER.match(projectDescription.getPlatformVersion());
}

@Bean
@ConditionalOnRequestedDependency("kafka")
Expand All @@ -47,8 +60,15 @@ SpringKafkaBuildCustomizer springKafkaBuildCustomizer() {
ServiceConnectionsCustomizer kafkaServiceConnectionsCustomizer(Build build, DockerServiceResolver serviceResolver) {
return (serviceConnections) -> {
if (isKafkaEnabled(build)) {
serviceResolver.doWith("kafka", (service) -> serviceConnections.addServiceConnection(
ServiceConnection.ofContainer("kafka", service, TESTCONTAINERS_CLASS_NAME, false)));
if (this.isSpringBoot34OrLater) {
serviceResolver.doWith("kafka-native", (service) -> serviceConnections.addServiceConnection(
ServiceConnection.ofContainer("kafka", service, TESTCONTAINERS_APACHE_CLASS_NAME, false)));
}
else {
serviceResolver.doWith("kafka",
(service) -> serviceConnections.addServiceConnection(ServiceConnection.ofContainer("kafka",
service, TESTCONTAINERS_CONFLUENT_CLASS_NAME, false)));
}
}
};
}
Expand Down

0 comments on commit 9414aa4

Please sign in to comment.