generated from aura-ascend/template-service
Compare commits
1 Commits
feature/re
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| bf04fa5077 |
@@ -49,6 +49,5 @@ tasks.withType<Test> {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
tasks.withType<BootJar> {
|
||||
enabled = false
|
||||
enabled = false
|
||||
}
|
||||
|
||||
|
||||
@@ -3,13 +3,12 @@ package ltd.lulz.repository
|
||||
import java.util.UUID
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import org.springframework.data.r2dbc.repository.Query
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository
|
||||
import org.springframework.data.repository.kotlin.CoroutineCrudRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@Repository
|
||||
interface AccountRepository : ReactiveCrudRepository<AccountEntity, UUID> {
|
||||
interface AccountRepository : CoroutineCrudRepository<AccountEntity, UUID> {
|
||||
|
||||
@Query("SELECT * FROM accounts WHERE id = :id FOR UPDATE NOWAIT")
|
||||
fun findByIdForUpdate(id: UUID): Mono<AccountEntity>
|
||||
suspend fun findByIdForUpdate(id: UUID): AccountEntity?
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import ltd.lulz.model.AccountEntity
|
||||
import ltd.lulz.repository.AccountRepository
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
@@ -17,28 +16,28 @@ class AccountService(
|
||||
private val accountRepository: AccountRepository,
|
||||
) {
|
||||
|
||||
fun create(
|
||||
suspend fun create(
|
||||
name: String,
|
||||
amount: BigDecimal
|
||||
): Mono<AccountEntity> = accountRepository.save(AccountEntity(name = name, amount = amount))
|
||||
.doOnNext { log.debug { "account created with id: ${it.id}" } }
|
||||
): AccountEntity = accountRepository.save(AccountEntity(name = name, amount = amount))
|
||||
.also { log.debug { "account created with id: ${it.id}" } }
|
||||
|
||||
fun getById(
|
||||
suspend fun getById(
|
||||
id: UUID,
|
||||
): Mono<AccountEntity> = accountRepository.findById(id)
|
||||
.doOnNext { log.debug { "found account by id: ${it.id}" } }
|
||||
.switchIfEmpty(Mono.error(AccountNotFoundException()))
|
||||
): AccountEntity = accountRepository.findById(id)
|
||||
?.also { log.debug { "found account by id: ${it.id}" } }
|
||||
?: throw AccountNotFoundException()
|
||||
|
||||
@Transactional
|
||||
fun getForUpdateById(
|
||||
suspend fun getForUpdateById(
|
||||
id: UUID,
|
||||
): Mono<AccountEntity> = accountRepository.findByIdForUpdate(id)
|
||||
.doOnNext { log.trace { "account with id: ${it.id} locked for update" } }
|
||||
.switchIfEmpty(Mono.error(AccountNotFoundException()))
|
||||
): AccountEntity = accountRepository.findByIdForUpdate(id)
|
||||
?.also { log.trace { "account with id: ${it.id} locked for update" } }
|
||||
?: throw AccountNotFoundException()
|
||||
|
||||
@Transactional
|
||||
fun save(
|
||||
suspend fun save(
|
||||
entity: AccountEntity,
|
||||
): Mono<AccountEntity> = accountRepository.save(entity)
|
||||
.doOnNext { log.trace { "account with id: ${it.id} saved" } }
|
||||
): AccountEntity = accountRepository.save(entity)
|
||||
.also { log.trace { "account with id: ${it.id} saved" } }
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import ltd.lulz.exception.InsufficientFundsException
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
@@ -18,31 +17,36 @@ class TransactionService(
|
||||
) {
|
||||
|
||||
@Transactional
|
||||
fun deposit(
|
||||
suspend fun deposit(
|
||||
id: UUID,
|
||||
amount: BigDecimal,
|
||||
): Mono<AccountEntity> = accountService.getForUpdateById(id)
|
||||
.map { it.copy(amount = it.amount + amount) }
|
||||
.doOnNext { log.trace { "Deposited $amount to account ${it.id}" } }
|
||||
.flatMap { accountService.save(it) }
|
||||
): AccountEntity = accountService.getForUpdateById(id)
|
||||
.let {
|
||||
log.trace { "Deposited $amount to account ${it.id}" }
|
||||
accountService.save(it.copy(amount = it.amount + amount))
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun withdrawal(
|
||||
suspend fun withdrawal(
|
||||
account: UUID,
|
||||
amount: BigDecimal,
|
||||
): Mono<AccountEntity> = accountService.getForUpdateById(account)
|
||||
.map { it.copy(amount = it.amount - amount) }
|
||||
.filter { it.amount >= ZERO }
|
||||
.doOnNext { log.trace { "withdrawal $amount from account ${it.id}" } }
|
||||
.switchIfEmpty(Mono.error(InsufficientFundsException()))
|
||||
.flatMap { accountService.save(it) }
|
||||
): AccountEntity = accountService.getForUpdateById(account)
|
||||
.let {
|
||||
val entity = it.copy(amount = it.amount - amount)
|
||||
if (entity.amount < ZERO) {
|
||||
throw InsufficientFundsException()
|
||||
}
|
||||
log.trace { "withdrawal $amount from account ${it.id}" }
|
||||
accountService.save(entity)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun transfer(
|
||||
suspend fun transfer(
|
||||
account: UUID,
|
||||
receiver: UUID,
|
||||
amount: BigDecimal,
|
||||
): Mono<Void> = withdrawal(account, amount)
|
||||
.zipWith(deposit(receiver, amount))
|
||||
.then()
|
||||
) {
|
||||
withdrawal(account, amount)
|
||||
deposit(receiver, amount)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
package ltd.lulz.service
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.mockk
|
||||
import io.mockk.slot
|
||||
import io.mockk.verify
|
||||
import java.math.BigDecimal
|
||||
import java.util.UUID
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import ltd.lulz.exception.AccountNotFoundException
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import ltd.lulz.repository.AccountRepository
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
|
||||
@Suppress("ReactiveStreamsUnusedPublisher", "MayBeConstant")
|
||||
@Suppress("MayBeConstant")
|
||||
class AccountServiceTest {
|
||||
|
||||
companion object {
|
||||
@@ -33,104 +33,102 @@ class AccountServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create account`() {
|
||||
fun `create account`() = runTest {
|
||||
// given
|
||||
val capture = slot<AccountEntity>()
|
||||
every { repository.save(capture(capture)) } answers { Mono.just(capture.captured.copy(id = uuid)) }
|
||||
coEvery { repository.save(capture(capture)) }
|
||||
.answers { capture.captured.copy(id = uuid) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.create(name, amount))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
.verifyComplete()
|
||||
// when
|
||||
val result = service.create(name, amount)
|
||||
|
||||
verify { repository.save(any()) }
|
||||
// then
|
||||
coVerify { repository.save(any()) }
|
||||
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get by id`() {
|
||||
fun `get by id`() = runTest {
|
||||
// given
|
||||
val capture = slot<UUID>()
|
||||
every { repository.findById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
coEvery { repository.findById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, name, amount) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.getById(uuid))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
.verifyComplete()
|
||||
// when
|
||||
val result = service.getById(uuid)
|
||||
|
||||
verify { repository.findById(any(UUID::class)) }
|
||||
// then
|
||||
coVerify { repository.findById(any(UUID::class)) }
|
||||
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get by id - fail`() {
|
||||
fun `get by id - fail`() = runTest {
|
||||
// given
|
||||
every { repository.findById(any(UUID::class)) } returns Mono.empty()
|
||||
coEvery { repository.findById(any(UUID::class)) } returns null
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.getById(uuid))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<AccountNotFoundException> {
|
||||
service.getById(uuid)
|
||||
}
|
||||
|
||||
verify { repository.findById(any(UUID::class)) }
|
||||
// then
|
||||
coVerify { repository.findById(any(UUID::class)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get for update by id`() {
|
||||
fun `get for update by id`() = runTest {
|
||||
// given
|
||||
val capture = slot<UUID>()
|
||||
every { repository.findByIdForUpdate(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
coEvery { repository.findByIdForUpdate(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, name, amount) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.getForUpdateById(uuid))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
.verifyComplete()
|
||||
// when
|
||||
val result = service.getForUpdateById(uuid)
|
||||
|
||||
verify { repository.findByIdForUpdate(any(UUID::class)) }
|
||||
// then
|
||||
coVerify { repository.findByIdForUpdate(any(UUID::class)) }
|
||||
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get for update by id - fail`() {
|
||||
fun `get for update by id - fail`() = runTest {
|
||||
// given
|
||||
coEvery { repository.findByIdForUpdate(any(UUID::class)) } returns null
|
||||
|
||||
every { repository.findByIdForUpdate(any(UUID::class)) } returns Mono.empty()
|
||||
// when
|
||||
assertThrows<AccountNotFoundException> {
|
||||
service.getForUpdateById(uuid)
|
||||
}
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.getForUpdateById(uuid))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
|
||||
verify { repository.findByIdForUpdate(any(UUID::class)) }
|
||||
// then
|
||||
coVerify { repository.findByIdForUpdate(any(UUID::class)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `save change`() {
|
||||
fun `save change`() = runTest {
|
||||
// given
|
||||
val entity = AccountEntity(name = name, amount = amount)
|
||||
|
||||
val capture = slot<AccountEntity>()
|
||||
every { repository.save(capture(capture)) }
|
||||
.answers { Mono.just(capture.captured) }
|
||||
coEvery { repository.save(capture(capture)) }
|
||||
.answers { capture.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.save(entity))
|
||||
.assertNext { result ->
|
||||
assertThat(result).isEqualTo(entity)
|
||||
}
|
||||
.verifyComplete()
|
||||
// when
|
||||
val result = service.save(entity)
|
||||
|
||||
verify { repository.save(any()) }
|
||||
// then
|
||||
coVerify { repository.save(any()) }
|
||||
|
||||
assertThat(result).isEqualTo(entity)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package ltd.lulz.service
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.mockk
|
||||
import io.mockk.slot
|
||||
import io.mockk.verify
|
||||
import java.math.BigDecimal
|
||||
import java.util.UUID
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import ltd.lulz.exception.AccountNotFoundException
|
||||
import ltd.lulz.exception.InsufficientFundsException
|
||||
import ltd.lulz.model.AccountEntity
|
||||
@@ -13,10 +14,9 @@ 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
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
|
||||
@Suppress("MayBeConstant", "ReactiveStreamsUnusedPublisher")
|
||||
@Suppress("MayBeConstant")
|
||||
class TransactionServiceTest {
|
||||
|
||||
companion object {
|
||||
@@ -40,44 +40,44 @@ class TransactionServiceTest {
|
||||
inner class Deposit {
|
||||
|
||||
@Test
|
||||
fun `deposit to account - success`() {
|
||||
fun `deposit to account - success`() = runTest {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, accountName, accountAmount) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
coEvery { accountService.save(capture(entity)) }
|
||||
.answers { entity.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.deposit(accountUuid, deposit))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(accountUuid)
|
||||
assertThat(result.name).isEqualTo(accountName)
|
||||
assertThat(result.amount).isEqualTo(accountAmount + deposit)
|
||||
}
|
||||
.verifyComplete()
|
||||
// when
|
||||
val result = service.deposit(accountUuid, deposit)
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 1) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 1) { accountService.save(any()) }
|
||||
|
||||
assertThat(result.id).isEqualTo(accountUuid)
|
||||
assertThat(result.name).isEqualTo(accountName)
|
||||
assertThat(result.amount).isEqualTo(accountAmount + deposit)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deposit to account - account not found`() {
|
||||
fun `deposit to account - account not found`() = runTest {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||
coEvery { accountService.getForUpdateById(any()) } throws AccountNotFoundException()
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.deposit(accountUuid, deposit))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<AccountNotFoundException> {
|
||||
service.deposit(accountUuid, deposit)
|
||||
}
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,62 +85,63 @@ class TransactionServiceTest {
|
||||
inner class Withdrawal {
|
||||
|
||||
@Test
|
||||
fun `withdrawal from account - success`() {
|
||||
fun `withdrawal from account - success`() = runTest {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.01)
|
||||
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, accountName, accountAmount) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
coEvery { accountService.save(capture(entity)) }
|
||||
.answers { entity.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.withdrawal(accountUuid, deposit))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(accountUuid)
|
||||
assertThat(result.name).isEqualTo(accountName)
|
||||
assertThat(result.amount).isEqualTo(accountAmount - deposit)
|
||||
}
|
||||
.verifyComplete()
|
||||
// when
|
||||
val result = service.withdrawal(accountUuid, deposit)
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 1) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 1) { accountService.save(any()) }
|
||||
|
||||
assertThat(result.id).isEqualTo(accountUuid)
|
||||
assertThat(result.name).isEqualTo(accountName)
|
||||
assertThat(result.amount).isEqualTo(accountAmount - deposit)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withdrawal from account - insufficient founds`() {
|
||||
fun `withdrawal from account - insufficient founds`() = runTest {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
val capture = slot<UUID>()
|
||||
every { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, accountName, accountAmount)) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, accountName, accountAmount) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.withdrawal(accountUuid, deposit))
|
||||
.expectError(InsufficientFundsException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<InsufficientFundsException> {
|
||||
service.withdrawal(accountUuid, deposit)
|
||||
}
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withdrawal from account - account not found`() {
|
||||
fun `withdrawal from account - account not found`() = runTest {
|
||||
// given
|
||||
val deposit = BigDecimal.valueOf(1.10)
|
||||
|
||||
every { accountService.getForUpdateById(any()) } returns Mono.error(AccountNotFoundException())
|
||||
coEvery { accountService.getForUpdateById(any()) } throws AccountNotFoundException()
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.withdrawal(accountUuid, deposit))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<AccountNotFoundException> {
|
||||
service.withdrawal(accountUuid, deposit)
|
||||
}
|
||||
|
||||
verify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,90 +149,93 @@ class TransactionServiceTest {
|
||||
inner class Transfer {
|
||||
|
||||
@Test
|
||||
fun `transfer from account - success`() {
|
||||
fun `transfer from account - success`() = runTest {
|
||||
// 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)) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, accountName, accountAmount) }
|
||||
.andThenAnswer { AccountEntity(capture.captured, receiverName, receiverAmount) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
coEvery { accountService.save(capture(entity)) }
|
||||
.answers { entity.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||
.verifyComplete()
|
||||
// when
|
||||
service.transfer(accountUuid, receiverUuid, transfer)
|
||||
|
||||
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 2) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 2) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `transfer from account - insufficient founds`() {
|
||||
fun `transfer from account - insufficient founds`() = runTest {
|
||||
// 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)) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, accountName, accountAmount) }
|
||||
.andThenAnswer { AccountEntity(capture.captured, receiverName, receiverAmount) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
coEvery { accountService.save(capture(entity)) }
|
||||
.answers { entity.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||
.expectError(InsufficientFundsException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<InsufficientFundsException> {
|
||||
service.transfer(accountUuid, receiverUuid, transfer)
|
||||
}
|
||||
|
||||
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `transfer from account - account not found`() {
|
||||
fun `transfer from account - account not found`() = runTest {
|
||||
// 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)) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.throws(AccountNotFoundException())
|
||||
.andThenAnswer { AccountEntity(capture.captured, receiverName, receiverAmount) }
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
coEvery { accountService.save(capture(entity)) }
|
||||
.answers { entity.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<AccountNotFoundException> {
|
||||
service.transfer(accountUuid, receiverUuid, transfer)
|
||||
}
|
||||
|
||||
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 0) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 1) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 0) { accountService.save(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `transfer from account - receiver not found`() {
|
||||
fun `transfer from account - receiver not found`() = runTest {
|
||||
// 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()) }
|
||||
coEvery { accountService.getForUpdateById(capture(capture)) }
|
||||
.answers { AccountEntity(capture.captured, accountName, accountAmount) }
|
||||
.andThenThrows(AccountNotFoundException())
|
||||
val entity = slot<AccountEntity>()
|
||||
every { accountService.save(capture(entity)) }
|
||||
.answers { Mono.just(entity.captured) }
|
||||
coEvery { accountService.save(capture(entity)) }
|
||||
.answers { entity.captured }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.transfer(accountUuid, receiverUuid, transfer))
|
||||
.expectError(AccountNotFoundException::class.java)
|
||||
.verify()
|
||||
// when
|
||||
assertThrows<AccountNotFoundException> {
|
||||
service.transfer(accountUuid, receiverUuid, transfer)
|
||||
}
|
||||
|
||||
verify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
verify(exactly = 1) { accountService.save(any()) }
|
||||
// then
|
||||
coVerify(exactly = 2) { accountService.getForUpdateById(any(UUID::class)) }
|
||||
coVerify(exactly = 1) { accountService.save(any()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user