1 package de.juplo.kafka.chat.backend.persistence;
3 import de.juplo.kafka.chat.backend.domain.*;
4 import de.juplo.kafka.chat.backend.persistence.inmemory.ChatHomeService;
5 import de.juplo.kafka.chat.backend.persistence.inmemory.SimpleChatHome;
6 import lombok.extern.slf4j.Slf4j;
7 import org.junit.jupiter.api.Test;
10 import java.util.UUID;
12 import static pl.rzrz.assertj.reactor.Assertions.*;
16 public abstract class AbstractStorageStrategyIT
18 protected ChatHome chathome;
19 protected ChatRoomFactory chatRoomFactory;
22 protected abstract StorageStrategy getStorageStrategy();
23 protected abstract StorageStrategyITConfig getConfig();
25 protected void start()
27 StorageStrategyITConfig config = getConfig();
28 chathome = new SimpleChatHome(config.getChatHomeService());
29 chatRoomFactory = config.getChatRoomFactory();
34 getStorageStrategy().write(chathome.getChatRooms());
38 protected void testStoreAndRecreate()
42 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
44 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
45 ChatRoomInfo info = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block();
46 log.debug("Created chat-room {}", info);
47 ChatRoom chatroom = chathome.getChatRoom(chatRoomId).block();
48 Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
49 Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
50 Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
51 Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
53 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
54 assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
56 .getChatRoom(chatroom.getId())
57 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
62 assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
63 assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
65 .getChatRoom(chatroom.getId())
66 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
70 protected void testStoreAndRecreateParallelChatRooms()
74 assertThat(chathome.getChatRooms().toStream()).hasSize(0);
76 UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
77 ChatRoomInfo infoA = chatRoomFactory.createChatRoom(chatRoomAId, "FOO").block();
78 log.debug("Created chat-room {}", infoA);
79 ChatRoom chatroomA = chathome.getChatRoom(chatRoomAId).block();
80 Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
81 Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
82 Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
83 Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
85 UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
86 ChatRoomInfo infoB = chatRoomFactory.createChatRoom(chatRoomBId, "BAR").block();
87 log.debug("Created chat-room {}", infoB);
88 ChatRoom chatroomB = chathome.getChatRoom(chatRoomBId).block();
89 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
90 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
91 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
92 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
94 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
95 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
97 .getChatRoom(chatroomA.getId())
98 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
99 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
101 .getChatRoom(chatroomB.getId())
102 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
107 assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
108 assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
110 .getChatRoom(chatroomA.getId())
111 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
112 assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
114 .getChatRoom(chatroomB.getId())
115 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
119 interface StorageStrategyITConfig
121 ChatHomeService getChatHomeService();
122 ChatRoomFactory getChatRoomFactory();