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.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;
19 import java.time.Clock;
20 import java.time.Duration;
21 import java.util.UUID;
23 import static pl.rzrz.assertj.reactor.Assertions.assertThat;
26 @SpringJUnitConfig(classes = {
27 InMemoryServicesConfiguration.class,
28 FilesStorageConfiguration.class,
29 ChatHomeServiceTest.TestConfiguration.class })
30 @EnableConfigurationProperties(ChatBackendProperties.class)
31 public abstract class ChatHomeServiceTest
34 ChatHomeService chatHomeService;
38 @DisplayName("Assert chatroom is delivered, if it exists")
39 void testGetExistingChatroom()
42 UUID chatRoomId = UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7");
45 Mono<ChatRoomData> mono = Mono
46 .defer(() -> chatHomeService.getChatRoomData(chatRoomId))
47 .log("testGetExistingChatroom")
49 .backoff(5, Duration.ofSeconds(1))
50 .filter(throwable -> throwable instanceof LoadInProgressException));
53 assertThat(mono).emitsCount(1);
57 @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
58 void testGetNonExistentChatroom()
61 UUID chatRoomId = UUID.fromString("7f59ec77-832e-4a17-8d22-55ef46242c17");
64 Mono<ChatRoomData> mono = Mono
65 .defer(() -> chatHomeService.getChatRoomData(chatRoomId))
66 .log("testGetNonExistentChatroom")
68 .backoff(5, Duration.ofSeconds(1))
69 .filter(throwable -> throwable instanceof LoadInProgressException));
72 assertThat(mono).sendsError(e ->
74 assertThat(e).isInstanceOf(UnknownChatroomException.class);
75 UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e;
76 assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId);
80 static class TestConfiguration
83 ObjectMapper objectMapper()
85 ObjectMapper objectMapper = new ObjectMapper();
86 objectMapper.registerModule(new JavaTimeModule());
93 return Clock.systemDefaultZone();