c7692228ece3d1efe398dff28c029a391df126db
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / persistence / LocalJsonFilesStorageStrategyIT.java
1 package de.juplo.kafka.chat.backend.persistence;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.fasterxml.jackson.databind.SerializationFeature;
5 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
6 import de.juplo.kafka.chat.backend.domain.ChatHome;
7 import de.juplo.kafka.chat.backend.domain.ChatRoom;
8 import de.juplo.kafka.chat.backend.domain.Message;
9 import lombok.extern.slf4j.Slf4j;
10 import org.junit.jupiter.api.BeforeEach;
11 import org.junit.jupiter.api.Test;
12 import reactor.core.publisher.Flux;
13
14 import java.io.IOException;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.nio.file.Paths;
18 import java.time.Clock;
19 import java.util.List;
20
21 import static pl.rzrz.assertj.reactor.Assertions.*;
22
23
24 @Slf4j
25 public class LocalJsonFilesStorageStrategyIT
26 {
27   final static Path path = Paths.get("target","local-json-files");
28
29   InMemoryChatHomeService service;
30   StorageStrategy storageStrategy;
31   ChatHome chathome;
32
33   void start()
34   {
35     Clock clock = Clock.systemDefaultZone();
36     service = new InMemoryChatHomeService(clock, 8);
37     ObjectMapper mapper = new ObjectMapper();
38     mapper.registerModule(new JavaTimeModule());
39     mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
40     storageStrategy = new LocalJsonFilesStorageStrategy(path, mapper, service);
41     chathome = new ChatHome(service, storageStrategy.readChatrooms());
42   }
43
44   void stop()
45   {
46     storageStrategy.writeChatrooms(Flux.fromStream(chathome.list()));
47   }
48
49   @Test
50   void testStoreAndRecreate()
51   {
52     start();
53
54     assertThat(chathome.list()).hasSize(0);
55
56     ChatRoom chatroom = chathome.createChatroom("FOO");
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.list()).containsExactlyElementsOf(List.of(chatroom));
63     assertThat(chathome.getChatroom(chatroom.getId())).contains(chatroom);
64     assertThat(chathome
65         .getChatroom(chatroom.getId())
66         .get()
67         .getMessages()).emitsExactly(m1, m2, m3, m4);
68
69     stop();
70     start();
71
72     assertThat(chathome.list()).containsExactlyElementsOf(List.of(chatroom));
73     assertThat(chathome.getChatroom(chatroom.getId())).contains(chatroom);
74     assertThat(chathome
75         .getChatroom(chatroom.getId())
76         .get()
77         .getMessages()).emitsExactly(m1, m2, m3, m4);
78   }
79
80   @BeforeEach
81   void reset() throws Exception
82   {
83     if (Files.exists(path))
84     {
85       Files
86           .walk(path)
87           .forEach(file ->
88           {
89             try
90             {
91               if (!file.equals(path))
92               {
93                 log.debug("Deleting file {}", file);
94                 Files.delete(file);
95               }
96             }
97             catch (IOException e)
98             {
99               throw new RuntimeException(e);
100             }
101           });
102       log.debug("Deleting data-directory {}", path);
103       Files.delete(path);
104     }
105   }
106 }