generated from aura-ascend/template-service
update TransactionController with transfer
This commit is contained in:
@@ -15,3 +15,13 @@ Content-Type: application/json
|
|||||||
"account": "00000000-0000-0000-0000-000000000000",
|
"account": "00000000-0000-0000-0000-000000000000",
|
||||||
"amount": 1
|
"amount": 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
### Withdrawal
|
||||||
|
POST {{url}}/transfer
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"account": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"receiver": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"amount": 1
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import jakarta.validation.Valid
|
|||||||
import ltd.lulz.exception.AccountNotFoundException
|
import ltd.lulz.exception.AccountNotFoundException
|
||||||
import ltd.lulz.exception.InsufficientFundsException
|
import ltd.lulz.exception.InsufficientFundsException
|
||||||
import ltd.lulz.model.Transaction
|
import ltd.lulz.model.Transaction
|
||||||
|
import ltd.lulz.model.Transfer
|
||||||
import ltd.lulz.service.TransactionService
|
import ltd.lulz.service.TransactionService
|
||||||
import org.springframework.http.HttpStatus.CREATED
|
import org.springframework.http.HttpStatus.CREATED
|
||||||
import org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
|
import org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
|
||||||
@@ -54,4 +55,19 @@ class TransactionController(
|
|||||||
}
|
}
|
||||||
.doOnError { log.warn { "withdrawal account ${request.account}: " + it.localizedMessage } }
|
.doOnError { log.warn { "withdrawal account ${request.account}: " + it.localizedMessage } }
|
||||||
.then()
|
.then()
|
||||||
|
|
||||||
|
@PostMapping("/transfer")
|
||||||
|
@ResponseStatus(CREATED)
|
||||||
|
fun transfer(
|
||||||
|
@Valid @RequestBody request: Transfer.Request,
|
||||||
|
): Mono<Void> = transactionService.transfer(request.account, request.receiver, request.amount)
|
||||||
|
.onErrorResume {
|
||||||
|
when (it) {
|
||||||
|
is InsufficientFundsException -> Mono.error(ResponseStatusException(NOT_ACCEPTABLE))
|
||||||
|
is AccountNotFoundException -> Mono.error(ResponseStatusException(NOT_FOUND))
|
||||||
|
else -> Mono.error(ResponseStatusException(INTERNAL_SERVER_ERROR))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.doOnError { log.warn { "transfer account ${request.account}: " + it.localizedMessage } }
|
||||||
|
.then()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import ltd.lulz.exception.AccountNotFoundException
|
|||||||
import ltd.lulz.exception.InsufficientFundsException
|
import ltd.lulz.exception.InsufficientFundsException
|
||||||
import ltd.lulz.model.AccountEntity
|
import ltd.lulz.model.AccountEntity
|
||||||
import ltd.lulz.model.Transaction
|
import ltd.lulz.model.Transaction
|
||||||
|
import ltd.lulz.model.Transfer
|
||||||
import ltd.lulz.service.TransactionService
|
import ltd.lulz.service.TransactionService
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
@@ -23,6 +24,7 @@ class TransactionControllerTest {
|
|||||||
companion object {
|
companion object {
|
||||||
val amount: BigDecimal = BigDecimal.valueOf(0.01)
|
val amount: BigDecimal = BigDecimal.valueOf(0.01)
|
||||||
val uuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
val uuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
||||||
|
val receiver = UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||||
}
|
}
|
||||||
|
|
||||||
private val transactionService: TransactionService = mockk()
|
private val transactionService: TransactionService = mockk()
|
||||||
@@ -194,4 +196,92 @@ class TransactionControllerTest {
|
|||||||
// then
|
// then
|
||||||
result.expectStatus().is5xxServerError
|
result.expectStatus().is5xxServerError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer success`() {
|
||||||
|
// given
|
||||||
|
val request = Transfer.Request(uuid, receiver, amount)
|
||||||
|
|
||||||
|
every { transactionService.transfer(any(), any(), any()) } returns Mono.empty()
|
||||||
|
|
||||||
|
// when
|
||||||
|
val result = webTestClient.post()
|
||||||
|
.uri("/transfer")
|
||||||
|
.contentType(APPLICATION_JSON)
|
||||||
|
.bodyValue(request)
|
||||||
|
.exchange()
|
||||||
|
|
||||||
|
// then
|
||||||
|
result.expectStatus().isCreated
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer fail amount to small`() {
|
||||||
|
// given
|
||||||
|
val request = Transfer.Request(uuid, receiver, BigDecimal.valueOf(0.009))
|
||||||
|
|
||||||
|
// when
|
||||||
|
val result = webTestClient.post()
|
||||||
|
.uri("/transfer")
|
||||||
|
.contentType(APPLICATION_JSON)
|
||||||
|
.bodyValue(request)
|
||||||
|
.exchange()
|
||||||
|
|
||||||
|
// then
|
||||||
|
result.expectStatus().isBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer insufficient Funds`() {
|
||||||
|
// given
|
||||||
|
val request = Transfer.Request(uuid, receiver, amount)
|
||||||
|
|
||||||
|
every { transactionService.transfer(any(), any(), any()) } returns Mono.error(InsufficientFundsException())
|
||||||
|
|
||||||
|
// when
|
||||||
|
val result = webTestClient.post()
|
||||||
|
.uri("/transfer")
|
||||||
|
.contentType(APPLICATION_JSON)
|
||||||
|
.bodyValue(request)
|
||||||
|
.exchange()
|
||||||
|
|
||||||
|
// then
|
||||||
|
result.expectStatus().isEqualTo(NOT_ACCEPTABLE)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer account not found`() {
|
||||||
|
// given
|
||||||
|
val request = Transfer.Request(uuid, receiver, amount)
|
||||||
|
|
||||||
|
every { transactionService.transfer(any(), any(), any()) } returns Mono.error(AccountNotFoundException())
|
||||||
|
|
||||||
|
// when
|
||||||
|
val result = webTestClient.post()
|
||||||
|
.uri("/transfer")
|
||||||
|
.contentType(APPLICATION_JSON)
|
||||||
|
.bodyValue(request)
|
||||||
|
.exchange()
|
||||||
|
|
||||||
|
// then
|
||||||
|
result.expectStatus().isNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer default error`() {
|
||||||
|
// given
|
||||||
|
val request = Transfer.Request(uuid, receiver, amount)
|
||||||
|
|
||||||
|
every { transactionService.transfer(any(), any(), any()) } returns Mono.error(RuntimeException())
|
||||||
|
|
||||||
|
// when
|
||||||
|
val result = webTestClient.post()
|
||||||
|
.uri("/transfer")
|
||||||
|
.contentType(APPLICATION_JSON)
|
||||||
|
.bodyValue(request)
|
||||||
|
.exchange()
|
||||||
|
|
||||||
|
// then
|
||||||
|
result.expectStatus().is5xxServerError
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user