test: HandoverIT-POC - Splitted up code into smaller classes -- MOVE
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / InMemoryWithFilesStorageIT.java
index 12abf7a..0d63b7e 100644 (file)
@@ -1,54 +1,69 @@
 package de.juplo.kafka.chat.backend;
 
-import org.junit.jupiter.api.Test;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.web.server.LocalServerPort;
-import org.springframework.http.MediaType;
-import org.springframework.test.web.reactive.server.WebTestClient;
-import org.testcontainers.shaded.org.awaitility.Awaitility;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
 
-import java.time.Duration;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 
-@SpringBootTest(
-               webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
-               properties = "chat.backend.storage-directory=target/test-classes/data/files")
-class InMemoryWithFilesStorageIT
+@TestPropertySource(properties = {
+    "chat.backend.inmemory.sharding-strategy=none",
+    "chat.backend.inmemory.storage-strategy=files",
+    "chat.backend.inmemory.storage-directory=target/files" })
+@ContextConfiguration(classes = InMemoryWithFilesStorageIT.TestConfig.class)
+@Slf4j
+public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
 {
-       @LocalServerPort
-       private int port;
-       @Autowired
-       private WebTestClient webTestClient;
+  @BeforeEach
+  void resetStorage(
+      @Autowired ChatBackendProperties properties)
+      throws Exception
+  {
+    Path path = Paths.get(properties.getInmemory().getStorageDirectory());
+    if (Files.exists(path))
+    {
+      Files
+          .walk(path)
+          .forEach(file ->
+          {
+            try
+            {
+              if (!file.equals(path))
+              {
+                log.debug("Deleting file {}", file);
+                Files.delete(file);
+              }
+            }
+            catch (IOException e)
+            {
+              throw new RuntimeException(e);
+            }
+          });
+      log.debug("Deleting data-directory {}", path);
+      Files.delete(path);
+    }
+  }
 
-       @Test
-       void contextLoads()
-       {
-               Awaitility
-                               .await()
-                               .atMost(Duration.ofSeconds(15))
-                               .untilAsserted(() ->
-                               {
-                                       webTestClient
-                                                       .get()
-                                                       .uri("http://localhost:{port}/actuator/health", port)
-                                                       .exchange()
-                                                       .expectStatus().isOk()
-                                                       .expectBody().jsonPath("$.status").isEqualTo("UP");
-                                       webTestClient
-                                                       .get()
-                                                       .uri("http://localhost:{port}/618e89ae-fdc0-4c65-8055-f49908295e8f", port)
-                                                       .accept(MediaType.APPLICATION_JSON)
-                                                       .exchange()
-                                                       .expectStatus().isOk()
-                                                       .expectBody().jsonPath("$.name").isEqualTo("Peter's Chat-Room");
-                                       webTestClient
-                                                       .get()
-                                                       .uri("http://localhost:{port}/618e89ae-fdc0-4c65-8055-f49908295e8f/ute/1478", port)
-                                                       .accept(MediaType.APPLICATION_JSON)
-                                                       .exchange()
-                                                       .expectStatus().isOk()
-                                                       .expectBody().jsonPath("$.text").isEqualTo("Nachricht Nr. 1478");
-                               });
-       }
+
+  static class TestConfig
+  {
+    @Bean
+    ObjectMapper objectMapper()
+    {
+      ObjectMapper objectMapper = new ObjectMapper();
+      objectMapper.registerModule(new JavaTimeModule());
+      objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+      return objectMapper;
+    }
+  }
 }