FIX
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / AbstractStorageStrategyIT.java
1 package de.juplo.kafka.chat.backend;
2
3 import de.juplo.kafka.chat.backend.domain.*;
4 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
5 import lombok.extern.slf4j.Slf4j;
6 import org.junit.jupiter.api.Test;
7
8 import java.util.List;
9 import java.util.UUID;
10
11 import static pl.rzrz.assertj.reactor.Assertions.*;
12
13
14 @Slf4j
15 public abstract class AbstractStorageStrategyIT
16 {
17   protected ChatHomeService chathome;
18
19
20   protected abstract StorageStrategy getStorageStrategy();
21   protected abstract StorageStrategyITConfig getConfig();
22
23   protected void start()
24   {
25     StorageStrategyITConfig config = getConfig();
26     chathome = config.getChatHome();
27   }
28
29   protected void stop()
30   {
31     getStorageStrategy()
32         .write(chathome)
33         .subscribe();
34   }
35
36   @Test
37   protected void testStoreAndRecreate()
38   {
39     start();
40
41     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
42
43     UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
44     ChatRoomInfo info = chathome.createChatRoom(chatRoomId, "FOO").block();
45     log.debug("Created chat-room {}", info);
46     ChatRoomData chatroom = chathome.getChatRoomData(chatRoomId).block();
47     Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
48     Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
49     Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
50     Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
51
52     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
53     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
54     assertThat(chathome
55         .getChatRoomData(chatRoomId)
56         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
57
58     stop();
59     start();
60
61     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
62     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
63     assertThat(chathome
64         .getChatRoomData(chatRoomId)
65         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
66   }
67
68   @Test
69   protected void testStoreAndRecreateParallelChatRooms()
70   {
71     start();
72
73     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
74
75     UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
76     ChatRoomInfo infoA = chathome.createChatRoom(chatRoomAId, "FOO").block();
77     log.debug("Created chat-room {}", infoA);
78     ChatRoomData chatroomA = chathome.getChatRoomData(chatRoomAId).block();
79     Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
80     Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
81     Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
82     Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
83
84     UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
85     ChatRoomInfo infoB = chathome.createChatRoom(chatRoomBId, "BAR").block();
86     log.debug("Created chat-room {}", infoB);
87     ChatRoomData chatroomB = chathome.getChatRoomData(chatRoomBId).block();
88     Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
89     Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
90     Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
91     Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
92
93     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
94     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
95     assertThat(chathome
96         .getChatRoomData(chatRoomAId)
97         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
98     assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
99     assertThat(chathome
100         .getChatRoomData(chatRoomBId)
101         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
102
103     stop();
104     start();
105
106     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
107     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
108     assertThat(chathome
109         .getChatRoomData(chatRoomAId)
110         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
111     assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
112     assertThat(chathome
113         .getChatRoomData(chatRoomBId)
114         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
115   }
116
117
118   interface StorageStrategyITConfig
119   {
120     ChatHomeService getChatHome();
121   }
122 }