WIP:test: HandoverIT-POC - splitted up code into smaller classes -- ALIGN
authorKai Moritz <kai@juplo.de>
Tue, 27 Feb 2024 09:13:47 +0000 (10:13 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 27 Feb 2024 10:54:03 +0000 (11:54 +0100)
src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java
src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java
src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverITContainers.java

index 97cf767..d5a4210 100644 (file)
@@ -2,32 +2,12 @@ package de.juplo.kafka.chat.backend;
 
 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
 import de.juplo.kafka.chat.backend.api.MessageTo;
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.Setter;
-import lombok.ToString;
 import lombok.extern.slf4j.Slf4j;
-import org.awaitility.Awaitility;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.springframework.core.ParameterizedTypeReference;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.codec.ServerSentEvent;
-import org.springframework.web.reactive.function.client.WebClient;
-import org.testcontainers.containers.BindMode;
-import org.testcontainers.containers.GenericContainer;
-import org.testcontainers.containers.Network;
-import org.testcontainers.containers.output.Slf4jLogConsumer;
-import org.testcontainers.containers.wait.strategy.Wait;
-import org.testcontainers.images.ImagePullPolicy;
 import org.testcontainers.junit.jupiter.Testcontainers;
-import org.testcontainers.utility.DockerImageName;
-import reactor.core.publisher.Flux;
-import reactor.core.publisher.Mono;
 import reactor.util.retry.Retry;
 
-import java.io.IOException;
 import java.time.Duration;
 import java.util.stream.IntStream;
 
@@ -36,6 +16,9 @@ import java.util.stream.IntStream;
 @Slf4j
 public abstract class AbstractHandoverIT
 {
+  private final AbstractContainerTemplates containerTemplates;
+
+
   @Test
   void test() throws InterruptedException
   {
@@ -57,116 +40,9 @@ public abstract class AbstractHandoverIT
         .block();
   }
 
-
-  abstract void setUpExtra() throws IOException, InterruptedException;
-
   @BeforeEach
   void setUp() throws Exception
   {
-    setUpExtra();
-    haproxy.start();
-    backend1.start();
-    // backend2.start();
-    // backend3.start();
-
-    Awaitility
-        .await()
-        .atMost(Duration.ofMinutes(10))
-        .until(() -> WebClient
-            .create("http://localhost:" + backend1.getMappedPort(8080))
-            .get()
-            .uri("/actuator/health")
-            .exchangeToMono(response ->
-            {
-              if (response.statusCode().equals(HttpStatus.OK))
-              {
-                return response
-                    .bodyToMono(StatusTo.class)
-                    .map(StatusTo::getStatus)
-                    .map(status -> status.equalsIgnoreCase("UP"));
-              }
-              else
-              {
-                return Mono.just(false);
-              }
-            })
-            .block());
-
-    haproxy
-        .getDockerClient()
-        .killContainerCmd(haproxy.getContainerId())
-        .withSignal("HUP")
-        .exec();
-
-
-    Awaitility
-        .await()
-        .atMost(Duration.ofMinutes(10))
-        .until(() -> WebClient
-            .create("http://localhost:" + haproxy.getMappedPort(8400))
-            .get()
-            .uri("/actuator/health")
-            .exchangeToMono(response ->
-            {
-              if (response.statusCode().equals(HttpStatus.OK))
-              {
-                return response
-                    .bodyToMono(StatusTo.class)
-                    .map(StatusTo::getStatus)
-                    .map(status -> status.equalsIgnoreCase("UP"));
-              }
-              else
-              {
-                return Mono.just(false);
-              }
-            })
-            .block());
-  }
-
-  GenericContainer haproxy;
-
-  abstract String[] getCommandBackend1();
-  GenericContainer backend1;
-  abstract String[] getCommandBackend2();
-  GenericContainer backend2;
-
-  abstract String[] getCommandBackend3();
-  GenericContainer backend3 =
-      new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT"))
-          .withImagePullPolicy(NEVER_PULL)
-          .withNetwork(network)
-          .withNetworkAliases("backend-3")
-          .withCommand(getCommandBackend3())
-          .withExposedPorts(8080)
-          .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1))
-          .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3"));
-
-
-  @EqualsAndHashCode
-  @ToString
-  class User
-  {
-    @Getter
-    private final String name;
-    private int serial = 0;
-
-
-    User (String name)
-    {
-      this.name = name;
-    }
-
-
-    int nextSerial()
-    {
-      return ++serial;
-    }
-  }
-
-  @Getter
-  @Setter
-  static class StatusTo
-  {
-    String status;
+    containerTemplates.setUp();
   }
 }
index 1a48e27..751455f 100644 (file)
@@ -1,6 +1,9 @@
 package de.juplo.kafka.chat.backend;
 
 import lombok.extern.slf4j.Slf4j;
+import org.awaitility.Awaitility;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.reactive.function.client.WebClient;
 import org.testcontainers.containers.BindMode;
 import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.containers.Network;
@@ -8,20 +11,96 @@ import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.containers.wait.strategy.Wait;
 import org.testcontainers.images.ImagePullPolicy;
 import org.testcontainers.utility.DockerImageName;
+import reactor.core.publisher.Mono;
 
+import java.time.Duration;
 import java.util.Arrays;
 
 
 @Slf4j
