test: HandoverIT-POC - Splitted up code into smaller classes -- MOVE
[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   @Autowired
32   ChatHomeService chathome;
33   @Autowired
34   StorageStrategy storageStrategy;
35
36
37   abstract void restore();
38
39   void store()
40   {
41     storageStrategy
42         .write(chathome)
43         .block();
44   }
45
46   @Test
47   void testStoreAndRecreate()
48   {
49     restore();
50
51     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
52
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();
61
62     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
63     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
64     assertThat(chathome
65         .getChatRoomData(chatRoomId)
66         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
67
68     store();
69     restore();
70
71     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info));
72     assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info);
73     assertThat(chathome
74         .getChatRoomData(chatRoomId)
75         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
76   }
77
78   @Test
79   void testStoreAndRecreateParallelChatRooms()
80   {
81     restore();
82
83     assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0);
84
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();
93
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();
102
103     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
104     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
105     assertThat(chathome
106         .getChatRoomData(chatRoomAId)
107         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
108     assertThat(chathome.getChatRoomData(chatRoomBId)).emitsExactly(chatroomB);
109     assertThat(chathome
110         .getChatRoomData(chatRoomBId)
111         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
112
113     store();
114     restore();
115
116     assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB));
117     assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA);
118     assertThat(chathome
119         .getChatRoomData(chatRoomAId)
120         .flatMapMany(cr -> cr.getMessages())).emitsExactly(ma1, ma2, ma3, ma4);
121     assertThat(chathome.getChatRoomInfo(chatRoomBId)).emitsExactly(infoB);
122     assertThat(chathome
123         .getChatRoomData(chatRoomBId)
124         .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4);
125   }
126
127
128   static class TestConfig
129   {
130     @Bean
131     Clock clock()
132     {
133       return Clock.systemDefaultZone();
134     }
135   }
136 }