void testGetExistingMessage()
{
// Given
- Message message = new Message(key, 0l, timestamp, "Bar");
- when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
+ when(chatMessageService.getMessage(any(Message.MessageKey.class)))
+ .thenReturn(Mono.just(someMessage()));
// When
Mono<Message> mono = chatRoomData.getMessage(user, messageId);
// Then
- assertThat(mono).emitsExactly(message);
+ assertThat(mono).emitsExactly(someMessage());
}
@Test
void testGetNonExistentMessage()
{
// Given
- when(chatMessageService.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);
void testAddNewMessageEmitsPersistedMessage()
{
// Given
- String messageText = "Bar";
- Message message = new Message(key, 0l, timestamp, messageText);
- 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(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(someMessage()));
// When
- Mono<Message> mono = chatRoomData.addMessage(messageId, user, messageText);
+ Mono<Message> mono = chatRoomData.addMessage(messageId, user, "Some Text");
// Then
- assertThat(mono).emitsExactly(message);
+ assertThat(mono).emitsExactly(someMessage());
}
@Test
{
// Given
String messageText = "Bar";
- Message message = new Message(key, 0l, timestamp, messageText);
- 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(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(someMessage()));
// When
chatRoomData
{
// Given
String messageText = "Bar";
- Message message = new Message(key, 0l, timestamp, messageText);
- when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
+ Message existingMessage = new Message(key, 0l, timestamp, messageText);
+ when(chatMessageService.getMessage(any(Message.MessageKey.class)))
+ .thenReturn(Mono.just(existingMessage));
// When
Mono<Message> mono = chatRoomData.addMessage(messageId, user, messageText);
// Then
- assertThat(mono).emitsExactly(message);
+ assertThat(mono).emitsExactly(existingMessage);
}
@Test
{
// Given
String messageText = "Bar";
- Message message = new Message(key, 0l, timestamp, messageText);
- when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
+ Message existingMessage = new Message(key, 0l, timestamp, messageText);
+ when(chatMessageService.getMessage(any(Message.MessageKey.class)))
+ .thenReturn(Mono.just(existingMessage));
// When
chatRoomData
// Given
String messageText = "Bar";
String mutatedText = "Boom!";
- Message message = new Message(key, 0l, timestamp, messageText);
- when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
+ Message existingMessage = new Message(key, 0l, timestamp, messageText);
+ when(chatMessageService.getMessage(any(Message.MessageKey.class)))
+ .thenReturn(Mono.just(existingMessage));
// When
Mono<Message> mono = chatRoomData.addMessage(messageId, user, mutatedText);
// Given
String messageText = "Bar";
String mutatedText = "Boom!";
- Message message = new Message(key, 0l, timestamp, messageText);
- when(chatMessageService.getMessage(any(Message.MessageKey.class))).thenReturn(Mono.just(message));
+ Message existingMessage = new Message(key, 0l, timestamp, messageText);
+ when(chatMessageService.getMessage(any(Message.MessageKey.class)))
+ .thenReturn(Mono.just(existingMessage));
// When
chatRoomData
// Then
verify(chatMessageService, never()).persistMessage(any(), any(), any());
}
+
+
+ /**
+ * This message is used, when methods of {@link ChatMessageService} are mocked,
+ * that return a {@link Message}.
+ * The contents of the message are set to arbitrary values, in order to underline
+ * the fact, that the test can only assert, that the message that was returned
+ * by {@link ChatMessageService} is handed on by {@link ChatRoomData} correctly.
+ * @return a message.
+ */
+ private Message someMessage()
+ {
+ return new Message(
+ Message.MessageKey.of("FOO", 666l),
+ 666l,
+ LocalDateTime.of(2024, 3, 8, 12, 13, 00),
+ "Just some message...");
+ }
}