1 package de.juplo.kafka.chat.backend.domain;
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;
20 import java.time.Clock;
21 import java.time.Duration;
22 import java.util.UUID;
24 import static pl.rzrz.assertj.reactor.Assertions.assertThat;
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
36 ChatHomeService chatHomeService;
40 @DisplayName("Assert chatroom is delivered, if it exists")
41 void testGetExistingChatroom()
44 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
47 Mono<ChatRoomData> mono = Mono
48 .defer(() -> chatHomeService.getChatRoomData(chatRoomId))
49 .log("testGetExistingChatroom")
51 .backoff(5, Duration.ofSeconds(1))
52 .filter(throwable -> throwable instanceof LoadInProgressException));
55 assertThat(mono).emitsCount(1);
59 @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
60 void testGetNonExistentChatroom()
63 UUID chatRoomId = UUID.fromString("7f59ec77-832e-4a17-8d22-55ef46242c17");
66 Mono<ChatRoomData> mono = Mono
67 .defer(() -> chatHomeService.getChatRoomData(chatRoomId))
68 .log("testGetNonExistentChatroom")
70 .backoff(5, Duration.ofSeconds(1))
71 .filter(throwable -> throwable instanceof LoadInProgressException));
74 assertThat(mono).sendsError(e ->
76 assertThat(e).isInstanceOf(UnknownChatroomException.class);
77 UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e;
78 assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
82 static class TestConfiguration
85 ObjectMapper objectMapper()
87 ObjectMapper objectMapper = new ObjectMapper();
88 objectMapper.registerModule(new JavaTimeModule());
95 return Clock.systemDefaultZone();