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