feat: Implemented problem-details for `UnknownChatroomException`
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 16:14:56 +0000 (17:14 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:37:24 +0000 (19:37 +0100)
- 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
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java [new file with mode: 0644]
src/test/resources/application.yml

diff --git a/pom.xml b/pom.xml
index 4b24058..fc9e763 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <version>${assertj-reactor.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
index fb9c0f2..b64f7e0 100644 (file)
@@ -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 (file)
index 0000000..4392968
--- /dev/null
@@ -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());
+  }
+}
index bf920ed..c26dd3e 100644 (file)
@@ -1,3 +1,3 @@
 logging:
   level:
-    de.juplo.kafka.chat.backend.persistence: DEBUG
+    de.juplo.kafka.chat.backend: DEBUG