test: Refactored test-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 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
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.getChatRooms());
31   }
32
33   @Test
34   protected void testStoreAndRecreate()
35   {
36     start();
37
38     assertThat(chathome.getChatRooms().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     ChatRoom chatroom = chathome.getChatRoom(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
50     assertThat(chathome.getChatRooms().toStream()).containsExactlyElementsOf(List.of(chatroom));
51     assertThat(chathome.getChatRoom(chatRoomId)).emitsExactly(chatroom);
52     assertThat(chathome
53         .getChatRoom(chatRoomId)
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(chatRoomId)).emitsExactly(chatroom);
61     assertThat(chathome
62         .getChatRoom(chatRoomId)
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     ChatRoomInfo infoA = chathome.createChatRoom(chatRoomAId, "FOO").block();
75     log.debug("Created chat-room {}", infoA);
76     ChatRoom chatroomA = chathome.getChatRoom(chatRoomAId).block();
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     ChatRoomInfo infoB = chathome.createChatRoom(chatRoomBId, "BAR").block();
84     log.debug("Created chat-room {}", infoB);
85     ChatRoom chatroomB = chathome.getChatRoom(chatRoomBId).block();
86     Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
87     Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
88     Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
89     Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
90
91     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
92     assertThat(chathome.getChatRoom(chatRoomAId)).emitsExactly(chatroomA);
93     assertThat(chathome
94         .getChatRoom(chatRoomAId)
95         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
96     assertThat(chathome.getChatRoom(chatRoomBId)).emitsExactly(chatroomB);
97     assertThat(chathome
98         .getChatRoom(chatRoomBId)
99         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
100
101     stop();
102     start();
103
104     assertThat(chathome.getChatRooms().toStream()).containsExactlyInAnyOrderElementsOf(List.of(chatroomA, chatroomB));
105     assertThat(chathome.getChatRoom(chatRoomAId)).emitsExactly(chatroomA);
106     assertThat(chathome
107         .getChatRoom(chatRoomAId)
108         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
109     assertThat(chathome.getChatRoom(chatRoomBId)).emitsExactly(chatroomB);
110     assertThat(chathome
111         .getChatRoom(chatRoomBId)
112         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
113   }
114
115
116   interface StorageStrategyITConfig
117   {
118     ChatHome getChatHome();
119   }
120 }