X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2FInMemoryWithFilesStorageIT.java;h=78f46250dc6ed2a8a5f05af6283909b3ed6a882e;hb=2800b80bb0a02feb70ab9d0c804b2fcc9915f479;hp=12abf7aa38bd065f0a8c284634ef5964f34bf57c;hpb=e8b40c65505d31a2ad3373be8b1e1f175c52687b;p=demos%2Fkafka%2Fchat diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java index 12abf7aa..78f46250 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java @@ -1,54 +1,75 @@ package de.juplo.kafka.chat.backend; -import org.junit.jupiter.api.Test; -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 com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import de.juplo.kafka.chat.backend.implementation.StorageStrategy; +import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; -import java.time.Duration; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Clock; +import java.util.logging.Level; -@SpringBootTest( - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = "chat.backend.storage-directory=target/test-classes/data/files") -class InMemoryWithFilesStorageIT +@Slf4j +public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT { - @LocalServerPort - private int port; - @Autowired - private WebTestClient webTestClient; - - @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"); - }); - } + final static Path path = Paths.get("target","files"); + + final ObjectMapper mapper; + final FilesStorageStrategy storageStrategy; + + + public InMemoryWithFilesStorageIT() + { + super(Clock.systemDefaultZone()); + mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + storageStrategy = new FilesStorageStrategy( + path, + chatRoomId -> 0, + mapper, + Level.FINE, + true); + } + + + @Override + protected StorageStrategy getStorageStrategy() + { + return storageStrategy; + } + + @BeforeEach + void reset() throws Exception + { + 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); + } + } }