Adds backend module, tests for /version and /conversations (not real tests yet)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
4
backend/src/main/AndroidManifest.xml
Normal file
4
backend/src/main/AndroidManifest.xml
Normal 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>
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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>>
|
||||
}
|
||||
@@ -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()!!
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
)
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user