d136fa91ea41e771fb51d2fd84b6f6ccfe0493d5
[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   }
68
69   @BeforeEach
70   void reset() throws Exception
71   {
72     if (Files.exists(path))
73     {
74       Files
75           .walk(path)
76           .forEach(file ->
77           {
78             try
79             {
80               if (!file.equals(path))
81               {
82                 log.debug("Deleting file {}", file);
83                 Files.delete(file);
84               }
85             }
86             catch (IOException e)
87             {
88               throw new RuntimeException(e);
89             }
90           });
91       log.debug("Deleting data-directory {}", path);
92       Files.delete(path);
93     }
94   }
95 }