1 package de.juplo.kafka.chat.backend;
3 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
4 import de.juplo.kafka.chat.backend.api.MessageTo;
5 import lombok.extern.slf4j.Slf4j;
6 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.Test;
8 import org.springframework.core.ParameterizedTypeReference;
9 import org.springframework.http.HttpStatus;
10 import org.springframework.http.MediaType;
11 import org.springframework.http.codec.ServerSentEvent;
12 import org.springframework.web.reactive.function.client.WebClient;
13 import org.testcontainers.junit.jupiter.Testcontainers;
14 import reactor.core.publisher.Flux;
15 import reactor.core.publisher.Mono;
16 import reactor.util.retry.Retry;
18 import java.time.Duration;
19 import java.util.stream.IntStream;
24 public abstract class HandoverIT
26 static final ParameterizedTypeReference<ServerSentEvent<String>> SSE_TYPE = new ParameterizedTypeReference<>() {};
29 private final HandoverITContainers containers;
32 HandoverIT(HandoverITContainers containers)
34 this.containers = containers;
39 void test() throws InterruptedException
41 ChatRoomInfoTo chatRoom = createChatRoom("bar").block();
42 TestClient testClient = new TestClient(
43 containers.haproxy.getMappedPort(8400),
47 .mapToObj(i ->testClient.sendMessage(chatRoom, "Message #" + i))
49 .map(MessageTo::toString)
50 .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
52 .forEach(result -> log.info("{}", result));
54 receiveMessages(chatRoom)
56 .doOnNext(message -> log.info("message: {}", message))
61 Mono<ChatRoomInfoTo> createChatRoom(String name)
66 .contentType(MediaType.TEXT_PLAIN)
68 .accept(MediaType.APPLICATION_JSON)
69 .exchangeToMono(response ->
71 if (response.statusCode().equals(HttpStatus.OK))
73 return response.bodyToMono(ChatRoomInfoTo.class);
77 return response.createError();
82 Flux<ServerSentEvent<String>> receiveMessages(ChatRoomInfoTo chatRoom)
87 "/{chatRoomId}/listen",
89 .accept(MediaType.TEXT_EVENT_STREAM)
91 .bodyToFlux(SSE_TYPE);
98 void setUp() throws Exception
102 Integer port = containers.haproxy.getMappedPort(8400);
103 webClient = WebClient.create("http://localhost:" + port);