Integrate ChatRepository and CachedChatDatabase
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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<MessageDeliveredEvent>()
|
||||
|
||||
private data class OutgoingMessageInfo (
|
||||
@@ -52,6 +56,10 @@ class ChatRepository(private val apiInterface: APIInterface) {
|
||||
return guid
|
||||
}
|
||||
|
||||
fun close() {
|
||||
database.close()
|
||||
}
|
||||
|
||||
// - private
|
||||
|
||||
private fun outgoingMessageQueueMain() {
|
||||
|
||||
@@ -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<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 {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user