test: モックを導入しテスト名を詳細に

This commit is contained in:
usbharu 2023-04-10 11:37:08 +09:00
parent 68cc469a43
commit ee46d05226
2 changed files with 23 additions and 90 deletions

View File

@ -69,10 +69,12 @@ dependencies {
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
testImplementation("io.ktor:ktor-client-mock:$ktor_version")
implementation("tech.barbero.http-messages-signing:http-messages-signing-core:1.0.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0")
testImplementation("org.mockito:mockito-inline:5.2.0")
implementation("org.drewcarlson:kjob-core:0.6.0")
testImplementation("io.ktor:ktor-server-test-host-jvm:2.2.4")

View File

@ -9,35 +9,29 @@ import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.ap.Image
import dev.usbharu.hideout.ap.Key
import dev.usbharu.hideout.ap.Person
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.domain.model.job.HideoutJob
import dev.usbharu.hideout.plugins.configureRouting
import dev.usbharu.hideout.plugins.configureSerialization
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.activitypub.ActivityPubService
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
import dev.usbharu.hideout.service.activitypub.ActivityType
import dev.usbharu.hideout.service.impl.UserService
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService
import dev.usbharu.hideout.util.HttpUtil.Activity
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.config.*
import io.ktor.server.testing.*
import kjob.core.dsl.JobContextWithProps
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyString
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import kotlin.test.assertEquals
class UsersAPTest {
@Test
fun testHandleUsersName() = testApplication {
@Test()
fun `ユーザのURLにAcceptヘッダーをActivityにしてアクセスしたときPersonが返ってくるか`() = testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
@ -65,82 +59,18 @@ class UsersAPTest {
)
)
person.context = listOf("https://www.w3.org/ns/activitystreams")
val httpSignatureVerifyService = mock<HttpSignatureVerifyService> {}
val activityPubService = mock<ActivityPubService> {}
val userService = mock<UserService> {}
val activityPubUserService = mock<ActivityPubUserService> {
onBlocking { getPersonByName(anyString()) } doReturn person
}
application {
configureSerialization()
configureRouting(object : HttpSignatureVerifyService {
override fun verify(headers: Headers): Boolean {
return true
}
}, object : ActivityPubService {
override fun parseActivity(json: String): ActivityType {
TODO("Not yet implemented")
}
override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? {
TODO("Not yet implemented")
}
override suspend fun <T : HideoutJob> processActivity(
job: JobContextWithProps<T>,
hideoutJob: HideoutJob
) {
TODO("Not yet implemented")
}
}, UserService(object : IUserRepository {
override suspend fun create(user: User): UserEntity {
TODO("Not yet implemented")
}
override suspend fun findById(id: Long): UserEntity? {
TODO("Not yet implemented")
}
override suspend fun findByName(name: String): UserEntity? {
TODO("Not yet implemented")
}
override suspend fun findByUrl(url: String): UserEntity? {
TODO("Not yet implemented")
}
override suspend fun update(userEntity: UserEntity) {
TODO("Not yet implemented")
}
override suspend fun delete(id: Long) {
TODO("Not yet implemented")
}
override suspend fun findAll(): List<User> {
TODO("Not yet implemented")
}
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<UserEntity> {
TODO("Not yet implemented")
}
override suspend fun createFollower(id: Long, follower: Long) {
TODO("Not yet implemented")
}
override suspend fun deleteFollower(id: Long, follower: Long) {
TODO("Not yet implemented")
}
override suspend fun findFollowersById(id: Long): List<UserEntity> {
TODO("Not yet implemented")
}
}), object : ActivityPubUserService {
override suspend fun getPersonByName(name: String): Person {
return person
}
override suspend fun fetchPerson(url: String): Person {
TODO("Not yet implemented")
}
})
configureRouting(httpSignatureVerifyService, activityPubService, userService, activityPubUserService)
}
client.get("/users/test") {
accept(ContentType.Application.Activity)
@ -148,12 +78,13 @@ class UsersAPTest {
val objectMapper = jacksonObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
objectMapper.configOverride(List::class.java).setSetterInfo(JsonSetter.Value.forValueNulls(
Nulls.AS_EMPTY))
objectMapper.configOverride(List::class.java).setSetterInfo(
JsonSetter.Value.forValueNulls(
Nulls.AS_EMPTY
)
)
val actual = it.bodyAsText()
val readValue = objectMapper.readValue<Person>(actual)
println(objectMapper.writeValueAsString(person))
println(objectMapper.writeValueAsString(readValue))
assertEquals(person, readValue)
}
}