From 477329f8d236375582357da714627b60ce0e56fb Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 23 Jan 2023 17:47:44 +0100 Subject: [PATCH] WIP --- .../kafka/KafkaChatHomeService.java | 2 +- .../backend/persistence/kafka/MessageTo.java | 33 ++++++++++++++++ .../persistence/kafka/MessageToTest.java | 39 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageTo.java create mode 100644 src/test/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageToTest.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java index cdeccec5..42ac9430 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java @@ -15,7 +15,7 @@ import java.util.*; @Slf4j public class KafkaChatHomeService implements ChatHomeService, ConsumerRebalanceListener { - private Consumer + private Consumer private final long[] offsets; private final Map[] chatrooms; diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageTo.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageTo.java new file mode 100644 index 00000000..2de8ad58 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageTo.java @@ -0,0 +1,33 @@ +package de.juplo.kafka.chat.backend.persistence.kafka; + +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 +{ + private Long id; + private String user; + private String text; + + public Message toMessage(long offset, LocalDateTime timestamp) + { + return new Message(Message.MessageKey.of(user, id), offset, timestamp, text); + } + + public static MessageTo from(Message message) + { + return + new MessageTo( + message.getId(), + message.getUsername(), + message.getMessageText()); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageToTest.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageToTest.java new file mode 100644 index 00000000..0c4884bf --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/kafka/MessageToTest.java @@ -0,0 +1,39 @@ +package de.juplo.kafka.chat.backend.persistence.kafka; + +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, + "text": "Hallo, ich heiße Peter!", + "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.getText()).isEqualTo("Hallo, ich heiße Peter!"); + assertThat(message.getUser()).isEqualTo("Peter"); + } +} -- 2.20.1