package de.juplo.kafka.chat.backend.domain;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
-import java.util.*;
+import java.util.UUID;
-@Slf4j
-@RequiredArgsConstructor
-public class ChatHome
+public interface ChatHome
{
- private final ChatHomeService service;
- private final int shard;
+ Mono<ChatRoom> putChatRoom(ChatRoom chatRoom);
- public Mono<ChatRoom> putChatRoom(ChatRoom chatRoom)
- {
- return service.putChatRoom(chatRoom);
- }
+ Mono<ChatRoom> getChatRoom(UUID id);
- public Mono<ChatRoom> getChatRoom(UUID id)
- {
- return service
- .getChatRoom(shard, id)
- .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
- }
-
- public Flux<ChatRoom> getChatRooms()
- {
- return service.getChatRooms(shard);
- }
+ Flux<ChatRoom> getChatRooms();
}
--- /dev/null
+package de.juplo.kafka.chat.backend.domain;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.util.*;
+
+
+@Slf4j
+@RequiredArgsConstructor
+public class SimpleChatHome implements ChatHome
+{
+ private final ChatHomeService service;
+ private final int shard;
+
+ @Override
+ public Mono<ChatRoom> putChatRoom(ChatRoom chatRoom)
+ {
+ return service.putChatRoom(chatRoom);
+ }
+
+ @Override
+ public Mono<ChatRoom> getChatRoom(UUID id)
+ {
+ return service
+ .getChatRoom(shard, id)
+ .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
+ }
+
+ @Override
+ public Flux<ChatRoom> getChatRooms()
+ {
+ return service.getChatRooms(shard);
+ }
+}
import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy;
import de.juplo.kafka.chat.backend.api.ShardingStrategy;
import de.juplo.kafka.chat.backend.domain.ChatHome;
+import de.juplo.kafka.chat.backend.domain.SimpleChatHome;
import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
InMemoryChatHomeService chatHomeService,
StorageStrategy storageStrategy)
{
- ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
+ SimpleChatHome[] chatHomes = new SimpleChatHome[properties.getInmemory().getNumShards()];
storageStrategy
.read()
.subscribe(chatRoom ->
{
int shard = chatRoom.getShard();
if (chatHomes[shard] == null)
- chatHomes[shard] = new ChatHome(chatHomeService, shard);
+ chatHomes[shard] = new SimpleChatHome(chatHomeService, shard);
});
return chatHomes;
}
ChatBackendProperties properties,
InMemoryChatHomeService service)
{
- ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
+ SimpleChatHome[] chatHomes = new SimpleChatHome[properties.getInmemory().getNumShards()];
Arrays
.stream(properties.getInmemory().getOwnedShards())
- .forEach(i -> chatHomes[i] = new ChatHome(service, i));
+ .forEach(i -> chatHomes[i] = new SimpleChatHome(service, i));
return chatHomes;
}
}
+++ /dev/null
-package de.juplo.kafka.chat.backend.domain;
-
-import org.junit.jupiter.api.DisplayName;
-import org.junit.jupiter.api.Test;
-import reactor.core.publisher.Mono;
-
-import java.time.Clock;
-import java.util.UUID;
-
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static pl.rzrz.assertj.reactor.Assertions.assertThat;
-
-
-public class ChatHomeTest
-{
- @Test
- @DisplayName("Assert chatroom is delivered, if it exists")
- void testGetExistingChatroom()
- {
- // Given
- ChatHomeService chatHomeService = mock(ChatHomeService.class);
- ChatRoom chatRoom = new ChatRoom(
- UUID.randomUUID(),
- "Foo",
- 0,
- Clock.systemDefaultZone(),
- mock(ChatRoomService.class),
- 8);
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom));
- ChatHome chatHome = new ChatHome(chatHomeService, 0);
-
- // When
- Mono<ChatRoom> mono = chatHome.getChatRoom(chatRoom.getId());
-
- // Then
- assertThat(mono).emitsExactly(chatRoom);
- }
-
- @Test
- @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
- void testGetNonExistentChatroom()
- {
- // Given
- ChatHomeService chatHomeService = mock(ChatHomeService.class);
- when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
- ChatHome chatHome = new ChatHome(chatHomeService, 0);
-
- // When
- Mono<ChatRoom> mono = chatHome.getChatRoom(UUID.randomUUID());
-
- // Then
- assertThat(mono).sendsError();
- }
-}
--- /dev/null
+package de.juplo.kafka.chat.backend.domain;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Mono;
+
+import java.time.Clock;
+import java.util.UUID;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static pl.rzrz.assertj.reactor.Assertions.assertThat;
+
+
+public class SimpleChatHomeTest
+{
+ @Test
+ @DisplayName("Assert chatroom is delivered, if it exists")
+ void testGetExistingChatroom()
+ {
+ // Given
+ ChatHomeService chatHomeService = mock(ChatHomeService.class);
+ ChatRoom chatRoom = new ChatRoom(
+ UUID.randomUUID(),
+ "Foo",
+ 0,
+ Clock.systemDefaultZone(),
+ mock(ChatRoomService.class),
+ 8);
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.just(chatRoom));
+ SimpleChatHome chatHome = new SimpleChatHome(chatHomeService, 0);
+
+ // When
+ Mono<ChatRoom> mono = chatHome.getChatRoom(chatRoom.getId());
+
+ // Then
+ assertThat(mono).emitsExactly(chatRoom);
+ }
+
+ @Test
+ @DisplayName("Assert UnknownChatroomException is thrown, if chatroom does not exist")
+ void testGetNonExistentChatroom()
+ {
+ // Given
+ ChatHomeService chatHomeService = mock(ChatHomeService.class);
+ when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty());
+ SimpleChatHome chatHome = new SimpleChatHome(chatHomeService, 0);
+
+ // When
+ Mono<ChatRoom> mono = chatHome.getChatRoom(UUID.randomUUID());
+
+ // Then
+ assertThat(mono).sendsError();
+ }
+}
protected void start()
{
- chathome = new ChatHome(getChatHomeServiceSupplier().get(), 0);
+ chathome = new SimpleChatHome(getChatHomeServiceSupplier().get(), 0);
chatRoomFactory = getChatRoomFactory();
}