-public abstract class ContainerTemplates
+public abstract class AbstractContainerTemplates
 {
   static final ImagePullPolicy NEVER_PULL = imageName -> false;
-  static final Network NETWORK = Network.newNetwork();
 
-  static GenericContainer createHaproxyContainer()
+
+  final Network network = Network.newNetwork();
+
+  final GenericContainer haproxy, backend1, backend2, backend3;
+
+
+  void setUpExtra() throws Exception
+  {
+    log.info("This setup does not need any extra containers");
+  }
+
+  void setUp() throws Exception
+  {
+    setUpExtra();
+    haproxy.start();
+    backend1.start();
+    // backend2.start();
+    // backend3.start();
+
+    Awaitility
+        .await()
+        .atMost(Duration.ofMinutes(10))
+        .until(() -> WebClient
+            .create("http://localhost:" + backend1.getMappedPort(8080))
+            .get()
+            .uri("/actuator/health")
+            .exchangeToMono(response ->
+            {
+              if (response.statusCode().equals(HttpStatus.OK))
+              {
+                return response
+                    .bodyToMono(StatusTO.class)
+                    .map(StatusTO::getStatus)
+                    .map(status -> status.equalsIgnoreCase("UP"));
+              }
+              else
+              {
+                return Mono.just(false);
+              }
+            })
+            .block());
+
+    haproxy
+        .getDockerClient()
+        .killContainerCmd(haproxy.getContainerId())
+        .withSignal("HUP")
+        .exec();
+
+
+    Awaitility
+        .await()
+        .atMost(Duration.ofMinutes(10))
+        .until(() -> WebClient
+            .create("http://localhost:" + haproxy.getMappedPort(8400))
+            .get()
+            .uri("/actuator/health")
+            .exchangeToMono(response ->
+            {
+              if (response.statusCode().equals(HttpStatus.OK))
+              {
+                return response
+                    .bodyToMono(StatusTO.class)
+                    .map(StatusTO::getStatus)
+                    .map(status -> status.equalsIgnoreCase("UP"));
+              }
+              else
+              {
+                return Mono.just(false);
+              }
+            })
+            .block());
+  }
+
+  abstract String[] getBackendCommand();
+
+  final GenericContainer createHaproxyContainer()
   {
     return new GenericContainer(DockerImageName.parse("haproxytech/haproxy-debian:2.8"))
-        .withNetwork(NETWORK)
+        .withNetwork(network)
         .withNetworkAliases("haproxy")
         .withClasspathResourceMapping(
             "haproxy.cfg",
@@ -35,13 +114,13 @@ public abstract class ContainerTemplates
         .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("HAPROXY"));
   }
 
-  static GenericContainer createBackendContainer(String id, String[] command)
+  final GenericContainer createBackendContainer(String id)
   {
     return new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT"))
       .withImagePullPolicy(NEVER_PULL)
-      .withNetwork(NETWORK)
+      .withNetwork(network)
       .withNetworkAliases("backend-ID".replaceAll("ID", id))
-      .withCommand(Arrays.stream(command)
+      .withCommand(Arrays.stream(getBackendCommand())
           .map(commandPart -> commandPart.replaceAll("ID", id))
           .toArray(size -> new String[size]))
       .withExposedPorts(8080)
index 7c511fd..42380b2 100644 (file)
@@ -1,7 +1,8 @@
 package de.juplo.kafka.chat.backend;
 
 import lombok.extern.slf4j.Slf4j;
-import org.testcontainers.containers.*;
+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;
@@ -10,8 +11,17 @@ import java.io.IOException;
 
 
 @Slf4j
-class KafkaHandoverIT extends AbstractHandoverIT
+class KafkaContainerTemplates extends AbstractContainerTemplates
 {
+  private final KafkaContainer kafka;
+
+
+  KafkaContainerTemplates()
+  {
+    kafka = createKafkaContainer();
+  }
+
+
   @Override
   void setUpExtra() throws IOException, InterruptedException
   {
@@ -48,59 +58,28 @@ class KafkaHandoverIT extends AbstractHandoverIT
         result.getStdout());
   }
 
-
-  KafkaContainer kafka =
-      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[] getCommandBackend1()
+  private final KafkaContainer createKafkaContainer()
   {
-    return new String[]
-    {
-        "--chat.backend.instance-id=backend-1",
-        "--chat.backend.services=kafka",
-        "--chat.backend.kafka.bootstrap-servers=kafka:9999",
-        "--chat.backend.kafka.instance-uri=http://backend-1:8080",
-        "--chat.backend.kafka.num-partitions=10",
-        "--chat.backend.kafka.client-id-prefix=B1",
-        "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401",
-        "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map"
-    };
-  }
-
-  @Override
-  String[] getCommandBackend2()
-  {
-    return new String[]
-    {
-        "--chat.backend.instance-id=backend-2",
-        "--chat.backend.services=kafka",
-        "--chat.backend.kafka.bootstrap-servers=kafka:9999",
-        "--chat.backend.kafka.instance-uri=http://backend-2:8080",
-        "--chat.backend.kafka.num-partitions=10",
-        "--chat.backend.kafka.client-id-prefix=B2",
-        "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401",
-        "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map"
-    };
+    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[] getCommandBackend3()
+  String[] getBackendCommand()
   {
     return new String[]
     {
-        "--chat.backend.instance-id=backend-3",
+        "--chat.backend.instance-id=backend-ID",
         "--chat.backend.services=kafka",
         "--chat.backend.kafka.bootstrap-servers=kafka:9999",
-        "--chat.backend.kafka.instance-uri=http://backend-3:8080",
+        "--chat.backend.kafka.instance-uri=http://backend-ID:8080",
         "--chat.backend.kafka.num-partitions=10",
-        "--chat.backend.kafka.client-id-prefix=B3",
+        "--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"
     };