Adds backend module, tests for /version and /conversations (not real tests yet)
This commit is contained in:
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
52
backend/build.gradle
Normal file
52
backend/build.gradle
Normal file
@@ -0,0 +1,52 @@
|
||||
plugins {
|
||||
id 'com.android.library'
|
||||
id 'org.jetbrains.kotlin.android'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'net.buzzert.kordophone.backend'
|
||||
compileSdk 33
|
||||
|
||||
defaultConfig {
|
||||
minSdk 31
|
||||
targetSdk 33
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.8.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.4.1'
|
||||
implementation 'com.google.android.material:material:1.5.0'
|
||||
implementation 'androidx.core:core-ktx:+'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
|
||||
// Third-party
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
|
||||
implementation 'com.google.code.gson:gson:2.9.0'
|
||||
|
||||
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core
|
||||
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.7.3', ext: 'pom'
|
||||
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
|
||||
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.1'
|
||||
}
|
||||
0
backend/consumer-rules.pro
Normal file
0
backend/consumer-rules.pro
Normal file
21
backend/proguard-rules.pro
vendored
Normal file
21
backend/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -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