package de.juplo.kafka.chat.backend;
import de.juplo.kafka.chat.backend.domain.ChatHomeService;
-import de.juplo.kafka.chat.backend.implementation.inmemory.SimpleChatHomeService;
-import lombok.RequiredArgsConstructor;
+import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
+import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
+import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryTestUtils;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
import java.time.Clock;
-@RequiredArgsConstructor
+@ContextConfiguration(classes = InMemoryTestUtils.class)
@Slf4j
public abstract class AbstractInMemoryStorageIT extends AbstractStorageStrategyIT
{
- final Clock clock;
+ @Autowired
+ InMemoryTestUtils testUtils;
@Override
- protected StorageStrategyITConfig getConfig()
+ ChatHomeService getChatHome()
{
- return new StorageStrategyITConfig()
- {
- int bufferSize = 8;
-
- SimpleChatHomeService simpleChatHome = new SimpleChatHomeService(
- getStorageStrategy(),
- clock,
- bufferSize);
-
- @Override
- public ChatHomeService getChatHome()
- {
- return simpleChatHome;
- }
- };
+ return testUtils.createNoneShardingChatHomeService();
}
}
import de.juplo.kafka.chat.backend.domain.*;
import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
+import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
+import de.juplo.kafka.chat.backend.storage.files.FilesStorageConfiguration;
+import de.juplo.kafka.chat.backend.storage.mongodb.MongoDbStorageConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
+import java.time.Clock;
import java.util.List;
import java.util.UUID;
import static pl.rzrz.assertj.reactor.Assertions.*;
+@SpringJUnitConfig(classes = {
+ InMemoryServicesConfiguration.class,
+ FilesStorageConfiguration.class,
+ MongoDbStorageConfiguration.class,
+ AbstractStorageStrategyIT.TestConfig.class })
+@EnableConfigurationProperties(ChatBackendProperties.class)
@Slf4j
public abstract class AbstractStorageStrategyIT
{
- protected ChatHomeService chathome;
+ ChatHomeService chathome;
+ @Autowired
+ StorageStrategy storageStrategy;
- protected abstract StorageStrategy getStorageStrategy();
- protected abstract StorageStrategyITConfig getConfig();
+ abstract ChatHomeService getChatHome();
protected void start()
{
- StorageStrategyITConfig config = getConfig();
- chathome = config.getChatHome();
+ chathome = getChatHome();
}
protected void stop()
{
- getStorageStrategy()
+ storageStrategy
.write(chathome)
.subscribe();
}
}
- interface StorageStrategyITConfig
+ static class TestConfig
{
- ChatHomeService getChatHome();
+ @Bean
+ Clock clock()
+ {
+ return Clock.systemDefaultZone();
+ }
}
}
package de.juplo.kafka.chat.backend;
-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 org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.TestPropertySource;
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;
+@TestPropertySource(properties = {
+ "chat.backend.inmemory.sharding-strategy=none",
+ "chat.backend.inmemory.storage-strategy=files",
+ "chat.backend.inmemory.storage-directory=target/files" })
@Slf4j
public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT
{
- 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
+ void resetStorage(
+ @Autowired ChatBackendProperties properties)
+ throws Exception
{
+ Path path = Paths.get(properties.getInmemory().getStorageDirectory());
if (Files.exists(path))
{
Files
package de.juplo.kafka.chat.backend;
-import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
import de.juplo.kafka.chat.backend.storage.mongodb.ChatRoomRepository;
import de.juplo.kafka.chat.backend.storage.mongodb.MessageRepository;
-import de.juplo.kafka.chat.backend.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.boot.testcontainers.service.connection.ServiceConnection;
-import org.springframework.context.annotation.Bean;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.context.TestPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
-import java.time.Clock;
-
+@TestPropertySource(properties = {
+ "chat.backend.inmemory.sharding-strategy=none",
+ "chat.backend.inmemory.storage-strategy=mongodb" })
@Testcontainers
-@ExtendWith({SpringExtension.class})
@EnableAutoConfiguration
-@AutoConfigureDataMongo
@Slf4j
public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT
{
- @Autowired
- MongoDbStorageStrategy storageStrategy;
- @Autowired
- ChatRoomRepository chatRoomRepository;
- @Autowired
- MessageRepository messageRepository;
-
-
- public InMemoryWithMongoDbStorageIT()
- {
- super(Clock.systemDefaultZone());
- }
-
-
- @Override
- protected StorageStrategy getStorageStrategy()
- {
- return storageStrategy;
- }
-
- @TestConfiguration
- static class InMemoryWithMongoDbStorageStrategyITConfig
- {
- @Bean
- MongoDbStorageStrategy storageStrategy(
- ChatRoomRepository chatRoomRepository,
- MessageRepository messageRepository)
- {
- return new MongoDbStorageStrategy(chatRoomRepository, messageRepository);
- }
-
- @Bean
- Clock clock()
- {
- return Clock.systemDefaultZone();
- }
- }
-
@Container
@ServiceConnection
private static final GenericContainer MONGODB = new MongoDBContainer("mongo:6");
+ @BeforeEach
+ void resetStorage(
+ @Autowired ChatRoomRepository chatRoomRepository,
+ @Autowired MessageRepository messageRepository)
+ {
+ chatRoomRepository.deleteAll();
+ messageRepository.deleteAll();
+ }
+
@BeforeEach
void setUpLogging()
{
Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
MONGODB.followOutput(logConsumer);
- chatRoomRepository.deleteAll();
- messageRepository.deleteAll();
}
}
--- /dev/null
+package de.juplo.kafka.chat.backend.implementation.inmemory;
+
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.domain.ChatHomeService;
+import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.time.Clock;
+
+
+public class InMemoryTestUtils
+{
+ private final InMemoryServicesConfiguration config =
+ new InMemoryServicesConfiguration();
+
+ @Autowired
+ ChatBackendProperties properties;
+ @Autowired
+ StorageStrategy storageStrategy;
+ @Autowired
+ Clock clock;
+
+
+ public ChatHomeService createNoneShardingChatHomeService()
+ {
+ return config.noneShardingChatHome(
+ properties,
+ storageStrategy,
+ clock);
+ }
+}