test: `LocalJsonFilesStorageStrategyIT` asserts, that the state is restored
[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 org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
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.getChatroom(chatroom.getId()).get().getMessages().toStream()).containsExactlyElementsOf(List.of(m1, m2, m3, m4));
65
66     stop();
67     start();
68
69     assertThat(chathome.list()).containsExactlyElementsOf(List.of(chatroom));
70     assertThat(chathome.getChatroom(chatroom.getId())).contains(chatroom);
71     assertThat(chathome.getChatroom(chatroom.getId()).get().getMessages().toStream()).containsExactlyElementsOf(List.of(m1, m2, m3, m4));
72   }
73
74   @BeforeEach
75   void reset() throws Exception
76   {
77     if (Files.exists(path))
78     {
79       Files
80           .walk(path)
81           .forEach(file ->
82           {
83             try
84             {
85               if (!file.equals(path))
86               {
87                 log.debug("Deleting file {}", file);
88                 Files.delete(file);
89               }
90             }
91             catch (IOException e)
92             {
93               throw new RuntimeException(e);
94             }
95           });
96       log.debug("Deleting data-directory {}", path);
97       Files.delete(path);
98     }
99   }
100 }