From 32e68a80b2aab1f86abdb83f2e94f41ca3e34ae3 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Wed, 9 Aug 2023 23:00:28 -0700 Subject: [PATCH] Integrate ChatRepository and CachedChatDatabase --- .../backend/db/CachedChatDatabase.kt | 6 +- .../backend/server/ChatRepository.kt | 10 ++- .../kordophone/backend/BackendTests.kt | 71 ++++++++++++------- .../kordophone/backend/DatabaseTests.kt | 6 ++ 4 files changed, 67 insertions(+), 26 deletions(-) diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt index 513fe46..99bfd08 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt @@ -10,7 +10,7 @@ import net.buzzert.kordophone.backend.model.GUID import net.buzzert.kordophone.backend.model.Conversation as ModelConversation import net.buzzert.kordophone.backend.model.Message as ModelMessage -internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) { +class CachedChatDatabase (private val realmConfig: RealmConfiguration) { companion object { private val schema = setOf(Conversation::class, Message::class) @@ -60,6 +60,10 @@ internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) return dbConversation.messages.map { it.toMessage() } } + fun close() { + realm.close() + } + private fun getConversationByGuid(guid: GUID): Conversation { return realm.query(Conversation::class, "guid == '$guid'") .find() diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt index adefe66..84442a7 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt @@ -3,6 +3,7 @@ package net.buzzert.kordophone.backend.server import android.util.Log import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.runBlocking +import net.buzzert.kordophone.backend.db.CachedChatDatabase import net.buzzert.kordophone.backend.events.MessageDeliveredEvent import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.model.GUID @@ -14,7 +15,10 @@ import java.util.concurrent.ArrayBlockingQueue const val REPO_LOG: String = "ChatRepository" -class ChatRepository(private val apiInterface: APIInterface) { +class ChatRepository( + private val apiInterface: APIInterface, + private val database: CachedChatDatabase, +) { val messageDeliveredChannel = Channel() private data class OutgoingMessageInfo ( @@ -52,6 +56,10 @@ class ChatRepository(private val apiInterface: APIInterface) { return guid } + fun close() { + database.close() + } + // - private private fun outgoingMessageQueueMain() { diff --git a/backend/src/test/java/net/buzzert/kordophone/backend/BackendTests.kt b/backend/src/test/java/net/buzzert/kordophone/backend/BackendTests.kt index 1d77776..975730b 100644 --- a/backend/src/test/java/net/buzzert/kordophone/backend/BackendTests.kt +++ b/backend/src/test/java/net/buzzert/kordophone/backend/BackendTests.kt @@ -1,12 +1,14 @@ package net.buzzert.kordophone.backend import kotlinx.coroutines.runBlocking +import net.buzzert.kordophone.backend.db.CachedChatDatabase import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.model.Message import net.buzzert.kordophone.backend.server.APIClient import net.buzzert.kordophone.backend.server.APIInterface import net.buzzert.kordophone.backend.server.ChatRepository import net.buzzert.kordophone.backend.server.MockServer +import org.junit.AfterClass import org.junit.Assert.assertEquals import org.junit.Test import java.net.URL @@ -14,62 +16,81 @@ import java.util.Date import java.util.UUID class BackendTests { - private val mockServer = MockServer().also { - val conversation = Conversation( - date = Date(), - participants = listOf("james@magahern.com"), - displayName = null, - unreadCount = 0, - lastMessagePreview = "Hello", - guid = UUID.randomUUID().toString() - ) - - it.addConversation(conversation) - - val message = Message( - date = Date(), - text = "Hey", - guid = UUID.randomUUID().toString(), - sender = null, - ) - - it.addMessagesToConversation(conversation, listOf(message)) + private fun liveRepository(host: String): ChatRepository { + val client = APIClient(URL(host)) + val apiInterface = client.getClient().create(APIInterface::class.java) + val database = CachedChatDatabase.testDatabase() + return ChatRepository(apiInterface, database) } - private val repository = ChatRepository(mockServer) + private fun mockRepository(): Pair { + val mockServer = MockServer().also { + val conversation = Conversation( + date = Date(), + participants = listOf("james@magahern.com"), + displayName = null, + unreadCount = 0, + lastMessagePreview = "Hello", + guid = UUID.randomUUID().toString() + ) - private fun getLiveRepo(host: String): ChatRepository { - val client = APIClient(URL(host)) - return ChatRepository(client.getClient().create(APIInterface::class.java)) + it.addConversation(conversation) + + val message = Message( + date = Date(), + text = "Hey", + guid = UUID.randomUUID().toString(), + sender = null, + ) + + it.addMessagesToConversation(conversation, listOf(message)) + } + + val database = CachedChatDatabase.testDatabase() + val repository = ChatRepository(mockServer, database) + return Pair(repository, mockServer) } @Test fun testGetVersion() = runBlocking { + val (repository, mockServer) = mockRepository() val version = repository.getVersion() assertEquals(version, mockServer.version) + + repository.close() } @Test fun testFetchConversations() = runBlocking { + val (repository, mockServer) = mockRepository() + val conversations = repository.fetchConversations() assertEquals(conversations.count(), 1) val conversation = conversations.first() assertEquals(conversation.participants, listOf("james@magahern.com")) + + repository.close() } @Test fun testFetchMessages() = runBlocking { + val (repository, mockServer) = mockRepository() + val conversations = repository.fetchConversations() val messages = repository.fetchMessages(conversations.first()) assertEquals(messages.count(), 1) val message = messages.first() assertEquals(message.text, "Hey") + + repository.close() } @Test fun testSendMessage() = runBlocking { + val (repository, mockServer) = mockRepository() + val conversation = repository.fetchConversations().first() val outgoingMessage = Message( date = Date(), @@ -83,5 +104,7 @@ class BackendTests { val event = repository.messageDeliveredChannel.receive() assertEquals(event.guid, guid) assertEquals(event.message.text, outgoingMessage.text) + + repository.close() } } \ No newline at end of file diff --git a/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt b/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt index a08746d..4b85226 100644 --- a/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt +++ b/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt @@ -3,6 +3,7 @@ package net.buzzert.kordophone.backend import net.buzzert.kordophone.backend.db.CachedChatDatabase import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.model.Message +import org.junit.AfterClass import org.junit.Assert.assertEquals import org.junit.Test import java.util.Date @@ -28,6 +29,8 @@ class DatabaseTests { val readConversation = readBackConversations[0] assertEquals(readConversation, conversation) + + db.close() } @Test @@ -59,5 +62,8 @@ class DatabaseTests { val readMessage = readMessages[0] assertEquals(readMessage, message) + + + db.close() } } \ No newline at end of file