refactor: Moved exceptions into package `exceptions` - Aligned Code
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / api / ChatBackendControllerTest.java
index 32b1e35..f831dba 100644 (file)
@@ -2,8 +2,8 @@ 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.persistence.inmemory.ShardingStrategy;
+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;
@@ -17,10 +17,7 @@ import reactor.core.publisher.Mono;
 
 import java.time.Clock;
 import java.time.LocalDateTime;
-import java.util.Set;
 import java.util.UUID;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.*;
@@ -28,9 +25,6 @@ import static org.mockito.Mockito.*;
 
 @SpringBootTest(properties = {
     "spring.main.allow-bean-definition-overriding=true",
-    "chat.backend.inmemory.sharding-strategy=kafkalike",
-    "chat.backend.inmemory.num-shards=10",
-    "chat.backend.inmemory.owned-shards=6",
     })
 @AutoConfigureWebTestClient
 @Slf4j
@@ -38,11 +32,9 @@ public class ChatBackendControllerTest
 {
   @Autowired
   ChatBackendProperties properties;
-  @Autowired
-  ShardingStrategy shardingStrategy;
 
   @MockBean
-  InMemoryChatHomeService chatHomeService;
+  ChatHome chatHome;
   @MockBean
   ChatRoomService chatRoomService;
 
@@ -51,9 +43,8 @@ public class ChatBackendControllerTest
   void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 });
+    UUID chatroomId = UUID.randomUUID();
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -72,9 +63,8 @@ public class ChatBackendControllerTest
   void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 });
+    UUID chatroomId = UUID.randomUUID();
+    when(chatHome.getChatRoomInfo(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -92,11 +82,10 @@ public class ChatBackendControllerTest
   void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
+    UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 });
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -119,11 +108,10 @@ public class ChatBackendControllerTest
   void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
+    UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 });
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -145,9 +133,8 @@ public class ChatBackendControllerTest
   void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
-    when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 });
+    UUID chatroomId = UUID.randomUUID();
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -176,7 +163,7 @@ public class ChatBackendControllerTest
   void testMessageMutationException(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
+    UUID chatroomId = UUID.randomUUID();
     String user = "foo";
     Long messageId = 66l;
     Message.MessageKey key = Message.MessageKey.of(user, messageId);
@@ -185,13 +172,11 @@ public class ChatBackendControllerTest
     LocalDateTime timeExistingMessage = LocalDateTime.parse(timeExistingMessageAsString);
     String textExistingMessage = "Existing";
     String textMutatedMessage = "Mutated!";
-    ChatRoom chatRoom = new ChatRoom(
-        chatroomId,
-        "Test-ChatRoom",
-        0,
+    ChatRoomData chatRoomData = new ChatRoomData(
         Clock.systemDefaultZone(),
-        chatRoomService, 8);
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom));
+        chatRoomService,
+        8);
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenReturn(Mono.just(chatRoomData));
     Message existingMessage = new Message(
         key,
         serialNumberExistingMessage,
@@ -232,19 +217,17 @@ public class ChatBackendControllerTest
   void testInvalidUsernameException(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForOwnedShard();
+    UUID chatroomId = UUID.randomUUID();
     String user = "Foo";
     Long messageId = 66l;
     Message.MessageKey key = Message.MessageKey.of(user, messageId);
     String textMessage = "Hallo Welt";
-    ChatRoom chatRoom = new ChatRoom(
-        chatroomId,
-        "Test-ChatRoom",
-        0,
+    ChatRoomData chatRoomData = new ChatRoomData(
         Clock.systemDefaultZone(),
-        chatRoomService, 8);
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class)))
-        .thenReturn(Mono.just(chatRoom));
+        chatRoomService,
+        8);
+    when(chatHome.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
@@ -275,7 +258,9 @@ public class ChatBackendControllerTest
   void testShardNotOwnedExceptionForGetChatroom(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForForeignShard();
+    UUID chatroomId = UUID.randomUUID();
+    int shard = 666;
+    when(chatHome.getChatRoomInfo(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -285,7 +270,7 @@ public class ChatBackendControllerTest
         .exchange();
 
     // Then
-    assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+    assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
   }
 
   @Test
@@ -293,7 +278,9 @@ public class ChatBackendControllerTest
   void testShardNotOwnedExceptionForListChatroom(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForForeignShard();
+    UUID chatroomId = UUID.randomUUID();
+    int shard = 666;
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -303,18 +290,19 @@ public class ChatBackendControllerTest
         .exchange();
 
     // Then
-    assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+    assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
   }
 
   @Test
-  @DisplayName("Assert expected problem-details for now owned shard on PUT /put/{chatroomId}/{username}/{messageId}")
+  @DisplayName("Assert expected problem-details for not owned shard on PUT /put/{chatroomId}/{username}/{messageId}")
   void testShardNotOwnedExceptionForPutMessage(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForForeignShard();
+    UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+    int shard = 666;
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -329,7 +317,7 @@ public class ChatBackendControllerTest
         .exchange();
 
     // Then
-    assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+    assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
   }
 
   @Test
@@ -337,10 +325,11 @@ public class ChatBackendControllerTest
   void testShardNotOwnedExceptionForGetMessage(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForForeignShard();
+    UUID chatroomId = UUID.randomUUID();
     String username = "foo";
     Long messageId = 66l;
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+    int shard = 666;
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -354,7 +343,7 @@ public class ChatBackendControllerTest
         .exchange();
 
     // Then
-    assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+    assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
   }
 
   @Test
@@ -362,8 +351,9 @@ public class ChatBackendControllerTest
   void testShardNotOwnedExceptionForListenChatroom(@Autowired WebTestClient client)
   {
     // Given
-    UUID chatroomId = getRandomIdForForeignShard();
-    when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+    UUID chatroomId = UUID.randomUUID();
+    int shard = 666;
+    when(chatHome.getChatRoomData(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard));
 
     // When
     WebTestClient.ResponseSpec responseSpec = client
@@ -373,7 +363,7 @@ public class ChatBackendControllerTest
         .exchange();
 
     // Then
-    assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+    assertProblemDetailsForShardNotOwnedException(responseSpec, shard);
   }
 
   private void assertProblemDetailsForShardNotOwnedException(
@@ -386,40 +376,4 @@ public class ChatBackendControllerTest
         .jsonPath("$.type").isEqualTo("/problem/shard-not-owned")
         .jsonPath("$.shard").isEqualTo(shard);
   }
-
-  private UUID getRandomIdForOwnedShard()
-  {
-    Set<Integer> ownedShards = ownedShards();
-    UUID randomId;
-
-    do
-    {
-      randomId = UUID.randomUUID();
-    }
-    while (!ownedShards.contains(shardingStrategy.selectShard(randomId)));
-
-    return randomId;
-  }
-
-  private UUID getRandomIdForForeignShard()
-  {
-    Set<Integer> ownedShards = ownedShards();
-    UUID randomId;
-
-    do
-    {
-      randomId = UUID.randomUUID();
-    }
-    while (ownedShards.contains(shardingStrategy.selectShard(randomId)));
-
-    return randomId;
-  }
-
-  private Set<Integer> ownedShards()
-  {
-    return IntStream
-        .of(properties.getInmemory().getOwnedShards())
-        .mapToObj(shard -> Integer.valueOf(shard))
-        .collect(Collectors.toSet());
-  }
 }