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;
10 import static pl.rzrz.assertj.reactor.Assertions.*;
14 public abstract class AbstractStorageStrategyIT
16 protected ChatHome chathome;
17 protected ChatRoomFactory chatRoomFactory;
20 protected abstract StorageStrategy getStorageStrategy();
21 protected abstract StorageStrategyITConfig getConfig();
23 protected void start()
25 StorageStrategyITConfig config = getConfig();
26 chathome = new SimpleChatHome(config.getChatHomeService());
27 chatRoomFactory = config.getChatRoomFactory();
32 getStorageStrategy().write(chathome.getChatRooms());
36 protected void testStoreAndRecreate()
40 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
42 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
43 ChatRoom chatroom = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block();
44 chathome.putChatRoom(chatroom);
45 Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
46 Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
47 Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
48 Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
50 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
51 assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
53 .getChatRoom(chatroom.getId())
54 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
59 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
60 assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
62 .getChatRoom(chatroom.getId())
63 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
67 protected void testStoreAndRecreateParallelChatRooms()
71 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
73 UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
74 ChatRoom chatroomA = chatRoomFactory.createChatRoom(chatRoomAId, "FOO").block();
75 chathome.putChatRoom(chatroomA);
76 Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
77 Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
78 Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
79 Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
81 UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
82 ChatRoom chatroomB = chatRoomFactory.createChatRoom(chatRoomBId, "BAR").block();
83 chathome.putChatRoom(chatroomB);
84 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
85 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
86 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
87 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
89 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
90 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
92 .getChatRoom(chatroomA.getId())
93 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
94 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
96 .getChatRoom(chatroomB.getId())
97 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
102 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
103 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
105 .getChatRoom(chatroomA.getId())
106 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
107 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
109 .getChatRoom(chatroomB.getId())
110 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
114 interface StorageStrategyITConfig
116 ChatHomeService getChatHomeService();
117 ChatRoomFactory getChatRoomFactory();