From: Kai Moritz Date: Sun, 8 Jan 2023 11:02:33 +0000 (+0100) Subject: fix: Implemented a test for `MessageTo` and fixed deserialization X-Git-Tag: wip~65 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=42c6495004e3c95808b0889d4456d74bfdcdff70;p=demos%2Fkafka%2Fchat fix: Implemented a test for `MessageTo` and fixed deserialization --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/MessageTo.java b/src/main/java/de/juplo/kafka/chat/backend/api/MessageTo.java index 3525d563..eb508827 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/MessageTo.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/MessageTo.java @@ -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 index 00000000..55fbd015 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/api/MessageToTest.java @@ -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"); + } +}