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.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()
|
||||||
|
|||||||
@@ -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() {
|
||||||
|
|||||||
@@ -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,7 +16,15 @@ 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 client = APIClient(URL(host))
|
||||||
|
val apiInterface = client.getClient().create(APIInterface::class.java)
|
||||||
|
val database = CachedChatDatabase.testDatabase()
|
||||||
|
return ChatRepository(apiInterface, database)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mockRepository(): Pair<ChatRepository, MockServer> {
|
||||||
|
val mockServer = MockServer().also {
|
||||||
val conversation = Conversation(
|
val conversation = Conversation(
|
||||||
date = Date(),
|
date = Date(),
|
||||||
participants = listOf("james@magahern.com"),
|
participants = listOf("james@magahern.com"),
|
||||||
@@ -36,40 +46,51 @@ class BackendTests {
|
|||||||
it.addMessagesToConversation(conversation, listOf(message))
|
it.addMessagesToConversation(conversation, listOf(message))
|
||||||
}
|
}
|
||||||
|
|
||||||
private val repository = ChatRepository(mockServer)
|
val database = CachedChatDatabase.testDatabase()
|
||||||
|
val repository = ChatRepository(mockServer, database)
|
||||||
private fun getLiveRepo(host: String): ChatRepository {
|
return Pair(repository, mockServer)
|
||||||
val client = APIClient(URL(host))
|
|
||||||
return ChatRepository(client.getClient().create(APIInterface::class.java))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user