+
+ @Test
+ @DisplayName("Assert expected problem-details for message mutation on PUT /put/{chatroomId}/{username}/{messageId}")
+ void testMessageMutationException(@Autowired WebTestClient client) throws Exception
+ {
+ // Given
+ UUID chatroomId = UUID.randomUUID();
+ String user = "foo";
+ Long messageId = 66l;
+ Message.MessageKey key = Message.MessageKey.of(user, messageId);
+ Long serialNumberExistingMessage = 0l;
+ String timeExistingMessageAsString = "2023-01-09T20:44:57.389665447";
+ LocalDateTime timeExistingMessage = LocalDateTime.parse(timeExistingMessageAsString);
+ String textExistingMessage = "Existing";
+ String textMutatedMessage = "Mutated!";
+ ChatRoom chatRoom = new ChatRoom(
+ chatroomId,
+ "Test-ChatRoom",
+ 0,
+ Clock.systemDefaultZone(),
+ chatRoomService, 8);
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom));
+ Message existingMessage = new Message(
+ key,
+ serialNumberExistingMessage,
+ timeExistingMessage,
+ textExistingMessage);
+ when(chatRoomService.getMessage(any(Message.MessageKey.class)))
+ .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));
+
+ // When
+ client
+ .put()
+ .uri(
+ "/{chatroomId}/{username}/{messageId}",
+ chatroomId,
+ user,
+ messageId)
+ .bodyValue(textMutatedMessage)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange()
+ // Then
+ .expectStatus().is4xxClientError()
+ .expectBody()
+ .jsonPath("$.type").isEqualTo("/problem/message-mutation")
+ .jsonPath("$.existingMessage.id").isEqualTo(messageId)
+ .jsonPath("$.existingMessage.serial").isEqualTo(serialNumberExistingMessage)
+ .jsonPath("$.existingMessage.time").isEqualTo(timeExistingMessageAsString)
+ .jsonPath("$.existingMessage.user").isEqualTo(user)
+ .jsonPath("$.existingMessage.text").isEqualTo(textExistingMessage)
+ .jsonPath("$.mutatedText").isEqualTo(textMutatedMessage);
+ verify(chatRoomService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class));
+ }
+
+ @Test
+ @DisplayName("Assert expected problem-details for invalid username on PUT /put/{chatroomId}/{username}/{messageId}")
+ void testInvalidUsernameException(@Autowired WebTestClient client) throws Exception
+ {
+ // Given
+ 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,
+ Clock.systemDefaultZone(),
+ chatRoomService, 8);
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class)))
+ .thenReturn(Mono.just(chatRoom));
+ 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));
+
+ // When
+ client
+ .put()
+ .uri(
+ "/{chatroomId}/{username}/{messageId}",
+ chatroomId,
+ user,
+ messageId)
+ .bodyValue(textMessage)
+ .accept(MediaType.APPLICATION_JSON)
+ .exchange()
+ // Then
+ .expectStatus().is4xxClientError()
+ .expectBody()
+ .jsonPath("$.type").isEqualTo("/problem/invalid-username")
+ .jsonPath("$.username").isEqualTo(user);
+ verify(chatRoomService, never()).persistMessage(eq(key), any(LocalDateTime.class), any(String.class));
+ }