From 4d995889f5f2214dc009c87d81bd3ce8a8ea2b84 Mon Sep 17 00:00:00 2001 From: geisterfurz007 Date: Sun, 22 Oct 2023 18:30:16 +0200 Subject: [PATCH] Complete kotlin refactor --- src/SDK/Language/Android.php | 2 +- src/SDK/Language/Kotlin.php | 39 ++++++++++++++++++- templates/android/library/build.gradle.twig | 2 +- templates/kotlin/build.gradle.twig | 2 + .../main/kotlin/io/appwrite/Client.kt.twig | 2 +- .../appwrite/services/ServiceTemplate.kt.twig | 3 -- .../io/appwrite/services/ServiceTest.kt.twig | 23 +++++++---- 7 files changed, 58 insertions(+), 15 deletions(-) rename templates/{android/library/src/test/java => kotlin/src/test/kotlin}/io/appwrite/services/ServiceTest.kt.twig (87%) diff --git a/src/SDK/Language/Android.php b/src/SDK/Language/Android.php index 966d1cc089..345885c706 100644 --- a/src/SDK/Language/Android.php +++ b/src/SDK/Language/Android.php @@ -253,7 +253,7 @@ public function getFiles(): array [ 'scope' => 'service', 'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/services/{{ service.name | caseUcfirst }}ServiceTest.kt', - 'template' => '/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig', ], [ 'scope' => 'default', diff --git a/src/SDK/Language/Kotlin.php b/src/SDK/Language/Kotlin.php index a7a4c5aeaa..d30ddfff6e 100644 --- a/src/SDK/Language/Kotlin.php +++ b/src/SDK/Language/Kotlin.php @@ -353,25 +353,50 @@ public function getFiles(): array 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Permission.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/Permission.kt.twig', ], + [ + 'scope' => 'default', + 'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/PermissionTest.kt', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/PermissionTest.kt.twig', + ], [ 'scope' => 'default', 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Role.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/Role.kt.twig', ], + [ + 'scope' => 'default', + 'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/RoleTest.kt', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/RoleTest.kt.twig', + ], [ 'scope' => 'default', 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/ID.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/ID.kt.twig', ], + [ + 'scope' => 'default', + 'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/IDTest.kt', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/IDTest.kt.twig', + ], [ 'scope' => 'default', 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Query.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/Query.kt.twig', ], + [ + 'scope' => 'default', + 'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/QueryTest.kt', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/QueryTest.kt.twig', + ], + [ + 'scope' => 'default', + 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Response.kt', + 'template' => '/kotlin/src/main/kotlin/io/appwrite/Response.kt.twig', + ], [ 'scope' => 'default', 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/coroutines/Callback.kt', - 'template' => '/android/library/src/main/java/io/appwrite/coroutines/Callback.kt.twig', + 'template' => '/kotlin/src/main/kotlin/io/appwrite/coroutines/Callback.kt.twig', ], [ 'scope' => 'default', @@ -404,6 +429,11 @@ public function getFiles(): array 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig', ], + [ + 'scope' => 'service', + 'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}ServiceTest.kt', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig', + ], [ 'scope' => 'default', 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/InputFile.kt', @@ -419,6 +449,11 @@ public function getFiles(): array 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Model.kt.twig', ], + [ + 'scope' => 'definition', + 'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}Test.kt', + 'template' => '/kotlin/src/test/kotlin/io/appwrite/models/ModelTest.kt.twig', + ], ]; } @@ -443,7 +478,7 @@ public function getFilters(): array protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'T'): string { if ($method['type'] === 'webAuth') { - return 'Bool'; + return 'Boolean'; } if ($method['type'] === 'location') { return 'ByteArray'; diff --git a/templates/android/library/build.gradle.twig b/templates/android/library/build.gradle.twig index 4a1922f0bf..4775630e93 100644 --- a/templates/android/library/build.gradle.twig +++ b/templates/android/library/build.gradle.twig @@ -79,7 +79,7 @@ dependencies { testImplementation "androidx.test.ext:junit-ktx:1.1.5" testImplementation "androidx.test:core-ktx:1.5.0" testImplementation "org.robolectric:robolectric:4.10.3" - testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3") + testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3" androidTestImplementation 'androidx.test:runner:1.5.2' } diff --git a/templates/kotlin/build.gradle.twig b/templates/kotlin/build.gradle.twig index 9ae0c32e89..8803473cb3 100644 --- a/templates/kotlin/build.gradle.twig +++ b/templates/kotlin/build.gradle.twig @@ -36,7 +36,9 @@ dependencies { implementation("com.squareup.okhttp3:logging-interceptor") implementation("com.google.code.gson:gson:2.9.0") + testImplementation 'io.mockk:mockk:1.13.7' testImplementation 'org.jetbrains.kotlin:kotlin-test-junit' + testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3" } test { diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index 605bd0d05f..4dbc61337a 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -2,6 +2,7 @@ package {{ sdk.namespace | caseDot }} import com.google.gson.GsonBuilder import com.google.gson.reflect.TypeToken +import {{ sdk.namespace | caseDot }}.Response import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception import {{ sdk.namespace | caseDot }}.extensions.fromJson import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter @@ -109,7 +110,6 @@ class Client @JvmOverloads constructor( val builder = OkHttpClient() .newBuilder() - .cookieJar(JavaNetCookieJar(cookieJar)) if (!selfSigned) { http = builder.build() diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig index 5fb0f80692..ddf44fe5ce 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig @@ -36,9 +36,6 @@ class {{ service.name | caseUcfirst }} : Service { {%~ endif %} @Throws({{ spec.title | caseUcfirst }}Exception::class) suspend fun {% if method.responseModel | hasGenericType(spec) %}{{ '' | raw }} {% endif %}{{ method.name | caseCamel }}( - {%~ if method.type == "webAuth" %} - activity: ComponentActivity, - {%~ endif %} {%~ for parameter in method.parameters.all %} {{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %}, {%~ endfor %} diff --git a/templates/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig b/templates/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig similarity index 87% rename from templates/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig rename to templates/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig index 9b4851688e..84ebd536eb 100644 --- a/templates/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig +++ b/templates/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig @@ -1,11 +1,7 @@ package {{ sdk.namespace | caseDot }}.services -import androidx.activity.ComponentActivity -import androidx.test.core.app.ApplicationProvider -import androidx.test.ext.junit.runners.AndroidJUnit4 import {{ sdk.namespace | caseDot }}.Client import {{ sdk.namespace | caseDot }}.Response -import {{ sdk.namespace | caseDot }}.WebAuthComponent import {{ sdk.namespace | caseDot }}.models.InputFile import io.mockk.coEvery import io.mockk.mockkObject @@ -13,17 +9,24 @@ import io.mockk.spyk import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Test -import org.junit.runner.RunWith import kotlin.test.assertIs +{% if language.name == 'Android' %} +import androidx.activity.ComponentActivity +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import {{ sdk.namespace | caseDot }}.WebAuthComponent +import org.junit.runner.RunWith + @RunWith(AndroidJUnit4::class) +{% endif %} class {{service.name | caseUcfirst}}ServiceTest { private lateinit var client: Client private lateinit var {{service.name | caseCamel}}: {{service.name | caseUcfirst}} @Before fun setup() { - client = spyk(Client(ApplicationProvider.getApplicationContext())) + client = spyk(Client({% if language.name == 'Android' %}ApplicationProvider.getApplicationContext(){% endif %})) {{service.name | caseCamel}} = {{service.name | caseUcfirst}}(client) } @@ -46,10 +49,16 @@ class {{service.name | caseUcfirst}}ServiceTest { {% endif %} {%~ if method.type == 'webAuth' ~%} + {%~ if language.name == 'Android' ~%} mockkObject(WebAuthComponent.Companion) coEvery { WebAuthComponent.Companion.authenticate(any(), any(), any(), any()) } answers { arg<((Result) -> Unit)?>(3)?.invoke(Result.success("http://localhost/oauth?key=example&secret=example")) } + {%~ else ~%} + coEvery { + client.awaitResponse(any(), any()) + } returns Response(true) + {%~ endif ~%} {%~ else ~%} coEvery { client.awaitResponse(any(), any()) @@ -59,7 +68,7 @@ class {{service.name | caseUcfirst}}ServiceTest { runTest { val response = {{service.name | caseCamel}}.{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%} {{parameter.name | escapeKeyword | caseCamel}} = {% if parameter.type == 'object' %}mapOf(){% elseif parameter.type == 'array' %}listOf(){% elseif parameter.type == 'file' %}InputFile.fromBytes(byteArrayOf(), mimeType = "image/png"){% elseif parameter.type == 'boolean' %}true{% elseif parameter.type == 'string' %}"{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}"{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%},{%~ endfor ~%} - {%~ if method.type == "webAuth" %} + {%~ if method.type == "webAuth" and language.name == 'Android' %} activity = ComponentActivity(), {%~ endif %} )