83e905b9bcd0b58cf78dc8691fa12f86a8e39788
[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   protected ChatRoomFactory chatRoomFactory;
18
19
20   protected abstract StorageStrategy getStorageStrategy();
21   protected abstract StorageStrategyITConfig getConfig();
22
23   protected void start()
24   {
25     StorageStrategyITConfig config = getConfig();
26     chathome = new ChatHome(config.getChatHomeService());
27     chatRoomFactory = config.getChatRoomFactory();
28   }
29
30   protected void stop()
31   {
32     getStorageStrategy().write(chathome.getChatRooms());
33   }
34
35   @Test
36   protected void testStoreAndRecreate()
37   {
38     start();
39
40     assertThat(chathome.getChatRooms().toStream()).hasSize(0);
41
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();
50
51     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
52     assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
53     assertThat(chathome
54         .getChatRoom(chatroom.getId())
55         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
56
57     stop();
58     start();
59
60     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
61     assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
62     assertThat(chathome
63         .getChatRoom(chatroom.getId())
64         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
65   }
66
67   @Test
68   protected void testStoreAndRecreateParallelChatRooms()
69   {
70     start();
71
72     assertThat(chathome.getChatRooms().toStream()).hasSize(0);
73
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();
82
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();
91
92     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
93     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
94     assertThat(chathome
95         .getChatRoom(chatroomA.getId())
96         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
97     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
98     assertThat(chathome
99         .getChatRoom(chatroomB.getId())
100         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
101
102     stop();
103     start();
104
105     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
106     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
107     assertThat(chathome
108         .getChatRoom(chatroomA.getId())
109         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
110     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
111     assertThat(chathome
112         .getChatRoom(chatroomB.getId())
113         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
114   }
115
116
117   interface StorageStrategyITConfig
118   {
119     ChatHomeService getChatHomeService();
120     ChatRoomFactory getChatRoomFactory();
121   }
122 }