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.core.ParameterizedTypeReference;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.http.MediaType;
9 import org.springframework.http.codec.ServerSentEvent;
10 import org.springframework.web.reactive.function.client.WebClient;
11 import reactor.core.publisher.Flux;
12 import reactor.core.publisher.Mono;
16 public class TestClient
18 static final ParameterizedTypeReference<ServerSentEvent<String>> SSE_TYPE = new ParameterizedTypeReference<>() {};
21 Mono<ChatRoomInfoTo> createChatRoom(String name)
26 .contentType(MediaType.TEXT_PLAIN)
28 .accept(MediaType.APPLICATION_JSON)
29 .exchangeToMono(response ->
31 if (response.statusCode().equals(HttpStatus.OK))
33 return response.bodyToMono(ChatRoomInfoTo.class);
37 return response.createError();
42 Mono<MessageTo> sendMessage(
43 ChatRoomInfoTo chatRoom,
50 "/{chatRoomId}/{username}/{serial}",
54 .contentType(MediaType.TEXT_PLAIN)
55 .accept(MediaType.APPLICATION_JSON)
57 .exchangeToMono(response ->
59 if (response.statusCode().equals(HttpStatus.OK))
61 return response.bodyToMono(MessageTo.class);
65 return response.createError();
70 Flux<ServerSentEvent<String>> receiveMessages(ChatRoomInfoTo chatRoom)
75 "/{chatRoomId}/listen",
77 .accept(MediaType.TEXT_EVENT_STREAM)
79 .bodyToFlux(SSE_TYPE);
83 private final WebClient webClient;
86 TestClient(Integer port)
88 webClient = WebClient.create("http://localhost:" + port);