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 ChatRoomInfo info = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block();
44 log.debug("Created chat-room {}", info);
45 ChatRoom chatroom = chathome.getChatRoom(chatRoomId).block();
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 ChatRoomInfo infoA = chatRoomFactory.createChatRoom(chatRoomAId, "FOO").block();
76 log.debug("Created chat-room {}", infoA);
77 ChatRoom chatroomA = chathome.getChatRoom(chatRoomAId).block();
78 Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
79 Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
80 Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
81 Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
83 UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
84 ChatRoomInfo infoB = chatRoomFactory.createChatRoom(chatRoomBId, "BAR").block();
85 log.debug("Created chat-room {}", infoB);
86 ChatRoom chatroomB = chathome.getChatRoom(chatRoomBId).block();
87 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
88 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
89 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
90 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
92 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
93 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
95 .getChatRoom(chatroomA.getId())
96 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
97 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
99 .getChatRoom(chatroomB.getId())
100 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
105 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
106 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
108 .getChatRoom(chatroomA.getId())
109 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
110 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
112 .getChatRoom(chatroomB.getId())
113 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
117 interface StorageStrategyITConfig
119 ChatHomeService getChatHomeService();
120 ChatRoomFactory getChatRoomFactory();