FIX
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / InMemoryWithFilesStorageIT.java
1 package de.juplo.kafka.chat.backend;
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.implementation.StorageStrategy;
7 import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
8 import lombok.extern.slf4j.Slf4j;
9 import org.junit.jupiter.api.BeforeEach;
10
11 import java.io.IOException;
12 import java.nio.file.Files;
13 import java.nio.file.Path;
14 import java.nio.file.Paths;
15 import java.time.Clock;
16
17
18 @Slf4j
19 public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
20 {
21   final static Path path = Paths.get("target","files");
22
23   final ObjectMapper mapper;
24   final FilesStorageStrategy storageStrategy;
25
26
27   public InMemoryWithFilesStorageIT()
28   {
29     super(Clock.systemDefaultZone());
30     mapper = new ObjectMapper();
31     mapper.registerModule(new JavaTimeModule());
32     mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
33     storageStrategy = new FilesStorageStrategy(
34         path,
35         chatRoomId -> 0,
36         mapper);
37   }
38
39
40   @Override
41   protected StorageStrategy getStorageStrategy()
42   {
43     return storageStrategy;
44   }
45
46   @BeforeEach
47   void reset() throws Exception
48   {
49     if (Files.exists(path))
50     {
51       Files
52           .walk(path)
53           .forEach(file ->
54           {
55             try
56             {
57               if (!file.equals(path))
58               {
59                 log.debug("Deleting file {}", file);
60                 Files.delete(file);
61               }
62             }
63             catch (IOException e)
64             {
65               throw new RuntimeException(e);
66             }
67           });
68       log.debug("Deleting data-directory {}", path);
69       Files.delete(path);
70     }
71   }
72 }