1 package de.juplo.kafka.chat.backend.persistence;
3 import de.juplo.kafka.chat.backend.domain.*;
4 import lombok.extern.slf4j.Slf4j;
5 import org.junit.jupiter.api.Test;
9 import java.util.function.Supplier;
11 import static pl.rzrz.assertj.reactor.Assertions.*;
15 public abstract class AbstractStorageStrategyIT
17 protected ChatHome chathome;
18 protected ChatRoomFactory chatRoomFactory;
21 protected abstract StorageStrategy getStorageStrategy();
22 protected abstract Supplier<ChatHomeService> getChatHomeServiceSupplier();
23 protected abstract ChatRoomFactory getChatRoomFactory();
25 protected void start()
27 chathome = new SimpleChatHome(getChatHomeServiceSupplier().get());
28 chatRoomFactory = getChatRoomFactory();
33 getStorageStrategy().write(chathome.getChatRooms());
37 protected void testStoreAndRecreate()
41 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
43 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
44 ChatRoom chatroom = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block();
45 chathome.putChatRoom(chatroom);
46 Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
47 Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
48 Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
49 Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
51 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
52 assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
54 .getChatRoom(chatroom.getId())
55 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
60 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
61 assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
63 .getChatRoom(chatroom.getId())
64 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
68 protected void testStoreAndRecreateParallelChatRooms()
72 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
74 UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
75 ChatRoom chatroomA = chatRoomFactory.createChatRoom(chatRoomAId, "FOO").block();
76 chathome.putChatRoom(chatroomA);
77 Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
78 Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
79 Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
80 Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
82 UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
83 ChatRoom chatroomB = chatRoomFactory.createChatRoom(chatRoomBId, "BAR").block();
84 chathome.putChatRoom(chatroomB);
85 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
86 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
87 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
88 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
90 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
91 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
93 .getChatRoom(chatroomA.getId())
94 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
95 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
97 .getChatRoom(chatroomB.getId())
98 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
103 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
104 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
106 .getChatRoom(chatroomA.getId())
107 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
110 .getChatRoom(chatroomB.getId())
111 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);