Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.usbharu.multim.api.common
package dev.usbharu.multim

import com.github.michaelbull.result.*
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.plugins.contentnegotiation.*
Expand All @@ -9,6 +10,7 @@ import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import org.assertj.core.api.Fail.fail
import org.junit.jupiter.api.Assertions.assertInstanceOf


object TestUtil {
Expand Down Expand Up @@ -63,4 +65,14 @@ object TestUtil {
}
}
}


fun assertIsOk(result: Result<*,*>){
assertInstanceOf(Ok::class.java,result,"resultの型がOkではない")
}

inline fun <T, reified R : T> assertIsErr(result: Result<*,T>){
assertInstanceOf(Err::class.java,result,"resultの型がErrではない")
assertInstanceOf(R::class.java,result.getError(),"Errorの型が違う")
}
}
83 changes: 83 additions & 0 deletions core/src/test/kotlin/dev/usbharu/multim/api/AccountApiTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dev.usbharu.multim.api

import org.junit.jupiter.api.Test

abstract class AccountApiTest {

abstract val accountApi: AccountApi

@Test
abstract fun userTimeline()

@Test
open fun userTimeline_illegalAccount_returnErr() {
}

@Test
open fun userTimeline_requestWithSinceId_returnRangeTimeline() {
}

@Test
open fun userTimeline_requestWithUntilId_returnRangeTimeline() {
}

@Test
open fun userTimeline_requestWithSinceIdAndUntilId_returnRangeTimeline() {
}


@Test
abstract fun follow() // フォローに関しては、どのように振る舞うべきかは実装によって違うので詳細なテストは書かない

@Test
abstract fun follow_illegalAccount_returnErr()

@Test
abstract fun unfollow() //アンフォローも同じ

@Test
abstract fun unfollow_illegalAccount_returnErr()

@Test
abstract fun profile()

@Test
abstract fun profile_illegalAccount_returnErr()

@Test
abstract fun statuses()

@Test
abstract fun statusesWithIncludeRepost()

@Test
abstract fun statuses_illegalAccount_returnErr()

@Test
abstract fun relationships()

@Test
open fun relationships_sameAccount_returnErr() {
}

@Test
abstract fun relationships_illegalAccount_returnErr()

@Test
abstract fun requestCancel()

@Test
abstract fun requestCancel_illegalAccount_returnErr()

@Test
abstract fun requestAccept()

@Test
abstract fun requestAccept_illegalAccount_returnErr()

@Test
abstract fun requestReject()

@Test
abstract fun requestReject_illegalAccount_returnErr()
}
138 changes: 138 additions & 0 deletions core/src/test/kotlin/dev/usbharu/multim/api/ApiClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package dev.usbharu.multim.api

import com.github.michaelbull.result.Err
import com.github.michaelbull.result.get
import dev.usbharu.multim.TestUtil.assertIsErr
import dev.usbharu.multim.TestUtil.assertIsOk
import dev.usbharu.multim.error.HttpClientClientError
import dev.usbharu.multim.error.HttpClientServerError
import dev.usbharu.multim.error.ThrowableError
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

