test: Simplified the integration-tests for `StorageStrategy`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / InMemoryWithFilesStorageIT.java
1 package de.juplo.kafka.chat.backend;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.junit.jupiter.api.BeforeEach;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.test.context.TestPropertySource;
7
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11 import java.nio.file.Paths;
12
13
14 @TestPropertySource(properties = {
15     "chat.backend.inmemory.sharding-strategy=none",
16     "chat.backend.inmemory.storage-strategy=files",
17     "chat.backend.inmemory.storage-directory=target/files" })
18 @Slf4j
19 public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
20 {
21   @BeforeEach
22   void resetStorage(
23       @Autowired ChatBackendProperties properties)
24       throws Exception
25   {
26     Path path = Paths.get(properties.getInmemory().getStorageDirectory());
27     if (Files.exists(path))
28     {
29       Files
30           .walk(path)
31           .forEach(file ->
32           {
33             try
34             {
35               if (!file.equals(path))
36               {
37                 log.debug("Deleting file {}", file);
38                 Files.delete(file);
39               }
40             }
41             catch (IOException e)
42             {
43               throw new RuntimeException(e);
44             }
45           });
46       log.debug("Deleting data-directory {}", path);
47       Files.delete(path);
48     }
49   }
50 }