Private
Public Access
1
0

Adds backend module, tests for /version and /conversations (not real tests yet)

This commit is contained in:
2023-08-06 12:27:58 -07:00
parent 16148949c8
commit 2e9b62b654
17 changed files with 218 additions and 4 deletions

View File

@@ -0,0 +1,24 @@
package net.buzzert.kordophone.backend
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("net.buzzert.kordophone.backend.test", appContext.packageName)
}
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

View File

@@ -0,0 +1,16 @@
package net.buzzert.kordophone.backend
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.net.URL
class APIClient(baseURL: URL) {
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build()
fun getClient(): Retrofit {
return retrofit
}
}

View File

@@ -0,0 +1,15 @@
package net.buzzert.kordophone.backend
import net.buzzert.kordophone.backend.model.Conversation
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
interface APIInterface {
@GET("/version")
suspend fun getVersion(): ResponseBody
@GET("/conversations")
suspend fun getConversations(): Response<List<Conversation>>
}

View File

@@ -0,0 +1,19 @@
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()!!
}
}

View File

@@ -0,0 +1,24 @@
package net.buzzert.kordophone.backend.model
import com.google.gson.annotations.SerializedName
import java.util.Date
data class Conversation(
@SerializedName("date")
val date: Date,
@SerializedName("participantDisplayNames")
val participants: List<String>?,
@SerializedName("displayName")
val displayName: String?,
@SerializedName("unreadCount")
val unreadCount: Int,
@SerializedName("lastMessagePreview")
val lastMessagePreview: String,
@SerializedName("guid")
val guid: String,
)

View File

@@ -0,0 +1,33 @@
package net.buzzert.kordophone.backend
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.Assert.*
import java.net.URL
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
val repository = ChatRepository(URL("http://localhost:5738"))
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
@Test
fun testGetVersion() = runBlocking {
val version = repository.getVersion()
println("Version: $version")
}
@Test
fun testFetchConversations() = runBlocking {
val conversations = repository.fetchConversations()
println("Conversations: $conversations")
}
}