test: Simplified `ChatHomeServiceTest`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / domain / ChatHomeServiceTest.java
1 package de.juplo.kafka.chat.backend.domain;
2
3 import de.juplo.kafka.chat.backend.ChatBackendProperties;
4 import de.juplo.kafka.chat.backend.domain.exceptions.LoadInProgressException;
5 import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException;
6 import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration;
7 import de.juplo.kafka.chat.backend.storage.files.FilesStorageConfiguration;
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
14 import reactor.core.publisher.Mono;
15 import reactor.util.retry.Retry;
16
17 import java.time.Clock;
18 import java.time.Duration;
19 import java.util.UUID;
20
21 import static pl.rzrz.assertj.reactor.Assertions.assertThat;
22
23
24 @SpringJUnitConfig(classes = {
25     InMemoryServicesConfiguration.class,
26     FilesStorageConfiguration.class,
27     ChatHomeServiceTest.TestConfiguration.class })
28 @EnableConfigurationProperties(ChatBackendProperties.class)
29 public abstract class ChatHomeServiceTest
30 {
31   @Autowired
32   ChatHomeService chatHomeService;
33
34
35   @Test
36   @DisplayName("Assert chatroom is delivered, if it exists")
37   void testGetExistingChatroom()
38   {
39     // Given
40     UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
41
42     // When
43     Mono<ChatRoomData> mono = Mono
44         .defer(() -> chatHomeService.getChatRoomData(chatRoomId))
45         .log("testGetExistingChatroom")
46         .retryWhen(Retry
47             .backoff(5, Duration.ofSeconds(1))
48             .filter(throwable -> throwable instanceof LoadInProgressException));
49
50     // Then
51     assertThat(mono).emitsCount(1);
52   }
53
54   @Test
55   @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
56   void testGetNonExistentChatroom()
57   {
58     // Given
59     UUID chatRoomId = UUID.fromString("7f59ec77-832e-4a17-8d22-55ef46242c17");
60
61     // When
62     Mono<ChatRoomData> mono = Mono
63         .defer(() -> chatHomeService.getChatRoomData(chatRoomId))
64         .log("testGetNonExistentChatroom")
65         .retryWhen(Retry
66             .backoff(5, Duration.ofSeconds(1))
67             .filter(throwable -> throwable instanceof LoadInProgressException));
68
69     // Then
70     assertThat(mono).sendsError(e ->
71     {
72       assertThat(e).isInstanceOf(UnknownChatroomException.class);
73       UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e;
74       assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
75     });
76   }
77
78   static class TestConfiguration
79   {
80     @Bean
81     Clock clock()
82     {
83       return Clock.systemDefaultZone();
84     }
85   }
86 }