package de.juplo.kafka.chat.backend.api;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
import de.juplo.kafka.chat.backend.domain.*;
-import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
+import de.juplo.kafka.chat.backend.domain.exceptions.ShardNotOwnedException;
+import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@SpringBootTest(properties = {
"spring.main.allow-bean-definition-overriding=true",
- "chat.backend.inmemory.sharding-strategy=none" })
+ })
@AutoConfigureWebTestClient
@Slf4j
public class ChatBackendControllerTest
{
+ @Autowired
+ ChatBackendProperties properties;
+
@MockBean
- InMemoryChatHomeService chatHomeService;
+ ChatHomeService chatHomeService;
@MockBean
ChatRoomService chatRoomService;
{
// Given
UUID chatroomId = UUID.randomUUID();
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
// When
WebTestClient.ResponseSpec responseSpec = client
{
// Given
UUID chatroomId = UUID.randomUUID();
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+ when(chatHomeService.getChatRoomInfo(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
// When
WebTestClient.ResponseSpec responseSpec = client
UUID chatroomId = UUID.randomUUID();
String username = "foo";
Long messageId = 66l;
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
// When
WebTestClient.ResponseSpec responseSpec = client
UUID chatroomId = UUID.randomUUID();
String username = "foo";
Long messageId = 66l;
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
// When
WebTestClient.ResponseSpec responseSpec = client
{
// Given
UUID chatroomId = UUID.randomUUID();
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
// When
WebTestClient.ResponseSpec responseSpec = client
@Test
@DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
- void testMessageMutationException(@Autowired WebTestClient client) throws Exception
+ void testMessageMutationException(@Autowired WebTestClient client)
{
// Given
UUID chatroomId = UUID.randomUUID();
LocalDateTime timeExistingMessage = LocalDateTime.parse(timeExistingMessageAsString);
String textExistingMessage = "Existing";
String textMutatedMessage = "Mutated!";
- ChatRoom chatRoom = new ChatRoom(
- chatroomId,
- "Test-ChatRoom",
+ ChatRoomData chatRoomData = new ChatRoomData(
Clock.systemDefaultZone(),
- chatRoomService, 8);
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom));
+ chatRoomService,
+ 8);
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenReturn(Mono.just(chatRoomData));
Message existingMessage = new Message(
key,
serialNumberExistingMessage,
.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
@Test
@DisplayName("Assert expected problem-details for invalid username on PUT /put/{chatroomId}/{username}/{messageId}")
- void testInvalidUsernameException(@Autowired WebTestClient client) throws Exception
+ void testInvalidUsernameException(@Autowired WebTestClient client)
{
// Given
UUID chatroomId = UUID.randomUUID();
Long messageId = 66l;
Message.MessageKey key = Message.MessageKey.of(user, messageId);
String textMessage = "Hallo Welt";
- ChatRoom chatRoom = new ChatRoom(
- chatroomId,
- "Test-ChatRoom",
+ ChatRoomData chatRoomData = new ChatRoomData(
Clock.systemDefaultZone(),
- chatRoomService, 8);
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class)))
- .thenReturn(Mono.just(chatRoom));
+ chatRoomService,
+ 8);
+ when(chatHomeService.getChatRoomData(any(UUID.class)))
+ .thenReturn(Mono.just(chatRoomData));
when(chatRoomService.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)))
- .thenReturn(mock(Message.class));
+ .thenReturn(Mono.just(mock(Message.class)));
// When
client
.jsonPath("$.username").isEqualTo(user);
verify(chatRoomService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class));
}
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /{chatroomId}")
+ void testShardNotOwnedExceptionForGetChatroom(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = UUID.randomUUID();
+ int shard = 666;
+ when(chatHomeService.getChatRoomInfo(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri("/{chatroomId}", chatroomId)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /list/{chatroomId}")
+ void testShardNotOwnedExceptionForListChatroom(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = UUID.randomUUID();
+ int shard = 666;
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri("/{chatroomId}/list", chatroomId)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on PUT /put/{chatroomId}/{username}/{messageId}")
+ void testShardNotOwnedExceptionForPutMessage(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = UUID.randomUUID();
+ String username = "foo";
+ Long messageId = 66l;
+ int shard = 666;
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .put()
+ .uri(
+ "/{chatroomId}/{username}/{messageId}",
+ chatroomId,
+ username,
+ messageId)
+ .bodyValue("bar")
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /get/{chatroomId}/{username}/{messageId}")
+ void testShardNotOwnedExceptionForGetMessage(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = UUID.randomUUID();
+ String username = "foo";
+ Long messageId = 66l;
+ int shard = 666;
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri(
+ "/{chatroomId}/{username}/{messageId}",
+ chatroomId,
+ username,
+ messageId)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /listen/{chatroomId}")
+ void testShardNotOwnedExceptionForListenChatroom(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = UUID.randomUUID();
+ int shard = 666;
+ when(chatHomeService.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri("/{chatroomId}/listen", chatroomId)
+ // .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) << TODO: Does not work!
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
+ }
+
+ private void assertProblemDetailsForShardNotOwnedException(
+ WebTestClient.ResponseSpec responseSpec,
+ int shard)
+ {
+ responseSpec
+ .expectStatus().isNotFound()
+ .expectBody()
+ .jsonPath("$.type").isEqualTo("/problem/shard-not-owned")
+ .jsonPath("$.shard").isEqualTo(shard);
+ }
}