From b0c4f7606ec271c4358ece50d754360222f96187 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 11 Sep 2023 16:19:22 +0200 Subject: [PATCH] refactor: Renamed `ChatRoomService` into `ChatMessageService` -- ALIGNE --- .../backend/domain/ChatMessageService.java | 2 +- .../chat/backend/domain/ChatRoomData.java | 6 ++-- .../inmemory/InMemoryChatMessageService.java | 6 ++-- .../inmemory/SimpleChatHomeService.java | 4 +-- .../implementation/kafka/ChatRoomChannel.java | 8 ++--- .../kafka/KafkaChatMessageService.java | 4 +-- .../api/ChatBackendControllerTest.java | 18 +++++----- .../chat/backend/domain/ChatRoomDataTest.java | 36 +++++++++---------- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatMessageService.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatMessageService.java index 374a442b..640dc9e9 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatMessageService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatMessageService.java @@ -6,7 +6,7 @@ import reactor.core.publisher.Mono; import java.time.LocalDateTime; -public interface ChatRoomService +public interface ChatMessageService { Mono persistMessage( Message.MessageKey key, diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomData.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomData.java index 18ca0bf1..d783eb4c 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomData.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoomData.java @@ -19,7 +19,7 @@ public class ChatRoomData { public final static Pattern VALID_USER = Pattern.compile("^[a-z0-9-]{2,}$"); - private final ChatRoomService service; + private final ChatMessageService service; private final Clock clock; private final int bufferSize; private Sinks.Many sink; @@ -27,7 +27,7 @@ public class ChatRoomData public ChatRoomData( Clock clock, - ChatRoomService service, + ChatMessageService service, int bufferSize) { log.info("Created ChatRoom with buffer-size {}", bufferSize); @@ -78,7 +78,7 @@ public class ChatRoomData } - public ChatRoomService getChatRoomService() + public ChatMessageService getChatRoomService() { return service; } diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java index a4fcec8c..8d89aebf 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java @@ -1,7 +1,7 @@ package de.juplo.kafka.chat.backend.implementation.inmemory; +import de.juplo.kafka.chat.backend.domain.ChatMessageService; import de.juplo.kafka.chat.backend.domain.Message; -import de.juplo.kafka.chat.backend.domain.ChatRoomService; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -11,12 +11,12 @@ import java.util.LinkedHashMap; @Slf4j -public class InMemoryChatRoomService implements ChatRoomService +public class InMemoryChatMessageService implements ChatMessageService { private final LinkedHashMap messages; - public InMemoryChatRoomService(Flux messageFlux) + public InMemoryChatMessageService(Flux messageFlux) { log.debug("Creating InMemoryChatRoomService"); messages = new LinkedHashMap<>(); diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java index da01a541..30a181ee 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java @@ -73,7 +73,7 @@ public class SimpleChatHomeService implements ChatHomeService info.getId(), new ChatRoomData( clock, - new InMemoryChatRoomService(messageFlux), + new InMemoryChatMessageService(messageFlux), bufferSize)); }); this.clock = clock; @@ -85,7 +85,7 @@ public class SimpleChatHomeService implements ChatHomeService public Mono createChatRoom(UUID id, String name) { log.info("Creating ChatRoom with buffer-size {}", bufferSize); - ChatRoomService service = new InMemoryChatRoomService(Flux.empty()); + ChatMessageService service = new InMemoryChatMessageService(Flux.empty()); ChatRoomInfo chatRoomInfo = new ChatRoomInfo(id, name, shard); this.chatRoomInfo.put(id, chatRoomInfo); ChatRoomData chatRoomData = new ChatRoomData(clock, service, bufferSize); diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChatRoomChannel.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChatRoomChannel.java index 4143616d..d94bc659 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChatRoomChannel.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChatRoomChannel.java @@ -292,7 +292,7 @@ public class ChatRoomChannel implements Runnable, ConsumerRebalanceListener chatRoomId, partition, bufferSize); - KafkaChatRoomService service = new KafkaChatRoomService(this, chatRoomId); + KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId); ChatRoomData chatRoomData = new ChatRoomData( clock, service, @@ -309,7 +309,7 @@ public class ChatRoomChannel implements Runnable, ConsumerRebalanceListener { UUID id = chatRoomInfo.getId(); log.info("Creating ChatRoom {} with buffer-size {}", id, bufferSize); - KafkaChatRoomService service = new KafkaChatRoomService(this, id); + KafkaChatMessageService service = new KafkaChatMessageService(this, id); ChatRoomData chatRoomData = new ChatRoomData(clock, service, bufferSize); putChatRoom( chatRoomInfo.getId(), @@ -329,8 +329,8 @@ public class ChatRoomChannel implements Runnable, ConsumerRebalanceListener Message message = new Message(key, offset, timestamp, chatMessageTo.getText()); ChatRoomData chatRoomData = this.chatRoomData[partition].get(chatRoomId); - KafkaChatRoomService kafkaChatRoomService = - (KafkaChatRoomService) chatRoomData.getChatRoomService(); + KafkaChatMessageService kafkaChatRoomService = + (KafkaChatMessageService) chatRoomData.getChatRoomService(); kafkaChatRoomService.persistMessage(message); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatMessageService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatMessageService.java index 084798c7..df9ee733 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatMessageService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatMessageService.java @@ -1,6 +1,6 @@ package de.juplo.kafka.chat.backend.implementation.kafka; -import de.juplo.kafka.chat.backend.domain.ChatRoomService; +import de.juplo.kafka.chat.backend.domain.ChatMessageService; import de.juplo.kafka.chat.backend.domain.Message;import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; @@ -13,7 +13,7 @@ import java.util.UUID; @RequiredArgsConstructor @Slf4j -public class KafkaChatRoomService implements ChatRoomService +public class KafkaChatMessageService implements ChatMessageService { private final ChatRoomChannel chatRoomChannel; private final UUID chatRoomId; 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 946498ab..1478e7c5 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 @@ -36,7 +36,7 @@ public class ChatBackendControllerTest @MockBean ChatHomeService chatHomeService; @MockBean - ChatRoomService chatRoomService; + ChatMessageService chatMessageService; @Test @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}") @@ -174,7 +174,7 @@ public class ChatBackendControllerTest String textMutatedMessage = "Mutated!"; ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); when(chatHomeService.getChatRoomData(eq(chatroomId))).thenReturn(Mono.just(chatRoomData)); Message existingMessage = new Message( @@ -182,10 +182,10 @@ public class ChatBackendControllerTest serialNumberExistingMessage, timeExistingMessage, textExistingMessage); - when(chatRoomService.getMessage(any(Message.MessageKey.class))) + when(chatMessageService.getMessage(any(Message.MessageKey.class))) .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))) + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))) .thenReturn(Mono.just(mock(Message.class))); // When @@ -209,7 +209,7 @@ public class ChatBackendControllerTest .jsonPath("$.existingMessage.user").isEqualTo(user) .jsonPath("$.existingMessage.text").isEqualTo(textExistingMessage) .jsonPath("$.mutatedText").isEqualTo(textMutatedMessage); - verify(chatRoomService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class)); + verify(chatMessageService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class)); } @Test @@ -224,14 +224,14 @@ public class ChatBackendControllerTest String textMessage = "Hallo Welt"; ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); when(chatHomeService.getChatRoomData(any(UUID.class))) .thenReturn(Mono.just(chatRoomData)); - when(chatRoomService.getMessage(any(Message.MessageKey.class))) + when(chatMessageService.getMessage(any(Message.MessageKey.class))) .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))) + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))) .thenReturn(Mono.just(mock(Message.class))); // When @@ -250,7 +250,7 @@ public class ChatBackendControllerTest .expectBody() .jsonPath("$.type").isEqualTo("/problem/invalid-username") .jsonPath("$.username").isEqualTo(user); - verify(chatRoomService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class)); + verify(chatMessageService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class)); } @Test diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java index 790482fe..3c7d1dd2 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java @@ -22,15 +22,15 @@ public class ChatRoomDataTest // Given String user = "foo"; Long messageId = 1l; - ChatRoomService chatRoomService = mock(ChatRoomService.class); + ChatMessageService chatMessageService = mock(ChatMessageService.class); ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); Message.MessageKey key = Message.MessageKey.of(user, messageId); LocalDateTime timestamp = LocalDateTime.now(); Message message = new Message(key, 0l, timestamp, "Bar"); - when(chatRoomService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message)); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message)); // When Mono mono = chatRoomData.getMessage(user, messageId); @@ -46,12 +46,12 @@ public class ChatRoomDataTest // Given String user = "foo"; Long messageId = 1l; - ChatRoomService chatRoomService = mock(ChatRoomService.class); + ChatMessageService chatMessageService = mock(ChatMessageService.class); ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); - when(chatRoomService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.empty()); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.empty()); // When Mono mono = chatRoomData.getMessage(user, messageId); @@ -67,18 +67,18 @@ public class ChatRoomDataTest // Given String user = "foo"; Long messageId = 1l; - ChatRoomService chatRoomService = mock(ChatRoomService.class); + ChatMessageService chatMessageService = mock(ChatMessageService.class); ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); Message.MessageKey key = Message.MessageKey.of(user, messageId); Clock now = Clock.fixed(Instant.now(), ZoneId.systemDefault()); LocalDateTime timestamp = LocalDateTime.now(now); 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(Mono.just(message)); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.empty()); + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message)); // When Mono mono = chatRoomData.addMessage(messageId, user, messageText); @@ -94,18 +94,18 @@ public class ChatRoomDataTest // Given String user = "foo"; Long messageId = 1l; - ChatRoomService chatRoomService = mock(ChatRoomService.class); + ChatMessageService chatMessageService = mock(ChatMessageService.class); ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); Message.MessageKey key = Message.MessageKey.of(user, messageId); Clock now = Clock.fixed(Instant.now(), ZoneId.systemDefault()); LocalDateTime timestamp = LocalDateTime.now(now); 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(Mono.just(message)); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message)); + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message)); // When Mono mono = chatRoomData.addMessage(messageId, user, messageText); @@ -121,10 +121,10 @@ public class ChatRoomDataTest // Given String user = "foo"; Long messageId = 1l; - ChatRoomService chatRoomService = mock(ChatRoomService.class); + ChatMessageService chatMessageService = mock(ChatMessageService.class); ChatRoomData chatRoomData = new ChatRoomData( Clock.systemDefaultZone(), - chatRoomService, + chatMessageService, 8); Message.MessageKey key = Message.MessageKey.of(user, messageId); Clock now = Clock.fixed(Instant.now(), ZoneId.systemDefault()); @@ -132,8 +132,8 @@ public class ChatRoomDataTest String messageText = "Bar"; 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(Mono.just(message)); + when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message)); + when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class))).thenReturn(Mono.just(message)); // When Mono mono = chatRoomData.addMessage(messageId, user, mutatedText); -- 2.20.1