1 package de.juplo.kafka.chat.backend;
3 import de.juplo.kafka.chat.backend.domain.*;
4 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
5 import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
6 import de.juplo.kafka.chat.backend.storage.files.FilesStorageConfiguration;
7 import de.juplo.kafka.chat.backend.storage.mongodb.MongoDbStorageConfiguration;
8 import lombok.extern.slf4j.Slf4j;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
15 import java.time.Clock;
16 import java.util.List;
17 import java.util.UUID;
19 import static pl.rzrz.assertj.reactor.Assertions.*;
22 @SpringJUnitConfig(classes = {
23 InMemoryServicesConfiguration.class,
24 FilesStorageConfiguration.class,
25 MongoDbStorageConfiguration.class,
26 AbstractStorageStrategyIT.TestConfig.class })
27 @EnableConfigurationProperties(ChatBackendProperties.class)
29 public abstract class AbstractStorageStrategyIT
32 ChatHomeService chathome;
34 StorageStrategy storageStrategy;
37 abstract void restore();
47 void testStoreAndRecreate()
51 assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
53 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
54 ChatRoomInfo info = chathome.createChatRoom(chatRoomId, "FOO").block();
55 log.debug("Created chat-room {}", info);
56 ChatRoomData chatroom = chathome.getChatRoomData(chatRoomId).block();
57 Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
58 Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
59 Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
60 Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
62 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
63 assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
65 .getChatRoomData(chatRoomId)
66 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
71 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
72 assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
74 .getChatRoomData(chatRoomId)
75 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
79 void testStoreAndRecreateParallelChatRooms()
83 assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
85 UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
86 ChatRoomInfo infoA = chathome.createChatRoom(chatRoomAId, "FOO").block();
87 log.debug("Created chat-room {}", infoA);
88 ChatRoomData chatroomA = chathome.getChatRoomData(chatRoomAId).block();
89 Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
90 Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
91 Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
92 Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
94 UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
95 ChatRoomInfo infoB = chathome.createChatRoom(chatRoomBId, "BAR").block();
96 log.debug("Created chat-room {}", infoB);
97 ChatRoomData chatroomB = chathome.getChatRoomData(chatRoomBId).block();
98 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
99 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
100 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
101 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
103 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
104 assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
106 .getChatRoomData(chatRoomAId)
107 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108 assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
110 .getChatRoomData(chatRoomBId)
111 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
116 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
117 assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
119 .getChatRoomData(chatRoomAId)
120 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
121 assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
123 .getChatRoomData(chatRoomBId)
124 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
128 static class TestConfig
133 return Clock.systemDefaultZone();