refactor: Introduced `AbstractStorageStrategyIT` as base for more tests
authorKai Moritz <kai@juplo.de>
Mon, 9 Jan 2023 20:37:55 +0000 (21:37 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 20:59:37 +0000 (21:59 +0100)
- Moved the test-logic, that does not depend on implementation-details
  into an abstract base-class `AbstractStorageStrategyID`.
- `LocalJsonFilesStorageStrategyIT` extends this class and only instances
  for that specific implementation and the accompanying setup.

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

diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java
new file mode 100644 (file)
index 0000000..524e6f1
--- /dev/null
@@ -0,0 +1,63 @@
+package de.juplo.kafka.chat.backend.persistence;
+
+import de.juplo.kafka.chat.backend.domain.ChatHome;
+import de.juplo.kafka.chat.backend.domain.ChatHomeService;
+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.Test;
+
+import java.util.List;
+import java.util.function.Supplier;
+
+import static pl.rzrz.assertj.reactor.Assertions.*;
+
+
+@Slf4j
+public abstract class AbstractStorageStrategyIT
+{
+  ChatHome chathome;
+
+
+  abstract StorageStrategy getStorageStrategy();
+  abstract Supplier<ChatHomeService> chatHomeServiceSupplier();
+
+  void start()
+  {
+    chathome = new ChatHome(chatHomeServiceSupplier().get());
+  }
+
+  void stop()
+  {
+    getStorageStrategy().writeChatrooms(chathome.getChatRooms());
+  }
+
+  @Test
+  void testStoreAndRecreate()
+  {
+    start();
+
+    assertThat(chathome.getChatRooms().toStream()).hasSize(0);
+
+    ChatRoom chatroom = chathome.createChatroom("FOO").block();
+    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.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
+    assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
+    assertThat(chathome
+        .getChatRoom(chatroom.getId())
+        .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
+
+    stop();
+    start();
+
+    assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
+    assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
+    assertThat(chathome
+        .getChatRoom(chatroom.getId())
+        .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
+  }
+}
index fd413c3..515942c 100644 (file)
@@ -3,75 +3,49 @@ 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 de.juplo.kafka.chat.backend.domain.ChatHomeService;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
 
 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 pl.rzrz.assertj.reactor.Assertions.*;
+import java.util.function.Supplier;
 
 
 @Slf4j
-public class LocalJsonFilesStorageStrategyIT
+public class LocalJsonFilesStorageStrategyIT extends AbstractStorageStrategyIT
 {
   final static Path path = Paths.get("target","local-json-files");
 
-  InMemoryChatHomeService chatHomeService;
-  StorageStrategy storageStrategy;
-  ChatHome chathome;
+  final Clock clock;
+  final ObjectMapper mapper;
+  final LocalJsonFilesStorageStrategy storageStrategy;
+
 
-  void start()
+  public LocalJsonFilesStorageStrategyIT()
   {
-    Clock clock = Clock.systemDefaultZone();
-    ObjectMapper mapper = new ObjectMapper();
+    clock = Clock.systemDefaultZone();
+    mapper = new ObjectMapper();
     mapper.registerModule(new JavaTimeModule());
     mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
     storageStrategy = new LocalJsonFilesStorageStrategy(path, clock, 8, mapper);
-    chatHomeService = new InMemoryChatHomeService(storageStrategy.readChatrooms(), clock, 8);
-    chathome = new ChatHome(chatHomeService);
+
   }
 
-  void stop()
+
+  @Override
+  StorageStrategy getStorageStrategy()
   {
-    storageStrategy.writeChatrooms(chathome.getChatRooms());
+    return storageStrategy;
   }
 
-  @Test
-  void testStoreAndRecreate()
+  @Override
+  Supplier<ChatHomeService> chatHomeServiceSupplier()
   {
-    start();
-
-    assertThat(chathome.getChatRooms().toStream()).hasSize(0);
-
-    ChatRoom chatroom = chathome.createChatroom("FOO").block();
-    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.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
-    assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
-    assertThat(chathome
-        .getChatRoom(chatroom.getId())
-        .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
-
-    stop();
-    start();
-
-    assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
-    assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
-    assertThat(chathome
-        .getChatRoom(chatroom.getId())
-        .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
+    return () -> new InMemoryChatHomeService(getStorageStrategy().readChatrooms(), clock, 8);
   }
 
   @BeforeEach