Implements /message fetching
This commit is contained in:
4
.idea/inspectionProfiles/Project_Default.xml
generated
4
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -5,6 +5,10 @@
|
|||||||
<option name="composableFile" value="true" />
|
<option name="composableFile" value="true" />
|
||||||
<option name="previewFile" value="true" />
|
<option name="previewFile" value="true" />
|
||||||
</inspection_tool>
|
</inspection_tool>
|
||||||
|
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
|
||||||
|
<option name="composableFile" value="true" />
|
||||||
|
<option name="previewFile" value="true" />
|
||||||
|
</inspection_tool>
|
||||||
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
<option name="composableFile" value="true" />
|
<option name="composableFile" value="true" />
|
||||||
<option name="previewFile" value="true" />
|
<option name="previewFile" value="true" />
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package net.buzzert.kordophone.backend
|
|
||||||
|
|
||||||
import kotlinx.coroutines.coroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import net.buzzert.kordophone.backend.model.Conversation
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
class ChatRepository(baseURL: URL) {
|
|
||||||
private val client: APIClient = APIClient(baseURL)
|
|
||||||
private val apiInterface: APIInterface = client.getClient().create(APIInterface::class.java)
|
|
||||||
|
|
||||||
suspend fun getVersion(): String {
|
|
||||||
return apiInterface.getVersion().string()
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun fetchConversations(): List<Conversation> {
|
|
||||||
return apiInterface.getConversations().body()!!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package net.buzzert.kordophone.backend.model
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
|
data class Message(
|
||||||
|
@SerializedName("text")
|
||||||
|
val text: String,
|
||||||
|
|
||||||
|
@SerializedName("guid")
|
||||||
|
val guid: String,
|
||||||
|
|
||||||
|
@SerializedName("sender")
|
||||||
|
val sender: String?, // optional: nil means "from me"
|
||||||
|
|
||||||
|
@SerializedName("date")
|
||||||
|
val date: Date,
|
||||||
|
) {}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package net.buzzert.kordophone.backend
|
package net.buzzert.kordophone.backend.server
|
||||||
|
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package net.buzzert.kordophone.backend
|
package net.buzzert.kordophone.backend.server
|
||||||
|
|
||||||
import net.buzzert.kordophone.backend.model.Conversation
|
import net.buzzert.kordophone.backend.model.Conversation
|
||||||
|
import net.buzzert.kordophone.backend.model.Message
|
||||||
import okhttp3.ResponseBody
|
import okhttp3.ResponseBody
|
||||||
import retrofit2.Call
|
import retrofit2.Call
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.Query
|
||||||
|
|
||||||
interface APIInterface {
|
interface APIInterface {
|
||||||
@GET("/version")
|
@GET("/version")
|
||||||
@@ -12,4 +14,7 @@ interface APIInterface {
|
|||||||
|
|
||||||
@GET("/conversations")
|
@GET("/conversations")
|
||||||
suspend fun getConversations(): Response<List<Conversation>>
|
suspend fun getConversations(): Response<List<Conversation>>
|
||||||
|
|
||||||
|
@GET("/messages")
|
||||||
|
suspend fun getMessages(@Query("guid") conversationGUID: String): Response<List<Message>>
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package net.buzzert.kordophone.backend.server
|
||||||
|
|
||||||
|
import net.buzzert.kordophone.backend.model.Conversation
|
||||||
|
import net.buzzert.kordophone.backend.model.Message
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
class ChatRepository(apiInterface: APIInterface) {
|
||||||
|
// private val client: APIClient = APIClient(baseURL)
|
||||||
|
// private val apiInterface: APIInterface = client.getClient().create(APIInterface::class.java)
|
||||||
|
|
||||||
|
private val apiInterface: APIInterface = apiInterface
|
||||||
|
|
||||||
|
suspend fun getVersion(): String {
|
||||||
|
return apiInterface.getVersion().string()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun fetchConversations(): List<Conversation> {
|
||||||
|
return apiInterface.getConversations().body()!!
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun fetchMessages(conversation: Conversation): List<Message> {
|
||||||
|
return apiInterface.getMessages(conversation.guid).body()!!
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,18 @@
|
|||||||
package net.buzzert.kordophone.backend
|
package net.buzzert.kordophone.backend
|
||||||
|
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
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.Test
|
import org.junit.Test
|
||||||
|
|
||||||
import org.junit.Assert.*
|
import org.junit.Assert.*
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example local unit test, which will execute on the development machine (host).
|
* Example local unit test, which will execute on the development machine (host).
|
||||||
@@ -12,22 +20,58 @@ import java.net.URL
|
|||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
*/
|
*/
|
||||||
class ExampleUnitTest {
|
class ExampleUnitTest {
|
||||||
val repository = ChatRepository(URL("http://localhost:5738"))
|
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()
|
||||||
|
)
|
||||||
|
|
||||||
@Test
|
it.addConversation(conversation)
|
||||||
fun addition_isCorrect() {
|
|
||||||
assertEquals(4, 2 + 2)
|
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 getLiveRepo(host: String): ChatRepository {
|
||||||
|
val client = APIClient(URL(host))
|
||||||
|
return ChatRepository(client.getClient().create(APIInterface::class.java))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testGetVersion() = runBlocking {
|
fun testGetVersion() = runBlocking {
|
||||||
val version = repository.getVersion()
|
val version = repository.getVersion()
|
||||||
println("Version: $version")
|
assertEquals(version, mockServer.version)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFetchConversations() = runBlocking {
|
fun testFetchConversations() = runBlocking {
|
||||||
val conversations = repository.fetchConversations()
|
val conversations = repository.fetchConversations()
|
||||||
println("Conversations: $conversations")
|
assertEquals(conversations.count(), 1)
|
||||||
|
|
||||||
|
val conversation = conversations.first()
|
||||||
|
assertEquals(conversation.participants, listOf("james@magahern.com"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFetchMessages() = runBlocking {
|
||||||
|
val conversations = repository.fetchConversations()
|
||||||
|
val messages = repository.fetchMessages(conversations.first())
|
||||||
|
assertEquals(messages.count(), 1)
|
||||||
|
|
||||||
|
val message = messages.first()
|
||||||
|
assertEquals(message.text, "Hey")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user