test: Added IT for `ChatRoomRepository` and `MessageRepository`
authorKai Moritz <kai@juplo.de>
Wed, 21 Feb 2024 17:30:01 +0000 (18:30 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 15:08:07 +0000 (16:08 +0100)
pom.xml
src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 04fe374..9f6db20 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-testcontainers</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>pl.rzrz</groupId>
       <artifactId>assertj-reactor</artifactId>
@@ -72,7 +77,7 @@
     </dependency>
     <dependency>
       <groupId>org.testcontainers</groupId>
-      <artifactId>testcontainers</artifactId>
+      <artifactId>mongodb</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
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
new file mode 100644 (file)
index 0000000..5c6e10c
--- /dev/null
@@ -0,0 +1,68 @@
+package de.juplo.kafka.chat.backend.storage.mongodb;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
+import org.springframework.data.domain.Example;
+
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import static pl.rzrz.assertj.reactor.Assertions.assertThat;
+
+
+@SpringBootTest(
+    webEnvironment = SpringBootTest.WebEnvironment.NONE,
+    properties = {
+        "spring.data.mongodb.host=localhost",
+        "spring.data.mongodb.database=test",
+        "chat.backend.inmemory.storage-strategy=mongodb" })
+@Testcontainers
+public class ChatRoomRepositoryIT
+{
+  @Container
+  @ServiceConnection
+  static MongoDBContainer MONGODB = new MongoDBContainer("mongo:6");
+
+  @Autowired
+  ChatRoomRepository repository;
+
+  ChatRoomTo a, b, c;
+
+  @BeforeEach
+  public void setUp()
+  {
+    repository.deleteAll();
+
+    a = repository.save(new ChatRoomTo("a", "foo"));
+    b = repository.save(new ChatRoomTo("b", "bar"));
+    c = repository.save(new ChatRoomTo("c", "bar"));
+  }
+
+  @Test
+  public void findsAll()
+  {
+    assertThat(repository.findAll()).containsExactly(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();
+  }
+
+  @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();
+  }
+}
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
new file mode 100644 (file)
index 0000000..3e8a716
--- /dev/null
@@ -0,0 +1,117 @@
+package de.juplo.kafka.chat.backend.storage.mongodb;
+
+import de.juplo.kafka.chat.backend.domain.Message;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
+import org.springframework.data.domain.Example;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import java.time.Clock;
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+import static pl.rzrz.assertj.reactor.Assertions.assertThat;
+
+
+@SpringBootTest(
+    webEnvironment = SpringBootTest.WebEnvironment.NONE,
+    properties = {
+        "spring.data.mongodb.host=localhost",
+        "spring.data.mongodb.database=test",
+        "chat.backend.inmemory.storage-strategy=mongodb" })
+@Testcontainers
+public class MessageRepositoryIT
+{
+  @Container
+  @ServiceConnection
+  static MongoDBContainer MONGODB = new MongoDBContainer("mongo:6");
+
+  @Autowired
+  MessageRepository repository;
+
+  UUID foo, bar;
+  MessageTo foo_1, foo_2, bar_1, bar_2, bar_3;
+
+  @BeforeEach
+  public void setUp()
+  {
+    repository.deleteAll();
+
+    foo = UUID.randomUUID();
+    bar = UUID.randomUUID();
+
+    long serial = 0;
+    LocalDateTime now = LocalDateTime.now(Clock.systemDefaultZone());
+
+    foo_1 = repository.save(MessageTo.from(
+        foo,
+        new Message(
+            Message.MessageKey.of("peter", 1l),
+            serial++,
+            now.plusSeconds(serial),
+            "Nachricht #" + serial)));
+    foo_2 = repository.save(MessageTo.from(
+        foo,
+        new Message(
+            Message.MessageKey.of("ute", 2l),
+            serial++,
+            now.plusSeconds(serial),
+            "Nachricht #" + serial)));
+    bar_1 = repository.save(MessageTo.from(
+        bar,
+        new Message(
+            Message.MessageKey.of("klaus", 1l),
+            serial++,
+            now.plusSeconds(serial),
+            "Nachricht #" + serial)));
+    bar_2 = repository.save(MessageTo.from(
+        bar,
+        new Message(
+            Message.MessageKey.of("beate", 2l),
+            serial++,
+            now.plusSeconds(serial),
+            "Nachricht #" + serial)));
+    bar_3 = repository.save(MessageTo.from(
+        bar,
+        new Message(
+            Message.MessageKey.of("peter", 3l),
+            serial++,
+            now.plusSeconds(serial),
+            "Nachricht #" + serial)));
+  }
+
+  @Test
+  public void findsAll()
+  {
+    assertThat(repository.findAll()).containsExactly(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);
+  }
+
+  @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);
+  }
+
+  @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);
+  }
+}