refactor: Streamlined the API of the services
[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
13 import java.io.IOException;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.time.Clock;
18 import java.util.List;
19
20 import static pl.rzrz.assertj.reactor.Assertions.*;
21
22
23 @Slf4j
24 public class LocalJsonFilesStorageStrategyIT
25 {
26   final static Path path = Paths.get("target","local-json-files");
27
28   InMemoryChatHomeService service;
29   StorageStrategy storageStrategy;
30   ChatHome chathome;
31
32   void start()
33   {
34     Clock clock = Clock.systemDefaultZone();
35     service = new InMemoryChatHomeService(clock, 8);
36     ObjectMapper mapper = new ObjectMapper();
37     mapper.registerModule(new JavaTimeModule());
38     mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
39     storageStrategy = new LocalJsonFilesStorageStrategy(path, mapper, service);
40     chathome = new ChatHome(service, storageStrategy.readChatrooms());
41   }
42
43   void stop()
44   {
45     storageStrategy.writeChatrooms(chathome.list());
46   }
47
48   @Test
49   void testStoreAndRecreate()
50   {
51     start();
52
53     assertThat(chathome.list().toStream()).hasSize(0);
54
55     ChatRoom chatroom = chathome.createChatroom("FOO").block();
56     Message m1 = chatroom.addMessage(1l,"Peter", "Hallo, ich heiße Peter!").block();
57     Message m2 = chatroom.addMessage(1l, "Ute", "Ich bin Ute...").block();
58     Message m3 = chatroom.addMessage(2l, "Peter", "Willst du mit mir gehen?").block();
59     Message m4 = chatroom.addMessage(1l, "Klaus", "Ja? Nein? Vielleicht??").block();
60
61     assertThat(chathome.list().toStream()).containsExactlyElementsOf(List.of(chatroom));
62     assertThat(chathome.getChatroom(chatroom.getId())).emitsExactly(chatroom);
63     assertThat(chathome
64         .getChatroom(chatroom.getId())
65         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
66
67     stop();
68     start();
69
70     assertThat(chathome.list().toStream()).containsExactlyElementsOf(List.of(chatroom));
71     assertThat(chathome.getChatroom(chatroom.getId())).emitsExactly(chatroom);
72     assertThat(chathome
73         .getChatroom(chatroom.getId())
74         .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
75   }
76
77   @BeforeEach
78   void reset() throws Exception
79   {
80     if (Files.exists(path))
81     {
82       Files
83           .walk(path)
84           .forEach(file ->
85           {
86             try
87             {
88               if (!file.equals(path))
89               {
90                 log.debug("Deleting file {}", file);
91                 Files.delete(file);
92               }
93             }
94             catch (IOException e)
95             {
96               throw new RuntimeException(e);
97             }
98           });
99       log.debug("Deleting data-directory {}", path);
100       Files.delete(path);
101     }
102   }
103 }