From: Kai Moritz Date: Sat, 3 Feb 2024 23:24:07 +0000 (+0100) Subject: feat: Switched to `spring-boot-starter-data-mongodb-reactive` X-Git-Tag: rebase--2024-02-23--16-28~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=2ba733abfef3caf7d663397827801f58a18050cf;p=demos%2Fkafka%2Fchat feat: Switched to `spring-boot-starter-data-mongodb-reactive` --- diff --git a/pom.xml b/pom.xml index 9f6db208..e98a75e4 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ org.springframework.boot - spring-boot-starter-data-mongodb + spring-boot-starter-data-mongodb-reactive org.apache.kafka diff --git a/src/main/java/de/juplo/kafka/chat/backend/storage/files/FilesStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/storage/files/FilesStorageConfiguration.java index 630c1fa9..72f8e7d5 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/storage/files/FilesStorageConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/storage/files/FilesStorageConfiguration.java @@ -6,7 +6,8 @@ import de.juplo.kafka.chat.backend.implementation.ShardingStrategy; import de.juplo.kafka.chat.backend.implementation.StorageStrategy; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration; +import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration; +import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -21,7 +22,8 @@ import java.nio.file.Paths; @Configuration @EnableAutoConfiguration( exclude = { - MongoRepositoriesAutoConfiguration.class, + MongoReactiveDataAutoConfiguration.class, + MongoReactiveRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class }) public class FilesStorageConfiguration { diff --git a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepository.java b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepository.java index d9f6a0f5..9262bf66 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepository.java +++ b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepository.java @@ -1,8 +1,8 @@ package de.juplo.kafka.chat.backend.storage.mongodb; -import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; -public interface ChatRoomRepository extends MongoRepository +public interface ChatRoomRepository extends ReactiveMongoRepository { } diff --git a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepository.java b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepository.java index 712f5a4a..db8503c3 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepository.java +++ b/src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepository.java @@ -1,11 +1,10 @@ package de.juplo.kafka.chat.backend.storage.mongodb; -import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; +import reactor.core.publisher.Flux; -import java.util.List; - -public interface MessageRepository extends MongoRepository +public interface MessageRepository extends ReactiveMongoRepository { - List findByChatRoomIdOrderBySerialAsc(String chatRoomId); + Flux findByChatRoomIdOrderBySerialAsc(String chatRoomId); } 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 81696450..13f3c0d6 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 @@ -31,15 +31,15 @@ public class MongoDbStorageStrategy implements StorageStrategy loggingLevel, showOperatorLine) .map(ChatRoomTo::from) - .map(chatRoomRepository::save) + .flatMap(chatRoomRepository::save) .map(ChatRoomTo::toChatRoomInfo); } @Override public Flux readChatRoomInfo() { - return Flux - .fromIterable(chatRoomRepository.findAll()) + return chatRoomRepository + .findAll() .log( loggingCategory, loggingLevel, @@ -56,15 +56,15 @@ public class MongoDbStorageStrategy implements StorageStrategy loggingLevel, showOperatorLine) .map(message -> MessageTo.from(chatRoomId, message)) - .map(messageRepository::save) + .flatMap(messageRepository::save) .map(MessageTo::toMessage); } @Override public Flux readChatRoomData(UUID chatRoomId) { - return Flux - .fromIterable(messageRepository.findByChatRoomIdOrderBySerialAsc(chatRoomId.toString())) + return messageRepository + .findByChatRoomIdOrderBySerialAsc(chatRoomId.toString()) .log( loggingCategory, loggingLevel, diff --git a/src/main/java/de/juplo/kafka/chat/backend/storage/nostorage/NoStorageStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/storage/nostorage/NoStorageStorageConfiguration.java index 2e499ef3..dc2bdfcc 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/storage/nostorage/NoStorageStorageConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/storage/nostorage/NoStorageStorageConfiguration.java @@ -3,7 +3,8 @@ package de.juplo.kafka.chat.backend.storage.nostorage; import de.juplo.kafka.chat.backend.implementation.StorageStrategy; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration; +import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration; +import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -17,7 +18,8 @@ import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration( exclude = { - MongoRepositoriesAutoConfiguration.class, + MongoReactiveDataAutoConfiguration.class, + MongoReactiveRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class }) public class NoStorageStorageConfiguration { diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java index 7c9c07c3..74c7c6fd 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java @@ -40,7 +40,7 @@ public abstract class AbstractStorageStrategyIT { storageStrategy .write(chathome) - .subscribe(); + .block(); } @Test 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 8f4e37a3..464513bd 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java @@ -32,8 +32,8 @@ public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT @Autowired ChatRoomRepository chatRoomRepository, @Autowired MessageRepository messageRepository) { - chatRoomRepository.deleteAll(); - messageRepository.deleteAll(); + chatRoomRepository.deleteAll().block(); + messageRepository.deleteAll().block(); } @BeforeEach diff --git a/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java b/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java index 5c6e10cf..d0a7f6bd 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java @@ -36,33 +36,33 @@ public class ChatRoomRepositoryIT @BeforeEach public void setUp() { - repository.deleteAll(); + repository.deleteAll().block(); - a = repository.save(new ChatRoomTo("a", "foo")); - b = repository.save(new ChatRoomTo("b", "bar")); - c = repository.save(new ChatRoomTo("c", "bar")); + a = repository.save(new ChatRoomTo("a", "foo")).block(); + b = repository.save(new ChatRoomTo("b", "bar")).block(); + c = repository.save(new ChatRoomTo("c", "bar")).block(); } @Test public void findsAll() { - assertThat(repository.findAll()).containsExactly(a, b, c); + assertThat(repository.findAll()).emitsExactly(a, b, c); } @Test public void findsById() { - assertThat(repository.findById("a")).contains(a); - assertThat(repository.findById("b")).contains(b); - assertThat(repository.findById("c")).contains(c); - assertThat(repository.findById("666")).isEmpty(); + assertThat(repository.findById("a")).emitsExactly(a); + assertThat(repository.findById("b")).emitsExactly(b); + assertThat(repository.findById("c")).emitsExactly(c); + assertThat(repository.findById("666")).emitsExactly(); } @Test public void findsByExample() { - assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "foo")))).containsExactly(a); - assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "bar")))).containsExactly(b, c); - assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "foobar")))).isEmpty(); + assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "foo")))).emitsExactly(a); + assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "bar")))).emitsExactly(b, c); + assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "foobar")))).emitsExactly(); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java b/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java index 3e8a7160..06fddb2f 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java @@ -40,7 +40,7 @@ public class MessageRepositoryIT @BeforeEach public void setUp() { - repository.deleteAll(); + repository.deleteAll().block(); foo = UUID.randomUUID(); bar = UUID.randomUUID(); @@ -54,64 +54,64 @@ public class MessageRepositoryIT Message.MessageKey.of("peter", 1l), serial++, now.plusSeconds(serial), - "Nachricht #" + serial))); + "Nachricht #" + serial))).block(); foo_2 = repository.save(MessageTo.from( foo, new Message( Message.MessageKey.of("ute", 2l), serial++, now.plusSeconds(serial), - "Nachricht #" + serial))); + "Nachricht #" + serial))).block(); bar_1 = repository.save(MessageTo.from( bar, new Message( Message.MessageKey.of("klaus", 1l), serial++, now.plusSeconds(serial), - "Nachricht #" + serial))); + "Nachricht #" + serial))).block(); bar_2 = repository.save(MessageTo.from( bar, new Message( Message.MessageKey.of("beate", 2l), serial++, now.plusSeconds(serial), - "Nachricht #" + serial))); + "Nachricht #" + serial))).block(); bar_3 = repository.save(MessageTo.from( bar, new Message( Message.MessageKey.of("peter", 3l), serial++, now.plusSeconds(serial), - "Nachricht #" + serial))); + "Nachricht #" + serial))).block(); } @Test public void findsAll() { - assertThat(repository.findAll()).containsExactly(foo_1, foo_2, bar_1, bar_2, bar_3); + assertThat(repository.findAll()).emitsExactly(foo_1, foo_2, bar_1, bar_2, bar_3); } @Test public void findsByExample_chatRoomId() { - assertThat(repository.findAll(Example.of(new MessageTo(foo.toString(), null, null, null, null, null)))).containsExactly(foo_1, foo_2); - assertThat(repository.findAll(Example.of(new MessageTo(bar.toString(), null, null, null, null, null)))).containsExactly(bar_1, bar_2, bar_3); + assertThat(repository.findAll(Example.of(new MessageTo(foo.toString(), null, null, null, null, null)))).emitsExactly(foo_1, foo_2); + assertThat(repository.findAll(Example.of(new MessageTo(bar.toString(), null, null, null, null, null)))).emitsExactly(bar_1, bar_2, bar_3); } @Test public void findsByExample_user() { - assertThat(repository.findAll(Example.of(new MessageTo(null, "peter", null, null, null, null)))).containsExactly(foo_1, bar_3); - assertThat(repository.findAll(Example.of(new MessageTo(null, "klaus", null, null, null, null)))).containsExactly(bar_1); - assertThat(repository.findAll(Example.of(new MessageTo(null, "ute", null, null, null, null)))).containsExactly(foo_2); - assertThat(repository.findAll(Example.of(new MessageTo(null, "beate", null, null, null, null)))).containsExactly(bar_2); + assertThat(repository.findAll(Example.of(new MessageTo(null, "peter", null, null, null, null)))).emitsExactly(foo_1, bar_3); + assertThat(repository.findAll(Example.of(new MessageTo(null, "klaus", null, null, null, null)))).emitsExactly(bar_1); + assertThat(repository.findAll(Example.of(new MessageTo(null, "ute", null, null, null, null)))).emitsExactly(foo_2); + assertThat(repository.findAll(Example.of(new MessageTo(null, "beate", null, null, null, null)))).emitsExactly(bar_2); } @Test public void findsByExample_id() { - assertThat(repository.findAll(Example.of(new MessageTo(null, null, 1l, null, null, null)))).containsExactly(foo_1, bar_1); - assertThat(repository.findAll(Example.of(new MessageTo(null, null, 2l, null, null, null)))).containsExactly(foo_2, bar_2); - assertThat(repository.findAll(Example.of(new MessageTo(null, null, 3l, null, null, null)))).containsExactly(bar_3); + assertThat(repository.findAll(Example.of(new MessageTo(null, null, 1l, null, null, null)))).emitsExactly(foo_1, bar_1); + assertThat(repository.findAll(Example.of(new MessageTo(null, null, 2l, null, null, null)))).emitsExactly(foo_2, bar_2); + assertThat(repository.findAll(Example.of(new MessageTo(null, null, 3l, null, null, null)))).emitsExactly(bar_3); } }