X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fapi%2FChatBackendControllerTest.java;h=9b8b09f8f8fa169a11fda4c66a6618a70880def3;hb=33534454c9aafe5f32f238227d1b0852855eb417;hp=f66be45912b792b2664919849e079eff9f46404b;hpb=8837fa6b1caed563ef8fb1929e8d66609477153c;p=demos%2Fkafka%2Fchat 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 f66be459..9b8b09f8 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 @@ -2,7 +2,6 @@ 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 lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -16,10 +15,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.*; @@ -27,9 +23,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 @@ -37,11 +30,9 @@ public class ChatBackendControllerTest { @Autowired ChatBackendProperties properties; - @Autowired - ShardingStrategy shardingStrategy; @MockBean - InMemoryChatHomeService chatHomeService; + ChatHome chatHome; @MockBean ChatRoomService chatRoomService; @@ -50,8 +41,8 @@ public class ChatBackendControllerTest void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client) { // Given - UUID chatroomId = getRandomIdForOwnedShard(); - when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + UUID chatroomId = UUID.randomUUID(); + when(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId)); // When WebTestClient.ResponseSpec responseSpec = client @@ -70,8 +61,8 @@ public class ChatBackendControllerTest void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client) { // Given - UUID chatroomId = getRandomIdForOwnedShard(); - when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + UUID chatroomId = UUID.randomUUID(); + when(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId)); // When WebTestClient.ResponseSpec responseSpec = client @@ -89,10 +80,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(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId)); // When WebTestClient.ResponseSpec responseSpec = client @@ -115,10 +106,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(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId)); // When WebTestClient.ResponseSpec responseSpec = client @@ -140,8 +131,8 @@ public class ChatBackendControllerTest void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client) { // Given - UUID chatroomId = getRandomIdForOwnedShard(); - when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + UUID chatroomId = UUID.randomUUID(); + when(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new UnknownChatroomException(chatroomId)); // When WebTestClient.ResponseSpec responseSpec = client @@ -170,7 +161,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,7 +176,7 @@ public class ChatBackendControllerTest 0, Clock.systemDefaultZone(), chatRoomService, 8); - when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom)); + when(chatHome.getChatRoom(eq(chatroomId))).thenReturn(Mono.just(chatRoom)); Message existingMessage = new Message( key, serialNumberExistingMessage, @@ -226,7 +217,7 @@ 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); @@ -237,7 +228,7 @@ public class ChatBackendControllerTest 0, Clock.systemDefaultZone(), chatRoomService, 8); - when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))) + when(chatHome.getChatRoom(any(UUID.class))) .thenReturn(Mono.just(chatRoom)); when(chatRoomService.getMessage(any(Message.MessageKey.class))) .thenReturn(Mono.empty()); @@ -269,7 +260,9 @@ public class ChatBackendControllerTest void testShardNotOwnedExceptionForGetChatroom(@Autowired WebTestClient client) { // Given - UUID chatroomId = getRandomIdForForeignShard(); + UUID chatroomId = UUID.randomUUID(); + int shard = 666; + when(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard)); // When WebTestClient.ResponseSpec responseSpec = client @@ -279,7 +272,7 @@ public class ChatBackendControllerTest .exchange(); // Then - assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId)); + assertProblemDetailsForShardNotOwnedException(responseSpec, shard); } @Test @@ -287,7 +280,9 @@ public class ChatBackendControllerTest void testShardNotOwnedExceptionForListChatroom(@Autowired WebTestClient client) { // Given - UUID chatroomId = getRandomIdForForeignShard(); + UUID chatroomId = UUID.randomUUID(); + int shard = 666; + when(chatHome.getChatRoom(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard)); // When WebTestClient.ResponseSpec responseSpec = client @@ -297,18 +292,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.getChatRoom(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard)); // When WebTestClient.ResponseSpec responseSpec = client @@ -323,7 +319,7 @@ public class ChatBackendControllerTest .exchange(); // Then - assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId)); + assertProblemDetailsForShardNotOwnedException(responseSpec, shard); } @Test @@ -331,10 +327,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.getChatRoom(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard)); // When WebTestClient.ResponseSpec responseSpec = client @@ -348,7 +345,7 @@ public class ChatBackendControllerTest .exchange(); // Then - assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId)); + assertProblemDetailsForShardNotOwnedException(responseSpec, shard); } @Test @@ -356,8 +353,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.getChatRoom(eq(chatroomId))).thenThrow(new ShardNotOwnedException(shard)); // When WebTestClient.ResponseSpec responseSpec = client @@ -367,7 +365,7 @@ public class ChatBackendControllerTest .exchange(); // Then - assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId)); + assertProblemDetailsForShardNotOwnedException(responseSpec, shard); } private void assertProblemDetailsForShardNotOwnedException( @@ -380,40 +378,4 @@ public class ChatBackendControllerTest .jsonPath("$.type").isEqualTo("/problem/shard-not-owned") .jsonPath("$.shard").isEqualTo(shard); } - - private UUID getRandomIdForOwnedShard() - { - Set ownedShards = ownedShards(); - UUID randomId; - - do - { - randomId = UUID.randomUUID(); - } - while (!ownedShards.contains(shardingStrategy.selectShard(randomId))); - - return randomId; - } - - private UUID getRandomIdForForeignShard() - { - Set ownedShards = ownedShards(); - UUID randomId; - - do - { - randomId = UUID.randomUUID(); - } - while (ownedShards.contains(shardingStrategy.selectShard(randomId))); - - return randomId; - } - - private Set ownedShards() - { - return IntStream - .of(properties.getInmemory().getOwnedShards()) - .mapToObj(shard -> Integer.valueOf(shard)) - .collect(Collectors.toSet()); - } }