From: Kai Moritz Date: Tue, 27 Feb 2024 10:54:49 +0000 (+0100) Subject: WIP:test: HandoverIT-POC - splitted up code into smaller classes -- ALIGN X-Git-Tag: rebase--2024-02-27--11-55 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=7f8bccd1324f613e405d4ce8d46f201515bede4d;p=demos%2Fkafka%2Fchat WIP:test: HandoverIT-POC - splitted up code into smaller classes -- ALIGN --- diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java index af5c9583..dfbef4d4 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java @@ -21,15 +21,15 @@ import java.util.stream.IntStream; @Testcontainers @Slf4j -public abstract class HandoverIT +public abstract class AbstractHandoverIT { static final ParameterizedTypeReference> SSE_TYPE = new ParameterizedTypeReference<>() {}; - private final HandoverITContainers containers; + private final AbstractHandoverITContainers containers; - HandoverIT(HandoverITContainers containers) + AbstractHandoverIT(AbstractHandoverITContainers containers) { this.containers = containers; } diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java index 876d117c..d34d5541 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java @@ -18,7 +18,7 @@ import java.util.Arrays; @Slf4j -public abstract class HandoverITContainers +public abstract class AbstractHandoverITContainers { static final ImagePullPolicy NEVER_PULL = imageName -> false; @@ -27,7 +27,7 @@ public abstract class HandoverITContainers final GenericContainer haproxy, backend1, backend2, backend3; - HandoverITContainers() + AbstractHandoverITContainers() { haproxy = createHaproxyContainer(); backend1 = createBackendContainer("1"); diff --git a/src/test/java/de/juplo/kafka/chat/backend/KafkaAbstractHandoverITContainers.java b/src/test/java/de/juplo/kafka/chat/backend/KafkaAbstractHandoverITContainers.java new file mode 100644 index 00000000..0b4553b4 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/KafkaAbstractHandoverITContainers.java @@ -0,0 +1,87 @@ +package de.juplo.kafka.chat.backend; + +import lombok.extern.slf4j.Slf4j; +import org.testcontainers.containers.Container; +import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; + +import java.io.IOException; + + +@Slf4j +class KafkaAbstractHandoverITContainers extends AbstractHandoverITContainers +{ + private final KafkaContainer kafka; + + + KafkaAbstractHandoverITContainers() + { + kafka = createKafkaContainer(); + } + + + @Override + void setUpExtra() throws IOException, InterruptedException + { + kafka.start(); + + Container.ExecResult result; + result = kafka.execInContainer( + "kafka-topics", + "--bootstrap-server", + "kafka:9999", + "--create", + "--topic", + "info_channel", + "--partitions", + "3"); + log.info( + "EXIT-CODE={}, STDOUT={}, STDERR={}", + result.getExitCode(), + result.getStdout(), + result.getStdout()); + result = kafka.execInContainer( + "kafka-topics", + "--bootstrap-server", + "kafka:9999", + "--create", + "--topic", + "data_channel", + "--partitions", + "10"); + log.info( + "EXIT-CODE={}, STDOUT={}, STDERR={}", + result.getExitCode(), + result.getStdout(), + result.getStdout()); + } + + private final KafkaContainer createKafkaContainer() + { + return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) + .withNetwork(network) + .withNetworkAliases("kafka") + .withListener(() -> "kafka:9999") + .withKraft() + .waitingFor(Wait.forLogMessage(".*Kafka\\ Server\\ started.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("KAFKA")); + } + + @Override + String[] getBackendCommand() + { + return new String[] + { + "--chat.backend.instance-id=backend-ID", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-ID:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=BID", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + }; + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java index eaa24094..53315f18 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java @@ -4,10 +4,10 @@ import lombok.extern.slf4j.Slf4j; @Slf4j -class KafkaHandoverIT extends HandoverIT +class KafkaHandoverIT extends AbstractHandoverIT { KafkaHandoverIT() { - super(new KafkaHandoverITContainers()); + super(new KafkaAbstractHandoverITContainers()); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverITContainers.java b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverITContainers.java deleted file mode 100644 index 27eb044d..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverITContainers.java +++ /dev/null @@ -1,87 +0,0 @@ -package de.juplo.kafka.chat.backend; - -import lombok.extern.slf4j.Slf4j; -import org.testcontainers.containers.Container; -import org.testcontainers.containers.KafkaContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.utility.DockerImageName; - -import java.io.IOException; - - -@Slf4j -class KafkaHandoverITContainers extends HandoverITContainers -{ - private final KafkaContainer kafka; - - - KafkaHandoverITContainers() - { - kafka = createKafkaContainer(); - } - - - @Override - void setUpExtra() throws IOException, InterruptedException - { - kafka.start(); - - Container.ExecResult result; - result = kafka.execInContainer( - "kafka-topics", - "--bootstrap-server", - "kafka:9999", - "--create", - "--topic", - "info_channel", - "--partitions", - "3"); - log.info( - "EXIT-CODE={}, STDOUT={}, STDERR={}", - result.getExitCode(), - result.getStdout(), - result.getStdout()); - result = kafka.execInContainer( - "kafka-topics", - "--bootstrap-server", - "kafka:9999", - "--create", - "--topic", - "data_channel", - "--partitions", - "10"); - log.info( - "EXIT-CODE={}, STDOUT={}, STDERR={}", - result.getExitCode(), - result.getStdout(), - result.getStdout()); - } - - private final KafkaContainer createKafkaContainer() - { - return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) - .withNetwork(network) - .withNetworkAliases("kafka") - .withListener(() -> "kafka:9999") - .withKraft() - .waitingFor(Wait.forLogMessage(".*Kafka\\ Server\\ started.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("KAFKA")); - } - - @Override - String[] getBackendCommand() - { - return new String[] - { - "--chat.backend.instance-id=backend-ID", - "--chat.backend.services=kafka", - "--chat.backend.kafka.bootstrap-servers=kafka:9999", - "--chat.backend.kafka.instance-uri=http://backend-ID:8080", - "--chat.backend.kafka.num-partitions=10", - "--chat.backend.kafka.client-id-prefix=BID", - "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", - "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" - }; - } -}