Files
basic-banking/src/test/kotlin/ltd/lulz/controller/AccountControllerTest.kt
2025-09-14 18:12:11 +02:00

89 lines
2.4 KiB
Kotlin

package ltd.lulz.controller
import io.mockk.every
import io.mockk.mockk
import java.math.BigDecimal
import java.util.UUID
import ltd.lulz.model.Account
import ltd.lulz.model.AccountEntity
import ltd.lulz.service.AccountService
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.test.web.reactive.server.WebTestClient
import reactor.core.publisher.Mono
@Suppress("MayBeConstant")
class AccountControllerTest {
companion object {
val name: String = "some name"
val amount: BigDecimal = BigDecimal.valueOf(0.01)
val uuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000000")
}
private val accountService: AccountService = mockk()
private lateinit var webTestClient: WebTestClient
@BeforeEach
fun setUp() {
webTestClient = WebTestClient.bindToController(AccountController(accountService)).build()
}
@Test
fun `create account success`() {
// given
val request = Account.Request(name, amount)
every { accountService.create(any()) } returns Mono.just(
AccountEntity(id = uuid, name = name, amount = amount),
)
// when
val result = webTestClient.post()
.uri("/account")
.contentType(APPLICATION_JSON)
.bodyValue(request)
.exchange()
// then
result.expectStatus().isCreated
.expectBody()
.jsonPath("$.id").isEqualTo(uuid.toString())
.jsonPath("$.name").isEqualTo(name)
.jsonPath("$.amount").isEqualTo(amount.toString())
}
@Test
fun `create account fail no name`() {
// given
val request = Account.Request("", amount)
// when
val result = webTestClient.post()
.uri("/account")
.contentType(APPLICATION_JSON)
.bodyValue(request)
.exchange()
// then
result.expectStatus().isBadRequest
}
@Test
fun `create account fail zero or less amount`() {
// given
val request = Account.Request("name", BigDecimal.valueOf(0))
// when
val result = webTestClient.post()
.uri("/account")
.contentType(APPLICATION_JSON)
.bodyValue(request)
.exchange()
// then
result.expectStatus().isBadRequest
}
}