@OptIn(ExperimentalCoroutinesApi::class)
abstract class ApiClientTest {

abstract var apiClient: ApiClient

@Test
open fun postEmpty_emptyRequest_returnOk() = runTest {
val postEmpty = apiClient.postEmpty<PostEmptyData>("postEmpty/emptyRequest/returnOk")
assertIsOk(postEmpty)
}

@Test
open fun postEmpty_illegalRequest_returnErr() = runTest {
val postEmpty = apiClient.postEmpty<PostEmptyData>("postEmpty/illegalRequest/returnErr")
assertInstanceOf(Err::class.java, postEmpty, "返り値がErr型ではない")
assertInstanceOf(
HttpClientClientError::class.java,
(postEmpty as Err).error,
"ErrorがHttpClientClientError型ではない"
)
assertInstanceOf(
ClientRequestException::class.java,
postEmpty.error.throwable,
"throwableがClientRequestException型ではない"
)
}

@Test
fun postEmpty_serverError_returnErr() = runTest {
val postEmpty = apiClient.postEmpty<PostEmptyData>("postEmpty/serverError/returnErr")
assertInstanceOf(Err::class.java, postEmpty, "返り値がErr型ではない")
assertInstanceOf(
HttpClientServerError::class.java,
(postEmpty as Err).error,
"ErrorがHttpClientServerError型ではない"
)
assertInstanceOf(
ServerResponseException::class.java,
postEmpty.error.throwable,
"throwableがServerResponseException型ではない"
)
}

@Test
fun post_request_returnOkWithEchoBody() = runTest {
val expectBody = PostEmptyData("957a29f2-03bc-4856-b880-3ef42fbc4c77")
val actual = apiClient.post<PostEmptyData, PostEmptyData>(
expectBody,
"post/request/returnOkWithEchoBody"
)
assertIsOk(actual)
assertEquals(expectBody, actual.get())
}

@Test
fun post_illegalRequest_returnErr() = runTest {
val body = PostEmptyData("1974d2a0-552e-442d-a60c-07ab410d4e44")
val actual = apiClient.post<PostEmptyData, PostEmptyData>(
body,
"post/illegalRequest/returnErr"
)
assertIsErr<ThrowableError, HttpClientClientError>(actual)
}

@Test
fun post_serverError_returnErr() = runTest {
val body = PostEmptyData("3a84e1a1-5323-4158-824d-ccf9efee0bf7")
val actual = apiClient.post<PostEmptyData, PostEmptyData>(
body,
"post/serverError/returnErr"
)
assertIsErr<ThrowableError, HttpClientServerError>(actual)
}

@Test
fun postWithoutResponse_request_returnOk() = runTest {
val body = PostEmptyData("497676e7-731b-42cb-bc44-dee573858e66")
val actual =
apiClient.postWithoutResponse(body, "postWithoutResponse/request/returnOk")
assertIsOk(actual)
}

@Test
fun postWithoutResponse_illegalRequest_returnErr() = runTest {
val body = PostEmptyData("20b04325-1386-4076-8068-fb6370ba6150")
val actual =
apiClient.postWithoutResponse(body, "postWithoutResponse/illegalRequest/returnErr")
assertIsErr<ThrowableError, HttpClientClientError>(actual)
}

@Test
fun postWithoutResponse_serverError_returnErr() = runTest {
val body = PostEmptyData("5ef770aa-b228-4b02-90dd-1c1866933f16")
val actual =
apiClient.postWithoutResponse(body, "postWithoutResponse/serverError/returnErr")
assertIsErr<ThrowableError, HttpClientServerError>(actual)
}

@Test
fun get_request_returnOk() = runTest {
val expectBody = PostEmptyData("43c56ee1-ad7a-418e-9411-2fcb61b34cf9")
val actual = apiClient.get("get/request/returnOk") {}
assertIsOk(actual)
assertEquals(expectBody, actual.get()?.body<PostEmptyData>())
}

@Test
fun get_illegalRequest_returnErr() = runTest {
val actual = apiClient.get("get/illegalRequest/returnErr") {}
assertIsErr<ThrowableError, HttpClientClientError>(actual)
}

@Test
fun get_serverError_returnErr() = runTest {
val actual = apiClient.get("get/serverError/returnErr") {}
assertIsErr<ThrowableError, HttpClientServerError>(actual)
}

@Serializable
data class PostEmptyData(val testData: String)
}
93 changes: 93 additions & 0 deletions core/src/test/kotlin/dev/usbharu/multim/api/StatusApiTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package dev.usbharu.multim.api

import org.junit.jupiter.api.Test

abstract class StatusApiTest {
@Test
abstract fun post()

@Test
abstract fun post_illegalStatus()

@Test
abstract fun delete()

@Test
abstract fun delete_illegalStatusId()

@Test
abstract fun findById()

@Test
abstract fun findById_illegalStatusId()

@Test
abstract fun addReaction()

@Test
abstract fun addReaction_illegalStatusId()

@Test
abstract fun addReaction_illegalReaction()

@Test
abstract fun removeReaction()

@Test
abstract fun removeReaction_illegalStatusId()

@Test
abstract fun removeReaction_illegalReaction()

@Test
abstract fun reactions()

@Test
abstract fun reaction_illegalStatusId()

@Test
abstract fun replies()

@Test
abstract fun replies_illegalStatusId()

@Test
abstract fun repost()

@Test
abstract fun repost_illegalStatusId()

@Test
abstract fun unRepost()

@Test
abstract fun unRepost_illegalStatusId()

@Test
abstract fun replyTo()

@Test
abstract fun replyTo_illegalStatusId()

@Test
abstract fun replyTo_illegalStatus()

@Test
abstract fun addToBookmarks()

@Test
abstract fun addToBookmarks_illegalStatusId()

@Test
abstract fun removeFromBookmarks()

@Test
abstract fun removeFromBookmarks_illegalStatusId()

@Test
abstract fun getPreviousAndNext()

@Test
abstract fun getPreviousAndNext_illegalStatusIs()

}
27 changes: 27 additions & 0 deletions core/src/test/kotlin/dev/usbharu/multim/api/TimelineApiTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.usbharu.multim.api

import org.junit.jupiter.api.Test

abstract class TimelineApiTest {

@Test
abstract fun availableTimelines()

@Test
abstract fun listen()

@Test
abstract fun listen_illegalTimeline()

@Test
abstract fun connect()

@Test
abstract fun connect_illegalTimeline()

@Test
abstract fun disconnect()

@Test
abstract fun disconnect_illegalTimeline()
}

This file was deleted.