From 8e3e1cb5ca959f6ac096a2ec7d1a2c6a235cf32d Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 20 Feb 2024 13:05:21 +0100 Subject: [PATCH] refactor: Cleaned up code --- .../inmemory/InMemoryChatMessageService.java | 2 +- .../inmemory/SimpleChatHomeService.java | 1 - .../mongodb/MongoDbStorageConfiguration.java | 8 ++------ .../storage/mongodb/MongoDbStorageStrategy.java | 14 ++++---------- .../InMemoryWithMongoDbConfigurationIT.java | 2 +- .../chat/backend/InMemoryWithMongoDbStorageIT.java | 11 +++-------- 6 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java index 8d89aebf..8f3e4956 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryChatMessageService.java @@ -18,7 +18,7 @@ public class InMemoryChatMessageService implements ChatMessageService public InMemoryChatMessageService(Flux messageFlux) { - log.debug("Creating InMemoryChatRoomService"); + log.debug("Creating InMemoryChatMessageService"); messages = new LinkedHashMap<>(); messageFlux.subscribe(message -> messages.put(message.getKey(), message)); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java index 5ed039ea..93593d86 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java @@ -40,7 +40,6 @@ public class SimpleChatHomeService implements ChatHomeService Clock clock, int bufferSize) { -; this.shard = shard; log.info("Created {}", this); diff --git a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageConfiguration.java index ad57da5c..1b5803ac 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageConfiguration.java @@ -17,12 +17,8 @@ public class MongoDbStorageConfiguration @Bean public StorageStrategy storageStrategy( ChatRoomRepository chatRoomRepository, - MessageRepository messageRepository, - ShardingStrategy shardingStrategy) + MessageRepository messageRepository) { - return new MongoDbStorageStrategy( - chatRoomRepository, - messageRepository, - shardingStrategy); + return new MongoDbStorageStrategy(chatRoomRepository, messageRepository); } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageStrategy.java index 1428119e..fb040395 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageStrategy.java @@ -2,7 +2,6 @@ package de.juplo.kafka.chat.backend.storage.mongodb; import de.juplo.kafka.chat.backend.domain.ChatRoomInfo; import de.juplo.kafka.chat.backend.domain.Message; -import de.juplo.kafka.chat.backend.implementation.ShardingStrategy; import de.juplo.kafka.chat.backend.implementation.StorageStrategy; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -17,7 +16,6 @@ public class MongoDbStorageStrategy implements StorageStrategy { private final ChatRoomRepository chatRoomRepository; private final MessageRepository messageRepository; - private final ShardingStrategy shardingStrategy; @Override @@ -25,7 +23,7 @@ public class MongoDbStorageStrategy implements StorageStrategy { return chatRoomInfoFlux .map(ChatRoomTo::from) - .map(chatroomTo -> chatRoomRepository.save(chatroomTo)) + .map(chatRoomRepository::save) .map(ChatRoomTo::toChatRoomInfo); } @@ -34,11 +32,7 @@ public class MongoDbStorageStrategy implements StorageStrategy { return Flux .fromIterable(chatRoomRepository.findAll()) - .map(chatRoomTo -> - { - UUID chatRoomId = UUID.fromString(chatRoomTo.getId()); - return new ChatRoomInfo(chatRoomId, chatRoomTo.getName(), null); - }); + .map(ChatRoomTo::toChatRoomInfo); } @Override @@ -46,7 +40,7 @@ public class MongoDbStorageStrategy implements StorageStrategy { return messageFlux .map(message -> MessageTo.from(chatRoomId, message)) - .map(messageTo -> messageRepository.save(messageTo)) + .map(messageRepository::save) .map(MessageTo::toMessage); } @@ -55,6 +49,6 @@ public class MongoDbStorageStrategy implements StorageStrategy { return Flux .fromIterable(messageRepository.findByChatRoomIdOrderBySerialAsc(chatRoomId.toString())) - .map(messageTo -> messageTo.toMessage()); + .map(MessageTo::toMessage); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java index 69377369..c7ede36b 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbConfigurationIT.java @@ -37,7 +37,7 @@ class InMemoryWithMongoDbConfigurationIT extends AbstractConfigurationIT @DynamicPropertySource static void addMongoPortProperty(DynamicPropertyRegistry registry) { - registry.add("spring.data.mongodb.port", () -> CONTAINER.getMappedPort(27017)); + registry.add("spring.data.mongodb.port", () -> CONTAINER.getMappedPort(MONGODB_PORT)); } @BeforeEach diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java index 43a09003..7566f521 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java @@ -60,13 +60,9 @@ public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT @Bean MongoDbStorageStrategy storageStrategy( ChatRoomRepository chatRoomRepository, - MessageRepository messageRepository, - Clock clock) + MessageRepository messageRepository) { - return new MongoDbStorageStrategy( - chatRoomRepository, - messageRepository, - chatRoomId -> 0); + return new MongoDbStorageStrategy(chatRoomRepository, messageRepository); } @Bean @@ -80,8 +76,7 @@ public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT @Container private static final GenericContainer CONTAINER = - new GenericContainer("mongo:6") - .withExposedPorts(MONGODB_PORT); + new GenericContainer("mongo:6").withExposedPorts(MONGODB_PORT); public static class DataSourceInitializer implements ApplicationContextInitializer -- 2.20.1