* Using existing configuration-classes instead of hand-coded configuartion.
* Aligned the `KafkaChatHomeServiceTest` with the `in-memory`-tests.
package de.juplo.kafka.chat.backend.domain;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
import de.juplo.kafka.chat.backend.domain.exceptions.LoadInProgressException;
import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException;
+import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
+import de.juplo.kafka.chat.backend.storage.files.FilesStorageConfiguration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
+import java.time.Clock;
import java.time.Duration;
import java.util.UUID;
import static pl.rzrz.assertj.reactor.Assertions.assertThat;
-@ExtendWith(SpringExtension.class)
+@SpringJUnitConfig(classes = {
+ InMemoryServicesConfiguration.class,
+ FilesStorageConfiguration.class,
+ ChatHomeServiceTest.TestConfiguration.class })
+@EnableConfigurationProperties(ChatBackendProperties.class)
public abstract class ChatHomeServiceTest
{
@Autowired
assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
});
}
+
+ static class TestConfiguration
+ {
+ @Bean
+ ObjectMapper objectMapper()
+ {
+ ObjectMapper objectMapper = new ObjectMapper();
+ objectMapper.registerModule(new JavaTimeModule());
+ return objectMapper;
+ }
+
+ @Bean
+ Clock clock()
+ {
+ return Clock.systemDefaultZone();
+ }
+ }
}
package de.juplo.kafka.chat.backend.implementation.inmemory;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest;
-import de.juplo.kafka.chat.backend.implementation.ShardingStrategy;
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
-import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
-import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.TestPropertySource;
-import java.net.URI;
-import java.nio.file.Paths;
-import java.time.Clock;
-import java.util.logging.Level;
-import java.util.stream.IntStream;
+import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS;
+import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.OWNED_SHARD;
+
+@TestPropertySource(properties = {
+ "chat.backend.inmemory.sharding-strategy=kafkalike",
+ "chat.backend.inmemory.num-shards=" + NUM_SHARDS,
+ "chat.backend.inmemory.owned-shards=" + OWNED_SHARD,
+ "chat.backend.inmemory.storage-strategy=files",
+ "chat.backend.inmemory.storage-directory=target/test-classes/data/files" })
public class ShardedChatHomeServiceTest extends ChatHomeServiceWithShardsTest
{
- @TestConfiguration
- static class Configuration
- {
- @Bean
- ShardedChatHomeService chatHome(
- StorageStrategy storageStrategy,
- Clock clock)
- {
- SimpleChatHomeService[] chatHomes = new SimpleChatHomeService[NUM_SHARDS];
-
- IntStream
- .of(ownedShards())
- .forEach(shard -> chatHomes[shard] = new SimpleChatHomeService(
- shard,
- storageStrategy,
- clock,
- bufferSize()));
-
- ShardingStrategy strategy = new KafkaLikeShardingStrategy(NUM_SHARDS);
-
- return new ShardedChatHomeService(
- "http://instance-0",
- chatHomes,
- IntStream
- .range(0, NUM_SHARDS)
- .mapToObj(shard -> "http://instance-0")
- .map(uriString -> URI.create(uriString))
- .toArray(size -> new URI[size]),
- strategy);
- }
-
- @Bean
- FilesStorageStrategy storageStrategy(
- Clock clock,
- ObjectMapper objectMapper)
- {
- return new FilesStorageStrategy(
- Paths.get("target", "test-classes", "data", "files"),
- new KafkaLikeShardingStrategy(NUM_SHARDS),
- objectMapper,
- Level.FINE,
- true);
- }
-
- @Bean
- ObjectMapper objectMapper()
- {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.registerModule(new JavaTimeModule());
- return objectMapper;
- }
-
- @Bean
- Clock clock()
- {
- return Clock.systemDefaultZone();
- }
-
- int[] ownedShards()
- {
- return new int[] { OWNED_SHARD };
- }
-
- int bufferSize()
- {
- return 8;
- }
- }
}
package de.juplo.kafka.chat.backend.implementation.inmemory;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.juplo.kafka.chat.backend.domain.ChatHomeServiceTest;
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
-import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
-import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.context.annotation.Bean;
-
-import java.nio.file.Paths;
-import java.time.Clock;
-import java.util.logging.Level;
+import org.springframework.test.context.TestPropertySource;
+@TestPropertySource(properties = {
+ "chat.backend.inmemory.sharding-strategy=none",
+ "chat.backend.inmemory.storage-strategy=files",
+ "chat.backend.inmemory.storage-directory=target/test-classes/data/files" })
public class SimpleChatHomeServiceTest extends ChatHomeServiceTest
{
- @TestConfiguration
- static class Configuration
- {
- @Bean
- SimpleChatHomeService chatHome(
- StorageStrategy storageStrategy,
- Clock clock)
- {
- return new SimpleChatHomeService(
- storageStrategy,
- clock,
- bufferSize());
- }
-
- @Bean
- FilesStorageStrategy storageStrategy(
- Clock clock,
- ObjectMapper objectMapper)
- {
- return new FilesStorageStrategy(
- Paths.get("target", "test-classes", "data", "files"),
- chatRoomId -> 0,
- objectMapper,
- Level.FINE,
- true);
- }
-
- @Bean
- ObjectMapper objectMapper()
- {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.registerModule(new JavaTimeModule());
- return objectMapper;
- }
-
- @Bean
- Clock clock()
- {
- return Clock.systemDefaultZone();
- }
-
- int bufferSize()
- {
- return 8;
- }
- }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
-import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS;
import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.DATA_TOPIC;
import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.INFO_TOPIC;
-@SpringBootTest(
- classes = {
+@ContextConfiguration(classes = {
KafkaTestUtils.KafkaTestConfiguration.class,
KafkaAutoConfiguration.class,
TaskExecutionAutoConfiguration.class,
- },
- properties = {
- "spring.main.allow-bean-definition-overriding=true",
+ })
+@TestPropertySource(properties = {
"chat.backend.services=kafka",
"chat.backend.kafka.client-id-PREFIX=TEST",
"chat.backend.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
})
@EmbeddedKafka(
topics = { INFO_TOPIC, DATA_TOPIC },
- partitions = 10)
+ partitions = NUM_SHARDS)
@Slf4j
public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
{