refactor: `storage` is not a sub-package of `persistence` - Moved classes
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / 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 ChatHomeService 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);
31   }
32
33   @Test
34   protected void testStoreAndRecreate()
35   {
36     start();
37
38     assertThat(chathome.getChatRoomInfo().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     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();
48
49     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
50     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
51     assertThat(chathome
52         .getChatRoomData(chatRoomId)
53         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
54
55     stop();
56     start();
57
58     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
59     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
60     assertThat(chathome
61         .getChatRoomData(chatRoomId)
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.getChatRoomInfo().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     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();
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     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();
89
90     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
91     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
92     assertThat(chathome
93         .getChatRoomData(chatRoomAId)
94         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
95     assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
96     assertThat(chathome
97         .getChatRoomData(chatRoomBId)
98         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
99
100     stop();
101     start();
102
103     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
104     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
105     assertThat(chathome
106         .getChatRoomData(chatRoomAId)
107         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108     assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
109     assertThat(chathome
110         .getChatRoomData(chatRoomBId)
111         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
112   }
113
114
115   interface StorageStrategyITConfig
116   {
117     ChatHomeService getChatHome();
118   }
119 }