From 7281b4220170f451062bb32e0d5f63ec48d141d3 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 24 Jan 2023 19:05:18 +0100 Subject: [PATCH] refactor: `ChatRoomService.persistMessage(..)` returns a `Mono` --- .../java/de/juplo/kafka/chat/backend/domain/ChatRoom.java | 2 +- .../de/juplo/kafka/chat/backend/domain/ChatRoomService.java | 2 +- .../persistence/inmemory/InMemoryChatRoomService.java | 4 ++-- .../kafka/chat/backend/api/ChatBackendControllerTest.java | 4 ++-- .../de/juplo/kafka/chat/backend/domain/ChatRoomTest.java | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java index 02f5c08c..da5eba2a 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java @@ -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); diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomService.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomService.java index c70ffe4e..374a442b 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomService.java @@ -8,7 +8,7 @@ import java.time.LocalDateTime; public interface ChatRoomService { - Message persistMessage( + Mono persistMessage( Message.MessageKey key, LocalDateTime timestamp, String text); diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatRoomService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatRoomService.java index 314e1f03..e1d5a5e3 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatRoomService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatRoomService.java @@ -24,14 +24,14 @@ public class InMemoryChatRoomService implements ChatRoomService } @Override - public Message persistMessage( + public Mono 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 diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java index 1b25a11e..b72294d9 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -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 diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomTest.java index 9c418f17..822ffe77 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomTest.java @@ -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 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 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 mono = chatRoom.addMessage(messageId, user, mutatedText); -- 2.20.1