test: HandoverIT-POC - Refactored `TestListener`
authorKai Moritz <kai@juplo.de>
Mon, 11 Mar 2024 17:15:32 +0000 (18:15 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 14 Mar 2024 08:11:21 +0000 (09:11 +0100)
src/test/java/de/juplo/kafka/chat/backend/TestListener.java

index e58cb2b..e5b2529 100644 (file)
@@ -28,28 +28,34 @@ public class TestListener
         .fromArray(chatRooms)
         .flatMap(chatRoom ->
         {
-          log.info("Requesting messages for chat-room {}", chatRoom);
           List<MessageTo> list = new LinkedList<>();
           receivedMessages.put(chatRoom.getId(), list);
-          return receiveMessages(chatRoom)
-              .flatMap(sse ->
-              {
-                try
-                {
-                  return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
-                }
-                catch (Exception e)
-                {
-                  return Mono.error(e);
-                }
-              })
-              .doOnNext(message -> list.add(message))
-              .doOnComplete(() -> log.info("{} was completed!", chatRoom))
-              .doOnError(throwalbe -> log.error("{} failed: {}", chatRoom, throwalbe));
+          return receiveMessages(chatRoom);
         });
   }
 
-  Flux<ServerSentEvent<String>> receiveMessages(ChatRoomInfoTo chatRoom)
+  Flux<MessageTo> receiveMessages(ChatRoomInfoTo chatRoom)
+  {
+    log.info("Requesting messages for chat-room {}", chatRoom);
+    List<MessageTo> list = receivedMessages.get(chatRoom.getId());
+    return receiveServerSentEvents(chatRoom)
+        .flatMap(sse ->
+        {
+          try
+          {
+            return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
+          }
+          catch (Exception e)
+          {
+            return Mono.error(e);
+          }
+        })
+        .doOnNext(message -> list.add(message))
+        .doOnComplete(() -> log.info("{} was completed!", chatRoom))
+        .doOnError(throwalbe -> log.error("{} failed: {}", chatRoom, throwalbe));
+  }
+
+  Flux<ServerSentEvent<String>> receiveServerSentEvents(ChatRoomInfoTo chatRoom)
   {
     return webClient
         .get()