1 package de.juplo.kafka.chat.backend.persistence;
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;
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;
20 import static pl.rzrz.assertj.reactor.Assertions.*;
24 public class LocalJsonFilesStorageStrategyIT
26 final static Path path = Paths.get("target","local-json-files");
28 InMemoryChatHomeService service;
29 StorageStrategy storageStrategy;
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());
45 storageStrategy.writeChatrooms(chathome.list());
49 void testStoreAndRecreate()
53 assertThat(chathome.list().toStream()).hasSize(0);
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();
61 assertThat(chathome.list().toStream()).containsExactlyElementsOf(List.of(chatroom));
62 assertThat(chathome.getChatroom(chatroom.getId())).emitsExactly(chatroom);
64 .getChatroom(chatroom.getId())
65 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
70 assertThat(chathome.list().toStream()).containsExactlyElementsOf(List.of(chatroom));
71 assertThat(chathome.getChatroom(chatroom.getId())).emitsExactly(chatroom);
73 .getChatroom(chatroom.getId())
74 .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4);
78 void reset() throws Exception
80 if (Files.exists(path))
88 if (!file.equals(path))
90 log.debug("Deleting file {}", file);
96 throw new RuntimeException(e);
99 log.debug("Deleting data-directory {}", path);