diff --git a/data/network/build.gradle.kts b/data/network/build.gradle.kts index 26b85a26..31aa7a83 100644 --- a/data/network/build.gradle.kts +++ b/data/network/build.gradle.kts @@ -103,6 +103,19 @@ kotlin { implementation(libs.org.jetbrains.kotlin.test.junit) } } + val iosX64Test by getting + val iosArm64Test by getting + val iosSimulatorArm64Test by getting + val iosTest by creating { + dependsOn(getByName("commonTest")) + + iosX64Test.dependsOn(this) + iosArm64Test.dependsOn(this) + iosSimulatorArm64Test.dependsOn(this) + dependencies { + implementation(libs.org.jetbrains.kotlinx.coroutines.test) + } + } named("commonTest") { dependencies { implementation(kotlin("test")) diff --git a/data/network/src/androidTest/kotlin/social/androiddev/common/ResourceLoader.kt b/data/network/src/androidTest/kotlin/social/androiddev/common/ResourceLoader.kt new file mode 100644 index 00000000..4399503a --- /dev/null +++ b/data/network/src/androidTest/kotlin/social/androiddev/common/ResourceLoader.kt @@ -0,0 +1,16 @@ +/* + * This file is part of Dodo. + * + * Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Dodo. If not, see . + */ +package social.androiddev.common + +import java.io.File + +actual fun readBinaryResource(resourcePath: String): ByteArray { + return File(resourcePath).readBytes() +} diff --git a/data/network/src/commonMain/kotlin/social/androiddev/common/network/MastodonApiKtor.kt b/data/network/src/commonMain/kotlin/social/androiddev/common/network/MastodonApiKtor.kt index 4eebdf7d..20d48557 100644 --- a/data/network/src/commonMain/kotlin/social/androiddev/common/network/MastodonApiKtor.kt +++ b/data/network/src/commonMain/kotlin/social/androiddev/common/network/MastodonApiKtor.kt @@ -20,6 +20,7 @@ import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.contentType import io.ktor.http.path +import io.ktor.serialization.JsonConvertException import kotlinx.serialization.SerializationException import social.androiddev.common.network.model.Application import social.androiddev.common.network.model.AvailableInstance @@ -120,6 +121,8 @@ internal class MastodonApiKtor( ) } catch (exception: SerializationException) { Result.failure(exception = exception) + } catch (exception: JsonConvertException) { + Result.failure(exception = exception) } catch (exception: ResponseException) { Result.failure(exception = exception) } diff --git a/data/network/src/commonMain/kotlin/social/androiddev/common/network/model/Application.kt b/data/network/src/commonMain/kotlin/social/androiddev/common/network/model/Application.kt index 016bdb71..88eeb2cc 100644 --- a/data/network/src/commonMain/kotlin/social/androiddev/common/network/model/Application.kt +++ b/data/network/src/commonMain/kotlin/social/androiddev/common/network/model/Application.kt @@ -17,7 +17,6 @@ import kotlinx.serialization.Serializable */ @Serializable data class Application( - val id: String, val name: String, @SerialName("vapid_key") val vapidKey: String, diff --git a/data/network/src/commonTest/kotlin/social/androiddev/common/ResourceLoader.kt b/data/network/src/commonTest/kotlin/social/androiddev/common/ResourceLoader.kt new file mode 100644 index 00000000..1513d27c --- /dev/null +++ b/data/network/src/commonTest/kotlin/social/androiddev/common/ResourceLoader.kt @@ -0,0 +1,17 @@ +/* + * This file is part of Dodo. + * + * Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Dodo. If not, see . + */ +package social.androiddev.common + +/** + * the resourcePath is based on the module + * + * e.g.: "src/commonTest/resources/resource_name.json" + */ +expect fun readBinaryResource(resourcePath: String): ByteArray diff --git a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/AccountTests.kt b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/AccountTests.kt index b38260fe..4b463c7d 100644 --- a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/AccountTests.kt +++ b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/AccountTests.kt @@ -11,18 +11,16 @@ package social.androiddev.common.network.model import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlin.test.Ignore +import social.androiddev.common.readBinaryResource import kotlin.test.Test import kotlin.test.assertEquals class AccountTests { - // TODO: fix loading json from resources - @Ignore @Test fun `deserialize required fields should succeed`() { // given - // val json: String = javaClass.classLoader.getResource("response_account_required.json").readText() - val json: String = "" + val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_account_required.json") + val json: String = byteArray.decodeToString() // when val account = Json.decodeFromString(json) diff --git a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ContextTests.kt b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ContextTests.kt index e53fd60e..b94dce22 100644 --- a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ContextTests.kt +++ b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ContextTests.kt @@ -11,19 +11,17 @@ package social.androiddev.common.network.model import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlin.test.Ignore +import social.androiddev.common.readBinaryResource import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull class ContextTests { - // TODO: fix loading json from resources - @Ignore @Test fun `deserialize required fields should succeed`() { // given - // val json: String = javaClass.classLoader.getResource("response_context_required.json").readText() - val json: String = "" + val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_context_required.json") + val json: String = byteArray.decodeToString() // when val context = Json.decodeFromString(json) diff --git a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ConversationTests.kt b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ConversationTests.kt index abeb8a32..dbd29fdf 100644 --- a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ConversationTests.kt +++ b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/ConversationTests.kt @@ -11,19 +11,17 @@ package social.androiddev.common.network.model import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlin.test.Ignore +import social.androiddev.common.readBinaryResource import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull class ConversationTests { - // TODO: fix loading json from resources - @Ignore @Test fun `deserialize required fields should succeed`() { // given - // val json: String = javaClass.classLoader.getResource("response_conversation_required.json").readText() - val json: String = "" + val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_conversation_required.json") + val json: String = byteArray.decodeToString() // when val conversation = Json.decodeFromString(json) diff --git a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/NotificationTests.kt b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/NotificationTests.kt index 5baae4c0..4360644b 100644 --- a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/NotificationTests.kt +++ b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/NotificationTests.kt @@ -11,19 +11,17 @@ package social.androiddev.common.network.model import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlin.test.Ignore +import social.androiddev.common.readBinaryResource import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull class NotificationTests { - // TODO: fix loading json from resources - @Ignore @Test fun `deserialize required fields should succeed`() { // given - // val json: String = javaClass.classLoader.getResource("response_notification_required.json").readText() - val json: String = "" + val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_notification_required.json") + val json: String = byteArray.decodeToString() // when val notification = Json.decodeFromString(json) diff --git a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/StatusTests.kt b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/StatusTests.kt index 80224824..78b4b92e 100644 --- a/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/StatusTests.kt +++ b/data/network/src/commonTest/kotlin/social/androiddev/common/network/model/StatusTests.kt @@ -11,19 +11,17 @@ package social.androiddev.common.network.model import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlin.test.Ignore +import social.androiddev.common.readBinaryResource import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull class StatusTests { - // TODO: fix loading json from resources - @Ignore @Test fun `deserialize required fields should succeed`() { // given - // val json: String = javaClass.classLoader.getResource("response_status_required.json").readText() - val json: String = "" + val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_status_required.json") + val json: String = byteArray.decodeToString() // when val status = Json.decodeFromString(json) diff --git a/data/network/src/commonTest/resources/response_context_required.json b/data/network/src/commonTest/resources/response_context_required.json index 9920d577..41f3bfca 100644 --- a/data/network/src/commonTest/resources/response_context_required.json +++ b/data/network/src/commonTest/resources/response_context_required.json @@ -24,7 +24,8 @@ "application": { "name": "Web", - "website": null + "website": null, + "vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=" }, "account": { @@ -114,7 +115,8 @@ "application": { "name": "Web", - "website": null + "website": null, + "vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=" }, "account": { diff --git a/data/network/src/commonTest/resources/response_conversation_required.json b/data/network/src/commonTest/resources/response_conversation_required.json index 37cf4697..449a05ec 100644 --- a/data/network/src/commonTest/resources/response_conversation_required.json +++ b/data/network/src/commonTest/resources/response_conversation_required.json @@ -92,7 +92,8 @@ "application": { "name": "Web", - "website": null + "website": null, + "vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=" }, "account": { diff --git a/data/network/src/commonTest/resources/response_notification_required.json b/data/network/src/commonTest/resources/response_notification_required.json index f389f9a5..fbd8592c 100644 --- a/data/network/src/commonTest/resources/response_notification_required.json +++ b/data/network/src/commonTest/resources/response_notification_required.json @@ -91,7 +91,8 @@ "application": { "name": "Web", - "website": null + "website": null, + "vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=" }, "account": { diff --git a/data/network/src/commonTest/resources/response_status_required.json b/data/network/src/commonTest/resources/response_status_required.json index be736b8f..e02ada3c 100644 --- a/data/network/src/commonTest/resources/response_status_required.json +++ b/data/network/src/commonTest/resources/response_status_required.json @@ -21,7 +21,8 @@ "application": { "name": "Web", - "website": null + "website": null, + "vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=" }, "account": { diff --git a/data/network/src/desktopTest/kotlin/social/androiddev/common/ResourceLoader.kt b/data/network/src/desktopTest/kotlin/social/androiddev/common/ResourceLoader.kt new file mode 100644 index 00000000..4399503a --- /dev/null +++ b/data/network/src/desktopTest/kotlin/social/androiddev/common/ResourceLoader.kt @@ -0,0 +1,16 @@ +/* + * This file is part of Dodo. + * + * Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Dodo. If not, see . + */ +package social.androiddev.common + +import java.io.File + +actual fun readBinaryResource(resourcePath: String): ByteArray { + return File(resourcePath).readBytes() +} diff --git a/data/network/src/iosTest/kotlin/social/androiddev/common/ResourceLoader.kt b/data/network/src/iosTest/kotlin/social/androiddev/common/ResourceLoader.kt new file mode 100644 index 00000000..70fdaf4d --- /dev/null +++ b/data/network/src/iosTest/kotlin/social/androiddev/common/ResourceLoader.kt @@ -0,0 +1,37 @@ +/* + * This file is part of Dodo. + * + * Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Dodo. If not, see . + */ +package social.androiddev.common + +import kotlinx.cinterop.addressOf +import kotlinx.cinterop.usePinned +import platform.Foundation.NSBundle +import platform.Foundation.NSData +import platform.Foundation.NSUTF8StringEncoding +import platform.Foundation.dataWithContentsOfFile +import platform.posix.memcpy + +actual fun readBinaryResource(resourcePath: String): ByteArray { + val path = resourcePath.substringBeforeLast(".") + val fileType = resourcePath.substringAfterLast(".") + println("resourcePath=$resourcePath, path=$path, fileType=$fileType") + + val absolutePath = NSBundle.mainBundle.pathForResource(path, fileType) + println("absolutePath=$absolutePath") + + return NSData.dataWithContentsOfFile(absolutePath!!, NSUTF8StringEncoding, null)!!.toByteArray() +} + +internal fun NSData.toByteArray(): ByteArray { + return ByteArray(length.toInt()).apply { + usePinned { + memcpy(it.addressOf(0), bytes, length) + } + } +}