fix: Implemented a test for `MessageTo` and fixed deserialization
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 11:02:33 +0000 (12:02 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 9 Jan 2023 19:57:38 +0000 (20:57 +0100)
src/main/java/de/juplo/kafka/chat/backend/api/MessageTo.java
src/test/java/de/juplo/kafka/chat/backend/api/MessageToTest.java [new file with mode: 0644]

index 3525d56..eb50882 100644 (file)
@@ -3,11 +3,13 @@ package de.juplo.kafka.chat.backend.api;
 import de.juplo.kafka.chat.backend.domain.Message;
 import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.time.LocalDateTime;
 
 
 @Data
+@NoArgsConstructor
 @AllArgsConstructor
 public class MessageTo
 {
diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/MessageToTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/MessageToTest.java
new file mode 100644 (file)
index 0000000..55fbd01
--- /dev/null
@@ -0,0 +1,43 @@
+package de.juplo.kafka.chat.backend.api;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+
+
+public class MessageToTest
+{
+  final String json = """
+  {
+    "id": 1,
+    "serial": 0,
+    "text": "Hallo, ich heiße Peter!",
+    "time": "2023-01-08T00:10:13.625190635",
+    "user": "Peter"
+  }""";
+
+  ObjectMapper mapper;
+
+  @BeforeEach
+  public void setUp()
+  {
+    mapper = new ObjectMapper();
+    mapper.registerModule(new JavaTimeModule());
+    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+  }
+
+  @Test
+  public void testDeserialization() throws Exception
+  {
+    MessageTo message = mapper.readValue(json, MessageTo.class);
+    assertThat(message.getId()).isEqualTo(1l);
+    assertThat(message.getSerial()).isEqualTo(0l);
+    assertThat(message.getText()).isEqualTo("Hallo, ich heiße Peter!");
+    assertThat(message.getTime()).isEqualTo("2023-01-08T00:10:13.625190635");
+    assertThat(message.getUser()).isEqualTo("Peter");
+  }
+}