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