5b50314f78de7499ac97d2200f919f9d6d5cec36
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / domain / ChatHomeTest.java
1 package de.juplo.kafka.chat.backend.domain;
2
3 import org.junit.jupiter.api.DisplayName;
4 import org.junit.jupiter.api.Test;
5 import org.junit.jupiter.api.extension.ExtendWith;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.test.context.junit.jupiter.SpringExtension;
8 import reactor.core.publisher.Mono;
9 import reactor.util.retry.Retry;
10
11 import java.time.Duration;
12 import java.util.UUID;
13
14 import static pl.rzrz.assertj.reactor.Assertions.assertThat;
15
16
17 @ExtendWith(SpringExtension.class)
18 public abstract class ChatHomeTest
19 {
20   @Autowired
21   ChatHome chatHome;
22
23
24   @Test
25   @DisplayName("Assert chatroom is delivered, if it exists")
26   void testGetExistingChatroom()
27   {
28     // Given
29     UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
30
31     // When
32     Mono<ChatRoomData> mono = Mono
33         .defer(() -> chatHome.getChatRoomData(chatRoomId))
34         .log("testGetExistingChatroom")
35         .retryWhen(Retry
36             .backoff(5, Duration.ofSeconds(1))
37             .filter(throwable -> throwable instanceof LoadInProgressException));
38
39     // Then
40     assertThat(mono).emitsCount(1);
41   }
42
43   @Test
44   @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
45   void testGetNonExistentChatroom()
46   {
47     // Given
48     UUID chatRoomId = UUID.fromString("7f59ec77-832e-4a17-8d22-55ef46242c17");
49
50     // When
51     Mono<ChatRoomData> mono = Mono
52         .defer(() -> chatHome.getChatRoomData(chatRoomId))
53         .log("testGetNonExistentChatroom")
54         .retryWhen(Retry
55             .backoff(5, Duration.ofSeconds(1))
56             .filter(throwable -> throwable instanceof LoadInProgressException));
57
58     // Then
59     assertThat(mono).sendsError(e ->
60     {
61       assertThat(e).isInstanceOf(UnknownChatroomException.class);
62       UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e;
63       assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
64     });
65   }
66 }