+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /{chatroomId}")
+ void testShardNotOwnedExceptionForGetChatroom(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = getRandomIdForForeignShard();
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri("/{chatroomId}", chatroomId)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /list/{chatroomId}")
+ void testShardNotOwnedExceptionForListChatroom(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = getRandomIdForForeignShard();
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri("/{chatroomId}/list", chatroomId)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for now owned shard on PUT /put/{chatroomId}/{username}/{messageId}")
+ void testShardNotOwnedExceptionForPutMessage(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = getRandomIdForForeignShard();
+ String username = "foo";
+ Long messageId = 66l;
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .put()
+ .uri(
+ "/{chatroomId}/{username}/{messageId}",
+ chatroomId,
+ username,
+ messageId)
+ .bodyValue("bar")
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /get/{chatroomId}/{username}/{messageId}")
+ void testShardNotOwnedExceptionForGetMessage(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = getRandomIdForForeignShard();
+ String username = "foo";
+ Long messageId = 66l;
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+
+ // When
+ WebTestClient.ResponseSpec responseSpec = client
+ .get()
+ .uri(
+ "/{chatroomId}/{username}/{messageId}",
+ chatroomId,
+ username,
+ messageId)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange();
+
+ // Then
+ assertProblemDetailsForShardNotOwnedException(responseSpec, shardingStrategy.selectShard(chatroomId));
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for not owned shard on GET /listen/{chatroomId}")
+ void testShardNotOwnedExceptionForListenChatroom(@Autowired WebTestClient client)
+ {
+ // Given
+ UUID chatroomId = getRandomIdForForeignShard();
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+
+ // 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, shardingStrategy.selectShard(chatroomId));
+ }
+
+ private void assertProblemDetailsForShardNotOwnedException(
+ WebTestClient.ResponseSpec responseSpec,
+ int shard)
+ {
+ responseSpec
+ .expectStatus().isNotFound()
+ .expectBody()
+ .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());
+ }