add AccountController

This commit is contained in:
2025-09-11 12:58:04 +02:00
parent 37e20dcdbe
commit 0f8e6ab852
5 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package ltd.lulz.controller
import jakarta.validation.Valid
import ltd.lulz.model.Account
import ltd.lulz.service.AccountService
import ltd.lulz.util.toEntity
import ltd.lulz.util.toResponse
import org.springframework.http.HttpStatus.CREATED
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
@RestController
@Validated
class AccountController(
private val accountService: AccountService,
) {
@PostMapping("/account")
@ResponseStatus(CREATED)
fun create(
@Valid @RequestBody request: Account.Request,
): Mono<Account.Response> = accountService.create(request.toEntity())
.map { it.toResponse() }
}

View File

@@ -1,11 +1,16 @@
package ltd.lulz.model
import jakarta.validation.constraints.DecimalMin
import jakarta.validation.constraints.NotEmpty
import java.math.BigDecimal
import java.util.UUID
object Account {
data class Request(
@field:NotEmpty
val name: String,
@field:DecimalMin(value = "0.01")
val amount: BigDecimal,
)

View File

@@ -0,0 +1,88 @@
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
}
}