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;
12 import reactor.core.publisher.Flux;
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;
21 import static pl.rzrz.assertj.reactor.Assertions.*;
25 public class LocalJsonFilesStorageStrategyIT
27 final static Path path = Paths.get("target","local-json-files");
29 InMemoryChatHomeService service;
30 StorageStrategy storageStrategy;
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());
46 storageStrategy.writeChatrooms(Flux.fromStream(chathome.list()));
50 void testStoreAndRecreate()
54 assertThat(chathome.list()).hasSize(0);
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();
62 assertThat(chathome.list()).containsExactlyElementsOf(List.of(chatroom));
63 assertThat(chathome.getChatroom(chatroom.getId())).contains(chatroom);
65 .getChatroom(chatroom.getId())
67 .getMessages()).emitsExactly(m1, m2, m3, m4);
72 assertThat(chathome.list()).containsExactlyElementsOf(List.of(chatroom));
73 assertThat(chathome.getChatroom(chatroom.getId())).contains(chatroom);
75 .getChatroom(chatroom.getId())
77 .getMessages()).emitsExactly(m1, m2, m3, m4);
81 void reset() throws Exception
83 if (Files.exists(path))
91 if (!file.equals(path))
93 log.debug("Deleting file {}", file);
99 throw new RuntimeException(e);
102 log.debug("Deleting data-directory {}", path);