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