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
31 ChatHomeService chathome;
34 StorageStrategy storageStrategy;
36 abstract ChatHomeService getChatHome();
38 protected void start()
40 chathome = getChatHome();
51 protected void testStoreAndRecreate()
55 assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
57 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
58 ChatRoomInfo info = chathome.createChatRoom(chatRoomId, "FOO").block();
59 log.debug("Created chat-room {}", info);
60 ChatRoomData chatroom = chathome.getChatRoomData(chatRoomId).block();
61 Message m1 = chatroom.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
62 Message m2 = chatroom.addMessage(1l, "ute", "Ich bin Ute...").block();
63 Message m3 = chatroom.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
64 Message m4 = chatroom.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
66 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
67 assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
69 .getChatRoomData(chatRoomId)
70 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
75 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
76 assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
78 .getChatRoomData(chatRoomId)
79 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
83 protected void testStoreAndRecreateParallelChatRooms()
87 assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
89 UUID chatRoomAId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
90 ChatRoomInfo infoA = chathome.createChatRoom(chatRoomAId, "FOO").block();
91 log.debug("Created chat-room {}", infoA);
92 ChatRoomData chatroomA = chathome.getChatRoomData(chatRoomAId).block();
93 Message ma1 = chatroomA.addMessage(1l,"peter", "Hallo, ich heiße Peter!").block();
94 Message ma2 = chatroomA.addMessage(1l, "ute", "Ich bin Ute...").block();
95 Message ma3 = chatroomA.addMessage(2l, "peter", "Willst du mit mir gehen?").block();
96 Message ma4 = chatroomA.addMessage(1l, "klaus", "Ja? Nein? Vielleicht??").block();
98 UUID chatRoomBId = UUID.fromString("8763dfdc-4dda-4a74-bea4-4b389177abea");
99 ChatRoomInfo infoB = chathome.createChatRoom(chatRoomBId, "BAR").block();
100 log.debug("Created chat-room {}", infoB);
101 ChatRoomData chatroomB = chathome.getChatRoomData(chatRoomBId).block();
102 Message mb1 = chatroomB.addMessage(1l,"peter", "Hallo, ich heiße Uwe!").block();
103 Message mb2 = chatroomB.addMessage(1l, "ute", "Ich bin Ute...").block();
104 Message mb3 = chatroomB.addMessage(1l, "klaus", "Willst du mit mir gehen?").block();
105 Message mb4 = chatroomB.addMessage(2l, "peter", "Hä? Was jetzt?!? Isch glohb isch höb ühn däjah vüh...").block();
107 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
108 assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
110 .getChatRoomData(chatRoomAId)
111 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
112 assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
114 .getChatRoomData(chatRoomBId)
115 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
120 assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
121 assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
123 .getChatRoomData(chatRoomAId)
124 .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
125 assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
127 .getChatRoomData(chatRoomBId)
128 .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
132 static class TestConfig
137 return Clock.systemDefaultZone();