test: Simplified the integration-tests for `StorageStrategy`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / AbstractStorageStrategyIT.java
1 package de.juplo.kafka.chat.backend;
2
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;
14
15 import java.time.Clock;
16 import java.util.List;
17 import java.util.UUID;
18
19 import static pl.rzrz.assertj.reactor.Assertions.*;
20
21
22 @SpringJUnitConfig(classes = {
23     InMemoryServicesConfiguration.class,
24     FilesStorageConfiguration.class,
25     MongoDbStorageConfiguration.class,
26     AbstractStorageStrategyIT.TestConfig.class })
27 @EnableConfigurationProperties(ChatBackendProperties.class)
28 @Slf4j
29 public abstract class AbstractStorageStrategyIT
30 {
31   ChatHomeService chathome;
32
33   @Autowired
34   StorageStrategy storageStrategy;
35
36   abstract ChatHomeService getChatHome();
37
38   protected void start()
39   {
40     chathome = getChatHome();
41   }
42
43   protected void stop()
44   {
45     storageStrategy
46         .write(chathome)
47         .subscribe();
48   }
49
50   @Test
51   protected void testStoreAndRecreate()
52   {
53     start();
54
55     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
56
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();
65
66     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
67     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
68     assertThat(chathome
69         .getChatRoomData(chatRoomId)
70         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
71
72     stop();
73     start();
74
75     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
76     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
77     assertThat(chathome
78         .getChatRoomData(chatRoomId)
79         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
80   }
81
82   @Test
83   protected void testStoreAndRecreateParallelChatRooms()
84   {
85     start();
86
87     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
88
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();
97
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();
106
107     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
108     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
109     assertThat(chathome
110         .getChatRoomData(chatRoomAId)
111         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
112     assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
113     assertThat(chathome
114         .getChatRoomData(chatRoomBId)
115         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
116
117     stop();
118     start();
119
120     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
121     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
122     assertThat(chathome
123         .getChatRoomData(chatRoomAId)
124         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
125     assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
126     assertThat(chathome
127         .getChatRoomData(chatRoomBId)
128         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
129   }
130
131
132   static class TestConfig
133   {
134     @Bean
135     Clock clock()
136     {
137       return Clock.systemDefaultZone();
138     }
139   }
140 }