refactor: `ChatRoomService.persistMessage(..)` returns a `Mono<Message>`
authorKai Moritz <kai@juplo.de>
Tue, 24 Jan 2023 18:05:18 +0000 (19:05 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 21:11:58 +0000 (22:11 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomService.java
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatRoomService.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java
src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomTest.java

index 02f5c08..da5eba2 100644 (file)
@@ -81,7 +81,7 @@ public class ChatRoom
         })
         .switchIfEmpty(
             Mono
-                .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))
+                .defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
                 .doOnNext(m ->
                 {
                   Sinks.EmitResult result = sink.tryEmitNext(m);
index c70ffe4..374a442 100644 (file)
@@ -8,7 +8,7 @@ import java.time.LocalDateTime;
 
 public interface ChatRoomService
 {
-  Message persistMessage(
+  Mono<Message> persistMessage(
       Message.MessageKey key,
       LocalDateTime timestamp,
       String text);
index 314e1f0..e1d5a5e 100644 (file)
@@ -24,14 +24,14 @@ public class InMemoryChatRoomService implements ChatRoomService
   }
 
   @Override
-  public Message persistMessage(
+  public Mono<Message> persistMessage(
       Message.MessageKey key,
       LocalDateTime timestamp,
       String text)
   {
     Message message = new Message(key, (long)messages.size(), timestamp, text);
     messages.put(message.getKey(), message);
-    return message;
+    return Mono.just(message);
   }
 
   @Override
index 1b25a11..b72294d 100644 (file)
@@ -183,7 +183,7 @@ public class ChatBackendControllerTest
         .thenReturn(Mono.just(existingMessage));
     // Needed for readable error-reports, in case of a bug that leads to according unwanted call
     when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class)))
-        .thenReturn(mock(Message.class));
+        .thenReturn(Mono.just(mock(Message.class)));
 
     // When
     client
@@ -231,7 +231,7 @@ public class ChatBackendControllerTest
         .thenReturn(Mono.empty());
     // Needed for readable error-reports, in case of a bug that leads to according unwanted call
     when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class)))
-        .thenReturn(mock(Message.class));
+        .thenReturn(Mono.just(mock(Message.class)));
 
     // When
     client
index 9c418f1..822ffe7 100644 (file)
@@ -88,7 +88,7 @@ public class ChatRoomTest
     String messageText = "Bar";
     Message message = new Message(key, 0l, timestamp, messageText);
     when(chatRoomService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.empty());
-    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(message);
+    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message));
 
     // When
     Mono<Message> mono = chatRoom.addMessage(messageId, user, messageText);
@@ -118,7 +118,7 @@ public class ChatRoomTest
     String messageText = "Bar";
     Message message = new Message(key, 0l, timestamp, messageText);
     when(chatRoomService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
-    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(message);
+    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message));
 
     // When
     Mono<Message> mono = chatRoom.addMessage(messageId, user, messageText);
@@ -149,7 +149,7 @@ public class ChatRoomTest
     String mutatedText = "Boom!";
     Message message = new Message(key, 0l, timestamp, messageText);
     when(chatRoomService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
-    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(message);
+    when(chatRoomService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message));
 
     // When
     Mono<Message> mono = chatRoom.addMessage(messageId, user, mutatedText);