d5e02b834bc766faed1971f5ba2bba03f4cb3fa8
[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 SimpleChatHome(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     ChatRoom chatroom = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block();
44     chathome.putChatRoom(chatroom);
45     Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
46     Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
47     Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
48     Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
49
50     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
51     assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
52     assertThat(chathome
53         .getChatRoom(chatroom.getId())
54         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
55
56     stop();
57     start();
58
59     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
60     assertThat(chathome.getChatRoom(chatroom.getId())).emitsExactly(chatroom);
61     assertThat(chathome
62         .getChatRoom(chatroom.getId())
63         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
64   }
65
66   @Test
67   protected void testStoreAndRecreateParallelChatRooms()
68   {
69     start();
70
71     assertThat(chathome.getChatRooms().toStream()).hasSize(0);
72
73     UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
74     ChatRoom chatroomA = chatRoomFactory.createChatRoom(chatRoomAId, "FOO").block();
75     chathome.putChatRoom(chatroomA);
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     ChatRoom chatroomB = chatRoomFactory.createChatRoom(chatRoomBId, "BAR").block();
83     chathome.putChatRoom(chatroomB);
84     Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
85     Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
86     Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
87     Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
88
89     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
90     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
91     assertThat(chathome
92         .getChatRoom(chatroomA.getId())
93         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
94     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
95     assertThat(chathome
96         .getChatRoom(chatroomB.getId())
97         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
98
99     stop();
100     start();
101
102     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
103     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
104     assertThat(chathome
105         .getChatRoom(chatroomA.getId())
106         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
107     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
108     assertThat(chathome
109         .getChatRoom(chatroomB.getId())
110         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
111   }
112
113
114   interface StorageStrategyITConfig
115   {
116     ChatHomeService getChatHomeService();
117     ChatRoomFactory getChatRoomFactory();
118   }
119 }