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