test: Implemented a first IT for `LocalJsonFilesStorageStrategy`
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 10:14:45 +0000 (11:14 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:35:59 +0000 (19:35 +0100)
- This first version of the test does not realy test the storing.
- It is only a proof-of-concept for the setup.

src/test/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategyIT.java [new file with mode: 0644]
src/test/resources/application.yml [new file with mode: 0644]

diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategyIT.java
new file mode 100644 (file)
index 0000000..d136fa9
--- /dev/null
@@ -0,0 +1,95 @@
+package de.juplo.kafka.chat.backend.persistence;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import de.juplo.kafka.chat.backend.domain.ChatHome;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
+import de.juplo.kafka.chat.backend.domain.Message;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Flux;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.Clock;
+import java.util.List;
+
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+
+
+@Slf4j
+public class LocalJsonFilesStorageStrategyIT
+{
+  final static Path path = Paths.get("target","local-json-files");
+
+  InMemoryChatHomeService service;
+  StorageStrategy storageStrategy;
+  ChatHome chathome;
+
+  void start()
+  {
+    Clock clock = Clock.systemDefaultZone();
+    service = new InMemoryChatHomeService(clock, 8);
+    ObjectMapper mapper = new ObjectMapper();
+    mapper.registerModule(new JavaTimeModule());
+    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+    storageStrategy = new LocalJsonFilesStorageStrategy(path, mapper, service);
+    chathome = new ChatHome(service, storageStrategy.readChatrooms());
+  }
+
+  void stop()
+  {
+    storageStrategy.writeChatrooms(Flux.fromStream(chathome.list()));
+  }
+
+  @Test
+  void testStoreAndRecreate()
+  {
+    start();
+
+    assertThat(chathome.list()).hasSize(0);
+
+    ChatRoom chatroom = chathome.createChatroom("FOO");
+    Message m1 = chatroom.addMessage(1l,"Peter", "Hallo, ich heiße Peter!").block();
+    Message m2 = chatroom.addMessage(1l, "Ute", "Ich bin Ute...").block();
+    Message m3 = chatroom.addMessage(2l, "Peter", "Willst du mit mir gehen?").block();
+    Message m4 = chatroom.addMessage(1l, "Klaus", "Ja? Nein? Vielleicht??").block();
+
+    assertThat(chathome.list()).containsExactlyElementsOf(List.of(chatroom));
+    assertThat(chathome.getChatroom(chatroom.getId())).contains(chatroom);
+    assertThat(chathome.getChatroom(chatroom.getId()).get().getMessages().toStream()).containsExactlyElementsOf(List.of(m1, m2, m3, m4));
+
+    stop();
+  }
+
+  @BeforeEach
+  void reset() throws Exception
+  {
+    if (Files.exists(path))
+    {
+      Files
+          .walk(path)
+          .forEach(file ->
+          {
+            try
+            {
+              if (!file.equals(path))
+              {
+                log.debug("Deleting file {}", file);
+                Files.delete(file);
+              }
+            }
+            catch (IOException e)
+            {
+              throw new RuntimeException(e);
+            }
+          });
+      log.debug("Deleting data-directory {}", path);
+      Files.delete(path);
+    }
+  }
+}
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
new file mode 100644 (file)
index 0000000..bf920ed
--- /dev/null
@@ -0,0 +1,3 @@
+logging:
+  level:
+    de.juplo.kafka.chat.backend.persistence: DEBUG