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 org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
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);
64 assertThat(chathome.getChatroom(chatroom.getId()).get().getMessages().toStream()).containsExactlyElementsOf(List.of(m1, m2, m3, m4));
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));
75 void reset() throws Exception
77 if (Files.exists(path))
85 if (!file.equals(path))
87 log.debug("Deleting file {}", file);
93 throw new RuntimeException(e);
96 log.debug("Deleting data-directory {}", path);