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 ChatHomeService 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);
34 protected void testStoreAndRecreate()
38 assertThat(chathome.getChatRoomInfo().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 ChatRoomData chatroom = chathome.getChatRoomData(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();
49 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
50 assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
52 .getChatRoomData(chatRoomId)
53 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
58 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
59 assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
61 .getChatRoomData(chatRoomId)
62 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
66 protected void testStoreAndRecreateParallelChatRooms()
70 assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
72 UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
73 ChatRoomInfo infoA = chathome.createChatRoom(chatRoomAId, "FOO").block();
74 log.debug("Created chat-room {}", infoA);
75 ChatRoomData chatroomA = chathome.getChatRoomData(chatRoomAId).block();
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 ChatRoomInfo infoB = chathome.createChatRoom(chatRoomBId, "BAR").block();
83 log.debug("Created chat-room {}", infoB);
84 ChatRoomData chatroomB = chathome.getChatRoomData(chatRoomBId).block();
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.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
91 assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
93 .getChatRoomData(chatRoomAId)
94 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
95 assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
97 .getChatRoomData(chatRoomBId)
98 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
103 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
104 assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
106 .getChatRoomData(chatRoomAId)
107 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108 assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
110 .getChatRoomData(chatRoomBId)
111 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
115 interface StorageStrategyITConfig
117 ChatHomeService getChatHome();