5d22d12e43f6ded1610be02c86832ff6e87b65e5
[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 import java.util.function.Supplier;
10
11 import static pl.rzrz.assertj.reactor.Assertions.*;
12
13
14 @Slf4j
15 public abstract class AbstractStorageStrategyIT
16 {
17   protected ChatHome chathome;
18   protected ChatRoomFactory chatRoomFactory;
19
20
21   protected abstract StorageStrategy getStorageStrategy();
22   protected abstract Supplier<ChatHomeService> getChatHomeServiceSupplier();
23   protected abstract ChatRoomFactory getChatRoomFactory();
24
25   protected void start()
26   {
27     chathome = new SimpleChatHome(getChatHomeServiceSupplier().get());
28     chatRoomFactory = getChatRoomFactory();
29   }
30
31   protected void stop()
32   {
33     getStorageStrategy().write(chathome.getChatRooms());
34   }
35
36   @Test
37   protected void testStoreAndRecreate()
38   {
39     start();
40
41     assertThat(chathome.getChatRooms().toStream()).hasSize(0);
42
43     UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
44     ChatRoom chatroom = chatRoomFactory.createChatRoom(chatRoomId, "FOO").block();
45     chathome.putChatRoom(chatroom);
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     ChatRoom chatroomA = chatRoomFactory.createChatRoom(chatRoomAId, "FOO").block();
76     chathome.putChatRoom(chatroomA);
77     Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
78     Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
79     Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
80     Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
81
82     UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
83     ChatRoom chatroomB = chatRoomFactory.createChatRoom(chatRoomBId, "BAR").block();
84     chathome.putChatRoom(chatroomB);
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.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
91     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
92     assertThat(chathome
93         .getChatRoom(chatroomA.getId())
94         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
95     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
96     assertThat(chathome
97         .getChatRoom(chatroomB.getId())
98         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
99
100     stop();
101     start();
102
103     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
104     assertThat(chathome.getChatRoom(chatroomA.getId())).emitsExactly(chatroomA);
105     assertThat(chathome
106         .getChatRoom(chatroomA.getId())
107         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108     assertThat(chathome.getChatRoom(chatroomB.getId())).emitsExactly(chatroomB);
109     assertThat(chathome
110         .getChatRoom(chatroomB.getId())
111         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
112   }
113 }