From: Kai Moritz Date: Fri, 17 Feb 2023 23:10:03 +0000 (+0100) Subject: refactor: DRY for the configuration of the `AbstractStorageStrategyIT` X-Git-Tag: wip-sharding~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=977b454a542b95a5d08a94dd5f8b55814350b7cd;p=demos%2Fkafka%2Fchat refactor: DRY for the configuration of the `AbstractStorageStrategyIT` --- diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractInMemoryStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractInMemoryStorageIT.java new file mode 100644 index 00000000..832ebd99 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractInMemoryStorageIT.java @@ -0,0 +1,47 @@ +package de.juplo.kafka.chat.backend.persistence; + +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.inmemory.InMemoryChatRoomFactory; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.time.Clock; + + +@RequiredArgsConstructor +@Slf4j +public abstract class AbstractInMemoryStorageIT extends AbstractStorageStrategyIT +{ + final Clock clock; + + @Override + protected StorageStrategyITConfig getConfig() + { + return new StorageStrategyITConfig() + { + InMemoryChatHomeService chatHomeService = new InMemoryChatHomeService( + 1, + new int[] { 0 }, + getStorageStrategy().read()); + + InMemoryChatRoomFactory chatRoomFactory = new InMemoryChatRoomFactory( + chatRoomId -> 0, + clock, + 8); + + @Override + public ChatHomeService getChatHomeService() + { + return chatHomeService; + } + + @Override + public ChatRoomFactory getChatRoomFactory() + { + return chatRoomFactory; + } + }; + } +} 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 index 8ca7d472..1bb08708 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageIT.java @@ -3,11 +3,7 @@ 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.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; @@ -20,18 +16,17 @@ import java.time.Clock; @Slf4j -public class InMemoryWithFilesStorageIT extends AbstractStorageStrategyIT +public class InMemoryWithFilesStorageIT extends AbstractInMemoryStorageIT { final static Path path = Paths.get("target","files"); - final Clock clock; final ObjectMapper mapper; final FilesStorageStrategy storageStrategy; public InMemoryWithFilesStorageIT() { - clock = Clock.systemDefaultZone(); + super(Clock.systemDefaultZone()); mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); @@ -51,35 +46,6 @@ public class InMemoryWithFilesStorageIT extends AbstractStorageStrategyIT return storageStrategy; } - @Override - protected StorageStrategyITConfig getConfig() - { - return new StorageStrategyITConfig() - { - InMemoryChatHomeService chatHomeService = new InMemoryChatHomeService( - 1, - new int[] { 0 }, - getStorageStrategy().read()); - - InMemoryChatRoomFactory chatRoomFactory = new InMemoryChatRoomFactory( - chatRoomId -> 0, - clock, - 8); - - @Override - public ChatHomeService getChatHomeService() - { - return chatHomeService; - } - - @Override - public ChatRoomFactory getChatRoomFactory() - { - return chatRoomFactory; - } - }; - } - @BeforeEach void reset() throws Exception { 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 index f86c9d3f..7ca9cb2f 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithMongoDbStorageIT.java @@ -1,10 +1,6 @@ package de.juplo.kafka.chat.backend.persistence; -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; @@ -35,7 +31,7 @@ import java.time.Clock; @AutoConfigureDataMongo @ContextConfiguration(initializers = DataSourceInitializer.class) @Slf4j -public class InMemoryWithMongoDbStorageIT extends AbstractStorageStrategyIT +public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT { @Autowired MongoDbStorageStrategy storageStrategy; @@ -45,39 +41,16 @@ public class InMemoryWithMongoDbStorageIT extends AbstractStorageStrategyIT Clock clock; - @Override - protected StorageStrategy getStorageStrategy() + public InMemoryWithMongoDbStorageIT() { - return storageStrategy; + super(Clock.systemDefaultZone()); } + @Override - protected StorageStrategyITConfig getConfig() + protected StorageStrategy getStorageStrategy() { - return new StorageStrategyITConfig() - { - InMemoryChatHomeService chatHomeService = new InMemoryChatHomeService( - 1, - new int[] { 0 }, - getStorageStrategy().read()); - - InMemoryChatRoomFactory chatRoomFactory = new InMemoryChatRoomFactory( - chatRoomId -> 0, - clock, - 8); - - @Override - public ChatHomeService getChatHomeService() - { - return chatHomeService; - } - - @Override - public ChatRoomFactory getChatRoomFactory() - { - return chatRoomFactory; - } - }; + return storageStrategy; } @TestConfiguration