generated from aura-ascend/template-service
update TransactionService with withdrawal
This commit is contained in:
@@ -7,9 +7,11 @@ import io.mockk.verify
|
||||
import java.math.BigDecimal
|
||||
import java.util.UUID
|
||||
import ltd.lulz.exception.AccountNotFoundException
|
||||
import ltd.lulz.exception.InsufficientFundsException
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
@@ -31,44 +33,111 @@ class TransactionServiceTest {
|
||||
service = TransactionService(accountService)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deposit to account - success`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
@Nested
|
||||
inner class Deposit {
|
||||
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
@Test
|
||||
fun `deposit to account - success`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.deposit(uuid, deposit))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount + deposit)
|
||||
}
|
||||
.verifyComplete()
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 1) { accountService.save(any()) }
|
||||
// when stepped
|
||||
StepVerifier.create(service.deposit(uuid, deposit))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount + deposit)
|
||||
}
|
||||
.verifyComplete()
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 1) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deposit to account - account not found`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.deposit(uuid, deposit))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deposit to account - account not found`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
@Nested
|
||||
inner class Withdrawal {
|
||||
|
||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||
@Test
|
||||
fun `withdrawal from account - success`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.01)
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.deposit(uuid, deposit))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
// when stepped
|
||||
StepVerifier.create(service.withdrawal(uuid, deposit))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount - deposit)
|
||||
}
|
||||
.verifyComplete()
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 1) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withdrawal from account - insufficient founds`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.withdrawal(uuid, deposit))
|
||||
.expectError(InsufficientFundsException::class.java)
|
||||
.verify()
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withdrawal from account - account not found`() {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.withdrawal(uuid, deposit))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user