From 73367ea515892f487f7eaf9d34f92deb17fa0625 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 21 Feb 2024 17:34:56 +0100 Subject: [PATCH] test: Added IT for `ChatRoomRepository` and `MessageRepository` --- pom.xml | 7 +- .../storage/mongodb/ChatRoomRepositoryIT.java | 69 ++++++++++ .../storage/mongodb/MessageRepositoryIT.java | 118 ++++++++++++++++++ 3 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java create mode 100644 src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java diff --git a/pom.xml b/pom.xml index 04fe3747..9f6db208 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,11 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-testcontainers + test + pl.rzrz assertj-reactor @@ -72,7 +77,7 @@ org.testcontainers - testcontainers + mongodb test 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 index 00000000..53e3e659 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/ChatRoomRepositoryIT.java @@ -0,0 +1,69 @@ +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 MONGO_DB = 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 index 00000000..a4287535 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/storage/mongodb/MessageRepositoryIT.java @@ -0,0 +1,118 @@ +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 MONGO_DB = 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); + } +} -- 2.20.1