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.springframework.http.HttpStatus;
7 import org.springframework.http.MediaType;
8 import org.springframework.web.reactive.function.client.WebClient;
9 import reactor.core.publisher.Mono;
10 import reactor.util.retry.Retry;
12 import java.time.Duration;
13 import java.util.stream.IntStream;
17 public class TestClient
23 .mapToObj(i ->sendMessage(chatRoom, "Message #" + i))
25 .map(MessageTo::toString)
26 .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
28 .forEach(result -> log.info("{}", result));
31 private Mono<MessageTo> sendMessage(
32 ChatRoomInfoTo chatRoom,
38 "/{chatRoomId}/{username}/{serial}",
42 .contentType(MediaType.TEXT_PLAIN)
43 .accept(MediaType.APPLICATION_JSON)
45 .exchangeToMono(response ->
47 if (response.statusCode().equals(HttpStatus.OK))
49 return response.bodyToMono(MessageTo.class);
53 return response.createError();
59 private final WebClient webClient;
60 private final ChatRoomInfoTo chatRoom;
61 private final User user;
64 TestClient(Integer port, ChatRoomInfoTo chatRoom, String username)
66 webClient = WebClient.create("http://localhost:" + port);
67 this.chatRoom = chatRoom;
68 user = new User(username);