generated from aura-ascend/template-service
update TransactionService with transfer
This commit is contained in:
@@ -36,4 +36,13 @@ class TransactionService(
|
|||||||
.doOnNext { log.trace { "withdrawal $amount to account ${it.id}" } }
|
.doOnNext { log.trace { "withdrawal $amount to account ${it.id}" } }
|
||||||
.switchIfEmpty(Mono.error(InsufficientFundsException()))
|
.switchIfEmpty(Mono.error(InsufficientFundsException()))
|
||||||
.flatMap { accountService.save(it) }
|
.flatMap { accountService.save(it) }
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
fun transfer(
|
||||||
|
account: UUID,
|
||||||
|
receiver: UUID,
|
||||||
|
amount: BigDecimal,
|
||||||
|
): Mono<Void> = withdrawal(account, amount)
|
||||||
|
.zipWith(deposit(receiver, amount))
|
||||||
|
.then()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,12 @@ import reactor.test.StepVerifier
|
|||||||
class TransactionServiceTest {
|
class TransactionServiceTest {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val name: String = "some name"
|
val accountName: String = "some name"
|
||||||
val amount: BigDecimal = BigDecimal.valueOf(1.01)
|
val accountAmount: BigDecimal = BigDecimal.valueOf(1.01)
|
||||||
val uuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
val accountUuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
||||||
|
val receiverName: String = "different name"
|
||||||
|
val receiverAmount: BigDecimal = BigDecimal.valueOf(1.01)
|
||||||
|
val receiverUuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||||
}
|
}
|
||||||
|
|
||||||
private val accountService: AccountService = mockk()
|
private val accountService: AccountService = mockk()
|
||||||
@@ -43,17 +46,17 @@ class TransactionServiceTest {
|
|||||||
|
|
||||||
val capture = slot<UUID>()
|
val capture = slot<UUID>()
|
||||||
every { accountService.getForUpdateById(capture(capture)) }
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||||
val entity = slot<AccountEntity>()
|
val entity = slot<AccountEntity>()
|
||||||
every { accountService.save(capture(entity)) }
|
every { accountService.save(capture(entity)) }
|
||||||
.answers { Mono.just(entity.captured) }
|
.answers { Mono.just(entity.captured) }
|
||||||
|
|
||||||
// when stepped
|
// when stepped
|
||||||
StepVerifier.create(service.deposit(uuid, deposit))
|
StepVerifier.create(service.deposit(accountUuid, deposit))
|
||||||
.assertNext { result ->
|
.assertNext { result ->
|
||||||
assertThat(result.id).isEqualTo(uuid)
|
assertThat(result.id).isEqualTo(accountUuid)
|
||||||
assertThat(result.name).isEqualTo(name)
|
assertThat(result.name).isEqualTo(accountName)
|
||||||
assertThat(result.amount).isEqualTo(amount + deposit)
|
assertThat(result.amount).isEqualTo(accountAmount + deposit)
|
||||||
}
|
}
|
||||||
.verifyComplete()
|
.verifyComplete()
|
||||||
|
|
||||||
@@ -69,7 +72,7 @@ class TransactionServiceTest {
|
|||||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||||
|
|
||||||
// when stepped
|
// when stepped
|
||||||
StepVerifier.create(service.deposit(uuid, deposit))
|
StepVerifier.create(service.deposit(accountUuid, deposit))
|
||||||
.expectError(AccountNotFoundException::class.java)
|
.expectError(AccountNotFoundException::class.java)
|
||||||
.verify()
|
.verify()
|
||||||
|
|
||||||
@@ -88,17 +91,17 @@ class TransactionServiceTest {
|
|||||||
|
|
||||||
val capture = slot<UUID>()
|
val capture = slot<UUID>()
|
||||||
every { accountService.getForUpdateById(capture(capture)) }
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||||
val entity = slot<AccountEntity>()
|
val entity = slot<AccountEntity>()
|
||||||
every { accountService.save(capture(entity)) }
|
every { accountService.save(capture(entity)) }
|
||||||
.answers { Mono.just(entity.captured) }
|
.answers { Mono.just(entity.captured) }
|
||||||
|
|
||||||
// when stepped
|
// when stepped
|
||||||
StepVerifier.create(service.withdrawal(uuid, deposit))
|
StepVerifier.create(service.withdrawal(accountUuid, deposit))
|
||||||
.assertNext { result ->
|
.assertNext { result ->
|
||||||
assertThat(result.id).isEqualTo(uuid)
|
assertThat(result.id).isEqualTo(accountUuid)
|
||||||
assertThat(result.name).isEqualTo(name)
|
assertThat(result.name).isEqualTo(accountName)
|
||||||
assertThat(result.amount).isEqualTo(amount - deposit)
|
assertThat(result.amount).isEqualTo(accountAmount - deposit)
|
||||||
}
|
}
|
||||||
.verifyComplete()
|
.verifyComplete()
|
||||||
|
|
||||||
@@ -113,10 +116,10 @@ class TransactionServiceTest {
|
|||||||
|
|
||||||
val capture = slot<UUID>()
|
val capture = slot<UUID>()
|
||||||
every { accountService.getForUpdateById(capture(capture)) }
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||||
|
|
||||||
// when stepped
|
// when stepped
|
||||||
StepVerifier.create(service.withdrawal(uuid, deposit))
|
StepVerifier.create(service.withdrawal(accountUuid, deposit))
|
||||||
.expectError(InsufficientFundsException::class.java)
|
.expectError(InsufficientFundsException::class.java)
|
||||||
.verify()
|
.verify()
|
||||||
|
|
||||||
@@ -132,7 +135,7 @@ class TransactionServiceTest {
|
|||||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||||
|
|
||||||
// when stepped
|
// when stepped
|
||||||
StepVerifier.create(service.withdrawal(uuid, deposit))
|
StepVerifier.create(service.withdrawal(accountUuid, deposit))
|
||||||
.expectError(AccountNotFoundException::class.java)
|
.expectError(AccountNotFoundException::class.java)
|
||||||
.verify()
|
.verify()
|
||||||
|
|
||||||
@@ -140,4 +143,95 @@ class TransactionServiceTest {
|
|||||||
verify(exactly = 0) { accountService.save(any()) }
|
verify(exactly = 0) { accountService.save(any()) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
inner class Transfer {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer from account - success`() {
|
||||||
|
// given
|
||||||
|
val transfer = BigDecimal.valueOf(1.00)
|
||||||
|
|
||||||
|
val capture = slot<UUID>()
|
||||||
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
|
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||||
|
.andThenAnswer { Mono.just(AccountEntity(capture.captured, receiverName, receiverAmount)) }
|
||||||
|
val entity = slot<AccountEntity>()
|
||||||
|
every { accountService.save(capture(entity)) }
|
||||||
|
.answers { Mono.just(entity.captured) }
|
||||||
|
|
||||||
|
// when stepped
|
||||||
|
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||||
|
.verifyComplete()
|
||||||
|
|
||||||
|
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||||
|
verify(exactly = 2) { accountService.save(any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer from account - insufficient founds`() {
|
||||||
|
// given
|
||||||
|
val transfer = BigDecimal.valueOf(5.00)
|
||||||
|
|
||||||
|
val capture = slot<UUID>()
|
||||||
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
|
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||||
|
.andThenAnswer { Mono.just(AccountEntity(capture.captured, receiverName, receiverAmount)) }
|
||||||
|
val entity = slot<AccountEntity>()
|
||||||
|
every { accountService.save(capture(entity)) }
|
||||||
|
.answers { Mono.just(entity.captured) }
|
||||||
|
|
||||||
|
// when stepped
|
||||||
|
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||||
|
.expectError(InsufficientFundsException::class.java)
|
||||||
|
.verify()
|
||||||
|
|
||||||
|
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||||
|
verify(exactly = 0) { accountService.save(any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer from account - account not found`() {
|
||||||
|
// given
|
||||||
|
val transfer = BigDecimal.valueOf(1.00)
|
||||||
|
|
||||||
|
val capture = slot<UUID>()
|
||||||
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
|
.answers { Mono.error(AccountNotFoundException()) }
|
||||||
|
.andThenAnswer { Mono.just(AccountEntity(capture.captured, receiverName, receiverAmount)) }
|
||||||
|
val entity = slot<AccountEntity>()
|
||||||
|
every { accountService.save(capture(entity)) }
|
||||||
|
.answers { Mono.just(entity.captured) }
|
||||||
|
|
||||||
|
// when stepped
|
||||||
|
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||||
|
.expectError(AccountNotFoundException::class.java)
|
||||||
|
.verify()
|
||||||
|
|
||||||
|
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||||
|
verify(exactly = 0) { accountService.save(any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `transfer from account - receiver not found`() {
|
||||||
|
// given
|
||||||
|
val transfer = BigDecimal.valueOf(1.00)
|
||||||
|
|
||||||
|
val capture = slot<UUID>()
|
||||||
|
every { accountService.getForUpdateById(capture(capture)) }
|
||||||
|
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||||
|
.andThenAnswer { Mono.error(AccountNotFoundException()) }
|
||||||
|
val entity = slot<AccountEntity>()
|
||||||
|
every { accountService.save(capture(entity)) }
|
||||||
|
.answers { Mono.just(entity.captured) }
|
||||||
|
|
||||||
|
// when stepped
|
||||||
|
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||||
|
.expectError(AccountNotFoundException::class.java)
|
||||||
|
.verify()
|
||||||
|
|
||||||
|
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||||
|
verify(exactly = 1) { accountService.save(any()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user