test: Added IT for `ChatRoomRepository` and `MessageRepository`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / storage / mongodb / ChatRoomRepositoryIT.java
1 package de.juplo.kafka.chat.backend.storage.mongodb;
2
3 import org.junit.jupiter.api.BeforeEach;
4 import org.junit.jupiter.api.Test;
5
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
9 import org.springframework.data.domain.Example;
10
11 import org.testcontainers.containers.MongoDBContainer;
12 import org.testcontainers.junit.jupiter.Container;
13 import org.testcontainers.junit.jupiter.Testcontainers;
14
15 import static pl.rzrz.assertj.reactor.Assertions.assertThat;
16
17
18 @SpringBootTest(
19     webEnvironment = SpringBootTest.WebEnvironment.NONE,
20     properties = {
21         "spring.data.mongodb.host=localhost",
22         "spring.data.mongodb.database=test",
23         "chat.backend.inmemory.storage-strategy=mongodb" })
24 @Testcontainers
25 public class ChatRoomRepositoryIT
26 {
27
28   @Container
29   @ServiceConnection
30   static MongoDBContainer MONGO_DB = new MongoDBContainer("mongo:6");
31
32   @Autowired
33   ChatRoomRepository repository;
34
35   ChatRoomTo a, b, c;
36
37   @BeforeEach
38   public void setUp()
39   {
40     repository.deleteAll();
41
42     a = repository.save(new ChatRoomTo("a", "foo"));
43     b = repository.save(new ChatRoomTo("b", "bar"));
44     c = repository.save(new ChatRoomTo("c", "bar"));
45   }
46
47   @Test
48   public void findsAll()
49   {
50     assertThat(repository.findAll()).containsExactly(a, b, c);
51   }
52
53   @Test
54   public void findsById()
55   {
56     assertThat(repository.findById("a")).contains(a);
57     assertThat(repository.findById("b")).contains(b);
58     assertThat(repository.findById("c")).contains(c);
59     assertThat(repository.findById("666")).isEmpty();
60   }
61
62   @Test
63   public void findsByExample()
64   {
65     assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "foo")))).containsExactly(a);
66     assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "bar")))).containsExactly(b, c);
67     assertThat(repository.findAll(Example.of(new ChatRoomTo(null, "foobar")))).isEmpty();
68   }
69 }