refactor: Simplified implementation - Removed interface `ChatRoomFactory`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / persistence / AbstractStorageStrategyIT.java
1 package de.juplo.kafka.chat.backend.persistence;
2
3 import de.juplo.kafka.chat.backend.domain.*;
4 import lombok.extern.slf4j.Slf4j;
5 import org.junit.jupiter.api.Test;
6
7 import java.util.List;
8 import java.util.UUID;
9
10 import static pl.rzrz.assertj.reactor.Assertions.*;
11
12
13 @Slf4j
14 public abstract class AbstractStorageStrategyIT
15 {
16   protected ChatHome chathome;
17
18
19   protected abstract StorageStrategy getStorageStrategy();
20   protected abstract StorageStrategyITConfig getConfig();
21
22   protected void start()
23   {
24     StorageStrategyITConfig config = getConfig();
25     chathome = config.getChatHome();
26   }
27
28   protected void stop()
29   {
30     getStorageStrategy().write(chathome.getChatRooms());
31   }
32
33   @Test
34   protected void testStoreAndRecreate()
35   {
36     start();
37
38     assertThat(chathome.getChatRooms().toStream()).hasSize(0);
39
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();
48
49     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
50     assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
51     assertThat(chathome
52         .getChatRoom(chatroom.getId())
53         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
54
55     stop();
56     start();
57
58     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
59     assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
60     assertThat(chathome
61         .getChatRoom(chatroom.getId())
62         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
63   }
64
65   @Test
66   protected void testStoreAndRecreateParallelChatRooms()
67   {
68     start();
69
70     assertThat(chathome.getChatRooms().toStream()).hasSize(0);
71
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     ChatRoom chatroomA = chathome.getChatRoom(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();
80
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     ChatRoom chatroomB = chathome.getChatRoom(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();
89
90     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
91     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
92     assertThat(chathome
93         .getChatRoom(chatroomA.getId())
94         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
95     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
96     assertThat(chathome
97         .getChatRoom(chatroomB.getId())
98         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
99
100     stop();
101     start();
102
103     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
104     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
105     assertThat(chathome
106         .getChatRoom(chatroomA.getId())
107         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
109     assertThat(chathome
110         .getChatRoom(chatroomB.getId())
111         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
112   }
113
114
115   interface StorageStrategyITConfig
116   {
117     ChatHome getChatHome();
118   }
119 }