diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
index ed76bea..44ca2d9 100644
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -5,6 +5,10 @@
+
+
+
+
diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/ChatRepository.kt b/backend/src/main/java/net/buzzert/kordophone/backend/ChatRepository.kt
deleted file mode 100644
index 545e770..0000000
--- a/backend/src/main/java/net/buzzert/kordophone/backend/ChatRepository.kt
+++ /dev/null
@@ -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 {
- return apiInterface.getConversations().body()!!
- }
-}
\ No newline at end of file
diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/model/Message.kt b/backend/src/main/java/net/buzzert/kordophone/backend/model/Message.kt
new file mode 100644
index 0000000..3aff5a4
--- /dev/null
+++ b/backend/src/main/java/net/buzzert/kordophone/backend/model/Message.kt
@@ -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,
+) {}
\ No newline at end of file
diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/APIClient.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/APIClient.kt
similarity index 88%
rename from backend/src/main/java/net/buzzert/kordophone/backend/APIClient.kt
rename to backend/src/main/java/net/buzzert/kordophone/backend/server/APIClient.kt
index 06785bf..f9e325d 100644
--- a/backend/src/main/java/net/buzzert/kordophone/backend/APIClient.kt
+++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/APIClient.kt
@@ -1,4 +1,4 @@
-package net.buzzert.kordophone.backend
+package net.buzzert.kordophone.backend.server
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/APIInterface.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/APIInterface.kt
similarity index 58%
rename from backend/src/main/java/net/buzzert/kordophone/backend/APIInterface.kt
rename to backend/src/main/java/net/buzzert/kordophone/backend/server/APIInterface.kt
index e5a63b2..d17a5ff 100644
--- a/backend/src/main/java/net/buzzert/kordophone/backend/APIInterface.kt
+++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/APIInterface.kt
@@ -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.Message
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
+import retrofit2.http.Query
interface APIInterface {
@GET("/version")
@@ -12,4 +14,7 @@ interface APIInterface {
@GET("/conversations")
suspend fun getConversations(): Response>
+
+ @GET("/messages")
+ suspend fun getMessages(@Query("guid") conversationGUID: String): Response>
}
\ No newline at end of file
diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt
new file mode 100644
index 0000000..19593d6
--- /dev/null
+++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt
@@ -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 {
+ return apiInterface.getConversations().body()!!
+ }
+
+ suspend fun fetchMessages(conversation: Conversation): List {
+ return apiInterface.getMessages(conversation.guid).body()!!
+ }
+}
\ No newline at end of file
diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/server/MockServer.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/MockServer.kt
new file mode 100644
index 0000000..76c8603
--- /dev/null
+++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/MockServer.kt
@@ -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 = mutableListOf()
+ val messages: MutableMap> = mutableMapOf()
+
+ override suspend fun getVersion(): ResponseBody {
+ return ResponseBody.create(MediaType.get("text/plain"), version)
+ }
+
+ override suspend fun getConversations(): Response> {
+ return Response.success(conversations)
+ }
+
+ override suspend fun getMessages(conversationGUID: String): Response> {
+ return Response.success(messages[conversationGUID])
+ }
+
+ fun addConversation(conversation: Conversation) {
+ conversations.add(conversation)
+ }
+
+ fun addMessagesToConversation(conversation: Conversation, messages: List) {
+ val guid = conversation.guid
+ this.messages[guid]?.addAll(messages) ?: run {
+ this.messages[guid] = messages.toMutableList()
+ }
+ }
+}
\ No newline at end of file
diff --git a/backend/src/test/java/net/buzzert/kordophone/backend/ExampleUnitTest.kt b/backend/src/test/java/net/buzzert/kordophone/backend/ExampleUnitTest.kt
index 48bbb85..ff20f21 100644
--- a/backend/src/test/java/net/buzzert/kordophone/backend/ExampleUnitTest.kt
+++ b/backend/src/test/java/net/buzzert/kordophone/backend/ExampleUnitTest.kt
@@ -1,10 +1,18 @@
package net.buzzert.kordophone.backend
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.Assert.*
import java.net.URL
+import java.util.Date
+import java.util.UUID
/**
* 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).
*/
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
- fun addition_isCorrect() {
- assertEquals(4, 2 + 2)
+ 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 getLiveRepo(host: String): ChatRepository {
+ val client = APIClient(URL(host))
+ return ChatRepository(client.getClient().create(APIInterface::class.java))
}
@Test
fun testGetVersion() = runBlocking {
val version = repository.getVersion()
- println("Version: $version")
+ assertEquals(version, mockServer.version)
}
@Test
fun testFetchConversations() = runBlocking {
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")
+ }
+
}
\ No newline at end of file