Private
Public Access
1
0

BackendTest: add message/convo generation features

This commit is contained in:
2023-08-09 23:46:33 -07:00
parent 32e68a80b2
commit f43c348f92
4 changed files with 117 additions and 112 deletions

View File

@@ -1,51 +0,0 @@
package net.buzzert.kordophone.backend.server
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message
import okhttp3.MediaType
import okhttp3.ResponseBody
import retrofit2.Response
import java.util.Date
import java.util.UUID
class MockServer: APIInterface {
val version = "Kordophone-2.0"
val conversations: MutableList<Conversation> = mutableListOf()
val messages: MutableMap<String, MutableList<Message>> = mutableMapOf()
override suspend fun getVersion(): ResponseBody {
return ResponseBody.create(MediaType.get("text/plain"), version)
}
override suspend fun getConversations(): Response<List<Conversation>> {
return Response.success(conversations)
}
override suspend fun getMessages(conversationGUID: String): Response<List<Message>> {
return Response.success(messages[conversationGUID])
}
override suspend fun sendMessage(request: SendMessageRequest): Response<Void> {
messages[request.conversationGUID]!!.add(
Message(
text = request.body,
date = Date(),
guid = UUID.randomUUID().toString(),
sender = null, // me
)
)
return Response.success(null)
}
fun addConversation(conversation: Conversation) {
conversations.add(conversation)
}
fun addMessagesToConversation(conversation: Conversation, messages: List<Message>) {
val guid = conversation.guid
this.messages[guid]?.addAll(messages) ?: run {
this.messages[guid] = messages.toMutableList()
}
}
}

View File

@@ -7,8 +7,6 @@ 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
@@ -24,28 +22,7 @@ class BackendTests {
}
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()
)
it.addConversation(conversation)
val message = Message(
date = Date(),
text = "Hey",
guid = UUID.randomUUID().toString(),
sender = null,
)
it.addMessagesToConversation(conversation, listOf(message))
}
val mockServer = MockServer()
val database = CachedChatDatabase.testDatabase()
val repository = ChatRepository(mockServer, database)
return Pair(repository, mockServer)
@@ -64,11 +41,14 @@ class BackendTests {
fun testFetchConversations() = runBlocking {
val (repository, mockServer) = mockRepository()
// Add conversation to mock server
val inConversation = mockServer.addTestConversations(1).first()
val conversations = repository.fetchConversations()
assertEquals(conversations.count(), 1)
val conversation = conversations.first()
assertEquals(conversation.participants, listOf("james@magahern.com"))
val outConversation = conversations.first()
assertEquals(inConversation, outConversation)
repository.close()
}
@@ -77,12 +57,16 @@ class BackendTests {
fun testFetchMessages() = runBlocking {
val (repository, mockServer) = mockRepository()
// Add conversation & message to mock server
val inConversation = mockServer.addTestConversations(1).first()
val inMessage = mockServer.addTestMessages(1, inConversation).first()
val conversations = repository.fetchConversations()
val messages = repository.fetchMessages(conversations.first())
assertEquals(messages.count(), 1)
val message = messages.first()
assertEquals(message.text, "Hey")
val outMessage = messages.first()
assertEquals(outMessage, inMessage)
repository.close()
}
@@ -91,13 +75,8 @@ class BackendTests {
fun testSendMessage() = runBlocking {
val (repository, mockServer) = mockRepository()
val conversation = repository.fetchConversations().first()
val outgoingMessage = Message(
date = Date(),
text = "Hello there!",
guid = UUID.randomUUID().toString(),
sender = null,
)
val conversation = mockServer.addTestConversations(1).first()
val outgoingMessage = MockServer.generateMessage()
val guid = repository.enqueueOutgoingMessage(outgoingMessage, conversation)

View File

@@ -13,15 +13,7 @@ class DatabaseTests {
fun testConversationRetrieval() {
val db = CachedChatDatabase.testDatabase()
val conversation = Conversation(
date = Date(),
participants = listOf("james@magahern.com"),
displayName = "Test",
unreadCount = 1,
lastMessagePreview = "Hello!",
guid = "1234",
)
val conversation = MockServer.generateConversation()
db.writeConversations(listOf(conversation))
val readBackConversations = db.fetchConversations()
@@ -37,24 +29,10 @@ class DatabaseTests {
fun testMessageRetrieval() {
val db = CachedChatDatabase.testDatabase()
val conversation = Conversation(
date = Date(),
participants = listOf("james@magahern.com"),
displayName = "Test",
unreadCount = 1,
lastMessagePreview = "Hello!",
guid = "1234",
)
val conversation = MockServer.generateConversation()
db.writeConversations(listOf(conversation))
val message = Message(
text = "Hello!",
guid = "4321",
date = Date(),
sender = "james@magahern.com"
)
val message = MockServer.generateMessage()
db.writeMessages(listOf(message), conversation)
val readMessages = db.fetchMessages(conversation)
@@ -63,7 +41,6 @@ class DatabaseTests {
val readMessage = readMessages[0]
assertEquals(readMessage, message)
db.close()
}
}

View File

@@ -0,0 +1,100 @@
package net.buzzert.kordophone.backend
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message
import net.buzzert.kordophone.backend.server.APIInterface
import net.buzzert.kordophone.backend.server.SendMessageRequest
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.ResponseBody
import retrofit2.Response
import java.util.Date
import java.util.UUID
class MockServer: APIInterface {
val version = "Kordophone-2.0"
val conversations: MutableList<Conversation> = mutableListOf()
val messages: MutableMap<String, MutableList<Message>> = mutableMapOf()
companion object {
fun generateMessage(): Message {
return Message(
date = Date(),
text = "This is a test!",
guid = UUID.randomUUID().toString(),
sender = null,
)
}
fun generateConversation(): Conversation {
return Conversation(
date = Date(),
participants = listOf("james@magahern.com"),
displayName = null,
unreadCount = 0,
lastMessagePreview = "This is a test!",
guid = UUID.randomUUID().toString()
)
}
}
override suspend fun getVersion(): ResponseBody {
return ResponseBody.create("text/plain".toMediaType(), version)
}
override suspend fun getConversations(): Response<List<Conversation>> {
return Response.success(conversations)
}
override suspend fun getMessages(conversationGUID: String): Response<List<Message>> {
return Response.success(messages[conversationGUID])
}
override suspend fun sendMessage(request: SendMessageRequest): Response<Void> {
val message = Message(
text = request.body,
date = Date(),
guid = UUID.randomUUID().toString(),
sender = null, // me
)
messages[request.conversationGUID]?.add(message) ?: run {
messages[request.conversationGUID] = mutableListOf(message)
}
return Response.success(null)
}
fun addConversation(conversation: Conversation) {
conversations.add(conversation)
}
fun addMessagesToConversation(conversation: Conversation, messages: List<Message>) {
val guid = conversation.guid
this.messages[guid]?.addAll(messages) ?: run {
this.messages[guid] = messages.toMutableList()
}
}
fun addTestConversations(count: Int): List<Conversation> {
val testConversations = ArrayList<Conversation>()
for (i in 0..<count) {
val conversation = MockServer.generateConversation()
testConversations.add(conversation)
addConversation(conversation)
}
return testConversations
}
fun addTestMessages(count: Int, conversation: Conversation): List<Message> {
val testMessages = ArrayList<Message>()
for (i in 0..<count) {
val message = MockServer.generateMessage()
testMessages.add(message)
}
addMessagesToConversation(conversation, testMessages)
return testMessages
}
}