From f2f0cdc6901d1117ac385e10e2c8a28a1886726c Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 8 Jan 2023 17:14:56 +0100 Subject: [PATCH] feat: Implemented problem-details for `UnknownChatroomException` - Implemented an `@ExceptionHandler` for `UnknownChatroomException`. - Implemented `ChatBackendControllerTest` - Asserted, that the problem-details are generated as expected - Pinned down problems, that yield a refactoring and have to be resolved afterwards. --- pom.xml | 5 + .../api/ChatBackendControllerAdvice.java | 30 ++++ .../api/ChatBackendControllerTest.java | 151 ++++++++++++++++++ src/test/resources/application.yml | 2 +- 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java diff --git a/pom.xml b/pom.xml index 4b240588..fc9e7633 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,11 @@ ${assertj-reactor.version} test + + org.mockito + mockito-core + test + diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java index fb9c0f23..b64f7e00 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java @@ -18,6 +18,36 @@ public class ChatBackendControllerAdvice @Value("${server.context-path:/}") String contextPath; + @ExceptionHandler(UnknownChatroomException.class) + public final ProblemDetail handleException( + UnknownChatroomException e, + ServerWebExchange exchange, + UriComponentsBuilder uriComponentsBuilder) + { + final HttpStatus status = HttpStatus.NOT_FOUND; + ProblemDetail problem = ProblemDetail.forStatus(status); + + problem.setProperty("timestamp", new Date()); + + problem.setProperty("requestId", exchange.getRequest().getId()); + + problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/unknown-chatroom").build().toUri()); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(status.getReasonPhrase()); + stringBuilder.append(" - "); + stringBuilder.append(e.getMessage()); + problem.setTitle(stringBuilder.toString()); + + stringBuilder.setLength(0); + stringBuilder.append("Chatroom unknown: "); + stringBuilder.append(e.getChatroomId()); + problem.setDetail(stringBuilder.toString()); + + problem.setProperty("chatroomId", e.getChatroomId()); + + return problem; + } + @ExceptionHandler(MessageMutationException.class) public final ProblemDetail handleException( MessageMutationException e, 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 new file mode 100644 index 00000000..4392968d --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -0,0 +1,151 @@ +package de.juplo.kafka.chat.backend.api; + +import de.juplo.kafka.chat.backend.domain.*; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; + +import java.util.Optional; +import java.util.UUID; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + + +@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true") +@AutoConfigureWebTestClient +@Slf4j +public class ChatBackendControllerTest +{ + @MockBean + ChatHome chatHome; + + @Disabled + @Test + @DisplayName("Assert expected problem-details for unknown chatroom on GET /list/{chatroomId}") + void testUnknownChatroomExceptionForListChatroom(@Autowired WebTestClient client) + { + // Given + UUID chatroomId = UUID.randomUUID(); + when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty()); + + // When + WebTestClient.ResponseSpec responseSpec = client + .get() + .uri("/list/{chatroomId}", chatroomId) + .accept(MediaType.APPLICATION_JSON) + .exchange(); + + // Then + assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId); + } + + + @Disabled + @Test + @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}") + void testUnknownChatroomExceptionForGetChatroom(@Autowired WebTestClient client) + { + // Given + UUID chatroomId = UUID.randomUUID(); + when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty()); + + // When + WebTestClient.ResponseSpec responseSpec = client + .get() + .uri("/get/{chatroomId}", chatroomId) + .accept(MediaType.APPLICATION_JSON) + .exchange(); + + // Then + assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId); + } + + @Test + @DisplayName("Assert expected problem-details for unknown chatroom on PUT /put/{chatroomId}/{username}/{messageId}") + void testUnknownChatroomExceptionForPutMessage(@Autowired WebTestClient client) + { + // Given + UUID chatroomId = UUID.randomUUID(); + String username = "foo"; + Long messageId = 66l; + when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty()); + + // When + WebTestClient.ResponseSpec responseSpec = client + .put() + .uri( + "/put/{chatroomId}/{username}/{messageId}", + chatroomId, + username, + messageId) + .bodyValue("bar") + .accept(MediaType.APPLICATION_JSON) + .exchange(); + + // Then + assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId); + } + + @Test + @DisplayName("Assert expected problem-details for unknown chatroom on GET /get/{chatroomId}/{username}/{messageId}") + void testUnknownChatroomExceptionForGetMessage(@Autowired WebTestClient client) + { + // Given + UUID chatroomId = UUID.randomUUID(); + String username = "foo"; + Long messageId = 66l; + when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty()); + + // When + WebTestClient.ResponseSpec responseSpec = client + .get() + .uri( + "/get/{chatroomId}/{username}/{messageId}", + chatroomId, + username, + messageId) + .accept(MediaType.APPLICATION_JSON) + .exchange(); + + // Then + assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId); + } + + @Test + @DisplayName("Assert expected problem-details for unknown chatroom on GET /listen/{chatroomId}") + void testUnknownChatroomExceptionForListenChatroom(@Autowired WebTestClient client) + { + // Given + UUID chatroomId = UUID.randomUUID(); + when(chatHome.getChatroom(any(UUID.class))).thenReturn(Optional.empty()); + + // When + WebTestClient.ResponseSpec responseSpec = client + .get() + .uri("/listen/{chatroomId}", chatroomId) + .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) + .exchange(); + + // Then + assertProblemDetailsForUnknownChatroomException(responseSpec, chatroomId); + } + + private void assertProblemDetailsForUnknownChatroomException( + WebTestClient.ResponseSpec responseSpec, + UUID chatroomId) + { + responseSpec + .expectStatus().isNotFound() + .expectBody() + .jsonPath("$.type").isEqualTo("/problem/unknown-chatroom") + .jsonPath("$.chatroomId").isEqualTo(chatroomId.toString()); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index bf920ed9..c26dd3e1 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -1,3 +1,3 @@ logging: level: - de.juplo.kafka.chat.backend.persistence: DEBUG + de.juplo.kafka.chat.backend: DEBUG -- 2.20.1