WIP:refactor
authorKai Moritz <kai@juplo.de>
Tue, 24 Jan 2023 18:05:18 +0000 (19:05 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 24 Jan 2023 18:05:18 +0000 (19:05 +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 4f855b8..f5e3b06 100644 (file)
@@ -71,16 +71,16 @@ public class ChatRoom
             ? Mono.just(existing)
             : Mono.error(() -> new MessageMutationException(existing, text)))
         .switchIfEmpty(
-            Mono
-                .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))
-                .doOnNext(m ->
+            service
+              .persistMessage(key, LocalDateTime.now(clock), text)
+              .doOnNext(m ->
+              {
+                Sinks.EmitResult result = sink.tryEmitNext(m);
+                if (result.isFailure())
                 {
-                  Sinks.EmitResult result = sink.tryEmitNext(m);
-                  if (result.isFailure())
-                  {
-                    log.warn("Emitting of message failed with {} for {}", result.name(), m);
-                  }
-                }));
+                  log.warn("Emitting of message failed with {} for {}", result.name(), 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 dc8dfe8..1cca973 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..af23529 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(mock(Mono.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(mock(Mono.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);