Private
Public Access
1
0

Integrate ChatRepository and CachedChatDatabase

This commit is contained in:
2023-08-09 23:00:28 -07:00
parent 4fda6428ce
commit 32e68a80b2
4 changed files with 67 additions and 26 deletions

View File

@@ -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.Conversation as ModelConversation
import net.buzzert.kordophone.backend.model.Message as ModelMessage import net.buzzert.kordophone.backend.model.Message as ModelMessage
internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) { class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
companion object { companion object {
private val schema = setOf(Conversation::class, Message::class) 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() } return dbConversation.messages.map { it.toMessage() }
} }
fun close() {
realm.close()
}
private fun getConversationByGuid(guid: GUID): Conversation { private fun getConversationByGuid(guid: GUID): Conversation {
return realm.query(Conversation::class, "guid == '$guid'") return realm.query(Conversation::class, "guid == '$guid'")
.find() .find()

View File

@@ -3,6 +3,7 @@ package net.buzzert.kordophone.backend.server
import android.util.Log import android.util.Log
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.buzzert.kordophone.backend.db.CachedChatDatabase
import net.buzzert.kordophone.backend.events.MessageDeliveredEvent import net.buzzert.kordophone.backend.events.MessageDeliveredEvent
import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.GUID import net.buzzert.kordophone.backend.model.GUID
@@ -14,7 +15,10 @@ import java.util.concurrent.ArrayBlockingQueue
const val REPO_LOG: String = "ChatRepository" 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<MessageDeliveredEvent>() val messageDeliveredChannel = Channel<MessageDeliveredEvent>()
private data class OutgoingMessageInfo ( private data class OutgoingMessageInfo (
@@ -52,6 +56,10 @@ class ChatRepository(private val apiInterface: APIInterface) {
return guid return guid
} }
fun close() {
database.close()
}
// - private // - private
private fun outgoingMessageQueueMain() { private fun outgoingMessageQueueMain() {

View File

@@ -1,12 +1,14 @@
package net.buzzert.kordophone.backend package net.buzzert.kordophone.backend
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.buzzert.kordophone.backend.db.CachedChatDatabase
import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message import net.buzzert.kordophone.backend.model.Message
import net.buzzert.kordophone.backend.server.APIClient import net.buzzert.kordophone.backend.server.APIClient
import net.buzzert.kordophone.backend.server.APIInterface import net.buzzert.kordophone.backend.server.APIInterface
import net.buzzert.kordophone.backend.server.ChatRepository import net.buzzert.kordophone.backend.server.ChatRepository
import net.buzzert.kordophone.backend.server.MockServer import net.buzzert.kordophone.backend.server.MockServer
import org.junit.AfterClass
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
import java.net.URL import java.net.URL
@@ -14,62 +16,81 @@ import java.util.Date
import java.util.UUID import java.util.UUID
class BackendTests { class BackendTests {
private val mockServer = MockServer().also { private fun liveRepository(host: String): ChatRepository {
val conversation = Conversation( val client = APIClient(URL(host))
date = Date(), val apiInterface = client.getClient().create(APIInterface::class.java)
participants = listOf("james@magahern.com"), val database = CachedChatDatabase.testDatabase()
displayName = null, return ChatRepository(apiInterface, database)
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 val repository = ChatRepository(mockServer) private fun mockRepository(): Pair<ChatRepository, MockServer> {
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 { it.addConversation(conversation)
val client = APIClient(URL(host))
return ChatRepository(client.getClient().create(APIInterface::class.java)) 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 @Test
fun testGetVersion() = runBlocking { fun testGetVersion() = runBlocking {
val (repository, mockServer) = mockRepository()
val version = repository.getVersion() val version = repository.getVersion()
assertEquals(version, mockServer.version) assertEquals(version, mockServer.version)
repository.close()
} }
@Test @Test
fun testFetchConversations() = runBlocking { fun testFetchConversations() = runBlocking {
val (repository, mockServer) = mockRepository()
val conversations = repository.fetchConversations() val conversations = repository.fetchConversations()
assertEquals(conversations.count(), 1) assertEquals(conversations.count(), 1)
val conversation = conversations.first() val conversation = conversations.first()
assertEquals(conversation.participants, listOf("james@magahern.com")) assertEquals(conversation.participants, listOf("james@magahern.com"))
repository.close()
} }
@Test @Test
fun testFetchMessages() = runBlocking { fun testFetchMessages() = runBlocking {
val (repository, mockServer) = mockRepository()
val conversations = repository.fetchConversations() val conversations = repository.fetchConversations()
val messages = repository.fetchMessages(conversations.first()) val messages = repository.fetchMessages(conversations.first())
assertEquals(messages.count(), 1) assertEquals(messages.count(), 1)
val message = messages.first() val message = messages.first()
assertEquals(message.text, "Hey") assertEquals(message.text, "Hey")
repository.close()
} }
@Test @Test
fun testSendMessage() = runBlocking { fun testSendMessage() = runBlocking {
val (repository, mockServer) = mockRepository()
val conversation = repository.fetchConversations().first() val conversation = repository.fetchConversations().first()
val outgoingMessage = Message( val outgoingMessage = Message(
date = Date(), date = Date(),
@@ -83,5 +104,7 @@ class BackendTests {
val event = repository.messageDeliveredChannel.receive() val event = repository.messageDeliveredChannel.receive()
assertEquals(event.guid, guid) assertEquals(event.guid, guid)
assertEquals(event.message.text, outgoingMessage.text) assertEquals(event.message.text, outgoingMessage.text)
repository.close()
} }
} }

View File

@@ -3,6 +3,7 @@ package net.buzzert.kordophone.backend
import net.buzzert.kordophone.backend.db.CachedChatDatabase import net.buzzert.kordophone.backend.db.CachedChatDatabase
import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message import net.buzzert.kordophone.backend.model.Message
import org.junit.AfterClass
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
import java.util.Date import java.util.Date
@@ -28,6 +29,8 @@ class DatabaseTests {
val readConversation = readBackConversations[0] val readConversation = readBackConversations[0]
assertEquals(readConversation, conversation) assertEquals(readConversation, conversation)
db.close()
} }
@Test @Test
@@ -59,5 +62,8 @@ class DatabaseTests {
val readMessage = readMessages[0] val readMessage = readMessages[0]
assertEquals(readMessage, message) assertEquals(readMessage, message)
db.close()
} }
} }