refactor: Moved classes in package `persistence` in sub-packages -- Rename
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / persistence / InMemoryWithFileStorageStrategyIT.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.ChatHomeService;
7 import de.juplo.kafka.chat.backend.persistence.filestorage.FileStorageStrategy;
8 import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
9 import lombok.extern.slf4j.Slf4j;
10 import org.junit.jupiter.api.BeforeEach;
11
12 import java.io.IOException;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import java.time.Clock;
17 import java.util.function.Supplier;
18
19
20 @Slf4j
21 public class InMemoryWithFileStorageStrategyIT extends AbstractStorageStrategyIT
22 {
23   final static Path path = Paths.get("target","local-json-files");
24
25   final Clock clock;
26   final ObjectMapper mapper;
27   final FileStorageStrategy storageStrategy;
28
29
30   public InMemoryWithFileStorageStrategyIT()
31   {
32     clock = Clock.systemDefaultZone();
33     mapper = new ObjectMapper();
34     mapper.registerModule(new JavaTimeModule());
35     mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
36     storageStrategy = new FileStorageStrategy(path, clock, 8, mapper);
37
38   }
39
40
41   @Override
42   protected StorageStrategy getStorageStrategy()
43   {
44     return storageStrategy;
45   }
46
47   @Override
48   protected Supplier<ChatHomeService> chatHomeServiceSupplier()
49   {
50     return () -> new InMemoryChatHomeService(getStorageStrategy().readChatrooms(), clock, 8);
51   }
52
53   @BeforeEach
54   void reset() throws Exception
55   {
56     if (Files.exists(path))
57     {
58       Files
59           .walk(path)
60           .forEach(file ->
61           {
62             try
63             {
64               if (!file.equals(path))
65               {
66                 log.debug("Deleting file {}", file);
67                 Files.delete(file);
68               }
69             }
70             catch (IOException e)
71             {
72               throw new RuntimeException(e);
73             }
74           });
75       log.debug("Deleting data-directory {}", path);
76       Files.delete(path);
77     }
78   }
79 }