test: HandoverIT-POC - Splitted up code into smaller classes -- MOVE
[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 lombok.extern.slf4j.Slf4j;
7 import org.junit.jupiter.api.BeforeEach;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.context.annotation.Bean;
10 import org.springframework.test.context.ContextConfiguration;
11 import org.springframework.test.context.TestPropertySource;
12
13 import java.io.IOException;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17
18
19 @TestPropertySource(properties = {
20     "chat.backend.inmemory.sharding-strategy=none",
21     "chat.backend.inmemory.storage-strategy=files",
22     "chat.backend.inmemory.storage-directory=target/files" })
23 @ContextConfiguration(classes = InMemoryWithFilesStorageIT.TestConfig.class)
24 @Slf4j
25 public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
26 {
27   @BeforeEach
28   void resetStorage(
29       @Autowired ChatBackendProperties properties)
30       throws Exception
31   {
32     Path path = Paths.get(properties.getInmemory().getStorageDirectory());
33     if (Files.exists(path))
34     {
35       Files
36           .walk(path)
37           .forEach(file ->
38           {
39             try
40             {
41               if (!file.equals(path))
42               {
43                 log.debug("Deleting file {}", file);
44                 Files.delete(file);
45               }
46             }
47             catch (IOException e)
48             {
49               throw new RuntimeException(e);
50             }
51           });
52       log.debug("Deleting data-directory {}", path);
53       Files.delete(path);
54     }
55   }
56
57
58   static class TestConfig
59   {
60     @Bean
61     ObjectMapper objectMapper()
62     {
63       ObjectMapper objectMapper = new ObjectMapper();
64       objectMapper.registerModule(new JavaTimeModule());
65       objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
66       return objectMapper;
67     }
68   }
69 }