test: HandoverIT-POC - Renamed `TestClient` to `TestWriter` -- ALIGN
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / TestWriter.java
1 package de.juplo.kafka.chat.backend;
2
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 org.springframework.web.reactive.function.client.WebClientResponseException;
10 import reactor.core.publisher.Mono;
11 import reactor.util.retry.Retry;
12
13 import java.nio.charset.Charset;
14 import java.time.Duration;
15 import java.util.concurrent.ThreadLocalRandom;
16
17
18 @Slf4j
19 public class TestWriter implements Runnable
20 {
21   @Override
22   public void run()
23   {
24     for (int i = 0; running; i++)
25     {
26       String message = "Message #" + i;
27       for (ChatRoomInfoTo chatRoom : chatRooms)
28       {
29         sendMessage(chatRoom, message)
30             .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
31             .map(MessageTo::toString)
32             .onErrorResume(throwable ->
33             {
34               WebClientResponseException e = (WebClientResponseException)throwable.getCause();
35               return Mono.just(e.getResponseBodyAsString(Charset.defaultCharset()));
36             })
37             .subscribe(result -> log.info(
38                 "{} sent a message to {}: {}",
39                 user,
40                 chatRoom,
41                 result));
42       }
43       try
44       {
45         Thread.sleep(ThreadLocalRandom.current().nextLong(700, 1000));
46       }
47       catch (Exception e)
48       {
49         throw new RuntimeException(e);
50       }
51     }
52   }
53
54   private Mono<MessageTo> sendMessage(
55       ChatRoomInfoTo chatRoom,
56       String message)
57   {
58     return webClient
59         .put()
60         .uri(
61             "/{chatRoomId}/{username}/{serial}",
62             chatRoom.getId(),
63             user.getName(),
64             user.nextSerial())
65         .contentType(MediaType.TEXT_PLAIN)
66         .accept(MediaType.APPLICATION_JSON)
67         .bodyValue(message)
68         .exchangeToMono(response ->
69         {
70           if (response.statusCode().equals(HttpStatus.OK))
71           {
72             return response.bodyToMono(MessageTo.class);
73           }
74           else
75           {
76             return response.createError();
77           }
78         });
79   }
80
81
82   private final WebClient webClient;
83   private final ChatRoomInfoTo[] chatRooms;
84   private final User user;
85
86   volatile boolean running = true;
87
88
89   TestWriter(Integer port, ChatRoomInfoTo[] chatRooms, String username)
90   {
91     webClient = WebClient.create("http://localhost:" + port);
92     this.chatRooms = chatRooms;
93     user = new User(username);
94   }
95 }