From: Kai Moritz Date: Fri, 13 Jan 2023 20:29:29 +0000 (+0100) Subject: refactor: Tried to simplify & clearify the naming of the integration-tests X-Git-Tag: wip~33 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=fa1c5b6d1d6f79c6e1e2037af9c58a295ae04e97;p=demos%2Fkafka%2Fchat refactor: Tried to simplify & clearify the naming of the integration-tests --- diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java new file mode 100644 index 00000000..8aa0815e --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java @@ -0,0 +1,54 @@ +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 java.time.Duration; + + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = "chat.backend.storage-directory=target/test-classes/data/files") +class InMemoryWithFilesConfigurationIT +{ + @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"); + }); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java deleted file mode 100644 index 12abf7aa..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesStorageIT.java +++ /dev/null @@ -1,54 +0,0 @@ -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 java.time.Duration; - - -@SpringBootTest( - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = "chat.backend.storage-directory=target/test-classes/data/files") -class InMemoryWithFilesStorageIT -{ - @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"); - }); - } -} diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java new file mode 100644 index 00000000..4612aa8b --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java @@ -0,0 +1,101 @@ +package de.juplo.kafka.chat.backend; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +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.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.shaded.org.awaitility.Awaitility; + +import java.time.Duration; + + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "chat.backend.storage=mongodb", + "spring.data.mongodb.host=localhost", + "spring.data.mongodb.database=test" }) +@Testcontainers +@Slf4j +class InMemoryWithMongoDbConfigurationIT +{ + @LocalServerPort + private int port; + @Autowired + private WebTestClient webTestClient; + + @Test + @DisplayName("The app starts, the data is restored and accessible") + void test() + { + 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}/4ace69b7-a79f-481b-ad0d-d756d60b66ec", port) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.name").isEqualTo("FOO"); + webTestClient + .get() + .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/ute/1", port) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute..."); + webTestClient + .get() + .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/peter/1", port) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!"); + }); + } + + private static final int MONGODB_PORT = 27017; + + @Container + private static final GenericContainer CONTAINER = + new GenericContainer("mongo:6") + .withClasspathResourceMapping( + "data/mongodb", + "/docker-entrypoint-initdb.d", + BindMode.READ_ONLY) + .withExposedPorts(MONGODB_PORT); + + @DynamicPropertySource + static void addMongoPortProperty(DynamicPropertyRegistry registry) + { + registry.add("spring.data.mongodb.port", () -> CONTAINER.getMappedPort(27017)); + } + + @BeforeEach + void setUpLogging() + { + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log); + CONTAINER.followOutput(logConsumer); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java deleted file mode 100644 index 1a93841c..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java +++ /dev/null @@ -1,101 +0,0 @@ -package de.juplo.kafka.chat.backend; - -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -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.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.springframework.test.web.reactive.server.WebTestClient; -import org.testcontainers.containers.BindMode; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.shaded.org.awaitility.Awaitility; - -import java.time.Duration; - - -@SpringBootTest( - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = { - "chat.backend.storage=mongodb", - "spring.data.mongodb.host=localhost", - "spring.data.mongodb.database=test" }) -@Testcontainers -@Slf4j -class InMemoryWithMongoDbStorageIT -{ - @LocalServerPort - private int port; - @Autowired - private WebTestClient webTestClient; - - @Test - @DisplayName("The app starts, the data is restored and accessible") - void test() - { - 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}/4ace69b7-a79f-481b-ad0d-d756d60b66ec", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.name").isEqualTo("FOO"); - webTestClient - .get() - .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/ute/1", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.text").isEqualTo("Ich bin Ute..."); - webTestClient - .get() - .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/peter/1", port) - .accept(MediaType.APPLICATION_JSON) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.text").isEqualTo("Hallo, ich heiße Peter!"); - }); - } - - private static final int MONGODB_PORT = 27017; - - @Container - private static final GenericContainer CONTAINER = - new GenericContainer("mongo:6") - .withClasspathResourceMapping( - "data/mongodb", - "/docker-entrypoint-initdb.d", - BindMode.READ_ONLY) - .withExposedPorts(MONGODB_PORT); - - @DynamicPropertySource - static void addMongoPortProperty(DynamicPropertyRegistry registry) - { - registry.add("spring.data.mongodb.port", () -> CONTAINER.getMappedPort(27017)); - } - - @BeforeEach - void setUpLogging() - { - Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log); - CONTAINER.followOutput(logConsumer); - } -} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java new file mode 100644 index 00000000..bd1a5c6a --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java @@ -0,0 +1,94 @@ +package de.juplo.kafka.chat.backend.persistence; + +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.api.ShardingStrategy; +import de.juplo.kafka.chat.backend.domain.ChatHomeService; +import de.juplo.kafka.chat.backend.domain.ChatRoomFactory; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory; +import de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; + +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.function.Supplier; + + +@Slf4j +public class InMemoryWithFilesStorageIT extends AbstractStorageStrategyIT +{ + final static Path path = Paths.get("target","local-json-files"); + + final Clock clock; + final ObjectMapper mapper; + final FilesStorageStrategy storageStrategy; + + + public InMemoryWithFilesStorageIT() + { + clock = Clock.systemDefaultZone(); + mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + storageStrategy = new FilesStorageStrategy( + path, + clock, + 8, + messageFlux -> new InMemoryChatRoomService(messageFlux), + mapper); + } + + + @Override + protected StorageStrategy getStorageStrategy() + { + return storageStrategy; + } + + @Override + protected Supplier getChatHomeServiceSupplier() + { + return () -> new InMemoryChatHomeService(1, getStorageStrategy().read()); + } + + @Override + protected ChatRoomFactory getChatRoomFactory() + { + ShardingStrategy strategy = chatRoomId -> 0; + return new InMemoryChatRoomFactory(strategy, clock, 8); + } + + @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); + } + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java deleted file mode 100644 index 6fc9aac2..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java +++ /dev/null @@ -1,94 +0,0 @@ -package de.juplo.kafka.chat.backend.persistence; - -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.api.ShardingStrategy; -import de.juplo.kafka.chat.backend.domain.ChatHomeService; -import de.juplo.kafka.chat.backend.domain.ChatRoomFactory; -import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory; -import de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy; -import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService; -import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.BeforeEach; - -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.function.Supplier; - - -@Slf4j -public class InMemoryWithFilesStorageStrategyIT extends AbstractStorageStrategyIT -{ - final static Path path = Paths.get("target","local-json-files"); - - final Clock clock; - final ObjectMapper mapper; - final FilesStorageStrategy storageStrategy; - - - public InMemoryWithFilesStorageStrategyIT() - { - clock = Clock.systemDefaultZone(); - mapper = new ObjectMapper(); - mapper.registerModule(new JavaTimeModule()); - mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - storageStrategy = new FilesStorageStrategy( - path, - clock, - 8, - messageFlux -> new InMemoryChatRoomService(messageFlux), - mapper); - } - - - @Override - protected StorageStrategy getStorageStrategy() - { - return storageStrategy; - } - - @Override - protected Supplier getChatHomeServiceSupplier() - { - return () -> new InMemoryChatHomeService(1, getStorageStrategy().read()); - } - - @Override - protected ChatRoomFactory getChatRoomFactory() - { - ShardingStrategy strategy = chatRoomId -> 0; - return new InMemoryChatRoomFactory(strategy, clock, 8); - } - - @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); - } - } -} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageIT.java new file mode 100644 index 00000000..80067117 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageIT.java @@ -0,0 +1,116 @@ +package de.juplo.kafka.chat.backend.persistence; + +import de.juplo.kafka.chat.backend.api.ShardingStrategy; +import de.juplo.kafka.chat.backend.domain.ChatHomeService; +import de.juplo.kafka.chat.backend.domain.ChatRoomFactory; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService; +import de.juplo.kafka.chat.backend.persistence.InMemoryWithMongoDbStorageIT.DataSourceInitializer; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; +import de.juplo.kafka.chat.backend.persistence.storage.mongodb.ChatRoomRepository; +import de.juplo.kafka.chat.backend.persistence.storage.mongodb.MongoDbStorageStrategy; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.support.TestPropertySourceUtils; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.time.Clock; +import java.util.function.Supplier; + + +@Testcontainers +@ExtendWith({SpringExtension.class}) +@EnableAutoConfiguration +@AutoConfigureDataMongo +@ContextConfiguration(initializers = DataSourceInitializer.class) +@Slf4j +public class InMemoryWithMongoDbStorageIT extends AbstractStorageStrategyIT +{ + @Autowired + MongoDbStorageStrategy storageStrategy; + @Autowired + Clock clock; + + + @Override + protected StorageStrategy getStorageStrategy() + { + return storageStrategy; + } + + @Override + protected Supplier getChatHomeServiceSupplier() + { + return () -> new InMemoryChatHomeService(1, getStorageStrategy().read()); + } + + @Override + protected ChatRoomFactory getChatRoomFactory() + { + ShardingStrategy strategy = chatRoomId -> 0; + return new InMemoryChatRoomFactory(strategy, clock, 8); + } + + @TestConfiguration + static class InMemoryWithMongoDbStorageStrategyITConfig + { + @Bean + MongoDbStorageStrategy storageStrategy( + ChatRoomRepository chatRoomRepository, + Clock clock) + { + return new MongoDbStorageStrategy( + chatRoomRepository, + clock, + 8, + messageFlux -> new InMemoryChatRoomService(messageFlux)); + } + + @Bean + Clock clock() + { + return Clock.systemDefaultZone(); + } + } + + private static final int MONGODB_PORT = 27017; + + @Container + private static final GenericContainer CONTAINER = + new GenericContainer("mongo:6") + .withExposedPorts(MONGODB_PORT); + + public static class DataSourceInitializer + implements ApplicationContextInitializer + { + @Override + public void initialize(ConfigurableApplicationContext applicationContext) + { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment( + applicationContext, + "spring.data.mongodb.host=localhost", + "spring.data.mongodb.port=" + CONTAINER.getMappedPort(MONGODB_PORT), + "spring.data.mongodb.database=test"); + } + } + + @BeforeEach + void setUpLogging() + { + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log); + CONTAINER.followOutput(logConsumer); + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java deleted file mode 100644 index ef30d947..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageStrategyIT.java +++ /dev/null @@ -1,116 +0,0 @@ -package de.juplo.kafka.chat.backend.persistence; - -import de.juplo.kafka.chat.backend.api.ShardingStrategy; -import de.juplo.kafka.chat.backend.domain.ChatHomeService; -import de.juplo.kafka.chat.backend.domain.ChatRoomFactory; -import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService; -import de.juplo.kafka.chat.backend.persistence.InMemoryWithMongoDbStorageStrategyIT.DataSourceInitializer; -import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory; -import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; -import de.juplo.kafka.chat.backend.persistence.storage.mongodb.ChatRoomRepository; -import de.juplo.kafka.chat.backend.persistence.storage.mongodb.MongoDbStorageStrategy; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.support.TestPropertySourceUtils; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; - -import java.time.Clock; -import java.util.function.Supplier; - - -@Testcontainers -@ExtendWith({SpringExtension.class}) -@EnableAutoConfiguration -@AutoConfigureDataMongo -@ContextConfiguration(initializers = DataSourceInitializer.class) -@Slf4j -public class InMemoryWithMongoDbStorageStrategyIT extends AbstractStorageStrategyIT -{ - @Autowired - MongoDbStorageStrategy storageStrategy; - @Autowired - Clock clock; - - - @Override - protected StorageStrategy getStorageStrategy() - { - return storageStrategy; - } - - @Override - protected Supplier getChatHomeServiceSupplier() - { - return () -> new InMemoryChatHomeService(1, getStorageStrategy().read()); - } - - @Override - protected ChatRoomFactory getChatRoomFactory() - { - ShardingStrategy strategy = chatRoomId -> 0; - return new InMemoryChatRoomFactory(strategy, clock, 8); - } - - @TestConfiguration - static class InMemoryWithMongoDbStorageStrategyITConfig - { - @Bean - MongoDbStorageStrategy storageStrategy( - ChatRoomRepository chatRoomRepository, - Clock clock) - { - return new MongoDbStorageStrategy( - chatRoomRepository, - clock, - 8, - messageFlux -> new InMemoryChatRoomService(messageFlux)); - } - - @Bean - Clock clock() - { - return Clock.systemDefaultZone(); - } - } - - private static final int MONGODB_PORT = 27017; - - @Container - private static final GenericContainer CONTAINER = - new GenericContainer("mongo:6") - .withExposedPorts(MONGODB_PORT); - - public static class DataSourceInitializer - implements ApplicationContextInitializer - { - @Override - public void initialize(ConfigurableApplicationContext applicationContext) - { - TestPropertySourceUtils.addInlinedPropertiesToEnvironment( - applicationContext, - "spring.data.mongodb.host=localhost", - "spring.data.mongodb.port=" + CONTAINER.getMappedPort(MONGODB_PORT), - "spring.data.mongodb.database=test"); - } - } - - @BeforeEach - void setUpLogging() - { - Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log); - CONTAINER.followOutput(logConsumer); - } -}