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;
19 protected abstract StorageStrategy getStorageStrategy();
20 protected abstract StorageStrategyITConfig getConfig();
22 protected void start()
24 StorageStrategyITConfig config = getConfig();
25 chathome = config.getChatHome();
30 getStorageStrategy().write(chathome.getChatRooms());
34 protected void testStoreAndRecreate()
38 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
40 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
41 ChatRoomInfo info = chathome.createChatRoom(chatRoomId, "FOO").block();
42 log.debug("Created chat-room {}", info);
43 ChatRoom chatroom = chathome.getChatRoom(chatRoomId).block();
44 Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
45 Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
46 Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
47 Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
50 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
51 assertThat(chathome.getChatRoom(chatRoomId)).emitsExactly(chatroom);
53 .getChatRoom(chatRoomId)
54 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
59 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
60 assertThat(chathome.getChatRoom(chatRoomId)).emitsExactly(chatroom);
62 .getChatRoom(chatRoomId)
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 ChatRoomInfo infoA = chathome.createChatRoom(chatRoomAId, "FOO").block();
75 log.debug("Created chat-room {}", infoA);
76 ChatRoom chatroomA = chathome.getChatRoom(chatRoomAId).block();
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 ChatRoomInfo infoB = chathome.createChatRoom(chatRoomBId, "BAR").block();
84 log.debug("Created chat-room {}", infoB);
85 ChatRoom chatroomB = chathome.getChatRoom(chatRoomBId).block();
86 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
87 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
88 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
89 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
91 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
92 assertThat(chathome.getChatRoom(chatRoomAId)).emitsExactly(chatroomA);
94 .getChatRoom(chatRoomAId)
95 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
96 assertThat(chathome.getChatRoom(chatRoomBId)).emitsExactly(chatroomB);
98 .getChatRoom(chatRoomBId)
99 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
104 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
105 assertThat(chathome.getChatRoom(chatRoomAId)).emitsExactly(chatroomA);
107 .getChatRoom(chatRoomAId)
108 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
109 assertThat(chathome.getChatRoom(chatRoomBId)).emitsExactly(chatroomB);
111 .getChatRoom(chatRoomBId)
112 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
116 interface StorageStrategyITConfig
118 ChatHome getChatHome();