feat: Switched to `spring-boot-starter-data-mongodb-reactive`
authorKai Moritz <kai@juplo.de>
Sat, 3 Feb 2024 23:24:07 +0000 (00:24 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 16:15:04 +0000 (17:15 +0100)
pom.xml
src/main/java/de/juplo/kafka/chat/backend/storage/files/FilesStorageConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepository.java
src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepository.java
src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageStrategy.java
src/main/java/de/juplo/kafka/chat/backend/storage/nostorage/NoStorageStorageConfiguration.java
src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java
src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java
src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java
src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java

diff --git a/pom.xml b/pom.xml
index 9f6db20..e98a75e 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -44,7 +44,7 @@
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
-      <artifactId>spring-boot-starter-data-mongodb</artifactId>
+      <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
     </dependency>
     <dependency>
       <groupId>org.apache.kafka</groupId>
index 630c1fa..72f8e7d 100644 (file)
@@ -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
 {
index d9f6a0f..9262bf6 100644 (file)
@@ -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<ChatRoomTo, String>
+public interface ChatRoomRepository extends ReactiveMongoRepository<ChatRoomTo, String>
 {
 }
index 712f5a4..db8503c 100644 (file)
@@ -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<MessageTo, String>
+public interface MessageRepository extends ReactiveMongoRepository<MessageTo, String>
 {
-  List<MessageTo> findByChatRoomIdOrderBySerialAsc(String chatRoomId);
+  Flux<MessageTo> findByChatRoomIdOrderBySerialAsc(String chatRoomId);
 }
index 8169645..13f3c0d 100644 (file)
@@ -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<ChatRoomInfo> 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<Message> readChatRoomData(UUID chatRoomId)
   {
-    return Flux
-        .fromIterable(messageRepository.findByChatRoomIdOrderBySerialAsc(chatRoomId.toString()))
+    return messageRepository
+        .findByChatRoomIdOrderBySerialAsc(chatRoomId.toString())
         .log(
             loggingCategory,
             loggingLevel,
index 2e499ef..dc2bdfc 100644 (file)
@@ -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
 {
index 7c9c07c..74c7c6f 100644 (file)
@@ -40,7 +40,7 @@ public abstract class AbstractStorageStrategyIT
   {
     storageStrategy
         .write(chathome)
-        .subscribe();
+        .block();
   }
 
   @Test
index 8f4e37a..464513b 100644 (file)
@@ -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
index 5c6e10c..d0a7f6b 100644 (file)
@@ -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();
   }
 }
index 3e8a716..06fddb2 100644 (file)
@@ -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);
   }
 }