import java.time.LocalDateTime;
-public interface ChatRoomService
+public interface ChatMessageService
{
Mono<Message> persistMessage(
Message.MessageKey key,
{
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<Message> sink;
public ChatRoomData(
Clock clock,
- ChatRoomService service,
+ ChatMessageService service,
int bufferSize)
{
log.info("Created ChatRoom with buffer-size {}", bufferSize);
}
- public ChatRoomService getChatRoomService()
+ public ChatMessageService getChatRoomService()
{
return service;
}
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;
@Slf4j
-public class InMemoryChatRoomService implements ChatRoomService
+public class InMemoryChatMessageService implements ChatMessageService
{
private final LinkedHashMap<Message.MessageKey, Message> messages;
- public InMemoryChatRoomService(Flux<Message> messageFlux)
+ public InMemoryChatMessageService(Flux<Message> messageFlux)
{
log.debug("Creating InMemoryChatRoomService");
messages = new LinkedHashMap<>();
info.getId(),
new ChatRoomData(
clock,
- new InMemoryChatRoomService(messageFlux),
+ new InMemoryChatMessageService(messageFlux),
bufferSize));
});
this.clock = clock;
public Mono<ChatRoomInfo> 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);
chatRoomId,
partition,
bufferSize);
- KafkaChatRoomService service = new KafkaChatRoomService(this, chatRoomId);
+ KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
ChatRoomData chatRoomData = new ChatRoomData(
clock,
service,
{
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(),
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);
}
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;
@RequiredArgsConstructor
@Slf4j
-public class KafkaChatRoomService implements ChatRoomService
+public class KafkaChatMessageService implements ChatMessageService
{
private final ChatRoomChannel chatRoomChannel;
private final UUID chatRoomId;
@MockBean
ChatHomeService chatHomeService;
@MockBean
- ChatRoomService chatRoomService;
+ ChatMessageService chatMessageService;
@Test
@DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}")
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(
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
.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
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
.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
// 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<Message> mono = chatRoomData.getMessage(user, messageId);
// 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<Message> mono = chatRoomData.getMessage(user, messageId);
// 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<Message> mono = chatRoomData.addMessage(messageId, user, messageText);
// 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<Message> mono = chatRoomData.addMessage(messageId, user, messageText);
// 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());
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<Message> mono = chatRoomData.addMessage(messageId, user, mutatedText);