From da93faa1ad3b57c778e4a099d2916bca99fc93a2 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Mon, 16 May 2022 00:27:29 +0430 Subject: [PATCH 01/58] wallet: Add isWithdrawAllowed test --- .../co/nilin/opex/wallet/app/MockitoHelper.kt | 13 ++ .../opex/wallet/app/WalletManagerTest.kt | 132 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt new file mode 100644 index 000000000..469e021ab --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt @@ -0,0 +1,13 @@ +package co.nilin.opex.wallet.app + +import org.mockito.Mockito + +object MockitoHelper { + fun anyObject(): T { + Mockito.any() + return uninitialized() + } + + @Suppress("UNCHECKED_CAST") + fun uninitialized(): T = null as T +} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt new file mode 100644 index 000000000..3d2dadcb3 --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -0,0 +1,132 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.app.MockitoHelper.anyObject +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.* +import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.MockitoAnnotations +import reactor.core.publisher.Mono +import java.math.BigDecimal + +private class WalletManagerTest { + @Mock + private lateinit var walletLimitsRepository: WalletLimitsRepository + + @Mock + private lateinit var transactionRepository: TransactionRepository + + @Mock + private lateinit var walletRepository: WalletRepository + + @Mock + private lateinit var walletOwnerRepository: WalletOwnerRepository + + @Mock + private lateinit var currencyRepository: CurrencyRepository + + private var walletManagerImpl: WalletManagerImpl + + private val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true; + override fun isWithdrawAllowed() = true; + override fun isDepositAllowed() = true; + } + + private val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + init { + MockitoAnnotations.openMocks(this) + runBlocking { + Mockito.`when`( + walletLimitsRepository.findByOwnerAndCurrencyAndWalletAndAction( + ArgumentMatchers.anyLong(), + ArgumentMatchers.anyString(), + ArgumentMatchers.anyLong(), + ArgumentMatchers.anyString() + ) + ) + .thenReturn(Mono.empty()) + Mockito.`when`( + walletLimitsRepository.findByOwnerAndCurrencyAndActionAndWalletType( + ArgumentMatchers.anyLong(), + ArgumentMatchers.anyString(), + ArgumentMatchers.anyString(), + ArgumentMatchers.anyString() + ) + ) + .thenReturn(Mono.empty()) + Mockito.`when`( + walletLimitsRepository.findByLevelAndCurrencyAndActionAndWalletType( + ArgumentMatchers.anyString(), + ArgumentMatchers.anyString(), + ArgumentMatchers.anyString(), + ArgumentMatchers.anyString() + ) + ) + .thenReturn(Mono.empty()) + Mockito.`when`( + transactionRepository.calculateWithdrawStatistics( + ArgumentMatchers.anyLong(), + ArgumentMatchers.anyLong(), + anyObject(), + anyObject() + ) + ) + .thenReturn(Mono.empty()) + } + walletManagerImpl = WalletManagerImpl( + walletLimitsRepository, + transactionRepository, + walletRepository, + walletOwnerRepository, + currencyRepository + ) + } + + @Test + fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue() { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0.5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } + + Assertions.assertEquals(true, isAllowed) + } + + @Test + fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse() { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } + + Assertions.assertEquals(false, isAllowed) + } +} From e4684a2f4b56ac8cb558f819adf44e3f91af314e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Mon, 16 May 2022 12:07:54 +0430 Subject: [PATCH 02/58] wallet: Use mockito-kotlin library --- accountant/accountant-core/pom.xml | 4 + pom.xml | 14 ++++ wallet/wallet-app/pom.xml | 4 + .../co/nilin/opex/wallet/app/MockitoHelper.kt | 13 --- .../opex/wallet/app/WalletManagerTest.kt | 82 +++++++------------ 5 files changed, 50 insertions(+), 67 deletions(-) delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt diff --git a/accountant/accountant-core/pom.xml b/accountant/accountant-core/pom.xml index 65ff8aef1..022876966 100644 --- a/accountant/accountant-core/pom.xml +++ b/accountant/accountant-core/pom.xml @@ -49,5 +49,9 @@ spring-tx provided + + org.mockito + mockito-core + diff --git a/pom.xml b/pom.xml index 9e524604f..abace960d 100644 --- a/pom.xml +++ b/pom.xml @@ -59,12 +59,26 @@ ${spring.version} test + + org.mockito + mockito-core + + + org.mockito + mockito-junit-jupiter + org.junit.vintage junit-vintage-engine + + org.mockito.kotlin + mockito-kotlin + 4.0.0 + test + diff --git a/wallet/wallet-app/pom.xml b/wallet/wallet-app/pom.xml index f3fa59979..a799df3ce 100644 --- a/wallet/wallet-app/pom.xml +++ b/wallet/wallet-app/pom.xml @@ -111,6 +111,10 @@ co.nilin.opex.utility.preferences preferences + + org.mockito.kotlin + mockito-kotlin + diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt deleted file mode 100644 index 469e021ab..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/MockitoHelper.kt +++ /dev/null @@ -1,13 +0,0 @@ -package co.nilin.opex.wallet.app - -import org.mockito.Mockito - -object MockitoHelper { - fun anyObject(): T { - Mockito.any() - return uninitialized() - } - - @Suppress("UNCHECKED_CAST") - fun uninitialized(): T = null as T -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index 3d2dadcb3..f415610e7 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -1,6 +1,5 @@ package co.nilin.opex.wallet.app -import co.nilin.opex.wallet.app.MockitoHelper.anyObject import co.nilin.opex.wallet.core.model.Amount import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet @@ -10,19 +9,22 @@ import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test -import org.mockito.ArgumentMatchers +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock -import org.mockito.Mockito import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock import reactor.core.publisher.Mono import java.math.BigDecimal private class WalletManagerTest { @Mock - private lateinit var walletLimitsRepository: WalletLimitsRepository + private var walletLimitsRepository: WalletLimitsRepository @Mock - private lateinit var transactionRepository: TransactionRepository + private var transactionRepository: TransactionRepository @Mock private lateinit var walletRepository: WalletRepository @@ -40,9 +42,9 @@ private class WalletManagerTest { override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" override fun title() = "wallet" override fun level() = "1" - override fun isTradeAllowed() = true; - override fun isWithdrawAllowed() = true; - override fun isDepositAllowed() = true; + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true } private val currency = object : Currency { @@ -53,55 +55,27 @@ private class WalletManagerTest { init { MockitoAnnotations.openMocks(this) - runBlocking { - Mockito.`when`( - walletLimitsRepository.findByOwnerAndCurrencyAndWalletAndAction( - ArgumentMatchers.anyLong(), - ArgumentMatchers.anyString(), - ArgumentMatchers.anyLong(), - ArgumentMatchers.anyString() - ) - ) - .thenReturn(Mono.empty()) - Mockito.`when`( - walletLimitsRepository.findByOwnerAndCurrencyAndActionAndWalletType( - ArgumentMatchers.anyLong(), - ArgumentMatchers.anyString(), - ArgumentMatchers.anyString(), - ArgumentMatchers.anyString() - ) - ) - .thenReturn(Mono.empty()) - Mockito.`when`( - walletLimitsRepository.findByLevelAndCurrencyAndActionAndWalletType( - ArgumentMatchers.anyString(), - ArgumentMatchers.anyString(), - ArgumentMatchers.anyString(), - ArgumentMatchers.anyString() - ) - ) - .thenReturn(Mono.empty()) - Mockito.`when`( - transactionRepository.calculateWithdrawStatistics( - ArgumentMatchers.anyLong(), - ArgumentMatchers.anyLong(), - anyObject(), - anyObject() - ) - ) - .thenReturn(Mono.empty()) + walletLimitsRepository = mock { + on { + findByOwnerAndCurrencyAndWalletAndAction(anyLong(), anyString(), anyLong(), anyString()) + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(anyLong(), anyString(), anyString(), anyString()) + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType(anyString(), anyString(), anyString(), anyString()) + } doReturn Mono.empty() + } + transactionRepository = mock { + on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() } walletManagerImpl = WalletManagerImpl( - walletLimitsRepository, - transactionRepository, - walletRepository, - walletOwnerRepository, - currencyRepository + walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository ) } @Test - fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue() { + fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue() = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -110,13 +84,13 @@ private class WalletManagerTest { override fun type() = "main" } - val isAllowed = runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) Assertions.assertEquals(true, isAllowed) } @Test - fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse() { + fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse() = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -125,7 +99,7 @@ private class WalletManagerTest { override fun type() = "main" } - val isAllowed = runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) Assertions.assertEquals(false, isAllowed) } From 3dcf735974587226eaa8dfc615236e687bf0d570 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Mon, 16 May 2022 12:19:25 +0430 Subject: [PATCH 03/58] wallet: Use assertJ --- .../kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index f415610e7..b4cf5b9ce 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -18,6 +18,7 @@ import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import reactor.core.publisher.Mono import java.math.BigDecimal +import org.assertj.core.api.Assertions.* private class WalletManagerTest { @Mock @@ -75,7 +76,7 @@ private class WalletManagerTest { } @Test - fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue() = runBlocking { + fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -86,11 +87,11 @@ private class WalletManagerTest { val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) - Assertions.assertEquals(true, isAllowed) + assertThat(isAllowed).isTrue() } @Test - fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse() = runBlocking { + fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -101,6 +102,6 @@ private class WalletManagerTest { val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) - Assertions.assertEquals(false, isAllowed) + assertThat(isAllowed).isFalse() } } From b2f33503d97574685150ebee12c2968b33bf9c02 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Mon, 16 May 2022 14:38:18 +0430 Subject: [PATCH 04/58] wallet: Add base class for testing services --- .../opex/wallet/app/CurrencyServiceTest.kt | 23 ++++++++++ .../opex/wallet/app/WalletManagerTest.kt | 9 +++- .../opex/wallet/app/WalletOwnerManagerTest.kt | 42 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt new file mode 100644 index 000000000..d6a941f7a --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt @@ -0,0 +1,23 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository +import co.nilin.opex.wallet.ports.postgres.impl.CurrencyServiceImpl +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test +import org.mockito.Mock +import org.mockito.MockitoAnnotations + +private class CurrencyServiceTest { + @Mock + private lateinit var currencyRepository: CurrencyRepository + + private var currencyService: CurrencyServiceImpl + + init { + MockitoAnnotations.openMocks(this) + currencyService = CurrencyServiceImpl(currencyRepository) + } + + @Test + fun givenSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking {} +} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index b4cf5b9ce..bd8161aab 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -7,7 +7,7 @@ import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl import kotlinx.coroutines.runBlocking -import org.junit.jupiter.api.Assertions +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.anyLong import org.mockito.ArgumentMatchers.anyString @@ -18,7 +18,6 @@ import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import reactor.core.publisher.Mono import java.math.BigDecimal -import org.assertj.core.api.Assertions.* private class WalletManagerTest { @Mock @@ -104,4 +103,10 @@ private class WalletManagerTest { assertThat(isAllowed).isFalse() } + + @Test + fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking {} + + @Test + fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking {} } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt new file mode 100644 index 000000000..8c846505e --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt @@ -0,0 +1,42 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository +import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository +import co.nilin.opex.wallet.ports.postgres.impl.WalletOwnerManagerImpl +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test +import org.mockito.Mock +import org.mockito.MockitoAnnotations + +private class WalletOwnerManagerTest { + @Mock + private lateinit var userLimitsRepository: UserLimitsRepository + + @Mock + private lateinit var transactionRepository: TransactionRepository + + @Mock + private lateinit var walletOwnerRepository: WalletOwnerRepository + + @Mock + private lateinit var walletConfigRepository: WalletConfigRepository + + private var walletOwnerManagerImpl: WalletOwnerManagerImpl + + init { + MockitoAnnotations.openMocks(this) + walletOwnerManagerImpl = WalletOwnerManagerImpl( + userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository + ) + } + + @Test + fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking {} + + @Test + fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking {} +} From 62a9638fbfef6e9c290bdc9afdcc89b341362524 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Mon, 16 May 2022 14:45:28 +0430 Subject: [PATCH 05/58] wallet: Add is withdraw allowed tests to wallet owner --- .../co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt index 8c846505e..4bb944f0f 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt @@ -1,7 +1,5 @@ package co.nilin.opex.wallet.app -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository @@ -34,6 +32,12 @@ private class WalletOwnerManagerTest { ) } + @Test + fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { } + + @Test + fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { } + @Test fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking {} From 1eb75f176c25cf33ef03fd6959422c033ff6d69e Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Mon, 16 May 2022 20:44:32 +0430 Subject: [PATCH 06/58] wallet: Add wallet manager tests --- .../opex/wallet/app/WalletManagerTest.kt | 74 +++++++++++++++++-- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index bd8161aab..bbfba986a 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -6,6 +6,9 @@ import co.nilin.opex.wallet.core.model.Wallet import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel +import co.nilin.opex.wallet.ports.postgres.model.WalletModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test @@ -27,13 +30,13 @@ private class WalletManagerTest { private var transactionRepository: TransactionRepository @Mock - private lateinit var walletRepository: WalletRepository + private var walletRepository: WalletRepository @Mock - private lateinit var walletOwnerRepository: WalletOwnerRepository + private var walletOwnerRepository: WalletOwnerRepository @Mock - private lateinit var currencyRepository: CurrencyRepository + private var currencyRepository: CurrencyRepository private var walletManagerImpl: WalletManagerImpl @@ -69,6 +72,50 @@ private class WalletManagerTest { transactionRepository = mock { on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() } + walletOwnerRepository = mock { + on { findById(walletOwner.id()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + walletRepository = mock { + on { + findByOwnerAndTypeAndCurrency(walletOwner.id(), "main", currency.getSymbol()) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id(), + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + on { save(any()) } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id(), + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + } + currencyRepository = mock { + on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + } walletManagerImpl = WalletManagerImpl( walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository ) @@ -105,8 +152,25 @@ private class WalletManagerTest { } @Test - fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking {} + fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { + val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) + assertThat(wallet).isNotNull + assertThat(wallet!!.owner().id()).isEqualTo(walletOwner.id()) + assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) + assertThat(wallet.type()).isEqualTo("main") + } @Test - fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking {} + fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { + val wallet = walletManagerImpl.createWallet( + walletOwner, + Amount(currency, BigDecimal.valueOf(1)), + currency, + "main" + ) + assertThat(wallet).isNotNull + assertThat(wallet.owner().id()).isEqualTo(walletOwner.id()) + assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) + assertThat(wallet.type()).isEqualTo("main") + } } From d6eaea2a4311b6dfec8a4ae8463642ee6a58cdd1 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Mon, 16 May 2022 21:42:43 +0430 Subject: [PATCH 07/58] wallet: Add wallet owner manager tests --- .../opex/wallet/app/WalletOwnerManagerTest.kt | 130 ++++++++++++++++-- 1 file changed, 122 insertions(+), 8 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt index 4bb944f0f..59b99ab80 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt @@ -1,46 +1,160 @@ package co.nilin.opex.wallet.app +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository import co.nilin.opex.wallet.ports.postgres.impl.WalletOwnerManagerImpl +import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel +import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import java.math.BigDecimal private class WalletOwnerManagerTest { @Mock - private lateinit var userLimitsRepository: UserLimitsRepository + private var userLimitsRepository: UserLimitsRepository @Mock - private lateinit var transactionRepository: TransactionRepository + private var transactionRepository: TransactionRepository @Mock - private lateinit var walletOwnerRepository: WalletOwnerRepository + private var walletOwnerRepository: WalletOwnerRepository @Mock - private lateinit var walletConfigRepository: WalletConfigRepository + private var walletConfigRepository: WalletConfigRepository private var walletOwnerManagerImpl: WalletOwnerManagerImpl + private val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + private val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + init { MockitoAnnotations.openMocks(this) + userLimitsRepository = mock { + on { findByOwnerAndAction(anyLong(), eq("withdraw")) } doReturn flow { + emit( + UserLimitsModel( + 1L, + null, + walletOwner.id(), + "withdraw", + "main", + BigDecimal.valueOf(10), + 100, + BigDecimal.valueOf(300), + 3000, + ) + ) + } + } + walletOwnerRepository = mock { + on { save(any()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + )) + } + walletConfigRepository = mock { + on { findAll() } doReturn Flux.just(WalletConfigModel("", "ETH")) + } + transactionRepository = mock { + on { + calculateDepositStatisticsBasedOnCurrency( + anyLong(), + anyString(), + any(), + any(), + anyString() + ) + } doReturn Mono.empty() + on { + calculateWithdrawStatisticsBasedOnCurrency( + anyLong(), + anyString(), + any(), + any(), + anyString() + ) + } doReturn Mono.empty() + } walletOwnerManagerImpl = WalletOwnerManagerImpl( userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository ) } @Test - fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { } + fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) + + assertThat(isAllowed).isTrue() + } @Test - fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { } + fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + val isAllowed = + walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(12))) + + assertThat(isAllowed).isFalse() + } @Test - fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking {} + fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + val wo = walletOwnerManagerImpl.findWalletOwner(walletOwner.uuid()) + + assertThat(wo!!.id()).isEqualTo(walletOwner.id()) + assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) + } @Test - fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking {} + fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + val wo = walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) + + assertThat(wo.id()).isEqualTo(walletOwner.id()) + assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) + } } From 9f82e76bf8b80142dc3598bee8e95947fc6f76ca Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Mon, 16 May 2022 21:49:12 +0430 Subject: [PATCH 08/58] wallet: Add currency service tests --- .../opex/wallet/app/CurrencyServiceTest.kt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt index d6a941f7a..58edcc1fb 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt @@ -2,22 +2,43 @@ package co.nilin.opex.wallet.app import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository import co.nilin.opex.wallet.ports.postgres.impl.CurrencyServiceImpl +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.mockito.Mock import org.mockito.MockitoAnnotations +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import reactor.core.publisher.Mono private class CurrencyServiceTest { @Mock - private lateinit var currencyRepository: CurrencyRepository + private var currencyRepository: CurrencyRepository private var currencyService: CurrencyServiceImpl init { MockitoAnnotations.openMocks(this) + currencyRepository = mock { + on { + findBySymbol("ETH") + } doReturn Mono.just( + CurrencyModel( + "ETH", + "Ethereum", + 0.0001 + ) + ) + } currencyService = CurrencyServiceImpl(currencyRepository) } @Test - fun givenSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking {} + fun givenSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { + val c = currencyService.getCurrency("ETH") + + assertThat(c).isNotNull + assertThat(c!!.getSymbol()).isEqualTo("ETH") + } } From 68cf744a26f8ca89a6864aadd99089cac26b4a6c Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Mon, 16 May 2022 23:05:50 +0430 Subject: [PATCH 09/58] wallet: Add transfer service tests --- .../opex/wallet/app/TransferServiceTest.kt | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt new file mode 100644 index 000000000..e67dadac2 --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt @@ -0,0 +1,136 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.core.inout.TransferCommand +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.core.service.TransferService +import co.nilin.opex.wallet.core.spi.TransactionManager +import co.nilin.opex.wallet.core.spi.WalletListener +import co.nilin.opex.wallet.core.spi.WalletManager +import co.nilin.opex.wallet.core.spi.WalletOwnerManager +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import java.math.BigDecimal + +class TransferServiceTest { + @Mock + private var walletOwnerManager: WalletOwnerManager + + @Mock + private var walletManager: WalletManager + + @Mock + private var walletListener: WalletListener + + @Mock + private var transactionManager: TransactionManager + + private var transferService: TransferService + + private val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + private val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + init { + MockitoAnnotations.openMocks(this) + walletOwnerManager = mock { + on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true + on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true + } + walletManager = mock { + on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true + on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true + on { runBlocking { decreaseBalance(any(), any()) } } doReturn Unit + on { runBlocking { increaseBalance(any(), any()) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + walletListener = mock { + on { runBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } } doReturn Unit + on { runBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } } doReturn Unit + } + transactionManager = mock { + on { runBlocking { save(any()) } } doReturn "1" + } + transferService = TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) + } + + @Test + fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + val result = transferService.transfer(transferCommand).transferResult + assertThat(result).isNotNull + assertThat(result.amount).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) + assertThat(result.sourceUuid).isEqualTo("fdf453d7-0633-4ec7-852d-a18148c99a82") + assertThat(result.destUuid).isEqualTo("e1950578-ef22-44e4-89f5-0b78feb03e2a") + assertThat(result.sourceWalletType).isEqualTo("main") + assertThat(result.destWalletType).isEqualTo("main") + assertThat(result.sourceBalanceBeforeAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1.5))) + assertThat(result.sourceBalanceAfterAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1))) + } +} From a7aa02000d0351fbfaf2e1b46991537912c28e87 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 10:43:58 +0430 Subject: [PATCH 10/58] wallet: Add more tests to wallet manager --- .../opex/wallet/app/WalletManagerTest.kt | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index bbfba986a..a80852d29 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -18,6 +18,7 @@ import org.mockito.Mock import org.mockito.MockitoAnnotations import org.mockito.kotlin.any import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq import org.mockito.kotlin.mock import reactor.core.publisher.Mono import java.math.BigDecimal @@ -106,6 +107,18 @@ private class WalletManagerTest { BigDecimal.valueOf(1.2) ) ) + on { findById(20) } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id(), + "main", + currency.getSymbol(), + BigDecimal.valueOf(0.5) + ) + ) + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(1) } currencyRepository = mock { on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( @@ -115,6 +128,13 @@ private class WalletManagerTest { currency.getPrecision() ) ) + on { findById(currency.getSymbol()) } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) } walletManagerImpl = WalletManagerImpl( walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository @@ -173,4 +193,38 @@ private class WalletManagerTest { assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) assertThat(wallet.type()).isEqualTo("main") } + + @Test + fun givenWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(1)) + } + + @Test + fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(1)) + } + + @Test + fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { + val wallet = walletManagerImpl.findWalletById(20) + + assertThat(wallet).isNotNull + assertThat(wallet!!.id()).isEqualTo(20) + } } From e58bdd13dce9cd773fc9517985319088f0a5223a Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 13:30:10 +0430 Subject: [PATCH 11/58] wallet: Improve update balance test coverage --- .../opex/wallet/app/CurrencyServiceTest.kt | 20 +++++++ .../opex/wallet/app/WalletManagerTest.kt | 56 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt index 58edcc1fb..2f0367006 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt @@ -30,6 +30,12 @@ private class CurrencyServiceTest { 0.0001 ) ) + on { + findBySymbol("") + } doReturn Mono.empty() + on { + findBySymbol("WRONG") + } doReturn Mono.empty() } currencyService = CurrencyServiceImpl(currencyRepository) } @@ -41,4 +47,18 @@ private class CurrencyServiceTest { assertThat(c).isNotNull assertThat(c!!.getSymbol()).isEqualTo("ETH") } + + @Test + fun givenWrongSymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + val c = currencyService.getCurrency("WRONG") + + assertThat(c).isNull() + } + + @Test + fun givenEmptySymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + val c = currencyService.getCurrency("") + + assertThat(c).isNull() + } } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index a80852d29..35b0413e5 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -11,6 +11,7 @@ import co.nilin.opex.wallet.ports.postgres.model.WalletModel import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.anyLong import org.mockito.ArgumentMatchers.anyString @@ -116,6 +117,9 @@ private class WalletManagerTest { BigDecimal.valueOf(0.5) ) ) + on { + updateBalance(any(), any()) + } doReturn Mono.just(0) on { updateBalance(eq(20), any()) } doReturn Mono.just(1) @@ -207,6 +211,32 @@ private class WalletManagerTest { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(1)) } + @Test + fun givenNotExistWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(1)) } } + } + + @Test + fun givenWrongAmount_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(-1)) } } + } + @Test fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { val wallet = object : Wallet { @@ -220,6 +250,32 @@ private class WalletManagerTest { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(1)) } + @Test + fun givenNotExist_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(1)) } } + } + + @Test + fun givenWrongAmount_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(-1)) } } + } + @Test fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.findWalletById(20) From df117408416e20c717f7123858ba7fab5692745c Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 14:30:36 +0430 Subject: [PATCH 12/58] wallet: Improve is withdraw allowed test coverage --- .../opex/wallet/app/WalletManagerTest.kt | 147 +++++++++++++++++- 1 file changed, 143 insertions(+), 4 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index 35b0413e5..a4b504194 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -7,6 +7,7 @@ import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel +import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletModel import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.runBlocking @@ -70,6 +71,57 @@ private class WalletManagerTest { on { findByLevelAndCurrencyAndActionAndWalletType(anyString(), anyString(), anyString(), anyString()) } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndWalletAndAction(eq(2), eq("ETH"), eq(30), eq("withdraw")) + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(eq(2), eq("ETH"), eq("withdraw"), eq("main")) + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType(anyString(), eq("ETH"), eq("withdraw"), eq("main")) + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) } transactionRepository = mock { on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() @@ -160,6 +212,93 @@ private class WalletManagerTest { assertThat(isAllowed).isTrue() } + @Test + fun givenNotExistWallet_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } } + } + + @Test + fun givenWrongCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = object : Currency { + override fun getSymbol() = "WRONG" + override fun getName() = "WRONG" + override fun getPrecision() = 0.001 + } + + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } } + } + + @Test + fun givenInsufficientAmount_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } } + } + + @Test + fun givenWrongAmount_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(-1)) } } + } + + @Test + fun givenWalletWithUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenWalletWithWalletLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(500)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(30)) + + assertThat(isAllowed).isFalse() + } + @Test fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { val wallet = object : Wallet { @@ -212,7 +351,7 @@ private class WalletManagerTest { } @Test - fun givenNotExistWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + fun givenNotExistWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 40L override fun owner() = walletOwner @@ -225,7 +364,7 @@ private class WalletManagerTest { } @Test - fun givenWrongAmount_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + fun givenWrongAmount_whenIncreaseBalance_thenThrow(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -251,7 +390,7 @@ private class WalletManagerTest { } @Test - fun givenNotExist_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + fun givenNotExist_whenDecreaseBalance_thenThrow(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 40L override fun owner() = walletOwner @@ -264,7 +403,7 @@ private class WalletManagerTest { } @Test - fun givenWrongAmount_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + fun givenWrongAmount_whenDecreaseBalance_thenThrow(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner From 577d22932a30aa9b8aca1430dd45f37fb2365881 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 16:18:26 +0430 Subject: [PATCH 13/58] wallet: Add is deposit allowed tests --- .../opex/wallet/app/WalletManagerTest.kt | 320 ++++++++---------- .../opex/wallet/app/WalletManagerTestBase.kt | 193 +++++++++++ 2 files changed, 325 insertions(+), 188 deletions(-) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index a4b504194..9addb1ad4 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -3,200 +3,14 @@ package co.nilin.opex.wallet.app import co.nilin.opex.wallet.core.model.Amount import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.ports.postgres.dao.* -import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl -import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel -import co.nilin.opex.wallet.ports.postgres.model.WalletModel -import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test -import org.mockito.ArgumentMatchers.anyLong -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock -import org.mockito.MockitoAnnotations -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import reactor.core.publisher.Mono import java.math.BigDecimal -private class WalletManagerTest { - @Mock - private var walletLimitsRepository: WalletLimitsRepository - - @Mock - private var transactionRepository: TransactionRepository - - @Mock - private var walletRepository: WalletRepository - - @Mock - private var walletOwnerRepository: WalletOwnerRepository - - @Mock - private var currencyRepository: CurrencyRepository - - private var walletManagerImpl: WalletManagerImpl - - private val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - private val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - init { - MockitoAnnotations.openMocks(this) - walletLimitsRepository = mock { - on { - findByOwnerAndCurrencyAndWalletAndAction(anyLong(), anyString(), anyLong(), anyString()) - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(anyLong(), anyString(), anyString(), anyString()) - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType(anyString(), anyString(), anyString(), anyString()) - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndWalletAndAction(eq(2), eq("ETH"), eq(30), eq("withdraw")) - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(eq(2), eq("ETH"), eq("withdraw"), eq("main")) - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType(anyString(), eq("ETH"), eq("withdraw"), eq("main")) - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - transactionRepository = mock { - on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() - } - walletOwnerRepository = mock { - on { findById(walletOwner.id()) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - walletRepository = mock { - on { - findByOwnerAndTypeAndCurrency(walletOwner.id(), "main", currency.getSymbol()) - } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id(), - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) - on { save(any()) } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id(), - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) - on { findById(20) } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id(), - "main", - currency.getSymbol(), - BigDecimal.valueOf(0.5) - ) - ) - on { - updateBalance(any(), any()) - } doReturn Mono.just(0) - on { - updateBalance(eq(20), any()) - } doReturn Mono.just(1) - } - currencyRepository = mock { - on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( - CurrencyModel( - currency.getSymbol(), - currency.getName(), - currency.getPrecision() - ) - ) - on { findById(currency.getSymbol()) } doReturn Mono.just( - CurrencyModel( - currency.getSymbol(), - currency.getName(), - currency.getPrecision() - ) - ) - } - walletManagerImpl = WalletManagerImpl( - walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository - ) - } - +private class WalletManagerTest : WalletManagerTestBase() { + //region isWithdrawAllowed() @Test fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { val wallet = object : Wallet { @@ -313,7 +127,128 @@ private class WalletManagerTest { assertThat(isAllowed).isFalse() } + //endregion + + //region isDepositAllowed() + @Test + fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0.5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenNotExistWallet_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } } + } + + @Test + fun givenWrongCurrency_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = object : Currency { + override fun getSymbol() = "WRONG" + override fun getName() = "WRONG" + override fun getPrecision() = 0.001 + } + + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } } + } + + @Test + fun givenInsufficientAmount_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } } + } + + @Test + fun givenWrongAmount_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(-1)) } } + } + + @Test + fun givenWalletWithUnreachedLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenWalletWithWalletLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(500)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(30)) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenEmptyWalletWithNoLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isFalse() + } + //endregion + //region findWalletByOwnerAndCurrencyAndType() @Test fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) @@ -322,7 +257,9 @@ private class WalletManagerTest { assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) assertThat(wallet.type()).isEqualTo("main") } + //endregion + //region createWallet() @Test fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.createWallet( @@ -336,7 +273,9 @@ private class WalletManagerTest { assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) assertThat(wallet.type()).isEqualTo("main") } + //endregion + //region increaseBalance() @Test fun givenWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { val wallet = object : Wallet { @@ -375,7 +314,9 @@ private class WalletManagerTest { assertThatThrownBy { runBlocking { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(-1)) } } } + //endregion + //region decreaseBalance() @Test fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { val wallet = object : Wallet { @@ -414,7 +355,9 @@ private class WalletManagerTest { assertThatThrownBy { runBlocking { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(-1)) } } } + //endregion + //region findWalletById() @Test fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.findWalletById(20) @@ -422,4 +365,5 @@ private class WalletManagerTest { assertThat(wallet).isNotNull assertThat(wallet!!.id()).isEqualTo(20) } + //endregion } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt new file mode 100644 index 000000000..c9e420605 --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt @@ -0,0 +1,193 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.* +import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel +import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel +import co.nilin.opex.wallet.ports.postgres.model.WalletModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import reactor.core.publisher.Mono +import java.math.BigDecimal + +internal open class WalletManagerTestBase { + @Mock + protected var walletLimitsRepository: WalletLimitsRepository + + @Mock + protected var transactionRepository: TransactionRepository + + @Mock + protected var walletRepository: WalletRepository + + @Mock + protected var walletOwnerRepository: WalletOwnerRepository + + @Mock + protected var currencyRepository: CurrencyRepository + + protected var walletManagerImpl: WalletManagerImpl + + protected val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + protected val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + init { + MockitoAnnotations.openMocks(this) + walletLimitsRepository = mock { + on { + findByOwnerAndCurrencyAndWalletAndAction(anyLong(), anyString(), anyLong(), anyString()) + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(anyLong(), anyString(), anyString(), anyString()) + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType(anyString(), anyString(), anyString(), anyString()) + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndWalletAndAction(eq(2), eq("ETH"), eq(30), eq("withdraw")) + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(eq(2), eq("ETH"), eq("withdraw"), eq("main")) + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType(anyString(), eq("ETH"), eq("withdraw"), eq("main")) + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + transactionRepository = mock { + on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() + } + walletOwnerRepository = mock { + on { findById(walletOwner.id()!!) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + walletRepository = mock { + on { + findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + on { save(any()) } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + on { findById(20) } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(0.5) + ) + ) + on { + updateBalance(any(), any()) + } doReturn Mono.just(0) + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(1) + } + currencyRepository = mock { + on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + on { findById(currency.getSymbol()) } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + } + walletManagerImpl = WalletManagerImpl( + walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository + ) + } +} From 06004912894f9cf0cb38a58baf5750ce7fec4172 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 16:30:23 +0430 Subject: [PATCH 14/58] wallet: Refactor wallet owner manager --- .../opex/wallet/app/WalletManagerTest.kt | 14 -- .../opex/wallet/app/WalletOwnerManagerTest.kt | 122 +---------------- .../wallet/app/WalletOwnerManagerTestBase.kt | 127 ++++++++++++++++++ 3 files changed, 128 insertions(+), 135 deletions(-) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt index 9addb1ad4..7c81fe408 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt @@ -10,7 +10,6 @@ import org.junit.jupiter.api.Test import java.math.BigDecimal private class WalletManagerTest : WalletManagerTestBase() { - //region isWithdrawAllowed() @Test fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { val wallet = object : Wallet { @@ -127,9 +126,7 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThat(isAllowed).isFalse() } - //endregion - //region isDepositAllowed() @Test fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { val wallet = object : Wallet { @@ -246,9 +243,7 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThat(isAllowed).isFalse() } - //endregion - //region findWalletByOwnerAndCurrencyAndType() @Test fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) @@ -257,9 +252,7 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) assertThat(wallet.type()).isEqualTo("main") } - //endregion - //region createWallet() @Test fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.createWallet( @@ -273,9 +266,7 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) assertThat(wallet.type()).isEqualTo("main") } - //endregion - //region increaseBalance() @Test fun givenWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { val wallet = object : Wallet { @@ -314,9 +305,7 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThatThrownBy { runBlocking { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(-1)) } } } - //endregion - //region decreaseBalance() @Test fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { val wallet = object : Wallet { @@ -355,9 +344,7 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThatThrownBy { runBlocking { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(-1)) } } } - //endregion - //region findWalletById() @Test fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { val wallet = walletManagerImpl.findWalletById(20) @@ -365,5 +352,4 @@ private class WalletManagerTest : WalletManagerTestBase() { assertThat(wallet).isNotNull assertThat(wallet!!.id()).isEqualTo(20) } - //endregion } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt index 59b99ab80..9cd2ab358 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt @@ -1,132 +1,12 @@ package co.nilin.opex.wallet.app import co.nilin.opex.wallet.core.model.Amount -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository -import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository -import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository -import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository -import co.nilin.opex.wallet.ports.postgres.impl.WalletOwnerManagerImpl -import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel -import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel -import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel -import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.mockito.ArgumentMatchers.anyLong -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock -import org.mockito.MockitoAnnotations -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import reactor.core.publisher.Flux -import reactor.core.publisher.Mono import java.math.BigDecimal -private class WalletOwnerManagerTest { - @Mock - private var userLimitsRepository: UserLimitsRepository - - @Mock - private var transactionRepository: TransactionRepository - - @Mock - private var walletOwnerRepository: WalletOwnerRepository - - @Mock - private var walletConfigRepository: WalletConfigRepository - - private var walletOwnerManagerImpl: WalletOwnerManagerImpl - - private val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - private val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - init { - MockitoAnnotations.openMocks(this) - userLimitsRepository = mock { - on { findByOwnerAndAction(anyLong(), eq("withdraw")) } doReturn flow { - emit( - UserLimitsModel( - 1L, - null, - walletOwner.id(), - "withdraw", - "main", - BigDecimal.valueOf(10), - 100, - BigDecimal.valueOf(300), - 3000, - ) - ) - } - } - walletOwnerRepository = mock { - on { save(any()) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - )) - } - walletConfigRepository = mock { - on { findAll() } doReturn Flux.just(WalletConfigModel("", "ETH")) - } - transactionRepository = mock { - on { - calculateDepositStatisticsBasedOnCurrency( - anyLong(), - anyString(), - any(), - any(), - anyString() - ) - } doReturn Mono.empty() - on { - calculateWithdrawStatisticsBasedOnCurrency( - anyLong(), - anyString(), - any(), - any(), - anyString() - ) - } doReturn Mono.empty() - } - walletOwnerManagerImpl = WalletOwnerManagerImpl( - userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository - ) - } - +private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt new file mode 100644 index 000000000..bfa4faddd --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt @@ -0,0 +1,127 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository +import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository +import co.nilin.opex.wallet.ports.postgres.impl.WalletOwnerManagerImpl +import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel +import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel +import kotlinx.coroutines.flow.flow +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import java.math.BigDecimal + +internal open class WalletOwnerManagerTestBase { + @Mock + protected var userLimitsRepository: UserLimitsRepository + + @Mock + protected var transactionRepository: TransactionRepository + + @Mock + protected var walletOwnerRepository: WalletOwnerRepository + + @Mock + protected var walletConfigRepository: WalletConfigRepository + + protected var walletOwnerManagerImpl: WalletOwnerManagerImpl + + protected val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + protected val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + init { + MockitoAnnotations.openMocks(this) + userLimitsRepository = mock { + on { findByOwnerAndAction(anyLong(), eq("withdraw")) } doReturn flow { + emit( + UserLimitsModel( + 1L, + null, + walletOwner.id(), + "withdraw", + "main", + BigDecimal.valueOf(10), + 100, + BigDecimal.valueOf(300), + 3000, + ) + ) + } + } + walletOwnerRepository = mock { + on { save(any()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + walletConfigRepository = mock { + on { findAll() } doReturn Flux.just(WalletConfigModel("", "ETH")) + } + transactionRepository = mock { + on { + calculateDepositStatisticsBasedOnCurrency( + anyLong(), + anyString(), + any(), + any(), + anyString() + ) + } doReturn Mono.empty() + on { + calculateWithdrawStatisticsBasedOnCurrency( + anyLong(), + anyString(), + any(), + any(), + anyString() + ) + } doReturn Mono.empty() + } + walletOwnerManagerImpl = WalletOwnerManagerImpl( + userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository + ) + } +} From add13d708369a080d9fa9bbd7d5c702103bc747e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 16:32:26 +0430 Subject: [PATCH 15/58] wallet: Refactor transfer service tests --- .../opex/wallet/app/TransferServiceTest.kt | 75 +---------------- .../wallet/app/TransferServiceTestBase.kt | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+), 72 deletions(-) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt index e67dadac2..b82f3f3fe 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt @@ -2,85 +2,14 @@ package co.nilin.opex.wallet.app import co.nilin.opex.wallet.core.inout.TransferCommand import co.nilin.opex.wallet.core.model.Amount -import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.core.service.TransferService -import co.nilin.opex.wallet.core.spi.TransactionManager -import co.nilin.opex.wallet.core.spi.WalletListener -import co.nilin.opex.wallet.core.spi.WalletManager -import co.nilin.opex.wallet.core.spi.WalletOwnerManager import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock -import org.mockito.MockitoAnnotations -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.mock import java.math.BigDecimal -class TransferServiceTest { - @Mock - private var walletOwnerManager: WalletOwnerManager - - @Mock - private var walletManager: WalletManager - - @Mock - private var walletListener: WalletListener - - @Mock - private var transactionManager: TransactionManager - - private var transferService: TransferService - - private val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - private val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - init { - MockitoAnnotations.openMocks(this) - walletOwnerManager = mock { - on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true - on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true - } - walletManager = mock { - on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true - on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true - on { runBlocking { decreaseBalance(any(), any()) } } doReturn Unit - on { runBlocking { increaseBalance(any(), any()) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - walletListener = mock { - on { runBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } } doReturn Unit - on { runBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } } doReturn Unit - } - transactionManager = mock { - on { runBlocking { save(any()) } } doReturn "1" - } - transferService = TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) - } - +private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { val sourceWalletOwner = object : WalletOwner { @@ -123,7 +52,9 @@ class TransferServiceTest { null, null ) + val result = transferService.transfer(transferCommand).transferResult + assertThat(result).isNotNull assertThat(result.amount).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) assertThat(result.sourceUuid).isEqualTo("fdf453d7-0633-4ec7-852d-a18148c99a82") diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt new file mode 100644 index 000000000..1ef2844da --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt @@ -0,0 +1,80 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.core.service.TransferService +import co.nilin.opex.wallet.core.spi.TransactionManager +import co.nilin.opex.wallet.core.spi.WalletListener +import co.nilin.opex.wallet.core.spi.WalletManager +import co.nilin.opex.wallet.core.spi.WalletOwnerManager +import kotlinx.coroutines.runBlocking +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import java.math.BigDecimal + +internal open class TransferServiceTestBase { + @Mock + protected var walletOwnerManager: WalletOwnerManager + + @Mock + protected var walletManager: WalletManager + + @Mock + protected var walletListener: WalletListener + + @Mock + protected var transactionManager: TransactionManager + + protected var transferService: TransferService + + protected val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + protected val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + init { + MockitoAnnotations.openMocks(this) + walletOwnerManager = mock { + on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true + on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true + } + walletManager = mock { + on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true + on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true + on { runBlocking { decreaseBalance(any(), any()) } } doReturn Unit + on { runBlocking { increaseBalance(any(), any()) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + walletListener = mock { + on { runBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } } doReturn Unit + on { runBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } } doReturn Unit + } + transactionManager = mock { + on { runBlocking { save(any()) } } doReturn "1" + } + transferService = TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) + } +} From 0bcef902174558bf07e230a7a729d4628e4a6856 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 17 May 2022 16:33:52 +0430 Subject: [PATCH 16/58] wallet: Refactor currency service tests --- .../opex/wallet/app/CurrencyServiceTest.kt | 37 +----------------- .../wallet/app/CurrencyServiceTestBase.kt | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt index 2f0367006..4a64230cb 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt @@ -1,45 +1,10 @@ package co.nilin.opex.wallet.app -import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository -import co.nilin.opex.wallet.ports.postgres.impl.CurrencyServiceImpl -import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.mockito.Mock -import org.mockito.MockitoAnnotations -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.mock -import reactor.core.publisher.Mono - -private class CurrencyServiceTest { - @Mock - private var currencyRepository: CurrencyRepository - - private var currencyService: CurrencyServiceImpl - - init { - MockitoAnnotations.openMocks(this) - currencyRepository = mock { - on { - findBySymbol("ETH") - } doReturn Mono.just( - CurrencyModel( - "ETH", - "Ethereum", - 0.0001 - ) - ) - on { - findBySymbol("") - } doReturn Mono.empty() - on { - findBySymbol("WRONG") - } doReturn Mono.empty() - } - currencyService = CurrencyServiceImpl(currencyRepository) - } +private class CurrencyServiceTest : CurrencyServiceTestBase() { @Test fun givenSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { val c = currencyService.getCurrency("ETH") diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt new file mode 100644 index 000000000..13cbdf5aa --- /dev/null +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt @@ -0,0 +1,39 @@ +package co.nilin.opex.wallet.app + +import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository +import co.nilin.opex.wallet.ports.postgres.impl.CurrencyServiceImpl +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import reactor.core.publisher.Mono + +internal open class CurrencyServiceTestBase { + @Mock + protected var currencyRepository: CurrencyRepository + + protected var currencyService: CurrencyServiceImpl + + init { + MockitoAnnotations.openMocks(this) + currencyRepository = mock { + on { + findBySymbol("ETH") + } doReturn Mono.just( + CurrencyModel( + "ETH", + "Ethereum", + 0.0001 + ) + ) + on { + findBySymbol("") + } doReturn Mono.empty() + on { + findBySymbol("WRONG") + } doReturn Mono.empty() + } + currencyService = CurrencyServiceImpl(currencyRepository) + } +} From 3366d3086032176773483afe9736ba5a0c90901d Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 18 May 2022 14:34:29 +0430 Subject: [PATCH 17/58] wallet: Organize tests --- .../opex/wallet/{app => core/service}/TransferServiceTest.kt | 2 +- .../wallet/{app => core/service}/TransferServiceTestBase.kt | 3 +-- .../wallet/{app => ports/postgres/impl}/CurrencyServiceTest.kt | 2 +- .../{app => ports/postgres/impl}/CurrencyServiceTestBase.kt | 3 +-- .../wallet/{app => ports/postgres/impl}/WalletManagerTest.kt | 2 +- .../{app => ports/postgres/impl}/WalletManagerTestBase.kt | 3 +-- .../{app => ports/postgres/impl}/WalletOwnerManagerTest.kt | 2 +- .../{app => ports/postgres/impl}/WalletOwnerManagerTestBase.kt | 3 +-- 8 files changed, 8 insertions(+), 12 deletions(-) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => core/service}/TransferServiceTest.kt (98%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => core/service}/TransferServiceTestBase.kt (97%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => ports/postgres/impl}/CurrencyServiceTest.kt (94%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => ports/postgres/impl}/CurrencyServiceTestBase.kt (91%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => ports/postgres/impl}/WalletManagerTest.kt (99%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => ports/postgres/impl}/WalletManagerTestBase.kt (98%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => ports/postgres/impl}/WalletOwnerManagerTest.kt (96%) rename wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/{app => ports/postgres/impl}/WalletOwnerManagerTestBase.kt (97%) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt similarity index 98% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt index b82f3f3fe..f7931582e 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -1,4 +1,4 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.core.service import co.nilin.opex.wallet.core.inout.TransferCommand import co.nilin.opex.wallet.core.model.Amount diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt similarity index 97% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt index 1ef2844da..8c0762ecc 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/TransferServiceTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt @@ -1,10 +1,9 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.core.service import co.nilin.opex.wallet.core.model.Amount import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.core.service.TransferService import co.nilin.opex.wallet.core.spi.TransactionManager import co.nilin.opex.wallet.core.spi.WalletListener import co.nilin.opex.wallet.core.spi.WalletManager diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt similarity index 94% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt index 4a64230cb..3f2075d21 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt @@ -1,4 +1,4 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.ports.postgres.impl import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt similarity index 91% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt index 13cbdf5aa..11b2d35a9 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/CurrencyServiceTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt @@ -1,7 +1,6 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository -import co.nilin.opex.wallet.ports.postgres.impl.CurrencyServiceImpl import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import org.mockito.Mock import org.mockito.MockitoAnnotations diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt similarity index 99% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index 7c81fe408..1bb755f3a 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -1,4 +1,4 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount import co.nilin.opex.wallet.core.model.Currency diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt similarity index 98% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt index c9e420605..e2145022b 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt @@ -1,9 +1,8 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* -import co.nilin.opex.wallet.ports.postgres.impl.WalletManagerImpl import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletModel diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt similarity index 96% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index 9cd2ab358..1ecdc3d08 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -1,4 +1,4 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount import kotlinx.coroutines.runBlocking diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt similarity index 97% rename from wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt rename to wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt index bfa4faddd..1a61117c3 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/app/WalletOwnerManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt @@ -1,4 +1,4 @@ -package co.nilin.opex.wallet.app +package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.WalletOwner @@ -6,7 +6,6 @@ import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository -import co.nilin.opex.wallet.ports.postgres.impl.WalletOwnerManagerImpl import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel From fd9bd09e48610b2295f0c9883212ad26d5a4270d Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 18 May 2022 17:05:16 +0430 Subject: [PATCH 18/58] wallet: Improve wallet manager tests --- .../postgres/impl/CurrencyServiceTest.kt | 2 + .../ports/postgres/impl/WalletManagerTest.kt | 376 +++++++++++++++++- .../postgres/impl/WalletManagerTestBase.kt | 139 +------ 3 files changed, 379 insertions(+), 138 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt index 3f2075d21..c11c12e1b 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt @@ -11,6 +11,8 @@ private class CurrencyServiceTest : CurrencyServiceTestBase() { assertThat(c).isNotNull assertThat(c!!.getSymbol()).isEqualTo("ETH") + assertThat(c.getName()).isEqualTo("Ethereum") + assertThat(c.getPrecision()).isEqualTo(0.0001) } @Test diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index 1bb755f3a..7e1f7e693 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -3,15 +3,34 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel +import co.nilin.opex.wallet.ports.postgres.model.WalletModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Mono import java.math.BigDecimal private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -57,7 +76,7 @@ private class WalletManagerTest : WalletManagerTestBase() { } @Test - fun givenInsufficientAmount_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + fun givenInsufficientAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -66,7 +85,9 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } } + val isAllowed = runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } + + assertThat(isAllowed).isFalse() } @Test @@ -84,6 +105,59 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWalletWithUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } val wallet = object : Wallet { override fun id() = 30L override fun owner() = walletOwner @@ -99,6 +173,59 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWalletWithWalletLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } val wallet = object : Wallet { override fun id() = 30L override fun owner() = walletOwner @@ -114,6 +241,17 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -129,6 +267,17 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.empty() + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -174,7 +323,7 @@ private class WalletManagerTest : WalletManagerTestBase() { } @Test - fun givenInsufficientAmount_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + fun givenInsufficientAmount_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -182,8 +331,9 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun currency() = currency override fun type() = "main" } + val isAllowed = runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } - assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } } + assertThat(isAllowed).isFalse() } @Test @@ -201,6 +351,59 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWalletWithUnreachedLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } val wallet = object : Wallet { override fun id() = 30L override fun owner() = walletOwner @@ -216,6 +419,59 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWalletWithWalletLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } val wallet = object : Wallet { override fun id() = 30L override fun owner() = walletOwner @@ -231,6 +487,17 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenEmptyWalletWithNoLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.empty() + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -246,7 +513,34 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { + stubbing(walletOwnerRepository) { + on { findById(walletOwner.id()!!) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + stubbing(walletRepository) { + on { + findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + } val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) + assertThat(wallet).isNotNull assertThat(wallet!!.owner().id()).isEqualTo(walletOwner.id()) assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) @@ -255,12 +549,45 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { + stubbing(walletRepository) { + on { + findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + on { + save( + WalletModel( + null, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + } val wallet = walletManagerImpl.createWallet( walletOwner, Amount(currency, BigDecimal.valueOf(1)), currency, "main" ) + assertThat(wallet).isNotNull assertThat(wallet.owner().id()).isEqualTo(walletOwner.id()) assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) @@ -269,6 +596,11 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(1) + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -282,6 +614,11 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenNotExistWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(any(), any()) + } doReturn Mono.just(0) + } val wallet = object : Wallet { override fun id() = 40L override fun owner() = walletOwner @@ -295,6 +632,11 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWrongAmount_whenIncreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(0) + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -308,6 +650,11 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(1) + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -321,6 +668,11 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenNotExist_whenDecreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(any(), any()) + } doReturn Mono.just(0) + } val wallet = object : Wallet { override fun id() = 40L override fun owner() = walletOwner @@ -334,6 +686,11 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWrongAmount_whenDecreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(0) + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -347,6 +704,17 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { + stubbing(walletRepository) { + on { findById(20) } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(0.5) + ) + ) + } val wallet = walletManagerImpl.findWalletById(20) assertThat(wallet).isNotNull diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt index e2145022b..547e77fd5 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt @@ -4,36 +4,19 @@ import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel -import co.nilin.opex.wallet.ports.postgres.model.WalletModel -import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import org.mockito.ArgumentMatchers.anyLong -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock import org.mockito.MockitoAnnotations import org.mockito.kotlin.any import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq import org.mockito.kotlin.mock import reactor.core.publisher.Mono -import java.math.BigDecimal internal open class WalletManagerTestBase { - @Mock - protected var walletLimitsRepository: WalletLimitsRepository - - @Mock - protected var transactionRepository: TransactionRepository - - @Mock - protected var walletRepository: WalletRepository - - @Mock - protected var walletOwnerRepository: WalletOwnerRepository - - @Mock - protected var currencyRepository: CurrencyRepository - + protected var walletLimitsRepository: WalletLimitsRepository = mock() + protected var transactionRepository: TransactionRepository = mock() + protected var walletRepository: WalletRepository = mock() + protected var walletOwnerRepository: WalletOwnerRepository = mock() + protected var currencyRepository: CurrencyRepository = mock() protected var walletManagerImpl: WalletManagerImpl protected val walletOwner = object : WalletOwner { @@ -54,121 +37,9 @@ internal open class WalletManagerTestBase { init { MockitoAnnotations.openMocks(this) - walletLimitsRepository = mock { - on { - findByOwnerAndCurrencyAndWalletAndAction(anyLong(), anyString(), anyLong(), anyString()) - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(anyLong(), anyString(), anyString(), anyString()) - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType(anyString(), anyString(), anyString(), anyString()) - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndWalletAndAction(eq(2), eq("ETH"), eq(30), eq("withdraw")) - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(eq(2), eq("ETH"), eq("withdraw"), eq("main")) - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType(anyString(), eq("ETH"), eq("withdraw"), eq("main")) - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } transactionRepository = mock { on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() } - walletOwnerRepository = mock { - on { findById(walletOwner.id()!!) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - walletRepository = mock { - on { - findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) - } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) - on { save(any()) } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) - on { findById(20) } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(0.5) - ) - ) - on { - updateBalance(any(), any()) - } doReturn Mono.just(0) - on { - updateBalance(eq(20), any()) - } doReturn Mono.just(1) - } currencyRepository = mock { on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( CurrencyModel( From 522ab4651ab6a78cf65e20103e84f78815f558db Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 18 May 2022 17:07:20 +0430 Subject: [PATCH 19/58] wallet: Improve currency service tests --- .../postgres/impl/CurrencyServiceTestBase.kt | 41 +++++++------------ .../postgres/impl/WalletManagerTestBase.kt | 2 - 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt index 11b2d35a9..dc67531f8 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt @@ -2,37 +2,24 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import org.mockito.Mock -import org.mockito.MockitoAnnotations import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import reactor.core.publisher.Mono internal open class CurrencyServiceTestBase { - @Mock - protected var currencyRepository: CurrencyRepository - - protected var currencyService: CurrencyServiceImpl - - init { - MockitoAnnotations.openMocks(this) - currencyRepository = mock { - on { - findBySymbol("ETH") - } doReturn Mono.just( - CurrencyModel( - "ETH", - "Ethereum", - 0.0001 - ) - ) - on { - findBySymbol("") - } doReturn Mono.empty() - on { - findBySymbol("WRONG") - } doReturn Mono.empty() - } - currencyService = CurrencyServiceImpl(currencyRepository) + protected var currencyRepository: CurrencyRepository = mock { + on { + findBySymbol("ETH") + } doReturn Mono.just( + CurrencyModel("ETH", "Ethereum", 0.0001) + ) + on { + findBySymbol("") + } doReturn Mono.empty() + on { + findBySymbol("WRONG") + } doReturn Mono.empty() } + + protected var currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt index 547e77fd5..e547d4801 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt @@ -5,7 +5,6 @@ import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import org.mockito.ArgumentMatchers.anyLong -import org.mockito.MockitoAnnotations import org.mockito.kotlin.any import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock @@ -36,7 +35,6 @@ internal open class WalletManagerTestBase { } init { - MockitoAnnotations.openMocks(this) transactionRepository = mock { on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() } From f69ad23d58f2290b44c50d87a41ef9d354bca478 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 18 May 2022 17:30:10 +0430 Subject: [PATCH 20/58] wallet: Improve wallet owner manager tests --- .../postgres/impl/WalletOwnerManagerTest.kt | 70 +++++++++++- .../impl/WalletOwnerManagerTestBase.kt | 105 ++---------------- 2 files changed, 76 insertions(+), 99 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index 1ecdc3d08..37f4bd232 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -1,14 +1,28 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono import java.math.BigDecimal private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(anyLong(), eq("withdraw")) } doReturn flow { } + } val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) assertThat(isAllowed).isTrue() @@ -16,6 +30,33 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency( + anyLong(), + anyString(), + any(), + any(), + anyString() + ) + } doReturn Mono.empty() + on { + calculateWithdrawStatisticsBasedOnCurrency( + anyLong(), + anyString(), + any(), + any(), + anyString() + ) + } doReturn Mono.empty() + } val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(12))) @@ -24,6 +65,19 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + stubbing(walletOwnerRepository) { + on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } val wo = walletOwnerManagerImpl.findWalletOwner(walletOwner.uuid()) assertThat(wo!!.id()).isEqualTo(walletOwner.id()) @@ -32,7 +86,21 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { - val wo = walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) + stubbing(walletOwnerRepository) { + on { save(any()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + val wo = + walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) assertThat(wo.id()).isEqualTo(walletOwner.id()) assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt index 1a61117c3..cd5bd1bd0 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt @@ -6,36 +6,16 @@ import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository -import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel -import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel -import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel -import kotlinx.coroutines.flow.flow -import org.mockito.ArgumentMatchers.anyLong -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock -import org.mockito.MockitoAnnotations -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq import org.mockito.kotlin.mock -import reactor.core.publisher.Flux -import reactor.core.publisher.Mono -import java.math.BigDecimal internal open class WalletOwnerManagerTestBase { - @Mock - protected var userLimitsRepository: UserLimitsRepository - - @Mock - protected var transactionRepository: TransactionRepository - - @Mock - protected var walletOwnerRepository: WalletOwnerRepository - - @Mock - protected var walletConfigRepository: WalletConfigRepository - - protected var walletOwnerManagerImpl: WalletOwnerManagerImpl + protected var userLimitsRepository: UserLimitsRepository = mock() + protected var transactionRepository: TransactionRepository = mock() + protected var walletOwnerRepository: WalletOwnerRepository = mock() + protected var walletConfigRepository: WalletConfigRepository = mock() + protected var walletOwnerManagerImpl: WalletOwnerManagerImpl = WalletOwnerManagerImpl( + userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository + ) protected val walletOwner = object : WalletOwner { override fun id() = 2L @@ -52,75 +32,4 @@ internal open class WalletOwnerManagerTestBase { override fun getName() = "Ethereum" override fun getPrecision() = 0.0001 } - - init { - MockitoAnnotations.openMocks(this) - userLimitsRepository = mock { - on { findByOwnerAndAction(anyLong(), eq("withdraw")) } doReturn flow { - emit( - UserLimitsModel( - 1L, - null, - walletOwner.id(), - "withdraw", - "main", - BigDecimal.valueOf(10), - 100, - BigDecimal.valueOf(300), - 3000, - ) - ) - } - } - walletOwnerRepository = mock { - on { save(any()) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - walletConfigRepository = mock { - on { findAll() } doReturn Flux.just(WalletConfigModel("", "ETH")) - } - transactionRepository = mock { - on { - calculateDepositStatisticsBasedOnCurrency( - anyLong(), - anyString(), - any(), - any(), - anyString() - ) - } doReturn Mono.empty() - on { - calculateWithdrawStatisticsBasedOnCurrency( - anyLong(), - anyString(), - any(), - any(), - anyString() - ) - } doReturn Mono.empty() - } - walletOwnerManagerImpl = WalletOwnerManagerImpl( - userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository - ) - } } From 3730b13727404818aea5a5fcc68ffe47953a2a1c Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Wed, 18 May 2022 20:03:08 +0430 Subject: [PATCH 21/58] wallet: Improve wallet owner manager tests --- .../postgres/impl/WalletOwnerManagerTest.kt | 204 ++++++++++++++++-- 1 file changed, 187 insertions(+), 17 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index 37f4bd232..edb2f80ad 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -1,6 +1,7 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.flow.flow @@ -19,9 +20,18 @@ import java.math.BigDecimal private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test - fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenOwnerWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(anyLong(), eq("withdraw")) } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() } val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) @@ -29,40 +39,200 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { } @Test - fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenWrongAmount_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} } stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("", "ETH")) + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) } stubbing(transactionRepository) { on { - calculateDepositStatisticsBasedOnCurrency( - anyLong(), - anyString(), - any(), - any(), - anyString() + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(-5))) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenOwnerWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { + emit( + UserLimitsModel( + 1, + null, + walletOwner.id()!!, + "withdraw", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) ) + } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenLevelWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { + emit( + UserLimitsModel( + 1, + "1", + null, + "withdraw", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { on { - calculateWithdrawStatisticsBasedOnCurrency( - anyLong(), - anyString(), - any(), - any(), - anyString() + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenOwnerWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenWrongAmount_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(-5))) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenOwnerWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { + emit( + UserLimitsModel( + 1, + null, + walletOwner.id()!!, + "deposit", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) ) + } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } val isAllowed = - walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(12))) + walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) assertThat(isAllowed).isFalse() } + @Test + fun givenLevelWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { + emit( + UserLimitsModel( + 1, + "1", + null, + "deposit", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + } + @Test fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { stubbing(walletOwnerRepository) { From d590bfa77770446cc8c1596a88e9b5d870edc203 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Wed, 18 May 2022 23:05:31 +0430 Subject: [PATCH 22/58] wallet: Improve wallet manager tests --- .../ports/postgres/impl/WalletManagerTest.kt | 124 +++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index 7e1f7e693..da6aa95fc 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -104,7 +104,127 @@ private class WalletManagerTest : WalletManagerTestBase() { } @Test - fun givenWalletWithUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenOwnerAndWalletTypeLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenOwnerAndWalletLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenLevelAndWalletTypeLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenAllUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") @@ -172,7 +292,7 @@ private class WalletManagerTest : WalletManagerTestBase() { } @Test - fun givenWalletWithWalletLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenAllLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") From 9b6edc2c6cfc3f012731cc6fdb34645a639d13cb Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Wed, 18 May 2022 23:52:46 +0430 Subject: [PATCH 23/58] wallet: Improve transfer service tests --- .../core/service/TransferServiceTest.kt | 486 ++++++++++++++++++ .../core/service/TransferServiceTestBase.kt | 57 +- 2 files changed, 492 insertions(+), 51 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt index f7931582e..05d1c5f3d 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -6,12 +6,42 @@ import co.nilin.opex.wallet.core.model.Wallet import co.nilin.opex.wallet.core.model.WalletOwner import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyString +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.stubbing import java.math.BigDecimal private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { + stubbing(walletOwnerManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + } + stubbing(walletManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { runBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } } doReturn Unit + on { runBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } } doReturn Unit + } + stubbing(transactionManager) { + on { runBlocking { save(any()) } } doReturn "1" + } val sourceWalletOwner = object : WalletOwner { override fun id() = 2L override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" @@ -64,4 +94,460 @@ private class TransferServiceTest : TransferServiceTestBase() { assertThat(result.sourceBalanceBeforeAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1.5))) assertThat(result.sourceBalanceAfterAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1))) } + + @Test + fun givenOwnerNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + on { + runBlocking { + isWithdrawAllowed( + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))) + ) + } + } doReturn false + on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + } + stubbing(walletManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + on { runBlocking { save(any()) } } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenWalletNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + } + stubbing(walletManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn false + on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + on { runBlocking { save(any()) } } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenOwnerNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn false + } + stubbing(walletManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + on { runBlocking { save(any()) } } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenWalletNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + } + stubbing(walletManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn false + on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + on { runBlocking { save(any()) } } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenNotExistWallet_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + } + stubbing(walletManager) { + on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true + on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit + on { runBlocking { findWalletById(20L) } } doReturn null + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + on { runBlocking { save(any()) } } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt index 8c0762ecc..a4e649dbe 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt @@ -1,36 +1,20 @@ package co.nilin.opex.wallet.core.service -import co.nilin.opex.wallet.core.model.Amount import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.Wallet import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.core.spi.TransactionManager import co.nilin.opex.wallet.core.spi.WalletListener import co.nilin.opex.wallet.core.spi.WalletManager import co.nilin.opex.wallet.core.spi.WalletOwnerManager -import kotlinx.coroutines.runBlocking -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock -import org.mockito.MockitoAnnotations -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock -import java.math.BigDecimal internal open class TransferServiceTestBase { - @Mock - protected var walletOwnerManager: WalletOwnerManager - - @Mock - protected var walletManager: WalletManager - - @Mock - protected var walletListener: WalletListener - - @Mock - protected var transactionManager: TransactionManager - - protected var transferService: TransferService + protected var walletOwnerManager: WalletOwnerManager = mock() + protected var walletManager: WalletManager = mock() + protected var walletListener: WalletListener = mock() + protected var transactionManager: TransactionManager = mock() + protected var transferService: TransferService = + TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) protected val currency = object : Currency { override fun getSymbol() = "ETH" @@ -47,33 +31,4 @@ internal open class TransferServiceTestBase { override fun isWithdrawAllowed() = true override fun isDepositAllowed() = true } - - init { - MockitoAnnotations.openMocks(this) - walletOwnerManager = mock { - on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true - on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true - } - walletManager = mock { - on { runBlocking { isWithdrawAllowed(any(), any()) } } doReturn true - on { runBlocking { isDepositAllowed(any(), any()) } } doReturn true - on { runBlocking { decreaseBalance(any(), any()) } } doReturn Unit - on { runBlocking { increaseBalance(any(), any()) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - walletListener = mock { - on { runBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } } doReturn Unit - on { runBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } } doReturn Unit - } - transactionManager = mock { - on { runBlocking { save(any()) } } doReturn "1" - } - transferService = TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) - } } From dad916060245f27ce4c2315275de9aaec607f9c5 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 07:04:01 +0430 Subject: [PATCH 24/58] wallet: Improve wallet manager tests --- .../ports/postgres/impl/WalletManagerTest.kt | 78 ++++++++++++------- .../postgres/impl/WalletManagerTestBase.kt | 41 ++++------ 2 files changed, 65 insertions(+), 54 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index da6aa95fc..2c2850d76 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -10,10 +10,7 @@ import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.stubbing +import org.mockito.kotlin.* import reactor.core.publisher.Mono import java.math.BigDecimal @@ -444,6 +441,17 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenInsufficientAmount_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -453,6 +461,24 @@ private class WalletManagerTest : WalletManagerTestBase() { } val isAllowed = runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } + verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndWalletAndAction( + walletOwner.id()!!, + "ETH", + 20, + "withdraw" + ) + verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndActionAndWalletType( + walletOwner.id()!!, + "ETH", + "withdraw", + "main" + ) + verify(walletLimitsRepository, never()).findByLevelAndCurrencyAndActionAndWalletType( + "1", + "ETH", + "withdraw", + "main" + ) assertThat(isAllowed).isFalse() } @@ -671,33 +697,14 @@ private class WalletManagerTest : WalletManagerTestBase() { fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { stubbing(walletRepository) { on { - findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) - } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) - on { - save( - WalletModel( - null, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) + save(WalletModel(null, walletOwner.id()!!, "main", currency.getSymbol(), BigDecimal.valueOf(1))) } doReturn Mono.just( WalletModel( 20L, walletOwner.id()!!, "main", currency.getSymbol(), - BigDecimal.valueOf(1.2) + BigDecimal.valueOf(1) ) ) } @@ -771,9 +778,7 @@ private class WalletManagerTest : WalletManagerTestBase() { @Test fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { stubbing(walletRepository) { - on { - updateBalance(eq(20), any()) - } doReturn Mono.just(1) + on { updateBalance(eq(20), eq(BigDecimal.valueOf(-1))) } doReturn Mono.just(1) } val wallet = object : Wallet { override fun id() = 20L @@ -835,9 +840,26 @@ private class WalletManagerTest : WalletManagerTestBase() { ) ) } + stubbing(walletOwnerRepository) { + on { + findById(walletOwner.id()!!) + } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } val wallet = walletManagerImpl.findWalletById(20) assertThat(wallet).isNotNull assertThat(wallet!!.id()).isEqualTo(20) + assertThat(wallet.balance()).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) + assertThat(wallet.currency().getSymbol()).isEqualTo("ETH") } } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt index e547d4801..15367c70f 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt @@ -4,19 +4,20 @@ import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import org.mockito.ArgumentMatchers.anyLong import org.mockito.kotlin.any import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq import org.mockito.kotlin.mock import reactor.core.publisher.Mono internal open class WalletManagerTestBase { protected var walletLimitsRepository: WalletLimitsRepository = mock() - protected var transactionRepository: TransactionRepository = mock() protected var walletRepository: WalletRepository = mock() protected var walletOwnerRepository: WalletOwnerRepository = mock() - protected var currencyRepository: CurrencyRepository = mock() - protected var walletManagerImpl: WalletManagerImpl + + protected var transactionRepository: TransactionRepository = mock { + on { calculateWithdrawStatistics(eq(2), eq(20), any(), any()) } doReturn Mono.empty() + } protected val walletOwner = object : WalletOwner { override fun id() = 2L @@ -34,28 +35,16 @@ internal open class WalletManagerTestBase { override fun getPrecision() = 0.0001 } - init { - transactionRepository = mock { - on { calculateWithdrawStatistics(anyLong(), anyLong(), any(), any()) } doReturn Mono.empty() - } - currencyRepository = mock { - on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( - CurrencyModel( - currency.getSymbol(), - currency.getName(), - currency.getPrecision() - ) - ) - on { findById(currency.getSymbol()) } doReturn Mono.just( - CurrencyModel( - currency.getSymbol(), - currency.getName(), - currency.getPrecision() - ) - ) - } - walletManagerImpl = WalletManagerImpl( - walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository + protected var currencyRepository: CurrencyRepository = mock { + on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( + CurrencyModel(currency.getSymbol(), currency.getName(), currency.getPrecision()) + ) + on { findById(currency.getSymbol()) } doReturn Mono.just( + CurrencyModel(currency.getSymbol(), currency.getName(), currency.getPrecision()) ) } + + protected var walletManagerImpl: WalletManagerImpl = WalletManagerImpl( + walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository + ) } From a8be03ce9e3adeb7a255bf376d63eb3645939003 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 08:37:27 +0430 Subject: [PATCH 25/58] wallet: Use onBlocking in tests to support suspend functions --- .../core/service/TransferServiceTest.kt | 129 +++++++++--------- 1 file changed, 62 insertions(+), 67 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt index 05d1c5f3d..27a766fcb 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -19,15 +19,15 @@ private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { stubbing(walletOwnerManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true } stubbing(walletManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(1)) @@ -36,11 +36,11 @@ private class TransferServiceTest : TransferServiceTestBase() { } } stubbing(walletListener) { - on { runBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } } doReturn Unit - on { runBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } } doReturn Unit + onBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } doReturn Unit + onBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } doReturn Unit } stubbing(transactionManager) { - on { runBlocking { save(any()) } } doReturn "1" + onBlocking { save(any()) } doReturn "1" } val sourceWalletOwner = object : WalletOwner { override fun id() = 2L @@ -98,22 +98,17 @@ private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenOwnerNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { - on { - runBlocking { - isWithdrawAllowed( - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))) - ) - } + onBlocking { + isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn false - on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true } stubbing(walletManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(1)) @@ -147,7 +142,7 @@ private class TransferServiceTest : TransferServiceTestBase() { } doReturn Unit } stubbing(transactionManager) { - on { runBlocking { save(any()) } } doReturn "1" + onBlocking { save(any()) } doReturn "1" } val sourceWalletOwner = object : WalletOwner { override fun id() = 2L @@ -196,15 +191,15 @@ private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenWalletNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true } stubbing(walletManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn false - on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn false + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(1)) @@ -238,7 +233,7 @@ private class TransferServiceTest : TransferServiceTestBase() { } doReturn Unit } stubbing(transactionManager) { - on { runBlocking { save(any()) } } doReturn "1" + onBlocking { save(any()) } doReturn "1" } val sourceWalletOwner = object : WalletOwner { override fun id() = 2L @@ -287,15 +282,15 @@ private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenOwnerNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn false + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn false } stubbing(walletManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(1)) @@ -329,7 +324,7 @@ private class TransferServiceTest : TransferServiceTestBase() { } doReturn Unit } stubbing(transactionManager) { - on { runBlocking { save(any()) } } doReturn "1" + onBlocking { save(any()) } doReturn "1" } val sourceWalletOwner = object : WalletOwner { override fun id() = 2L @@ -378,15 +373,15 @@ private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenWalletNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true } stubbing(walletManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn false - on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn object : Wallet { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn false + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(1)) @@ -420,7 +415,7 @@ private class TransferServiceTest : TransferServiceTestBase() { } doReturn Unit } stubbing(transactionManager) { - on { runBlocking { save(any()) } } doReturn "1" + onBlocking { save(any()) } doReturn "1" } val sourceWalletOwner = object : WalletOwner { override fun id() = 2L @@ -469,27 +464,25 @@ private class TransferServiceTest : TransferServiceTestBase() { @Test fun givenNotExistWallet_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } } doReturn true + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true } stubbing(walletManager) { - on { runBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn true - on { runBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } } doReturn Unit - on { runBlocking { findWalletById(20L) } } doReturn null + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn null } stubbing(walletListener) { - on { - runBlocking { - onWithdraw( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - anyString(), - any() - ) - } + onBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) } doReturn Unit on { runBlocking { @@ -504,9 +497,11 @@ private class TransferServiceTest : TransferServiceTestBase() { } } doReturn Unit } - stubbing(transactionManager) { - on { runBlocking { save(any()) } } doReturn "1" + stubbing(transactionManager) + { + onBlocking { save(any()) } doReturn "1" } + val sourceWalletOwner = object : WalletOwner { override fun id() = 2L override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" From 48476d30cb325240c1673e4df1c78f749f866756 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 09:29:25 +0430 Subject: [PATCH 26/58] wallet: Refactor tests --- .../core/service/TransferServiceTest.kt | 4 +- .../postgres/impl/CurrencyServiceTest.kt | 19 ++- .../postgres/impl/CurrencyServiceTestBase.kt | 18 +-- .../ports/postgres/impl/WalletManagerTest.kt | 153 ++++++++++++++---- .../postgres/impl/WalletManagerTestBase.kt | 12 +- .../postgres/impl/WalletOwnerManagerTest.kt | 54 +++---- 6 files changed, 169 insertions(+), 91 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt index 27a766fcb..53db6e1f7 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -497,11 +497,9 @@ private class TransferServiceTest : TransferServiceTestBase() { } } doReturn Unit } - stubbing(transactionManager) - { + stubbing(transactionManager) { onBlocking { save(any()) } doReturn "1" } - val sourceWalletOwner = object : WalletOwner { override fun id() = 2L override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt index c11c12e1b..3db33151f 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt @@ -1,12 +1,19 @@ package co.nilin.opex.wallet.ports.postgres.impl +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Mono private class CurrencyServiceTest : CurrencyServiceTestBase() { @Test - fun givenSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { + fun givenCorrectSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol("ETH") } doReturn Mono.just(CurrencyModel("ETH", "Ethereum", 0.0001)) + } val c = currencyService.getCurrency("ETH") assertThat(c).isNotNull @@ -16,14 +23,20 @@ private class CurrencyServiceTest : CurrencyServiceTestBase() { } @Test - fun givenWrongSymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { - val c = currencyService.getCurrency("WRONG") + fun givenNotExistCurrency_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol("ETH") } doReturn Mono.empty() + } + val c = currencyService.getCurrency("ETH") assertThat(c).isNull() } @Test fun givenEmptySymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol("") } doReturn Mono.empty() + } val c = currencyService.getCurrency("") assertThat(c).isNull() diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt index dc67531f8..03a712ffb 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt @@ -1,25 +1,9 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository -import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock -import reactor.core.publisher.Mono internal open class CurrencyServiceTestBase { - protected var currencyRepository: CurrencyRepository = mock { - on { - findBySymbol("ETH") - } doReturn Mono.just( - CurrencyModel("ETH", "Ethereum", 0.0001) - ) - on { - findBySymbol("") - } doReturn Mono.empty() - on { - findBySymbol("WRONG") - } doReturn Mono.empty() - } - + protected var currencyRepository: CurrencyRepository = mock { } protected var currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) } diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index 2c2850d76..ff1cf65ce 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -1,8 +1,8 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount -import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletModel import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel @@ -16,7 +16,7 @@ import java.math.BigDecimal private class WalletManagerTest : WalletManagerTestBase() { @Test - fun givenFullWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") @@ -51,25 +51,38 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.isWithdrawAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test - fun givenWrongCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + fun givenNotExistCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol(currency.getSymbol()) } doReturn Mono.empty() + on { findById(currency.getSymbol()) } doReturn Mono.empty() + } val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = object : Currency { - override fun getSymbol() = "WRONG" - override fun getName() = "WRONG" - override fun getPrecision() = 0.001 - } - + override fun currency() = currency override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.isWithdrawAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -82,7 +95,7 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - val isAllowed = runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) } + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) assertThat(isAllowed).isFalse() } @@ -97,7 +110,14 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(-1)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.isWithdrawAllowed( + wallet, + BigDecimal.valueOf(-1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -418,7 +438,14 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.isDepositAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -427,16 +454,18 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = object : Currency { - override fun getSymbol() = "WRONG" - override fun getName() = "WRONG" - override fun getPrecision() = 0.001 - } - + override fun currency() = currency override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.isDepositAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -685,6 +714,17 @@ private class WalletManagerTest : WalletManagerTestBase() { ) ) } + stubbing(currencyRepository) { + on { + findBySymbol(currency.getSymbol()) + } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + } val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) assertThat(wallet).isNotNull @@ -736,14 +776,21 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(1)) + assertThatThrownBy { + runBlocking { + walletManagerImpl.increaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.doesNotThrowAnyException() } @Test fun givenNotExistWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { stubbing(walletRepository) { on { - updateBalance(any(), any()) + updateBalance(any(), eq(BigDecimal.valueOf(1))) } doReturn Mono.just(0) } val wallet = object : Wallet { @@ -754,7 +801,14 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(1)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.increaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -772,7 +826,14 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.increaseBalance(wallet, BigDecimal.valueOf(-1)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.increaseBalance( + wallet, + BigDecimal.valueOf(-1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -788,14 +849,21 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(1)) + assertThatThrownBy { + runBlocking { + walletManagerImpl.decreaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.doesNotThrowAnyException() } @Test fun givenNotExist_whenDecreaseBalance_thenThrow(): Unit = runBlocking { stubbing(walletRepository) { on { - updateBalance(any(), any()) + updateBalance(any(), eq(BigDecimal.valueOf(-1))) } doReturn Mono.just(0) } val wallet = object : Wallet { @@ -806,14 +874,21 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(1)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.decreaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test fun givenWrongAmount_whenDecreaseBalance_thenThrow(): Unit = runBlocking { stubbing(walletRepository) { on { - updateBalance(eq(20), any()) + updateBalance(eq(20), eq(BigDecimal.valueOf(-1))) } doReturn Mono.just(0) } val wallet = object : Wallet { @@ -824,7 +899,14 @@ private class WalletManagerTest : WalletManagerTestBase() { override fun type() = "main" } - assertThatThrownBy { runBlocking { walletManagerImpl.decreaseBalance(wallet, BigDecimal.valueOf(-1)) } } + assertThatThrownBy { + runBlocking { + walletManagerImpl.decreaseBalance( + wallet, + BigDecimal.valueOf(-1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test @@ -855,6 +937,17 @@ private class WalletManagerTest : WalletManagerTestBase() { ) ) } + stubbing(currencyRepository) { + on { + findById(currency.getSymbol()) + } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + } val wallet = walletManagerImpl.findWalletById(20) assertThat(wallet).isNotNull diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt index 15367c70f..ec07e0361 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt @@ -3,7 +3,6 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.WalletOwner import co.nilin.opex.wallet.ports.postgres.dao.* -import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import org.mockito.kotlin.any import org.mockito.kotlin.doReturn import org.mockito.kotlin.eq @@ -15,7 +14,7 @@ internal open class WalletManagerTestBase { protected var walletRepository: WalletRepository = mock() protected var walletOwnerRepository: WalletOwnerRepository = mock() - protected var transactionRepository: TransactionRepository = mock { + private var transactionRepository: TransactionRepository = mock { on { calculateWithdrawStatistics(eq(2), eq(20), any(), any()) } doReturn Mono.empty() } @@ -35,14 +34,7 @@ internal open class WalletManagerTestBase { override fun getPrecision() = 0.0001 } - protected var currencyRepository: CurrencyRepository = mock { - on { findBySymbol(currency.getSymbol()) } doReturn Mono.just( - CurrencyModel(currency.getSymbol(), currency.getName(), currency.getPrecision()) - ) - on { findById(currency.getSymbol()) } doReturn Mono.just( - CurrencyModel(currency.getSymbol(), currency.getName(), currency.getPrecision()) - ) - } + protected var currencyRepository: CurrencyRepository = mock { } protected var walletManagerImpl: WalletManagerImpl = WalletManagerImpl( walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index edb2f80ad..0ac9d00bc 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -200,37 +200,35 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { @Test fun givenLevelWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { - emit( - UserLimitsModel( - 1, - "1", - null, - "deposit", - "main", - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { + emit( + UserLimitsModel( + 1, + "1", + null, + "deposit", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 ) - } - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() + ) } - val isAllowed = - walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) - - assertThat(isAllowed).isFalse() } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() } @Test From dbc09be2c551b28b97fa47ca81cf2ddcb76b7e38 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 10:41:56 +0430 Subject: [PATCH 27/58] wallet: Refactor tests --- .../ports/postgres/impl/WalletOwnerManagerTest.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index 0ac9d00bc..e0ba07d23 100644 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -7,6 +7,7 @@ import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.anyLong import org.mockito.ArgumentMatchers.anyString @@ -39,7 +40,7 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { } @Test - fun givenWrongAmount_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenInvalidAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} @@ -52,9 +53,15 @@ private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } - val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(-5))) - assertThat(isAllowed).isTrue() + assertThatThrownBy { + runBlocking { + walletOwnerManagerImpl.isWithdrawAllowed( + walletOwner, + Amount(currency, BigDecimal.valueOf(-5)) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) } @Test From eeaec9c61b6e36ed8ceb32790b39b6ffe5f469ba Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 12:42:41 +0430 Subject: [PATCH 28/58] matching-gateway: Add create order tests --- matching-gateway/matching-gateway-app/pom.xml | 4 + .../gateway/app/service/OrderService.kt | 6 +- .../gateway/app/service/OrderServiceTest.kt | 97 +++++++++++++++++++ .../app/service/OrderServiceTestBase.kt | 23 +++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt create mode 100644 matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt diff --git a/matching-gateway/matching-gateway-app/pom.xml b/matching-gateway/matching-gateway-app/pom.xml index 583fe0d38..89c9fbcef 100644 --- a/matching-gateway/matching-gateway-app/pom.xml +++ b/matching-gateway/matching-gateway-app/pom.xml @@ -73,6 +73,10 @@ co.nilin.opex.utility.log logging-handler + + org.mockito.kotlin + mockito-kotlin + diff --git a/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt b/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt index 3647c7f21..c4a3d802a 100644 --- a/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt +++ b/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt @@ -50,8 +50,8 @@ class OrderService( if (!canCreateOrder) throw OpexException(OpexError.SubmitOrderForbiddenByAccountant) - if (!kafkaHealthIndicator.isHealthy) - throw OpexException(OpexError.ServiceUnavailable) +// if (!kafkaHealthIndicator.isHealthy) +// throw OpexException(OpexError.ServiceUnavailable) val orderSubmitRequest = OrderSubmitRequest( createOrderRequest.uuid!!, //get from auth2 @@ -74,4 +74,4 @@ class OrderService( val event = CancelOrderEvent(request.ouid, request.uuid, request.orderId, Pair(symbols[0], symbols[1])) return eventSubmitter.submit(event) } -} \ No newline at end of file +} diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt new file mode 100644 index 000000000..4049ae91c --- /dev/null +++ b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt @@ -0,0 +1,97 @@ +package co.nilin.opex.matching.gateway.app.service + +import co.nilin.opex.matching.engine.core.eventh.events.CancelOrderEvent +import co.nilin.opex.matching.engine.core.model.MatchConstraint +import co.nilin.opex.matching.engine.core.model.OrderDirection +import co.nilin.opex.matching.engine.core.model.OrderType +import co.nilin.opex.matching.engine.core.model.Pair +import co.nilin.opex.matching.gateway.app.inout.CancelOrderRequest +import co.nilin.opex.matching.gateway.app.inout.CreateOrderRequest +import co.nilin.opex.matching.gateway.app.inout.PairConfig +import co.nilin.opex.matching.gateway.app.inout.PairFeeConfig +import co.nilin.opex.matching.gateway.ports.kafka.submitter.inout.OrderSubmitRequest +import co.nilin.opex.matching.gateway.ports.kafka.submitter.inout.OrderSubmitResult +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.stubbing +import org.mockito.kotlin.whenever +import java.math.BigDecimal + +private class OrderServiceTest : OrderServiceTestBase() { + @Test + fun givenLimitASKOrder_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "1") } doReturn PairFeeConfig( + PairConfig( + "ETH_USDT", + "ETH", + "USDT", + 0.0001, + 0.01 + ), + "ASK", + "1", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(100), + BigDecimal.valueOf(0.001), + OrderDirection.ASK, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "ETH_USDT", order.quantity) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit( + eq( + OrderSubmitRequest( + order.uuid!!, + Pair("ETH", "USDT"), + (order.price / BigDecimal.valueOf(0.01)).longValueExact(), + (order.quantity / BigDecimal.valueOf(0.0001)).longValueExact(), + order.direction, + order.matchConstraint, + order.orderType + ) + ) + ) + } doReturn OrderSubmitResult(null) + } + whenever(kafkaHealthIndicator.isHealthy).thenReturn(true) + + val orderSubmitResult = orderService.submitNewOrder(order) + + assertThat(orderSubmitResult).isNotNull + } + + @Test + fun givenValidCancelOrder_whenCancelOrder_thenOrderSubmitResult(): Unit = runBlocking { + val order = CancelOrderRequest( + "edee8090-62d9-4929-b70d-5b97de0c29eb", + "a2930d06-0c84-4448-bff7-65134184bb1d", + 1, + "ETH_USDT" + ) + stubbing(eventSubmitter) { + onBlocking { + submit(CancelOrderEvent(order.ouid, order.uuid, order.orderId, Pair("ETH", "USDT"))) + } doReturn OrderSubmitResult(null) + } + + val orderSubmitResult = orderService.cancelOrder(order) + + assertThat(orderSubmitResult).isNotNull + } +} diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt new file mode 100644 index 000000000..1c9abd3dd --- /dev/null +++ b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt @@ -0,0 +1,23 @@ +package co.nilin.opex.matching.gateway.app.service + +import co.nilin.opex.matching.gateway.app.spi.AccountantApiProxy +import co.nilin.opex.matching.gateway.app.spi.PairConfigLoader +import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.EventSubmitter +import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.KafkaHealthIndicator +import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.OrderSubmitter +import org.mockito.kotlin.mock + +internal open class OrderServiceTestBase { + protected val accountantApiProxy: AccountantApiProxy = mock() + protected val orderSubmitter: OrderSubmitter = mock() + protected val eventSubmitter: EventSubmitter = mock() + protected val pairConfigLoader: PairConfigLoader = mock() + protected val kafkaHealthIndicator: KafkaHealthIndicator = mock() + protected val orderService: OrderService = OrderService( + accountantApiProxy, + orderSubmitter, + eventSubmitter, + pairConfigLoader, + kafkaHealthIndicator + ) +} From 8b07dd103fd70c363becc87c1f982c72743620a3 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 13:56:50 +0430 Subject: [PATCH 29/58] matching-gateway: Improve tests --- .../gateway/app/service/OrderService.kt | 4 +- .../gateway/app/service/OrderServiceTest.kt | 43 ++++++------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt b/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt index c4a3d802a..dff49032a 100644 --- a/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt +++ b/matching-gateway/matching-gateway-app/src/main/kotlin/co/nilin/opex/matching/gateway/app/service/OrderService.kt @@ -50,8 +50,8 @@ class OrderService( if (!canCreateOrder) throw OpexException(OpexError.SubmitOrderForbiddenByAccountant) -// if (!kafkaHealthIndicator.isHealthy) -// throw OpexException(OpexError.ServiceUnavailable) + if (!kafkaHealthIndicator.isHealthy) + throw OpexException(OpexError.ServiceUnavailable) val orderSubmitRequest = OrderSubmitRequest( createOrderRequest.uuid!!, //get from auth2 diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt index 4049ae91c..25a42a265 100644 --- a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt +++ b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt @@ -1,39 +1,30 @@ package co.nilin.opex.matching.gateway.app.service -import co.nilin.opex.matching.engine.core.eventh.events.CancelOrderEvent import co.nilin.opex.matching.engine.core.model.MatchConstraint import co.nilin.opex.matching.engine.core.model.OrderDirection import co.nilin.opex.matching.engine.core.model.OrderType -import co.nilin.opex.matching.engine.core.model.Pair import co.nilin.opex.matching.gateway.app.inout.CancelOrderRequest import co.nilin.opex.matching.gateway.app.inout.CreateOrderRequest import co.nilin.opex.matching.gateway.app.inout.PairConfig import co.nilin.opex.matching.gateway.app.inout.PairFeeConfig -import co.nilin.opex.matching.gateway.ports.kafka.submitter.inout.OrderSubmitRequest import co.nilin.opex.matching.gateway.ports.kafka.submitter.inout.OrderSubmitResult import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.mockito.kotlin.any import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq import org.mockito.kotlin.stubbing -import org.mockito.kotlin.whenever import java.math.BigDecimal private class OrderServiceTest : OrderServiceTestBase() { @Test fun givenLimitASKOrder_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.ASK, "1") } doReturn PairFeeConfig( - PairConfig( - "ETH_USDT", - "ETH", - "USDT", - 0.0001, - 0.01 - ), + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, "ASK", - "1", + "", 0.01, 0.01 ) @@ -41,7 +32,7 @@ private class OrderServiceTest : OrderServiceTestBase() { val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", - BigDecimal.valueOf(100), + BigDecimal.valueOf(100000), BigDecimal.valueOf(0.001), OrderDirection.ASK, MatchConstraint.GTC, @@ -49,27 +40,17 @@ private class OrderServiceTest : OrderServiceTestBase() { ) stubbing(accountantApiProxy) { onBlocking { - canCreateOrder(order.uuid!!, "ETH_USDT", order.quantity) + canCreateOrder(order.uuid!!, "ETH", order.quantity) } doReturn true } stubbing(orderSubmitter) { onBlocking { - submit( - eq( - OrderSubmitRequest( - order.uuid!!, - Pair("ETH", "USDT"), - (order.price / BigDecimal.valueOf(0.01)).longValueExact(), - (order.quantity / BigDecimal.valueOf(0.0001)).longValueExact(), - order.direction, - order.matchConstraint, - order.orderType - ) - ) - ) + submit(any()) } doReturn OrderSubmitResult(null) } - whenever(kafkaHealthIndicator.isHealthy).thenReturn(true) + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } val orderSubmitResult = orderService.submitNewOrder(order) @@ -86,7 +67,7 @@ private class OrderServiceTest : OrderServiceTestBase() { ) stubbing(eventSubmitter) { onBlocking { - submit(CancelOrderEvent(order.ouid, order.uuid, order.orderId, Pair("ETH", "USDT"))) + submit(any()) } doReturn OrderSubmitResult(null) } From 5136d9d618b53cd2ba86226ed0b17490703dc4d2 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Thu, 19 May 2022 23:32:45 +0430 Subject: [PATCH 30/58] matching-gateway: Improve kafka health indicator --- .../ports/kafka/submitter/service/KafkaHealthIndicator.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/matching-gateway/matching-gateway-port/matching-gateway-submitter-kafka/src/main/kotlin/co/nilin/opex/matching/gateway/ports/kafka/submitter/service/KafkaHealthIndicator.kt b/matching-gateway/matching-gateway-port/matching-gateway-submitter-kafka/src/main/kotlin/co/nilin/opex/matching/gateway/ports/kafka/submitter/service/KafkaHealthIndicator.kt index 38f0f8e2f..f6e1d91b4 100644 --- a/matching-gateway/matching-gateway-port/matching-gateway-submitter-kafka/src/main/kotlin/co/nilin/opex/matching/gateway/ports/kafka/submitter/service/KafkaHealthIndicator.kt +++ b/matching-gateway/matching-gateway-port/matching-gateway-submitter-kafka/src/main/kotlin/co/nilin/opex/matching/gateway/ports/kafka/submitter/service/KafkaHealthIndicator.kt @@ -12,12 +12,13 @@ class KafkaHealthIndicator(private val adminClient: AdminClient) { private val logger = LoggerFactory.getLogger(KafkaHealthIndicator::class.java) private val options = DescribeClusterOptions().timeoutMs(1000) private val healthyNodeSize = 3 - var isHealthy = false - protected set + private var pIsHealthy = false + val isHealthy + get() = pIsHealthy @Scheduled(fixedDelay = 5000, initialDelay = 5000) fun check() { - isHealthy = try { + pIsHealthy = try { val description = adminClient.describeCluster(options) if (description.nodes().get().size < healthyNodeSize) throw IllegalStateException("Insufficient nodes") @@ -27,5 +28,4 @@ class KafkaHealthIndicator(private val adminClient: AdminClient) { false } } - } From 1df1d3fc248f9ac6521b9288bcd31792de36c705 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Fri, 20 May 2022 15:54:39 +0430 Subject: [PATCH 31/58] matching-gateway: Add more tests --- .../gateway/app/service/OrderServiceTest.kt | 422 ++++++++++++++++++ 1 file changed, 422 insertions(+) diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt index 25a42a265..71cdc08a8 100644 --- a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt +++ b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt @@ -10,6 +10,7 @@ import co.nilin.opex.matching.gateway.app.inout.PairFeeConfig import co.nilin.opex.matching.gateway.ports.kafka.submitter.inout.OrderSubmitResult import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import org.mockito.kotlin.any import org.mockito.kotlin.doReturn @@ -57,6 +58,427 @@ private class OrderServiceTest : OrderServiceTestBase() { assertThat(orderSubmitResult).isNotNull } + @Test + fun givenLimitASKOrderWithInvalidSymbol_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "BTC_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.ASK, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "ETH", order.quantity) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitASKOrderWithNotExistOwner_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "52f01a11-da0d-4050-bdd7-d38349ddfb6a", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.ASK, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "ETH", order.quantity) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitASKOrderWithInvalidPrice_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(-100000), + BigDecimal.valueOf(0.001), + OrderDirection.ASK, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "ETH", order.quantity) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitASKOrderWithInvalidQuantity_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(-0.001), + OrderDirection.ASK, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "ETH", order.quantity) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitASKOrderWithInvalidLevel_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "1") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "1", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.ASK, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "ETH", order.quantity) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitBIDOrder_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.BID, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + val orderSubmitResult = orderService.submitNewOrder(order) + + assertThat(orderSubmitResult).isNotNull + } + + + @Test + fun givenLimitBIDOrderWithInvalidSymbol_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "BTC_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.BID, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitBIDOrderWithNotExistOwner_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "55408c0a-ed53-42d1-b5ee-b2edf531b9d5", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.BID, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitBIDOrderWithInvalidPrice_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(-100000), + BigDecimal.valueOf(0.001), + OrderDirection.BID, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitBIDOrderWithInvalidQuantity_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(-0.001), + OrderDirection.BID, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenLimitBIDOrderWithInvalidLevel_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "1") } doReturn PairFeeConfig( + pairConfig, + "BID", + "1", + 0.01, + 0.01 + ) + } + val order = CreateOrderRequest( + "a2930d06-0c84-4448-bff7-65134184bb1d", + "ETH_USDT", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + OrderDirection.BID, + MatchConstraint.GTC, + OrderType.LIMIT_ORDER + ) + stubbing(accountantApiProxy) { + onBlocking { + canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) + } doReturn true + } + stubbing(orderSubmitter) { + onBlocking { + submit(any()) + } doReturn OrderSubmitResult(null) + } + stubbing(kafkaHealthIndicator) { + on { isHealthy } doReturn true + } + + assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) + } + @Test fun givenValidCancelOrder_whenCancelOrder_thenOrderSubmitResult(): Unit = runBlocking { val order = CancelOrderRequest( From ce23cbc4a7cb48ffaa359ac9d54d5eaa652c5208 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Fri, 20 May 2022 23:40:14 +0430 Subject: [PATCH 32/58] api: Add test classes --- .../opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt | 3 +++ .../nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt | 3 +++ .../co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt | 3 +++ .../nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt | 3 +++ .../nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt | 3 +++ 5 files changed, 15 insertions(+) create mode 100644 api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt create mode 100644 api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt create mode 100644 api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt create mode 100644 api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt create mode 100644 api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt new file mode 100644 index 000000000..b9889197e --- /dev/null +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -0,0 +1,3 @@ +package co.nilin.opex.api.ports.postgres.impl + +class MarketQueryHandlerTest diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt new file mode 100644 index 000000000..125e957ba --- /dev/null +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -0,0 +1,3 @@ +package co.nilin.opex.api.ports.postgres.impl + +class OrderPersisterTest diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt new file mode 100644 index 000000000..c6a6c374f --- /dev/null +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt @@ -0,0 +1,3 @@ +package co.nilin.opex.api.ports.postgres.impl + +class SymbolMapperTest diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt new file mode 100644 index 000000000..c1429af19 --- /dev/null +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -0,0 +1,3 @@ +package co.nilin.opex.api.ports.postgres.impl + +class TradePersisterTest diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt new file mode 100644 index 000000000..bd11c8a4e --- /dev/null +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -0,0 +1,3 @@ +package co.nilin.opex.api.ports.postgres.impl + +class UserQueryHandlerTest From 175785b0b662a40fe9bffe47a431a7d2c6735e63 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 21 May 2022 11:57:53 +0430 Subject: [PATCH 33/58] api: Add symbol mapper tests --- api/api-app/pom.xml | 4 + .../ports/postgres/impl/SymbolMapperTest.kt | 78 ++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/api/api-app/pom.xml b/api/api-app/pom.xml index 4d69c7fc1..904a06970 100644 --- a/api/api-app/pom.xml +++ b/api/api-app/pom.xml @@ -68,6 +68,10 @@ co.nilin.opex.utility.preferences preferences + + org.mockito.kotlin + mockito-kotlin + diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt index c6a6c374f..5c531434e 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt @@ -1,3 +1,79 @@ package co.nilin.opex.api.ports.postgres.impl -class SymbolMapperTest +import co.nilin.opex.api.ports.postgres.dao.SymbolMapRepository +import co.nilin.opex.api.ports.postgres.model.SymbolMapModel +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test +import org.mockito.kotlin.mock +import org.assertj.core.api.Assertions.* +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.TestInstance +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SymbolMapperTest { + private val symbolMapRepository: SymbolMapRepository = mock() + private val symbolMapper = SymbolMapperImpl(symbolMapRepository) + + @BeforeAll + fun setUp() { + stubbing(symbolMapRepository) { + on { + findByAliasKeyAndAlias("binance", "ETHUSDT") + } doReturn Mono.just( + SymbolMapModel( + 1, + "ETH_USDT", + "binance", + "ETHUSDT" + ) + ) + on { + findByAliasKeyAndSymbol("binance", "ETH_USDT") + } doReturn Mono.just( + SymbolMapModel( + 1, + "ETH_USDT", + "binance", + "ETHUSDT" + ) + ) + on { + findAll() + } doReturn Flux.just( + SymbolMapModel( + 1, + "ETH_USDT", + "binance", + "ETHUSDT" + ) + ) + } + } + + @Test + fun givenValidSymbol_whenMap_thenReturnAlias(): Unit = runBlocking { + val alis = symbolMapper.map("ETH_USDT") + + assertThat(alis).isEqualTo("ETHUSDT") + } + + @Test + fun givenValidAlias_whenUnmap_thenReturnSymbol(): Unit = runBlocking { + val symbol = symbolMapper.unmap("ETHUSDT") + + assertThat(symbol).isEqualTo("ETH_USDT") + } + + @Test + fun given_whenSymbolToAliasMap_thenReturnSymbol(): Unit = runBlocking { + val map = symbolMapper.symbolToAliasMap() + + assertThat(map).isNotNull + assertThat(map.size).isEqualTo(1) + assertThat(map["ETH_USDT"]).isNotNull() + } +} From 060fceed02be8434667a2441433be2ec2c62548b Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 21 May 2022 13:58:31 +0430 Subject: [PATCH 34/58] api: Add order persister tests --- .../ports/postgres/impl/OrderPersisterTest.kt | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt index 125e957ba..126dbb837 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -1,3 +1,60 @@ package co.nilin.opex.api.ports.postgres.impl -class OrderPersisterTest +import co.nilin.opex.api.core.event.RichOrder +import co.nilin.opex.api.core.event.RichOrderUpdate +import co.nilin.opex.api.core.inout.MatchConstraint +import co.nilin.opex.api.core.inout.MatchingOrderType +import co.nilin.opex.api.core.inout.OrderDirection +import co.nilin.opex.api.core.inout.OrderStatus +import co.nilin.opex.api.ports.postgres.dao.OrderRepository +import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test +import org.mockito.kotlin.mock +import org.assertj.core.api.Assertions.* +import java.math.BigDecimal + +class OrderPersisterTest { + private val orderRepository: OrderRepository = mock() + private val orderStatusRepository: OrderStatusRepository = mock() + private val orderPersister = OrderPersisterImpl(orderRepository, orderStatusRepository) + + @Test + fun givenRichOrder_whenSave_thenSuccess(): Unit = runBlocking { + val richOrder = RichOrder( + null, + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "1", + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0.0001), + BigDecimal.valueOf(0.01), + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + BigDecimal.valueOf(1000001), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + 0 + ) + + assertThatThrownBy { runBlocking { orderPersister.save(richOrder) } }.doesNotThrowAnyException() + } + + @Test + fun givenRichOrder_whenUpdate_thenSuccess(): Unit = runBlocking { + val richOrderUpdate = RichOrderUpdate( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + BigDecimal.valueOf(1000001), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0.08), + OrderStatus.PARTIALLY_FILLED + ) + + assertThatThrownBy { runBlocking { orderPersister.update(richOrderUpdate) } }.doesNotThrowAnyException() + } +} From 842c9130371a4cbc9aa17eeda10ed29ec9017132 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 21 May 2022 14:07:03 +0430 Subject: [PATCH 35/58] api: Add trade persister tests --- .../ports/postgres/impl/TradePersisterTest.kt | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt index c1429af19..8f74b63f2 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -1,3 +1,49 @@ package co.nilin.opex.api.ports.postgres.impl -class TradePersisterTest +import co.nilin.opex.api.core.event.RichTrade +import co.nilin.opex.api.core.inout.OrderDirection +import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import org.mockito.kotlin.mock +import java.math.BigDecimal +import java.time.LocalDateTime +import java.time.ZoneOffset + +class TradePersisterTest { + private val tradeRepository: TradeRepository = mock() + private val tradePersister = TradePersisterImpl(tradeRepository) + + @Test + fun givenRichTrade_whenSave_thenSuccess(): Unit = runBlocking { + val richOrder = RichTrade( + 1, // ? + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + 1, + OrderDirection.ASK, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + "", // ? + "26931efc-891b-4599-9921-1d265829b410", + "5296a097-6478-464f-91a6-5c434ac4207d", + 2, + OrderDirection.ASK, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + "", // ? + BigDecimal.valueOf(0), // ? + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + + Assertions.assertThatThrownBy { runBlocking { tradePersister.save(richOrder) } }.doesNotThrowAnyException() + } +} From fc44b5e25972261e096db3950d1f1fbb2432155b Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 21 May 2022 17:50:27 +0430 Subject: [PATCH 36/58] api: Add user query handler tests --- .../postgres/impl/UserQueryHandlerTest.kt | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index bd11c8a4e..e45b4c5f4 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -1,3 +1,64 @@ package co.nilin.opex.api.ports.postgres.impl -class UserQueryHandlerTest +import co.nilin.opex.api.core.inout.AllOrderRequest +import co.nilin.opex.api.core.inout.QueryOrderRequest +import co.nilin.opex.api.core.inout.TradeRequest +import co.nilin.opex.api.ports.postgres.dao.OrderRepository +import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository +import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test +import org.mockito.kotlin.mock +import java.security.Principal +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.util.* + +class UserQueryHandlerTest { + private val orderRepository: OrderRepository = mock() + private val tradeRepository: TradeRepository = mock() + private val orderStatusRepository: OrderStatusRepository = mock() + private val userQueryHandler = UserQueryHandlerImpl(orderRepository, tradeRepository, orderStatusRepository) + private val principal: Principal = Principal { "98c7ca9b-2d9c-46dd-afa8-b0cd2f52a97c" } + + @Test + fun given_whenAllOrders_then(): Unit = runBlocking { + val allOrderRequest = AllOrderRequest( + "ETH_USDT", + Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + 500 + ) + + userQueryHandler.allOrders(principal, allOrderRequest) + } + + @Test + fun given_whenAllTrades_then(): Unit = runBlocking { + val tradeRequest = TradeRequest( + "ETH_USDT", + 1, + Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + 500 + ) + + userQueryHandler.allTrades(principal, tradeRequest) + } + + @Test + fun given_whenOpenOrders_then(): Unit = runBlocking { + userQueryHandler.openOrders(principal, "ETH_USDT") + } + + @Test + fun given_whenQueryOrder_then(): Unit = runBlocking { + val queryOrderRequest = QueryOrderRequest( + "ETH_USDT", + 1, + "2" // ? + ) + + userQueryHandler.queryOrder(principal, queryOrderRequest) + } +} From 19ad3c26d8cdaa8642af9ed64d9821ea38ba01dc Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sat, 21 May 2022 21:59:10 +0430 Subject: [PATCH 37/58] api: Add market query handler tests --- .../postgres/impl/MarketQueryHandlerTest.kt | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index b9889197e..4e0523ab8 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -1,3 +1,45 @@ package co.nilin.opex.api.ports.postgres.impl -class MarketQueryHandlerTest +import co.nilin.opex.api.core.spi.SymbolMapper +import co.nilin.opex.api.ports.postgres.dao.OrderRepository +import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository +import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test +import org.mockito.kotlin.mock +import java.security.Principal + +class MarketQueryHandlerTest { + private val orderRepository: OrderRepository = mock() + private val tradeRepository: TradeRepository = mock() + private val orderStatusRepository: OrderStatusRepository = mock() + private val symbolMapper: SymbolMapper = mock() + private val marketQueryHandler = + MarketQueryHandlerImpl(orderRepository, tradeRepository, orderStatusRepository, symbolMapper) + + @Test + fun givenSymbol_whenOpenASKOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { + marketQueryHandler.openAskOrders("ETH_USDT", 10) + } + + @Test + fun givenSymbol_whenOpenBIDOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { + marketQueryHandler.openBidOrders("ETH_USDT", 10) + } + + @Test + fun givenSymbol_whenLastOrder_thenReturnQueryOrderResponse(): Unit = runBlocking { + marketQueryHandler.lastOrder("ETH_USDT") + } + + @Test + fun givenSymbol_whenLastPrice_thenPriceTickerResponse(): Unit = runBlocking { + marketQueryHandler.lastPrice("ETH_USDT") + } + + @Test + fun givenSymbol_whenRecentTrades_thenMarketTradeResponseFlow(): Unit = runBlocking { + marketQueryHandler.recentTrades("ETH_USDT", 10) + } +} + From 39e9874f3a5300c0c06c7928d6df7825f4e0672f Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sat, 21 May 2022 23:32:28 +0430 Subject: [PATCH 38/58] api: Add more order persister tests --- .../ports/postgres/impl/OrderPersisterTest.kt | 69 ++++++++++++++++++- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt index 126dbb837..a6ad4c243 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -8,11 +8,19 @@ import co.nilin.opex.api.core.inout.OrderDirection import co.nilin.opex.api.core.inout.OrderStatus import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository +import co.nilin.opex.api.ports.postgres.model.OrderModel +import co.nilin.opex.api.ports.postgres.model.OrderStatusModel import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock -import org.assertj.core.api.Assertions.* +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Mono import java.math.BigDecimal +import java.time.LocalDateTime +import java.time.ZoneOffset class OrderPersisterTest { private val orderRepository: OrderRepository = mock() @@ -41,8 +49,49 @@ class OrderPersisterTest { BigDecimal.valueOf(0), // ? 0 ) + stubbing(orderRepository) { + on { + save(any()) + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + stubbing(orderStatusRepository) { + on { + save(any()) + } doReturn Mono.just( + OrderStatusModel( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 0.0, // ? + 0.0, // ? + 0, // ? + 0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } - assertThatThrownBy { runBlocking { orderPersister.save(richOrder) } }.doesNotThrowAnyException() + assertThatNoException().isThrownBy { runBlocking { orderPersister.save(richOrder) } } } @Test @@ -54,7 +103,21 @@ class OrderPersisterTest { BigDecimal.valueOf(0.08), OrderStatus.PARTIALLY_FILLED ) + stubbing(orderStatusRepository) { + on { + save(any()) + } doReturn Mono.just( + OrderStatusModel( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 0.0, // ? + 0.0, // ? + 0, // ? + 0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } - assertThatThrownBy { runBlocking { orderPersister.update(richOrderUpdate) } }.doesNotThrowAnyException() + assertThatNoException().isThrownBy { runBlocking { orderPersister.update(richOrderUpdate) } } } } From a420778cf692529c435c8682d45ef733b2fa4113 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 11:42:26 +0430 Subject: [PATCH 39/58] api: Add market query handler test stubs --- .../postgres/impl/MarketQueryHandlerTest.kt | 303 +++++++++++++++++- .../ports/postgres/impl/TradePersisterTest.kt | 4 +- 2 files changed, 299 insertions(+), 8 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index 4e0523ab8..1bb64d1d5 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -1,13 +1,28 @@ package co.nilin.opex.api.ports.postgres.impl +import co.nilin.opex.api.core.inout.* import co.nilin.opex.api.core.spi.SymbolMapper import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import co.nilin.opex.api.ports.postgres.model.OrderModel +import co.nilin.opex.api.ports.postgres.model.OrderStatusModel +import co.nilin.opex.api.ports.postgres.model.TradeModel +import kotlinx.coroutines.flow.count +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock -import java.security.Principal +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import java.math.BigDecimal +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.util.* class MarketQueryHandlerTest { private val orderRepository: OrderRepository = mock() @@ -19,27 +34,303 @@ class MarketQueryHandlerTest { @Test fun givenSymbol_whenOpenASKOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { - marketQueryHandler.openAskOrders("ETH_USDT", 10) + stubbing(orderRepository) { + on { + findBySymbolAndDirectionAndStatusSortAscendingByPrice("ETH_USDT", OrderDirection.BID, 1, listOf(0)) + } doReturn Flux.just( + AggregatedOrderPriceModel( + 100000.0, + 0.001 + ) + ) + } + val orderBookResponses = marketQueryHandler.openAskOrders("ETH_USDT", 10) + + assertThat(orderBookResponses).isNotNull + assertThat(orderBookResponses.size).isEqualTo(1) + assertThat(orderBookResponses.first()).isEqualTo( + OrderBookResponse( + BigDecimal.valueOf(100000.0), + BigDecimal.valueOf(0.001) + ) + ) } @Test fun givenSymbol_whenOpenBIDOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { - marketQueryHandler.openBidOrders("ETH_USDT", 10) + stubbing(orderRepository) { + on { + findBySymbolAndDirectionAndStatusSortDescendingByPrice("ETH_USDT", OrderDirection.BID, 1, listOf(0)) + } doReturn Flux.just( + AggregatedOrderPriceModel( + 100000.0, + 0.001 + ) + ) + } + val orderBookResponses = marketQueryHandler.openBidOrders("ETH_USDT", 10) + + assertThat(orderBookResponses).isNotNull + assertThat(orderBookResponses.size).isEqualTo(1) + assertThat(orderBookResponses.first()).isEqualTo( + OrderBookResponse( + BigDecimal.valueOf(100000.0), + BigDecimal.valueOf(0.001) + ) + ) } @Test fun givenSymbol_whenLastOrder_thenReturnQueryOrderResponse(): Unit = runBlocking { - marketQueryHandler.lastOrder("ETH_USDT") + stubbing(orderRepository) { + on { + findLastOrderBySymbol("ETH_USDT") + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + stubbing(orderStatusRepository) { + on { + findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + } doReturn Mono.just( + OrderStatusModel( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 0.0, // ? + 0.0, // ? + 0, // ? + 0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + + val queryOrderResponse = marketQueryHandler.lastOrder("ETH_USDT") + + assertThat(queryOrderResponse).isNotNull + assertThat(queryOrderResponse).isEqualTo( + QueryOrderResponse( + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 1, + 1, // ?, + "id", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + BigDecimal.valueOf(100), + BigDecimal.valueOf(1), + OrderStatus.FILLED, + TimeInForce.GTC, + OrderType.LIMIT, + OrderSide.BUY, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(100000), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + true, + BigDecimal.valueOf(0) + ) + ) } @Test fun givenSymbol_whenLastPrice_thenPriceTickerResponse(): Unit = runBlocking { - marketQueryHandler.lastPrice("ETH_USDT") + stubbing(tradeRepository) { + on { + findAllGroupBySymbol() + } doReturn Flux.just( + TradeModel( + 1, + 1, + "ETH_USDT", + 0.001, + 100000.0, + 100000.0, + 0.001, + 0.001, + "", + "", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), + "99289106-2775-44d4-bffc-ca35fc25e58c", + "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", + "52c6d890-3dd4-4fa8-9425-d9e0d6274705", + "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + ) + on { + findBySymbolGroupBySymbol("ETH_USDT") + } doReturn Flux.just( + TradeModel( + 1, + 1, + "ETH_USDT", + 0.001, + 100000.0, + 100000.0, + 0.001, + 0.001, + "", + "", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), + "99289106-2775-44d4-bffc-ca35fc25e58c", + "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", + "52c6d890-3dd4-4fa8-9425-d9e0d6274705", + "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + ) + } + stubbing(orderRepository) { + on { + findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + val priceTickerResponse = marketQueryHandler.lastPrice("ETH_USDT") + + assertThat(priceTickerResponse).isNotNull + assertThat(priceTickerResponse.size).isEqualTo(1) + assertThat(priceTickerResponse.first().symbol).isEqualTo("ETH_USDT") + assertThat(priceTickerResponse.first().price).isEqualTo(100000) } @Test fun givenSymbol_whenRecentTrades_thenMarketTradeResponseFlow(): Unit = runBlocking { - marketQueryHandler.recentTrades("ETH_USDT", 10) + stubbing(tradeRepository) { + on { + findBySymbolSortDescendingByCreateDate("ETH_USDT", 10) + } doReturn flow { + emit( + TradeModel( + 1, + 1, + "ETH_USDT", + 0.001, + 100000.0, + 100000.0, + 0.001, + 0.001, + "", + "", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), + "99289106-2775-44d4-bffc-ca35fc25e58c", + "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", + "52c6d890-3dd4-4fa8-9425-d9e0d6274705", + "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + ) + } + } + stubbing(orderRepository) { + on { + findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + on { + findByOuid("2fa73fa2-6d70-44b8-8571-e2b24e2eea2b") + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + val marketTradeResponses = marketQueryHandler.recentTrades("ETH_USDT", 10) + + assertThat(marketTradeResponses).isNotNull + assertThat(marketTradeResponses.count()).isEqualTo(1) + assertThat(marketTradeResponses.first()).isEqualTo( + MarketTradeResponse( + "ETH_USDT", + 1, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + BigDecimal.valueOf(0.001), + Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + true, + true + ) + ) } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt index 8f74b63f2..33020e6e7 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -4,7 +4,7 @@ import co.nilin.opex.api.core.event.RichTrade import co.nilin.opex.api.core.inout.OrderDirection import co.nilin.opex.api.ports.postgres.dao.TradeRepository import kotlinx.coroutines.runBlocking -import org.assertj.core.api.Assertions +import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test import org.mockito.kotlin.mock import java.math.BigDecimal @@ -44,6 +44,6 @@ class TradePersisterTest { LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) ) - Assertions.assertThatThrownBy { runBlocking { tradePersister.save(richOrder) } }.doesNotThrowAnyException() + assertThatNoException().isThrownBy { runBlocking { tradePersister.save(richOrder) } } } } From b4a8b466b4bc55b8d554afe6d5a4cab769b3bd38 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 12:12:03 +0430 Subject: [PATCH 40/58] api: Add user query handler test stubs --- .../postgres/impl/UserQueryHandlerTest.kt | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index e45b4c5f4..dde4112c2 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -1,11 +1,11 @@ package co.nilin.opex.api.ports.postgres.impl -import co.nilin.opex.api.core.inout.AllOrderRequest -import co.nilin.opex.api.core.inout.QueryOrderRequest -import co.nilin.opex.api.core.inout.TradeRequest +import co.nilin.opex.api.core.inout.* import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import kotlinx.coroutines.flow.count +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Test import org.mockito.kotlin.mock @@ -13,6 +13,8 @@ import java.security.Principal import java.time.LocalDateTime import java.time.ZoneOffset import java.util.* +import org.assertj.core.api.Assertions.* +import java.math.BigDecimal class UserQueryHandlerTest { private val orderRepository: OrderRepository = mock() @@ -30,7 +32,33 @@ class UserQueryHandlerTest { 500 ) - userQueryHandler.allOrders(principal, allOrderRequest) + val queryOrderResponses = userQueryHandler.allOrders(principal, allOrderRequest) + + assertThat(queryOrderResponses).isNotNull + assertThat(queryOrderResponses.count()).isEqualTo(1) + assertThat(queryOrderResponses.first()).isEqualTo( + QueryOrderResponse( + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 1, + 1, // ?, + "id", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + BigDecimal.valueOf(100), + BigDecimal.valueOf(1), + OrderStatus.FILLED, + TimeInForce.GTC, + OrderType.LIMIT, + OrderSide.BUY, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(100000), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + true, + BigDecimal.valueOf(0) + ) + ) } @Test @@ -43,12 +71,18 @@ class UserQueryHandlerTest { 500 ) - userQueryHandler.allTrades(principal, tradeRequest) + val tradeResponses = userQueryHandler.allTrades(principal, tradeRequest) + + assertThat(tradeResponses).isNotNull + assertThat(tradeResponses.count()).isEqualTo(1) } @Test fun given_whenOpenOrders_then(): Unit = runBlocking { - userQueryHandler.openOrders(principal, "ETH_USDT") + val queryOrderResponses = userQueryHandler.openOrders(principal, "ETH_USDT") + + assertThat(queryOrderResponses).isNotNull + assertThat(queryOrderResponses.count()).isEqualTo(1) } @Test @@ -59,6 +93,7 @@ class UserQueryHandlerTest { "2" // ? ) - userQueryHandler.queryOrder(principal, queryOrderRequest) + val queryOrderResponse = userQueryHandler.queryOrder(principal, queryOrderRequest) + assertThat(queryOrderResponse).isNotNull } } From 7da068c288b0e8dfdc1e623a1f124a3c92422f8e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 13:29:45 +0430 Subject: [PATCH 41/58] api: Add stubbing methods --- .../ports/postgres/impl/TradePersisterTest.kt | 29 +++ .../postgres/impl/UserQueryHandlerTest.kt | 169 ++++++++++++++++++ 2 files changed, 198 insertions(+) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt index 33020e6e7..976af9201 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -3,10 +3,15 @@ package co.nilin.opex.api.ports.postgres.impl import co.nilin.opex.api.core.event.RichTrade import co.nilin.opex.api.core.inout.OrderDirection import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import co.nilin.opex.api.ports.postgres.model.TradeModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Mono import java.math.BigDecimal import java.time.LocalDateTime import java.time.ZoneOffset @@ -43,6 +48,30 @@ class TradePersisterTest { BigDecimal.valueOf(0), // ? LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) ) + stubbing(tradeRepository) { + on { + save(any()) + } doReturn Mono.just( + TradeModel( + 1, + 1, + "ETH_USDT", + 0.001, + 100000.0, + 100000.0, + 0.001, + 0.001, + "", + "", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), + "99289106-2775-44d4-bffc-ca35fc25e58c", + "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", + "52c6d890-3dd4-4fa8-9425-d9e0d6274705", + "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + ) + } assertThatNoException().isThrownBy { runBlocking { tradePersister.save(richOrder) } } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index dde4112c2..cec11e73e 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -4,8 +4,12 @@ import co.nilin.opex.api.core.inout.* import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository +import co.nilin.opex.api.ports.postgres.model.OrderModel +import co.nilin.opex.api.ports.postgres.model.OrderStatusModel +import co.nilin.opex.api.ports.postgres.model.TradeModel import kotlinx.coroutines.flow.count import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Test import org.mockito.kotlin.mock @@ -14,6 +18,9 @@ import java.time.LocalDateTime import java.time.ZoneOffset import java.util.* import org.assertj.core.api.Assertions.* +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Mono import java.math.BigDecimal class UserQueryHandlerTest { @@ -31,6 +38,40 @@ class UserQueryHandlerTest { Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), 500 ) + stubbing(orderRepository) { + on { + findByUuidAndSymbolAndTimeBetween( + principal.name, + allOrderRequest.symbol, + allOrderRequest.startTime, + allOrderRequest.endTime + ) + } doReturn flow { + emit( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + } val queryOrderResponses = userQueryHandler.allOrders(principal, allOrderRequest) @@ -70,6 +111,38 @@ class UserQueryHandlerTest { Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), 500 ) + stubbing(tradeRepository) { + on { + findByUuidAndSymbolAndTimeBetweenAndTradeIdGreaterThan( + principal.name, + tradeRequest.symbol, + 1, + tradeRequest.startTime, + tradeRequest.endTime + ) + } doReturn flow { + emit( + TradeModel( + 1, + 1, + "ETH_USDT", + 0.001, + 100000.0, + 100000.0, + 0.001, + 0.001, + "", + "", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), + "99289106-2775-44d4-bffc-ca35fc25e58c", + "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", + "52c6d890-3dd4-4fa8-9425-d9e0d6274705", + "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + ) + } + } val tradeResponses = userQueryHandler.allTrades(principal, tradeRequest) @@ -79,6 +152,36 @@ class UserQueryHandlerTest { @Test fun given_whenOpenOrders_then(): Unit = runBlocking { + stubbing(orderRepository) { + on { + findByUuidAndSymbolAndStatus(principal.name, "ETH_USDT", listOf(0)) + } doReturn flow { + emit( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + } + val queryOrderResponses = userQueryHandler.openOrders(principal, "ETH_USDT") assertThat(queryOrderResponses).isNotNull @@ -92,6 +195,72 @@ class UserQueryHandlerTest { 1, "2" // ? ) + stubbing(orderRepository) { + on { + findBySymbolAndClientOrderId("ETH_USDT", "id") + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + on { + findBySymbolAndOrderId("ETH_USDT", 1) + } doReturn Mono.just( + OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } + stubbing(orderStatusRepository) { + on { + findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + } doReturn Mono.just( + OrderStatusModel( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 0.0, // ? + 0.0, // ? + 0, // ? + 0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + ) + } val queryOrderResponse = userQueryHandler.queryOrder(principal, queryOrderRequest) assertThat(queryOrderResponse).isNotNull From aadcbd8a458ef4a633ed30a418392a4582e3e1ed Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 14:27:01 +0430 Subject: [PATCH 42/58] api: Refactor tests given data --- .../postgres/impl/MarketQueryHandlerTest.kt | 247 ++---------------- .../ports/postgres/impl/OrderPersisterTest.kt | 89 +------ .../ports/postgres/impl/SymbolMapperTest.kt | 35 +-- .../ports/postgres/impl/TradePersisterTest.kt | 56 +--- .../postgres/impl/UserQueryHandlerTest.kt | 211 ++------------- .../postgres/impl/testfixtures/GivenValid.kt | 196 ++++++++++++++ 6 files changed, 253 insertions(+), 581 deletions(-) create mode 100644 api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index 1bb64d1d5..2368bf51f 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -1,13 +1,11 @@ package co.nilin.opex.api.ports.postgres.impl -import co.nilin.opex.api.core.inout.* +import co.nilin.opex.api.core.inout.OrderDirection import co.nilin.opex.api.core.spi.SymbolMapper import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository -import co.nilin.opex.api.ports.postgres.model.OrderModel -import co.nilin.opex.api.ports.postgres.model.OrderStatusModel -import co.nilin.opex.api.ports.postgres.model.TradeModel +import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid import kotlinx.coroutines.flow.count import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow @@ -19,10 +17,6 @@ import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import reactor.core.publisher.Flux import reactor.core.publisher.Mono -import java.math.BigDecimal -import java.time.LocalDateTime -import java.time.ZoneOffset -import java.util.* class MarketQueryHandlerTest { private val orderRepository: OrderRepository = mock() @@ -37,23 +31,13 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findBySymbolAndDirectionAndStatusSortAscendingByPrice("ETH_USDT", OrderDirection.BID, 1, listOf(0)) - } doReturn Flux.just( - AggregatedOrderPriceModel( - 100000.0, - 0.001 - ) - ) + } doReturn Flux.just(Valid.AGGREGATED_ORDER_PRICE_MODEL) } val orderBookResponses = marketQueryHandler.openAskOrders("ETH_USDT", 10) assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) - assertThat(orderBookResponses.first()).isEqualTo( - OrderBookResponse( - BigDecimal.valueOf(100000.0), - BigDecimal.valueOf(0.001) - ) - ) + assertThat(orderBookResponses.first()).isEqualTo(Valid.ORDER_BOOK_RESPONSE) } @Test @@ -61,23 +45,14 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findBySymbolAndDirectionAndStatusSortDescendingByPrice("ETH_USDT", OrderDirection.BID, 1, listOf(0)) - } doReturn Flux.just( - AggregatedOrderPriceModel( - 100000.0, - 0.001 - ) - ) + } doReturn Flux.just(Valid.AGGREGATED_ORDER_PRICE_MODEL) } + val orderBookResponses = marketQueryHandler.openBidOrders("ETH_USDT", 10) assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) - assertThat(orderBookResponses.first()).isEqualTo( - OrderBookResponse( - BigDecimal.valueOf(100000.0), - BigDecimal.valueOf(0.001) - ) - ) + assertThat(orderBookResponses.first()).isEqualTo(Valid.ORDER_STATUS_MODEL) } @Test @@ -85,71 +60,18 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findLastOrderBySymbol("ETH_USDT") - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) } stubbing(orderStatusRepository) { on { findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") - } doReturn Mono.just( - OrderStatusModel( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 0.0, // ? - 0.0, // ? - 0, // ? - 0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) } val queryOrderResponse = marketQueryHandler.lastOrder("ETH_USDT") assertThat(queryOrderResponse).isNotNull - assertThat(queryOrderResponse).isEqualTo( - QueryOrderResponse( - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 1, - 1, // ?, - "id", - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.001), - BigDecimal.valueOf(100), - BigDecimal.valueOf(1), - OrderStatus.FILLED, - TimeInForce.GTC, - OrderType.LIMIT, - OrderSide.BUY, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(100000), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - true, - BigDecimal.valueOf(0) - ) - ) + assertThat(queryOrderResponse).isEqualTo(Valid.QUERY_ORDER_RESPONSE) } @Test @@ -157,75 +79,15 @@ class MarketQueryHandlerTest { stubbing(tradeRepository) { on { findAllGroupBySymbol() - } doReturn Flux.just( - TradeModel( - 1, - 1, - "ETH_USDT", - 0.001, - 100000.0, - 100000.0, - 0.001, - 0.001, - "", - "", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "99289106-2775-44d4-bffc-ca35fc25e58c", - "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", - "52c6d890-3dd4-4fa8-9425-d9e0d6274705", - "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) - ) - ) + } doReturn Flux.just(Valid.TRADE_MODEL) on { findBySymbolGroupBySymbol("ETH_USDT") - } doReturn Flux.just( - TradeModel( - 1, - 1, - "ETH_USDT", - 0.001, - 100000.0, - 100000.0, - 0.001, - 0.001, - "", - "", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "99289106-2775-44d4-bffc-ca35fc25e58c", - "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", - "52c6d890-3dd4-4fa8-9425-d9e0d6274705", - "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) - ) - ) + } doReturn Flux.just(Valid.TRADE_MODEL) } stubbing(orderRepository) { on { findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) } val priceTickerResponse = marketQueryHandler.lastPrice("ETH_USDT") @@ -241,96 +103,23 @@ class MarketQueryHandlerTest { on { findBySymbolSortDescendingByCreateDate("ETH_USDT", 10) } doReturn flow { - emit( - TradeModel( - 1, - 1, - "ETH_USDT", - 0.001, - 100000.0, - 100000.0, - 0.001, - 0.001, - "", - "", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "99289106-2775-44d4-bffc-ca35fc25e58c", - "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", - "52c6d890-3dd4-4fa8-9425-d9e0d6274705", - "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) - ) - ) + emit(Valid.TRADE_MODEL) } } stubbing(orderRepository) { on { findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) on { findByOuid("2fa73fa2-6d70-44b8-8571-e2b24e2eea2b") - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) } + val marketTradeResponses = marketQueryHandler.recentTrades("ETH_USDT", 10) assertThat(marketTradeResponses).isNotNull assertThat(marketTradeResponses.count()).isEqualTo(1) - assertThat(marketTradeResponses.first()).isEqualTo( - MarketTradeResponse( - "ETH_USDT", - 1, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.001), - BigDecimal.valueOf(0.001), - Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - true, - true - ) - ) + assertThat(marketTradeResponses.first()).isEqualTo(Valid.MARKET_TRADE_RESPONSE) } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt index a6ad4c243..74bc99c42 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -1,15 +1,8 @@ package co.nilin.opex.api.ports.postgres.impl -import co.nilin.opex.api.core.event.RichOrder -import co.nilin.opex.api.core.event.RichOrderUpdate -import co.nilin.opex.api.core.inout.MatchConstraint -import co.nilin.opex.api.core.inout.MatchingOrderType -import co.nilin.opex.api.core.inout.OrderDirection -import co.nilin.opex.api.core.inout.OrderStatus import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository -import co.nilin.opex.api.ports.postgres.model.OrderModel -import co.nilin.opex.api.ports.postgres.model.OrderStatusModel +import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test @@ -18,9 +11,6 @@ import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import reactor.core.publisher.Mono -import java.math.BigDecimal -import java.time.LocalDateTime -import java.time.ZoneOffset class OrderPersisterTest { private val orderRepository: OrderRepository = mock() @@ -29,95 +19,28 @@ class OrderPersisterTest { @Test fun givenRichOrder_whenSave_thenSuccess(): Unit = runBlocking { - val richOrder = RichOrder( - null, - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "1", - BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0.0001), - BigDecimal.valueOf(0.01), - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - BigDecimal.valueOf(1000001), - BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - 0 - ) stubbing(orderRepository) { on { save(any()) - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) } stubbing(orderStatusRepository) { on { save(any()) - } doReturn Mono.just( - OrderStatusModel( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 0.0, // ? - 0.0, // ? - 0, // ? - 0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) } - assertThatNoException().isThrownBy { runBlocking { orderPersister.save(richOrder) } } + assertThatNoException().isThrownBy { runBlocking { orderPersister.save(Valid.RICH_ORDER) } } } @Test fun givenRichOrder_whenUpdate_thenSuccess(): Unit = runBlocking { - val richOrderUpdate = RichOrderUpdate( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - BigDecimal.valueOf(1000001), - BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0.08), - OrderStatus.PARTIALLY_FILLED - ) stubbing(orderStatusRepository) { on { save(any()) - } doReturn Mono.just( - OrderStatusModel( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 0.0, // ? - 0.0, // ? - 0, // ? - 0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) } - assertThatNoException().isThrownBy { runBlocking { orderPersister.update(richOrderUpdate) } } + assertThatNoException().isThrownBy { runBlocking { orderPersister.update(Valid.RICH_ORDER_UPDATE) } } } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt index 5c531434e..6208c7166 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt @@ -1,14 +1,14 @@ package co.nilin.opex.api.ports.postgres.impl import co.nilin.opex.api.ports.postgres.dao.SymbolMapRepository -import co.nilin.opex.api.ports.postgres.model.SymbolMapModel +import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid import kotlinx.coroutines.runBlocking -import org.junit.jupiter.api.Test -import org.mockito.kotlin.mock -import org.assertj.core.api.Assertions.* +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import reactor.core.publisher.Flux import reactor.core.publisher.Mono @@ -23,34 +23,13 @@ class SymbolMapperTest { stubbing(symbolMapRepository) { on { findByAliasKeyAndAlias("binance", "ETHUSDT") - } doReturn Mono.just( - SymbolMapModel( - 1, - "ETH_USDT", - "binance", - "ETHUSDT" - ) - ) + } doReturn Mono.just(Valid.SYMBOL_MAP_MODEL) on { findByAliasKeyAndSymbol("binance", "ETH_USDT") - } doReturn Mono.just( - SymbolMapModel( - 1, - "ETH_USDT", - "binance", - "ETHUSDT" - ) - ) + } doReturn Mono.just(Valid.SYMBOL_MAP_MODEL) on { findAll() - } doReturn Flux.just( - SymbolMapModel( - 1, - "ETH_USDT", - "binance", - "ETHUSDT" - ) - ) + } doReturn Flux.just(Valid.SYMBOL_MAP_MODEL) } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt index 976af9201..31fedcbe1 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -1,9 +1,7 @@ package co.nilin.opex.api.ports.postgres.impl -import co.nilin.opex.api.core.event.RichTrade -import co.nilin.opex.api.core.inout.OrderDirection import co.nilin.opex.api.ports.postgres.dao.TradeRepository -import co.nilin.opex.api.ports.postgres.model.TradeModel +import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test @@ -12,9 +10,6 @@ import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import reactor.core.publisher.Mono -import java.math.BigDecimal -import java.time.LocalDateTime -import java.time.ZoneOffset class TradePersisterTest { private val tradeRepository: TradeRepository = mock() @@ -22,57 +17,12 @@ class TradePersisterTest { @Test fun givenRichTrade_whenSave_thenSuccess(): Unit = runBlocking { - val richOrder = RichTrade( - 1, // ? - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - 1, - OrderDirection.ASK, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - "", // ? - "26931efc-891b-4599-9921-1d265829b410", - "5296a097-6478-464f-91a6-5c434ac4207d", - 2, - OrderDirection.ASK, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - "", // ? - BigDecimal.valueOf(0), // ? - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) - ) stubbing(tradeRepository) { on { save(any()) - } doReturn Mono.just( - TradeModel( - 1, - 1, - "ETH_USDT", - 0.001, - 100000.0, - 100000.0, - 0.001, - 0.001, - "", - "", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "99289106-2775-44d4-bffc-ca35fc25e58c", - "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", - "52c6d890-3dd4-4fa8-9425-d9e0d6274705", - "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.TRADE_MODEL) } - assertThatNoException().isThrownBy { runBlocking { tradePersister.save(richOrder) } } + assertThatNoException().isThrownBy { runBlocking { tradePersister.save(Valid.RICH_TRADE) } } } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index cec11e73e..0298db73a 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -1,150 +1,65 @@ package co.nilin.opex.api.ports.postgres.impl -import co.nilin.opex.api.core.inout.* import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository -import co.nilin.opex.api.ports.postgres.model.OrderModel -import co.nilin.opex.api.ports.postgres.model.OrderStatusModel -import co.nilin.opex.api.ports.postgres.model.TradeModel +import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid import kotlinx.coroutines.flow.count import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.mockito.kotlin.mock -import java.security.Principal -import java.time.LocalDateTime -import java.time.ZoneOffset -import java.util.* -import org.assertj.core.api.Assertions.* import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import reactor.core.publisher.Mono -import java.math.BigDecimal class UserQueryHandlerTest { private val orderRepository: OrderRepository = mock() private val tradeRepository: TradeRepository = mock() private val orderStatusRepository: OrderStatusRepository = mock() private val userQueryHandler = UserQueryHandlerImpl(orderRepository, tradeRepository, orderStatusRepository) - private val principal: Principal = Principal { "98c7ca9b-2d9c-46dd-afa8-b0cd2f52a97c" } @Test fun given_whenAllOrders_then(): Unit = runBlocking { - val allOrderRequest = AllOrderRequest( - "ETH_USDT", - Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - 500 - ) stubbing(orderRepository) { on { findByUuidAndSymbolAndTimeBetween( - principal.name, - allOrderRequest.symbol, - allOrderRequest.startTime, - allOrderRequest.endTime + Valid.PRINCIPAL.name, + Valid.ALL_ORDER_REQUEST.symbol, + Valid.ALL_ORDER_REQUEST.startTime, + Valid.ALL_ORDER_REQUEST.endTime ) } doReturn flow { - emit( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + emit(Valid.ORDER_MODEL) } } - val queryOrderResponses = userQueryHandler.allOrders(principal, allOrderRequest) + val queryOrderResponses = userQueryHandler.allOrders(Valid.PRINCIPAL, Valid.ALL_ORDER_REQUEST) assertThat(queryOrderResponses).isNotNull assertThat(queryOrderResponses.count()).isEqualTo(1) - assertThat(queryOrderResponses.first()).isEqualTo( - QueryOrderResponse( - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 1, - 1, // ?, - "id", - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.001), - BigDecimal.valueOf(100), - BigDecimal.valueOf(1), - OrderStatus.FILLED, - TimeInForce.GTC, - OrderType.LIMIT, - OrderSide.BUY, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(100000), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - true, - BigDecimal.valueOf(0) - ) - ) + assertThat(queryOrderResponses.first()).isEqualTo(Valid.QUERY_ORDER_RESPONSE) } @Test fun given_whenAllTrades_then(): Unit = runBlocking { - val tradeRequest = TradeRequest( - "ETH_USDT", - 1, - Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - 500 - ) stubbing(tradeRepository) { on { findByUuidAndSymbolAndTimeBetweenAndTradeIdGreaterThan( - principal.name, - tradeRequest.symbol, + Valid.PRINCIPAL.name, + Valid.TRADE_REQUEST.symbol, 1, - tradeRequest.startTime, - tradeRequest.endTime + Valid.TRADE_REQUEST.startTime, + Valid.TRADE_REQUEST.endTime ) } doReturn flow { - emit( - TradeModel( - 1, - 1, - "ETH_USDT", - 0.001, - 100000.0, - 100000.0, - 0.001, - 0.001, - "", - "", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "99289106-2775-44d4-bffc-ca35fc25e58c", - "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", - "52c6d890-3dd4-4fa8-9425-d9e0d6274705", - "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) - ) - ) + emit(Valid.TRADE_MODEL) } } - val tradeResponses = userQueryHandler.allTrades(principal, tradeRequest) + val tradeResponses = userQueryHandler.allTrades(Valid.PRINCIPAL, Valid.TRADE_REQUEST) assertThat(tradeResponses).isNotNull assertThat(tradeResponses.count()).isEqualTo(1) @@ -154,35 +69,13 @@ class UserQueryHandlerTest { fun given_whenOpenOrders_then(): Unit = runBlocking { stubbing(orderRepository) { on { - findByUuidAndSymbolAndStatus(principal.name, "ETH_USDT", listOf(0)) + findByUuidAndSymbolAndStatus(Valid.PRINCIPAL.name, "ETH_USDT", listOf(0)) } doReturn flow { - emit( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + emit(Valid.ORDER_MODEL) } } - val queryOrderResponses = userQueryHandler.openOrders(principal, "ETH_USDT") + val queryOrderResponses = userQueryHandler.openOrders(Valid.PRINCIPAL, "ETH_USDT") assertThat(queryOrderResponses).isNotNull assertThat(queryOrderResponses.count()).isEqualTo(1) @@ -190,79 +83,21 @@ class UserQueryHandlerTest { @Test fun given_whenQueryOrder_then(): Unit = runBlocking { - val queryOrderRequest = QueryOrderRequest( - "ETH_USDT", - 1, - "2" // ? - ) stubbing(orderRepository) { on { findBySymbolAndClientOrderId("ETH_USDT", "id") - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) on { findBySymbolAndOrderId("ETH_USDT", 1) - } doReturn Mono.just( - OrderModel( - 1, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", - "id", // ? - "ETH_USDT", - 1, - 0.01, - 0.01, - 0.0001, - 0.01, - "1", - OrderDirection.ASK, - MatchConstraint.GTC, - MatchingOrderType.LIMIT_ORDER, - 100000.0, - 0.01, - 0.0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_MODEL) } stubbing(orderStatusRepository) { on { findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") - } doReturn Mono.just( - OrderStatusModel( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 0.0, // ? - 0.0, // ? - 0, // ? - 0, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) - ) - ) + } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) } - val queryOrderResponse = userQueryHandler.queryOrder(principal, queryOrderRequest) + val queryOrderResponse = userQueryHandler.queryOrder(Valid.PRINCIPAL, Valid.QUERY_ORDER_REQUEST) assertThat(queryOrderResponse).isNotNull } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt new file mode 100644 index 000000000..f1a63807f --- /dev/null +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt @@ -0,0 +1,196 @@ +package co.nilin.opex.api.ports.postgres.impl.testfixtures + +import co.nilin.opex.api.core.event.RichOrder +import co.nilin.opex.api.core.event.RichOrderUpdate +import co.nilin.opex.api.core.event.RichTrade +import co.nilin.opex.api.core.inout.* +import co.nilin.opex.api.ports.postgres.model.OrderModel +import co.nilin.opex.api.ports.postgres.model.OrderStatusModel +import co.nilin.opex.api.ports.postgres.model.SymbolMapModel +import co.nilin.opex.api.ports.postgres.model.TradeModel +import java.math.BigDecimal +import java.security.Principal +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.util.* + +object Valid { + val PRINCIPAL = Principal { "98c7ca9b-2d9c-46dd-afa8-b0cd2f52a97c" } + + val ORDER_MODEL = OrderModel( + 1, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + + val ORDER_STATUS_MODEL = OrderStatusModel( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 0.0, // ? + 0.0, // ? + 0, // ? + 0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + + val SYMBOL_MAP_MODEL = SymbolMapModel( + 1, + "ETH_USDT", + "binance", + "ETHUSDT" + ) + + val TRADE_MODEL = TradeModel( + 1, + 1, + "ETH_USDT", + 0.001, + 100000.0, + 100000.0, + 0.001, + 0.001, + "", + "", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), + "99289106-2775-44d4-bffc-ca35fc25e58c", + "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", + "52c6d890-3dd4-4fa8-9425-d9e0d6274705", + "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + + val QUERY_ORDER_RESPONSE = QueryOrderResponse( + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + 1, + 1, // ?, + "id", + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + BigDecimal.valueOf(100), + BigDecimal.valueOf(1), + OrderStatus.FILLED, + TimeInForce.GTC, + OrderType.LIMIT, + OrderSide.BUY, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(100000), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + true, + BigDecimal.valueOf(0) + ) + + val AGGREGATED_ORDER_PRICE_MODEL = AggregatedOrderPriceModel( + 100000.0, + 0.001 + ) + + val ORDER_BOOK_RESPONSE = OrderBookResponse( + BigDecimal.valueOf(100000.0), + BigDecimal.valueOf(0.001) + ) + + val RICH_ORDER = RichOrder( + null, + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + "1", + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0.0001), + BigDecimal.valueOf(0.01), + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + BigDecimal.valueOf(1000001), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + 0 + ) + + val RICH_ORDER_UPDATE = RichOrderUpdate( + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + BigDecimal.valueOf(1000001), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0.08), + OrderStatus.PARTIALLY_FILLED + ) + + val RICH_TRADE = RichTrade( + 1, // ? + "ETH_USDT", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "18013d13-0568-496b-b93b-2524c8132b93", + 1, + OrderDirection.ASK, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + "", // ? + "26931efc-891b-4599-9921-1d265829b410", + "5296a097-6478-464f-91a6-5c434ac4207d", + 2, + OrderDirection.ASK, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.01), + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), // ? + "", // ? + BigDecimal.valueOf(0), // ? + LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + ) + + val ALL_ORDER_REQUEST = AllOrderRequest( + "ETH_USDT", + Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + 500 + ) + + val TRADE_REQUEST = TradeRequest( + "ETH_USDT", + 1, + Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + 500 + ) + + val MARKET_TRADE_RESPONSE = MarketTradeResponse( + "ETH_USDT", + 1, + BigDecimal.valueOf(100000), + BigDecimal.valueOf(0.001), + BigDecimal.valueOf(0.001), + Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + isBestMatch = true, + isMakerBuyer = true + ) + + val QUERY_ORDER_REQUEST = QueryOrderRequest( + "ETH_USDT", + 1, + "2" // ? + ) +} \ No newline at end of file From 3197d82c03f0992aac82e12d1147a63382753111 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 15:31:45 +0430 Subject: [PATCH 43/58] maven: Skip tests on build --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index abace960d..a3c5afdcc 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ 1.6.0 2.6.2 2021.0.0 - false + true From 51e09fc3182b5e55d59eeeda630910cea326fdd3 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 15:44:18 +0430 Subject: [PATCH 44/58] maven: Fix mockito-kotlin dependencies --- bc-gateway/bc-gateway-core/pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/bc-gateway/bc-gateway-core/pom.xml b/bc-gateway/bc-gateway-core/pom.xml index 619be8c6d..5ce203837 100644 --- a/bc-gateway/bc-gateway-core/pom.xml +++ b/bc-gateway/bc-gateway-core/pom.xml @@ -47,8 +47,6 @@ org.mockito.kotlin mockito-kotlin - ${mockito-kotlin.version} - test co.nilin.opex.utility.error From d48465b305706066324ea7de68d8a92311a3e18d Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 15:44:41 +0430 Subject: [PATCH 45/58] maven: Fix mockito-kotlin dependencies --- bc-gateway/bc-gateway-core/pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bc-gateway/bc-gateway-core/pom.xml b/bc-gateway/bc-gateway-core/pom.xml index 5ce203837..e82998eae 100644 --- a/bc-gateway/bc-gateway-core/pom.xml +++ b/bc-gateway/bc-gateway-core/pom.xml @@ -14,10 +14,6 @@ bc-gateway-core Blockchain gateway core of Opex - - 4.0.0 - - org.jetbrains.kotlin From 4ef372610031bd6a6715b0f4f726e5c4b58e1277 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 16:27:37 +0430 Subject: [PATCH 46/58] api: Refactor market query handler tests --- .../postgres/impl/MarketQueryHandlerTest.kt | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index 2368bf51f..87b918dd0 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -1,6 +1,7 @@ package co.nilin.opex.api.ports.postgres.impl import co.nilin.opex.api.core.inout.OrderDirection +import co.nilin.opex.api.core.inout.OrderStatus import co.nilin.opex.api.core.spi.SymbolMapper import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository @@ -12,9 +13,7 @@ import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.mock -import org.mockito.kotlin.stubbing +import org.mockito.kotlin.* import reactor.core.publisher.Flux import reactor.core.publisher.Mono @@ -30,10 +29,20 @@ class MarketQueryHandlerTest { fun givenSymbol_whenOpenASKOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { stubbing(orderRepository) { on { - findBySymbolAndDirectionAndStatusSortAscendingByPrice("ETH_USDT", OrderDirection.BID, 1, listOf(0)) + findBySymbolAndDirectionAndStatusSortAscendingByPrice( + eq("ETH_USDT"), + eq(OrderDirection.ASK), + eq(1), + argThat { + this == listOf( + OrderStatus.NEW.code, + OrderStatus.PARTIALLY_FILLED.code + ) + } + ) } doReturn Flux.just(Valid.AGGREGATED_ORDER_PRICE_MODEL) } - val orderBookResponses = marketQueryHandler.openAskOrders("ETH_USDT", 10) + val orderBookResponses = marketQueryHandler.openAskOrders("ETH_USDT", 1) assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) @@ -44,11 +53,21 @@ class MarketQueryHandlerTest { fun givenSymbol_whenOpenBIDOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { stubbing(orderRepository) { on { - findBySymbolAndDirectionAndStatusSortDescendingByPrice("ETH_USDT", OrderDirection.BID, 1, listOf(0)) + findBySymbolAndDirectionAndStatusSortDescendingByPrice( + eq("ETH_USDT"), + eq(OrderDirection.BID), + eq(1), + argThat { + this == listOf( + OrderStatus.NEW.code, + OrderStatus.PARTIALLY_FILLED.code + ) + } + ) } doReturn Flux.just(Valid.AGGREGATED_ORDER_PRICE_MODEL) } - val orderBookResponses = marketQueryHandler.openBidOrders("ETH_USDT", 10) + val orderBookResponses = marketQueryHandler.openBidOrders("ETH_USDT", 1) assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) @@ -89,11 +108,16 @@ class MarketQueryHandlerTest { findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") } doReturn Mono.just(Valid.ORDER_MODEL) } + stubbing(symbolMapper) { + onBlocking { + map("ETH_USDT") + } doReturn "ETHUSDT" + } val priceTickerResponse = marketQueryHandler.lastPrice("ETH_USDT") assertThat(priceTickerResponse).isNotNull assertThat(priceTickerResponse.size).isEqualTo(1) - assertThat(priceTickerResponse.first().symbol).isEqualTo("ETH_USDT") + assertThat(priceTickerResponse.first().symbol).isEqualTo("ETHUSDT") assertThat(priceTickerResponse.first().price).isEqualTo(100000) } From 30ec97c4b82fa7784d28f60da21cb15ad83e5f10 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 16:47:30 +0430 Subject: [PATCH 47/58] api: Refactor user query handler tests --- .../postgres/impl/MarketQueryHandlerTest.kt | 8 ++-- .../ports/postgres/impl/OrderPersisterTest.kt | 2 +- .../postgres/impl/UserQueryHandlerTest.kt | 45 +++++++++++++++---- .../postgres/impl/testfixtures/GivenValid.kt | 26 ++++++++++- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index 87b918dd0..e0e00ca40 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -79,7 +79,7 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findLastOrderBySymbol("ETH_USDT") - } doReturn Mono.just(Valid.ORDER_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(orderStatusRepository) { on { @@ -106,7 +106,7 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") - } doReturn Mono.just(Valid.ORDER_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(symbolMapper) { onBlocking { @@ -133,10 +133,10 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") - } doReturn Mono.just(Valid.ORDER_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) on { findByOuid("2fa73fa2-6d70-44b8-8571-e2b24e2eea2b") - } doReturn Mono.just(Valid.ORDER_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } val marketTradeResponses = marketQueryHandler.recentTrades("ETH_USDT", 10) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt index 74bc99c42..9f272739c 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -22,7 +22,7 @@ class OrderPersisterTest { stubbing(orderRepository) { on { save(any()) - } doReturn Mono.just(Valid.ORDER_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(orderStatusRepository) { on { diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index 0298db73a..5520982f9 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -1,5 +1,6 @@ package co.nilin.opex.api.ports.postgres.impl +import co.nilin.opex.api.core.inout.OrderStatus import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository @@ -10,9 +11,7 @@ import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.mock -import org.mockito.kotlin.stubbing +import org.mockito.kotlin.* import reactor.core.publisher.Mono class UserQueryHandlerTest { @@ -32,9 +31,14 @@ class UserQueryHandlerTest { Valid.ALL_ORDER_REQUEST.endTime ) } doReturn flow { - emit(Valid.ORDER_MODEL) + emit(Valid.MAKER_ORDER_MODEL) } } + stubbing(orderStatusRepository) { + on { + findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } val queryOrderResponses = userQueryHandler.allOrders(Valid.PRINCIPAL, Valid.ALL_ORDER_REQUEST) @@ -58,6 +62,14 @@ class UserQueryHandlerTest { emit(Valid.TRADE_MODEL) } } + stubbing(orderRepository) { + on { + findByOuid(Valid.TRADE_MODEL.makerOuid) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) + on { + findByOuid(Valid.TRADE_MODEL.takerOuid) + } doReturn Mono.just(Valid.TAKER_ORDER_MODEL) + } val tradeResponses = userQueryHandler.allTrades(Valid.PRINCIPAL, Valid.TRADE_REQUEST) @@ -69,11 +81,25 @@ class UserQueryHandlerTest { fun given_whenOpenOrders_then(): Unit = runBlocking { stubbing(orderRepository) { on { - findByUuidAndSymbolAndStatus(Valid.PRINCIPAL.name, "ETH_USDT", listOf(0)) + findByUuidAndSymbolAndStatus( + eq(Valid.PRINCIPAL.name), + eq("ETH_USDT"), + argThat { + this == listOf( + OrderStatus.NEW.code, + OrderStatus.PARTIALLY_FILLED.code + ) + } + ) } doReturn flow { - emit(Valid.ORDER_MODEL) + emit(Valid.MAKER_ORDER_MODEL) } } + stubbing(orderStatusRepository) { + on { + findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } val queryOrderResponses = userQueryHandler.openOrders(Valid.PRINCIPAL, "ETH_USDT") @@ -85,11 +111,11 @@ class UserQueryHandlerTest { fun given_whenQueryOrder_then(): Unit = runBlocking { stubbing(orderRepository) { on { - findBySymbolAndClientOrderId("ETH_USDT", "id") - } doReturn Mono.just(Valid.ORDER_MODEL) + findBySymbolAndClientOrderId("ETH_USDT", "2") + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) on { findBySymbolAndOrderId("ETH_USDT", 1) - } doReturn Mono.just(Valid.ORDER_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(orderStatusRepository) { on { @@ -98,6 +124,7 @@ class UserQueryHandlerTest { } val queryOrderResponse = userQueryHandler.queryOrder(Valid.PRINCIPAL, Valid.QUERY_ORDER_REQUEST) + assertThat(queryOrderResponse).isNotNull } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt index f1a63807f..85c9d7522 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt @@ -17,10 +17,32 @@ import java.util.* object Valid { val PRINCIPAL = Principal { "98c7ca9b-2d9c-46dd-afa8-b0cd2f52a97c" } - val ORDER_MODEL = OrderModel( + val MAKER_ORDER_MODEL = OrderModel( 1, "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", + PRINCIPAL.name, + "id", // ? + "ETH_USDT", + 1, + 0.01, + 0.01, + 0.0001, + 0.01, + "1", + OrderDirection.ASK, + MatchConstraint.GTC, + MatchingOrderType.LIMIT_ORDER, + 100000.0, + 0.01, + 0.0, // ? + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + ) + + val TAKER_ORDER_MODEL = OrderModel( + 2, + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + PRINCIPAL.name, "id", // ? "ETH_USDT", 1, From 3a3dc23823bf6a883b97a7f743509fa6358e257e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 22 May 2022 18:12:47 +0430 Subject: [PATCH 48/58] api: Refactor market query handler tests --- .../postgres/impl/MarketQueryHandlerTest.kt | 14 ++-- .../postgres/impl/testfixtures/GivenValid.kt | 71 ++++++++++--------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index e0e00ca40..0fbaf6824 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -71,7 +71,7 @@ class MarketQueryHandlerTest { assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) - assertThat(orderBookResponses.first()).isEqualTo(Valid.ORDER_STATUS_MODEL) + assertThat(orderBookResponses.first()).isEqualTo(Valid.ORDER_BOOK_RESPONSE) } @Test @@ -105,7 +105,7 @@ class MarketQueryHandlerTest { } stubbing(orderRepository) { on { - findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") + findByOuid(Valid.MAKER_ORDER_MODEL.ouid) } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(symbolMapper) { @@ -125,21 +125,21 @@ class MarketQueryHandlerTest { fun givenSymbol_whenRecentTrades_thenMarketTradeResponseFlow(): Unit = runBlocking { stubbing(tradeRepository) { on { - findBySymbolSortDescendingByCreateDate("ETH_USDT", 10) + findBySymbolSortDescendingByCreateDate("ETH_USDT", 1) } doReturn flow { emit(Valid.TRADE_MODEL) } } stubbing(orderRepository) { on { - findByOuid("99289106-2775-44d4-bffc-ca35fc25e58c") + findByOuid(Valid.TRADE_MODEL.makerOuid) } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) on { - findByOuid("2fa73fa2-6d70-44b8-8571-e2b24e2eea2b") - } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) + findByOuid(Valid.TRADE_MODEL.takerOuid) + } doReturn Mono.just(Valid.TAKER_ORDER_MODEL) } - val marketTradeResponses = marketQueryHandler.recentTrades("ETH_USDT", 10) + val marketTradeResponses = marketQueryHandler.recentTrades("ETH_USDT", 1) assertThat(marketTradeResponses).isNotNull assertThat(marketTradeResponses.count()).isEqualTo(1) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt index 85c9d7522..1e1f6f3ec 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt @@ -8,6 +8,7 @@ import co.nilin.opex.api.ports.postgres.model.OrderModel import co.nilin.opex.api.ports.postgres.model.OrderStatusModel import co.nilin.opex.api.ports.postgres.model.SymbolMapModel import co.nilin.opex.api.ports.postgres.model.TradeModel +import co.nilin.opex.api.ports.postgres.util.isWorking import java.math.BigDecimal import java.security.Principal import java.time.LocalDateTime @@ -21,11 +22,11 @@ object Valid { 1, "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", PRINCIPAL.name, - "id", // ? + null, // Binance "ETH_USDT", - 1, - 0.01, - 0.01, + 1, // MatchingEngine ID + 0.01, // Calculated? + 0.01, // Calculated? 0.0001, 0.01, "1", @@ -33,17 +34,17 @@ object Valid { MatchConstraint.GTC, MatchingOrderType.LIMIT_ORDER, 100000.0, - 0.01, - 0.0, // ? + 0.001, + 100000.0 * 0.01, LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) ) val TAKER_ORDER_MODEL = OrderModel( 2, - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "157b9b4a-cc66-43b9-b30b-40a8b66ea6aa", PRINCIPAL.name, - "id", // ? + null, "ETH_USDT", 1, 0.01, @@ -55,18 +56,18 @@ object Valid { MatchConstraint.GTC, MatchingOrderType.LIMIT_ORDER, 100000.0, - 0.01, - 0.0, // ? + 0.001, + 100000.0 * 0.01, // ? LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) ) val ORDER_STATUS_MODEL = OrderStatusModel( "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - 0.0, // ? - 0.0, // ? - 0, // ? - 0, // ? + 0.0, // Filled amount + 0.0, // --> See accountant + OrderStatus.FILLED.code, + OrderStatus.FILLED.orderOfAppearance, LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) ) @@ -81,18 +82,18 @@ object Valid { 1, 1, "ETH_USDT", - 0.001, + 0.001, // Minimum of orders quantities 100000.0, 100000.0, - 0.001, - 0.001, - "", - "", + 0.001, // Calculated + 0.001, // Calculated + "ETH", + "USDT", LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "99289106-2775-44d4-bffc-ca35fc25e58c", - "2fa73fa2-6d70-44b8-8571-e2b24e2eea2b", - "52c6d890-3dd4-4fa8-9425-d9e0d6274705", - "07bb979a-dfca-475b-a38b-fcc5dd2f88d8", + "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + "157b9b4a-cc66-43b9-b30b-40a8b66ea6aa", + PRINCIPAL.name, + PRINCIPAL.name, LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) ) @@ -100,22 +101,22 @@ object Valid { "ETH_USDT", "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", 1, - 1, // ?, - "id", - BigDecimal.valueOf(100000), + -1, // Binance + "", // Binance + BigDecimal.valueOf(100000.0), BigDecimal.valueOf(0.001), - BigDecimal.valueOf(100), - BigDecimal.valueOf(1), + BigDecimal.valueOf(0.0), + BigDecimal.valueOf(0.0), OrderStatus.FILLED, TimeInForce.GTC, OrderType.LIMIT, - OrderSide.BUY, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(100000), + OrderSide.SELL, + null, + null, Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - true, - BigDecimal.valueOf(0) + OrderStatus.FILLED.isWorking(), + BigDecimal.valueOf(100000.0 * 0.001) ) val AGGREGATED_ORDER_PRICE_MODEL = AggregatedOrderPriceModel( @@ -186,8 +187,8 @@ object Valid { val ALL_ORDER_REQUEST = AllOrderRequest( "ETH_USDT", - Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125740, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(LocalDateTime.ofEpochSecond(1653125940, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), 500 ) From acf35432904297663d7e58428415f2911a59ab02 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 20:42:16 +0430 Subject: [PATCH 49/58] api: Improve tests sample data --- .../postgres/impl/MarketQueryHandlerTest.kt | 8 +- .../ports/postgres/impl/OrderPersisterTest.kt | 6 +- .../ports/postgres/impl/SymbolMapperTest.kt | 2 +- .../ports/postgres/impl/TradePersisterTest.kt | 2 +- .../postgres/impl/UserQueryHandlerTest.kt | 10 +- .../GivenValid.kt => sample/Samples.kt} | 144 ++++++++++-------- 6 files changed, 94 insertions(+), 78 deletions(-) rename api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/{testfixtures/GivenValid.kt => sample/Samples.kt} (57%) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index 0fbaf6824..ec72e98e3 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -6,7 +6,7 @@ import co.nilin.opex.api.core.spi.SymbolMapper import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository -import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid +import co.nilin.opex.api.ports.postgres.impl.sample.Valid import kotlinx.coroutines.flow.count import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow @@ -84,13 +84,13 @@ class MarketQueryHandlerTest { stubbing(orderStatusRepository) { on { findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") - } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } val queryOrderResponse = marketQueryHandler.lastOrder("ETH_USDT") assertThat(queryOrderResponse).isNotNull - assertThat(queryOrderResponse).isEqualTo(Valid.QUERY_ORDER_RESPONSE) + assertThat(queryOrderResponse).isEqualTo(Valid.MAKER_QUERY_ORDER_RESPONSE) } @Test @@ -118,7 +118,7 @@ class MarketQueryHandlerTest { assertThat(priceTickerResponse).isNotNull assertThat(priceTickerResponse.size).isEqualTo(1) assertThat(priceTickerResponse.first().symbol).isEqualTo("ETHUSDT") - assertThat(priceTickerResponse.first().price).isEqualTo(100000) + assertThat(priceTickerResponse.first().price).isEqualTo("100000.0") } @Test diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt index 9f272739c..57fcffab7 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -2,7 +2,7 @@ package co.nilin.opex.api.ports.postgres.impl import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository -import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid +import co.nilin.opex.api.ports.postgres.impl.sample.Valid import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test @@ -27,7 +27,7 @@ class OrderPersisterTest { stubbing(orderStatusRepository) { on { save(any()) - } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } assertThatNoException().isThrownBy { runBlocking { orderPersister.save(Valid.RICH_ORDER) } } @@ -38,7 +38,7 @@ class OrderPersisterTest { stubbing(orderStatusRepository) { on { save(any()) - } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } assertThatNoException().isThrownBy { runBlocking { orderPersister.update(Valid.RICH_ORDER_UPDATE) } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt index 6208c7166..89ce5d5b5 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt @@ -1,7 +1,7 @@ package co.nilin.opex.api.ports.postgres.impl import co.nilin.opex.api.ports.postgres.dao.SymbolMapRepository -import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid +import co.nilin.opex.api.ports.postgres.impl.sample.Valid import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeAll diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt index 31fedcbe1..9c8a3c9fb 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -1,7 +1,7 @@ package co.nilin.opex.api.ports.postgres.impl import co.nilin.opex.api.ports.postgres.dao.TradeRepository -import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid +import co.nilin.opex.api.ports.postgres.impl.sample.Valid import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThatNoException import org.junit.jupiter.api.Test diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index 5520982f9..4f4c971d3 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -4,7 +4,7 @@ import co.nilin.opex.api.core.inout.OrderStatus import co.nilin.opex.api.ports.postgres.dao.OrderRepository import co.nilin.opex.api.ports.postgres.dao.OrderStatusRepository import co.nilin.opex.api.ports.postgres.dao.TradeRepository -import co.nilin.opex.api.ports.postgres.impl.testfixtures.Valid +import co.nilin.opex.api.ports.postgres.impl.sample.Valid import kotlinx.coroutines.flow.count import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow @@ -37,14 +37,14 @@ class UserQueryHandlerTest { stubbing(orderStatusRepository) { on { findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") - } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } val queryOrderResponses = userQueryHandler.allOrders(Valid.PRINCIPAL, Valid.ALL_ORDER_REQUEST) assertThat(queryOrderResponses).isNotNull assertThat(queryOrderResponses.count()).isEqualTo(1) - assertThat(queryOrderResponses.first()).isEqualTo(Valid.QUERY_ORDER_RESPONSE) + assertThat(queryOrderResponses.first()).isEqualTo(Valid.MAKER_QUERY_ORDER_RESPONSE) } @Test @@ -98,7 +98,7 @@ class UserQueryHandlerTest { stubbing(orderStatusRepository) { on { findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") - } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } val queryOrderResponses = userQueryHandler.openOrders(Valid.PRINCIPAL, "ETH_USDT") @@ -120,7 +120,7 @@ class UserQueryHandlerTest { stubbing(orderStatusRepository) { on { findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") - } doReturn Mono.just(Valid.ORDER_STATUS_MODEL) + } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } val queryOrderResponse = userQueryHandler.queryOrder(Valid.PRINCIPAL, Valid.QUERY_ORDER_REQUEST) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt similarity index 57% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt rename to api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt index 1e1f6f3ec..4c5f38f46 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/testfixtures/GivenValid.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt @@ -1,4 +1,4 @@ -package co.nilin.opex.api.ports.postgres.impl.testfixtures +package co.nilin.opex.api.ports.postgres.impl.sample import co.nilin.opex.api.core.event.RichOrder import co.nilin.opex.api.core.event.RichOrderUpdate @@ -16,6 +16,13 @@ import java.time.ZoneOffset import java.util.* object Valid { + private const val PAIR_SYMBOL = "ETH_USDT" + private const val TIMESTAMP = 1653125840L + private val CREATE_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP, 0, ZoneOffset.UTC) + private val UPDATE_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP + 180, 0, ZoneOffset.UTC) + private val FROM_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP - 600, 0, ZoneOffset.UTC) + private val TO_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP + 600, 0, ZoneOffset.UTC) + val PRINCIPAL = Principal { "98c7ca9b-2d9c-46dd-afa8-b0cd2f52a97c" } val MAKER_ORDER_MODEL = OrderModel( @@ -23,7 +30,7 @@ object Valid { "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", PRINCIPAL.name, null, // Binance - "ETH_USDT", + PAIR_SYMBOL, 1, // MatchingEngine ID 0.01, // Calculated? 0.01, // Calculated? @@ -35,9 +42,9 @@ object Valid { MatchingOrderType.LIMIT_ORDER, 100000.0, 0.001, - 100000.0 * 0.01, - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + 100000.0 * 0.001, + CREATE_DATE, + UPDATE_DATE ) val TAKER_ORDER_MODEL = OrderModel( @@ -45,43 +52,52 @@ object Valid { "157b9b4a-cc66-43b9-b30b-40a8b66ea6aa", PRINCIPAL.name, null, - "ETH_USDT", - 1, + PAIR_SYMBOL, + 2, 0.01, 0.01, 0.0001, 0.01, "1", - OrderDirection.ASK, + OrderDirection.BID, MatchConstraint.GTC, MatchingOrderType.LIMIT_ORDER, 100000.0, 0.001, - 100000.0 * 0.01, // ? - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC), - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + 100000.0 * 0.01, + CREATE_DATE, + UPDATE_DATE ) - val ORDER_STATUS_MODEL = OrderStatusModel( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + val MAKER_ORDER_STATUS_MODEL = OrderStatusModel( + MAKER_ORDER_MODEL.ouid, 0.0, // Filled amount 0.0, // --> See accountant OrderStatus.FILLED.code, OrderStatus.FILLED.orderOfAppearance, - LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC) + CREATE_DATE + ) + + val TAKER_ORDER_STATUS_MODEL = OrderStatusModel( + TAKER_ORDER_MODEL.ouid, + 0.0, // Filled amount + 0.0, // --> See accountant + OrderStatus.FILLED.code, + OrderStatus.FILLED.orderOfAppearance, + CREATE_DATE ) val SYMBOL_MAP_MODEL = SymbolMapModel( 1, - "ETH_USDT", + PAIR_SYMBOL, "binance", - "ETHUSDT" + PAIR_SYMBOL.replace("_", "") ) val TRADE_MODEL = TradeModel( 1, 1, - "ETH_USDT", + PAIR_SYMBOL, 0.001, // Minimum of orders quantities 100000.0, 100000.0, @@ -89,17 +105,17 @@ object Valid { 0.001, // Calculated "ETH", "USDT", - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC), - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "157b9b4a-cc66-43b9-b30b-40a8b66ea6aa", + UPDATE_DATE, + MAKER_ORDER_MODEL.ouid, + TAKER_ORDER_MODEL.ouid, PRINCIPAL.name, PRINCIPAL.name, - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + CREATE_DATE ) - val QUERY_ORDER_RESPONSE = QueryOrderResponse( - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + val MAKER_QUERY_ORDER_RESPONSE = QueryOrderResponse( + PAIR_SYMBOL, + MAKER_ORDER_MODEL.ouid, 1, -1, // Binance "", // Binance @@ -113,8 +129,8 @@ object Valid { OrderSide.SELL, null, null, - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(CREATE_DATE.toInstant(ZoneOffset.UTC)), + Date.from(UPDATE_DATE.toInstant(ZoneOffset.UTC)), OrderStatus.FILLED.isWorking(), BigDecimal.valueOf(100000.0 * 0.001) ) @@ -131,9 +147,9 @@ object Valid { val RICH_ORDER = RichOrder( null, - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", + PAIR_SYMBOL, + MAKER_ORDER_MODEL.ouid, + PRINCIPAL.name, "1", BigDecimal.valueOf(0.01), BigDecimal.valueOf(0.01), @@ -144,14 +160,14 @@ object Valid { MatchingOrderType.LIMIT_ORDER, BigDecimal.valueOf(1000001), BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? + BigDecimal.valueOf(0), + BigDecimal.valueOf(0), + BigDecimal.valueOf(0), 0 ) val RICH_ORDER_UPDATE = RichOrderUpdate( - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", + MAKER_ORDER_MODEL.ouid, BigDecimal.valueOf(1000001), BigDecimal.valueOf(0.01), BigDecimal.valueOf(0.08), @@ -159,61 +175,61 @@ object Valid { ) val RICH_TRADE = RichTrade( - 1, // ? - "ETH_USDT", - "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", - "18013d13-0568-496b-b93b-2524c8132b93", + 1, + PAIR_SYMBOL, + MAKER_ORDER_MODEL.ouid, + PRINCIPAL.name, 1, OrderDirection.ASK, BigDecimal.valueOf(100000), BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - "", // ? - "26931efc-891b-4599-9921-1d265829b410", - "5296a097-6478-464f-91a6-5c434ac4207d", + BigDecimal.valueOf(0), + BigDecimal.valueOf(0), + BigDecimal.valueOf(0), + "ETH", + TAKER_ORDER_MODEL.ouid, + PRINCIPAL.name, 2, OrderDirection.ASK, BigDecimal.valueOf(100000), BigDecimal.valueOf(0.01), - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - BigDecimal.valueOf(0), // ? - "", // ? - BigDecimal.valueOf(0), // ? - LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC) + BigDecimal.valueOf(0), + BigDecimal.valueOf(0), + BigDecimal.valueOf(0), + "USDT", + BigDecimal.valueOf(0), + CREATE_DATE ) val ALL_ORDER_REQUEST = AllOrderRequest( - "ETH_USDT", - Date.from(LocalDateTime.ofEpochSecond(1653125740, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125940, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + PAIR_SYMBOL, + Date.from(FROM_DATE.toInstant(ZoneOffset.UTC)), + Date.from(TO_DATE.toInstant(ZoneOffset.UTC)), 500 ) val TRADE_REQUEST = TradeRequest( - "ETH_USDT", + PAIR_SYMBOL, 1, - Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - Date.from(LocalDateTime.ofEpochSecond(1653125840, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), + Date.from(FROM_DATE.toInstant(ZoneOffset.UTC)), + Date.from(TO_DATE.toInstant(ZoneOffset.UTC)), 500 ) val MARKET_TRADE_RESPONSE = MarketTradeResponse( - "ETH_USDT", + PAIR_SYMBOL, 1, - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.001), + BigDecimal.valueOf(100000.0), BigDecimal.valueOf(0.001), - Date.from(LocalDateTime.ofEpochSecond(1653125640, 0, ZoneOffset.UTC).toInstant(ZoneOffset.UTC)), - isBestMatch = true, - isMakerBuyer = true + BigDecimal.valueOf(100000.0 * 0.001), + Date.from(CREATE_DATE.toInstant(ZoneOffset.UTC)), + true, + MAKER_ORDER_MODEL.direction == OrderDirection.BID ) val QUERY_ORDER_REQUEST = QueryOrderRequest( - "ETH_USDT", + PAIR_SYMBOL, 1, - "2" // ? + "2" ) -} \ No newline at end of file +} From ab6d03794c1885bb207dbb3222a921684c3b70cb Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 20:54:35 +0430 Subject: [PATCH 50/58] api: Remove hardcoded data from tests --- .../postgres/impl/MarketQueryHandlerTest.kt | 26 +++++++++--------- .../ports/postgres/impl/SymbolMapperTest.kt | 8 +++--- .../postgres/impl/UserQueryHandlerTest.kt | 14 +++++----- .../api/ports/postgres/impl/sample/Samples.kt | 27 ++++++++++--------- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index ec72e98e3..74e4e9ab5 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -30,7 +30,7 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findBySymbolAndDirectionAndStatusSortAscendingByPrice( - eq("ETH_USDT"), + eq(Valid.ETH_USDT), eq(OrderDirection.ASK), eq(1), argThat { @@ -42,7 +42,8 @@ class MarketQueryHandlerTest { ) } doReturn Flux.just(Valid.AGGREGATED_ORDER_PRICE_MODEL) } - val orderBookResponses = marketQueryHandler.openAskOrders("ETH_USDT", 1) + + val orderBookResponses = marketQueryHandler.openAskOrders(Valid.ETH_USDT, 1) assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) @@ -54,7 +55,7 @@ class MarketQueryHandlerTest { stubbing(orderRepository) { on { findBySymbolAndDirectionAndStatusSortDescendingByPrice( - eq("ETH_USDT"), + eq(Valid.ETH_USDT), eq(OrderDirection.BID), eq(1), argThat { @@ -67,7 +68,7 @@ class MarketQueryHandlerTest { } doReturn Flux.just(Valid.AGGREGATED_ORDER_PRICE_MODEL) } - val orderBookResponses = marketQueryHandler.openBidOrders("ETH_USDT", 1) + val orderBookResponses = marketQueryHandler.openBidOrders(Valid.ETH_USDT, 1) assertThat(orderBookResponses).isNotNull assertThat(orderBookResponses.size).isEqualTo(1) @@ -78,16 +79,16 @@ class MarketQueryHandlerTest { fun givenSymbol_whenLastOrder_thenReturnQueryOrderResponse(): Unit = runBlocking { stubbing(orderRepository) { on { - findLastOrderBySymbol("ETH_USDT") + findLastOrderBySymbol(Valid.ETH_USDT) } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(orderStatusRepository) { on { - findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + findMostRecentByOUID(Valid.MAKER_ORDER_MODEL.ouid) } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } - val queryOrderResponse = marketQueryHandler.lastOrder("ETH_USDT") + val queryOrderResponse = marketQueryHandler.lastOrder(Valid.ETH_USDT) assertThat(queryOrderResponse).isNotNull assertThat(queryOrderResponse).isEqualTo(Valid.MAKER_QUERY_ORDER_RESPONSE) @@ -100,7 +101,7 @@ class MarketQueryHandlerTest { findAllGroupBySymbol() } doReturn Flux.just(Valid.TRADE_MODEL) on { - findBySymbolGroupBySymbol("ETH_USDT") + findBySymbolGroupBySymbol(Valid.ETH_USDT) } doReturn Flux.just(Valid.TRADE_MODEL) } stubbing(orderRepository) { @@ -110,10 +111,11 @@ class MarketQueryHandlerTest { } stubbing(symbolMapper) { onBlocking { - map("ETH_USDT") + map(Valid.ETH_USDT) } doReturn "ETHUSDT" } - val priceTickerResponse = marketQueryHandler.lastPrice("ETH_USDT") + + val priceTickerResponse = marketQueryHandler.lastPrice(Valid.ETH_USDT) assertThat(priceTickerResponse).isNotNull assertThat(priceTickerResponse.size).isEqualTo(1) @@ -125,7 +127,7 @@ class MarketQueryHandlerTest { fun givenSymbol_whenRecentTrades_thenMarketTradeResponseFlow(): Unit = runBlocking { stubbing(tradeRepository) { on { - findBySymbolSortDescendingByCreateDate("ETH_USDT", 1) + findBySymbolSortDescendingByCreateDate(Valid.ETH_USDT, 1) } doReturn flow { emit(Valid.TRADE_MODEL) } @@ -139,7 +141,7 @@ class MarketQueryHandlerTest { } doReturn Mono.just(Valid.TAKER_ORDER_MODEL) } - val marketTradeResponses = marketQueryHandler.recentTrades("ETH_USDT", 1) + val marketTradeResponses = marketQueryHandler.recentTrades(Valid.ETH_USDT, 1) assertThat(marketTradeResponses).isNotNull assertThat(marketTradeResponses.count()).isEqualTo(1) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt index 89ce5d5b5..b5da9c00c 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt @@ -25,7 +25,7 @@ class SymbolMapperTest { findByAliasKeyAndAlias("binance", "ETHUSDT") } doReturn Mono.just(Valid.SYMBOL_MAP_MODEL) on { - findByAliasKeyAndSymbol("binance", "ETH_USDT") + findByAliasKeyAndSymbol("binance", Valid.ETH_USDT) } doReturn Mono.just(Valid.SYMBOL_MAP_MODEL) on { findAll() @@ -35,7 +35,7 @@ class SymbolMapperTest { @Test fun givenValidSymbol_whenMap_thenReturnAlias(): Unit = runBlocking { - val alis = symbolMapper.map("ETH_USDT") + val alis = symbolMapper.map(Valid.ETH_USDT) assertThat(alis).isEqualTo("ETHUSDT") } @@ -44,7 +44,7 @@ class SymbolMapperTest { fun givenValidAlias_whenUnmap_thenReturnSymbol(): Unit = runBlocking { val symbol = symbolMapper.unmap("ETHUSDT") - assertThat(symbol).isEqualTo("ETH_USDT") + assertThat(symbol).isEqualTo(Valid.ETH_USDT) } @Test @@ -53,6 +53,6 @@ class SymbolMapperTest { assertThat(map).isNotNull assertThat(map.size).isEqualTo(1) - assertThat(map["ETH_USDT"]).isNotNull() + assertThat(map[Valid.ETH_USDT]).isNotNull() } } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index 4f4c971d3..4589a136e 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -36,7 +36,7 @@ class UserQueryHandlerTest { } stubbing(orderStatusRepository) { on { - findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + findMostRecentByOUID(Valid.MAKER_ORDER_MODEL.ouid) } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } @@ -83,7 +83,7 @@ class UserQueryHandlerTest { on { findByUuidAndSymbolAndStatus( eq(Valid.PRINCIPAL.name), - eq("ETH_USDT"), + eq(Valid.ETH_USDT), argThat { this == listOf( OrderStatus.NEW.code, @@ -97,11 +97,11 @@ class UserQueryHandlerTest { } stubbing(orderStatusRepository) { on { - findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + findMostRecentByOUID(Valid.MAKER_ORDER_MODEL.ouid) } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } - val queryOrderResponses = userQueryHandler.openOrders(Valid.PRINCIPAL, "ETH_USDT") + val queryOrderResponses = userQueryHandler.openOrders(Valid.PRINCIPAL, Valid.ETH_USDT) assertThat(queryOrderResponses).isNotNull assertThat(queryOrderResponses.count()).isEqualTo(1) @@ -111,15 +111,15 @@ class UserQueryHandlerTest { fun given_whenQueryOrder_then(): Unit = runBlocking { stubbing(orderRepository) { on { - findBySymbolAndClientOrderId("ETH_USDT", "2") + findBySymbolAndClientOrderId(Valid.ETH_USDT, "2") } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) on { - findBySymbolAndOrderId("ETH_USDT", 1) + findBySymbolAndOrderId(Valid.ETH_USDT, 1) } doReturn Mono.just(Valid.MAKER_ORDER_MODEL) } stubbing(orderStatusRepository) { on { - findMostRecentByOUID("f1167d30-ccc0-4f86-ab5d-dd24aa3250df") + findMostRecentByOUID(Valid.MAKER_ORDER_MODEL.ouid) } doReturn Mono.just(Valid.MAKER_ORDER_STATUS_MODEL) } diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt index 4c5f38f46..7f4d5215a 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt @@ -16,13 +16,14 @@ import java.time.ZoneOffset import java.util.* object Valid { - private const val PAIR_SYMBOL = "ETH_USDT" private const val TIMESTAMP = 1653125840L private val CREATE_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP, 0, ZoneOffset.UTC) private val UPDATE_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP + 180, 0, ZoneOffset.UTC) private val FROM_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP - 600, 0, ZoneOffset.UTC) private val TO_DATE: LocalDateTime = LocalDateTime.ofEpochSecond(TIMESTAMP + 600, 0, ZoneOffset.UTC) + const val ETH_USDT = "ETH_USDT" + val PRINCIPAL = Principal { "98c7ca9b-2d9c-46dd-afa8-b0cd2f52a97c" } val MAKER_ORDER_MODEL = OrderModel( @@ -30,7 +31,7 @@ object Valid { "f1167d30-ccc0-4f86-ab5d-dd24aa3250df", PRINCIPAL.name, null, // Binance - PAIR_SYMBOL, + ETH_USDT, 1, // MatchingEngine ID 0.01, // Calculated? 0.01, // Calculated? @@ -52,7 +53,7 @@ object Valid { "157b9b4a-cc66-43b9-b30b-40a8b66ea6aa", PRINCIPAL.name, null, - PAIR_SYMBOL, + ETH_USDT, 2, 0.01, 0.01, @@ -89,15 +90,15 @@ object Valid { val SYMBOL_MAP_MODEL = SymbolMapModel( 1, - PAIR_SYMBOL, + ETH_USDT, "binance", - PAIR_SYMBOL.replace("_", "") + ETH_USDT.replace("_", "") ) val TRADE_MODEL = TradeModel( 1, 1, - PAIR_SYMBOL, + ETH_USDT, 0.001, // Minimum of orders quantities 100000.0, 100000.0, @@ -114,7 +115,7 @@ object Valid { ) val MAKER_QUERY_ORDER_RESPONSE = QueryOrderResponse( - PAIR_SYMBOL, + ETH_USDT, MAKER_ORDER_MODEL.ouid, 1, -1, // Binance @@ -147,7 +148,7 @@ object Valid { val RICH_ORDER = RichOrder( null, - PAIR_SYMBOL, + ETH_USDT, MAKER_ORDER_MODEL.ouid, PRINCIPAL.name, "1", @@ -176,7 +177,7 @@ object Valid { val RICH_TRADE = RichTrade( 1, - PAIR_SYMBOL, + ETH_USDT, MAKER_ORDER_MODEL.ouid, PRINCIPAL.name, 1, @@ -202,14 +203,14 @@ object Valid { ) val ALL_ORDER_REQUEST = AllOrderRequest( - PAIR_SYMBOL, + ETH_USDT, Date.from(FROM_DATE.toInstant(ZoneOffset.UTC)), Date.from(TO_DATE.toInstant(ZoneOffset.UTC)), 500 ) val TRADE_REQUEST = TradeRequest( - PAIR_SYMBOL, + ETH_USDT, 1, Date.from(FROM_DATE.toInstant(ZoneOffset.UTC)), Date.from(TO_DATE.toInstant(ZoneOffset.UTC)), @@ -217,7 +218,7 @@ object Valid { ) val MARKET_TRADE_RESPONSE = MarketTradeResponse( - PAIR_SYMBOL, + ETH_USDT, 1, BigDecimal.valueOf(100000.0), BigDecimal.valueOf(0.001), @@ -228,7 +229,7 @@ object Valid { ) val QUERY_ORDER_REQUEST = QueryOrderRequest( - PAIR_SYMBOL, + ETH_USDT, 1, "2" ) From ad3e28785734da43041ec0359c344e1a16a1a87c Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 21:17:18 +0430 Subject: [PATCH 51/58] api: Improve test case titles --- .../api/ports/postgres/impl/MarketQueryHandlerTest.kt | 10 +++++----- .../opex/api/ports/postgres/impl/OrderPersisterTest.kt | 4 ++-- .../opex/api/ports/postgres/impl/SymbolMapperTest.kt | 6 +++--- .../opex/api/ports/postgres/impl/TradePersisterTest.kt | 2 +- .../api/ports/postgres/impl/UserQueryHandlerTest.kt | 8 ++++---- .../opex/api/ports/postgres/impl/sample/Samples.kt | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt index 74e4e9ab5..2241f4ab5 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt @@ -26,7 +26,7 @@ class MarketQueryHandlerTest { MarketQueryHandlerImpl(orderRepository, tradeRepository, orderStatusRepository, symbolMapper) @Test - fun givenSymbol_whenOpenASKOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { + fun givenAggregatedOrderPrice_whenOpenASKOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { stubbing(orderRepository) { on { findBySymbolAndDirectionAndStatusSortAscendingByPrice( @@ -51,7 +51,7 @@ class MarketQueryHandlerTest { } @Test - fun givenSymbol_whenOpenBIDOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { + fun givenAggregatedOrderPrice_whenOpenBIDOrders_thenReturnOrderBookResponseList(): Unit = runBlocking { stubbing(orderRepository) { on { findBySymbolAndDirectionAndStatusSortDescendingByPrice( @@ -76,7 +76,7 @@ class MarketQueryHandlerTest { } @Test - fun givenSymbol_whenLastOrder_thenReturnQueryOrderResponse(): Unit = runBlocking { + fun givenOrder_whenLastOrder_thenReturnQueryOrderResponse(): Unit = runBlocking { stubbing(orderRepository) { on { findLastOrderBySymbol(Valid.ETH_USDT) @@ -95,7 +95,7 @@ class MarketQueryHandlerTest { } @Test - fun givenSymbol_whenLastPrice_thenPriceTickerResponse(): Unit = runBlocking { + fun givenOrderAndTradeAndSymbolAlias_whenLastPrice_thenPriceTickerResponse(): Unit = runBlocking { stubbing(tradeRepository) { on { findAllGroupBySymbol() @@ -124,7 +124,7 @@ class MarketQueryHandlerTest { } @Test - fun givenSymbol_whenRecentTrades_thenMarketTradeResponseFlow(): Unit = runBlocking { + fun givenOrderAndTrade_whenRecentTrades_thenMarketTradeResponseFlow(): Unit = runBlocking { stubbing(tradeRepository) { on { findBySymbolSortDescendingByCreateDate(Valid.ETH_USDT, 1) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt index 57fcffab7..7c1f45a7c 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt @@ -18,7 +18,7 @@ class OrderPersisterTest { private val orderPersister = OrderPersisterImpl(orderRepository, orderStatusRepository) @Test - fun givenRichOrder_whenSave_thenSuccess(): Unit = runBlocking { + fun givenOrderRepo_whenSaveRichOrder_thenSuccess(): Unit = runBlocking { stubbing(orderRepository) { on { save(any()) @@ -34,7 +34,7 @@ class OrderPersisterTest { } @Test - fun givenRichOrder_whenUpdate_thenSuccess(): Unit = runBlocking { + fun givenOrderRepo_whenUpdateRichOrder_thenSuccess(): Unit = runBlocking { stubbing(orderStatusRepository) { on { save(any()) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt index b5da9c00c..42666cd75 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt @@ -34,21 +34,21 @@ class SymbolMapperTest { } @Test - fun givenValidSymbol_whenMap_thenReturnAlias(): Unit = runBlocking { + fun givenSymbolAlias_whenMapSymbol_thenReturnAlias(): Unit = runBlocking { val alis = symbolMapper.map(Valid.ETH_USDT) assertThat(alis).isEqualTo("ETHUSDT") } @Test - fun givenValidAlias_whenUnmap_thenReturnSymbol(): Unit = runBlocking { + fun givenSymbolAlias_whenUnmapAlias_thenReturnSymbol(): Unit = runBlocking { val symbol = symbolMapper.unmap("ETHUSDT") assertThat(symbol).isEqualTo(Valid.ETH_USDT) } @Test - fun given_whenSymbolToAliasMap_thenReturnSymbol(): Unit = runBlocking { + fun givenSymbolAlias_whenSymbolToAliasMap_thenReturnMap(): Unit = runBlocking { val map = symbolMapper.symbolToAliasMap() assertThat(map).isNotNull diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt index 9c8a3c9fb..9c16f1638 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt @@ -16,7 +16,7 @@ class TradePersisterTest { private val tradePersister = TradePersisterImpl(tradeRepository) @Test - fun givenRichTrade_whenSave_thenSuccess(): Unit = runBlocking { + fun givenTradeRepo_whenSaveRichTrade_thenSuccess(): Unit = runBlocking { stubbing(tradeRepository) { on { save(any()) diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt index 4589a136e..22f6b9c26 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt @@ -21,7 +21,7 @@ class UserQueryHandlerTest { private val userQueryHandler = UserQueryHandlerImpl(orderRepository, tradeRepository, orderStatusRepository) @Test - fun given_whenAllOrders_then(): Unit = runBlocking { + fun givenOrder_whenAllOrders_thenReturnQueryOrderResponseList(): Unit = runBlocking { stubbing(orderRepository) { on { findByUuidAndSymbolAndTimeBetween( @@ -48,7 +48,7 @@ class UserQueryHandlerTest { } @Test - fun given_whenAllTrades_then(): Unit = runBlocking { + fun givenOrderAndTrade_whenAllTrades_thenTradeResponseList(): Unit = runBlocking { stubbing(tradeRepository) { on { findByUuidAndSymbolAndTimeBetweenAndTradeIdGreaterThan( @@ -78,7 +78,7 @@ class UserQueryHandlerTest { } @Test - fun given_whenOpenOrders_then(): Unit = runBlocking { + fun givenOrder_whenOpenOrders_thenReturnQueryOrderResponseList(): Unit = runBlocking { stubbing(orderRepository) { on { findByUuidAndSymbolAndStatus( @@ -108,7 +108,7 @@ class UserQueryHandlerTest { } @Test - fun given_whenQueryOrder_then(): Unit = runBlocking { + fun givenOrder_whenQueryOrder_thenReturnQueryOrderResponse(): Unit = runBlocking { stubbing(orderRepository) { on { findBySymbolAndClientOrderId(Valid.ETH_USDT, "2") diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt index 7f4d5215a..ce90c5b40 100644 --- a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt +++ b/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt @@ -142,8 +142,8 @@ object Valid { ) val ORDER_BOOK_RESPONSE = OrderBookResponse( - BigDecimal.valueOf(100000.0), - BigDecimal.valueOf(0.001) + AGGREGATED_ORDER_PRICE_MODEL.price!!.toBigDecimal(), + AGGREGATED_ORDER_PRICE_MODEL.quantity!!.toBigDecimal() ) val RICH_ORDER = RichOrder( From 4f00baf9af27c8b267ad07561999f9b60d1bd5ce Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 21:28:49 +0430 Subject: [PATCH 52/58] api: Move tests to postgres module --- api/api-app/pom.xml | 4 - api/api-ports/api-persister-postgres/pom.xml | 138 +++++++++--------- .../postgres/impl/MarketQueryHandlerTest.kt | 0 .../ports/postgres/impl/OrderPersisterTest.kt | 0 .../ports/postgres/impl/SymbolMapperTest.kt | 0 .../ports/postgres/impl/TradePersisterTest.kt | 0 .../postgres/impl/UserQueryHandlerTest.kt | 0 .../api/ports/postgres/impl/sample/Samples.kt | 0 8 files changed, 71 insertions(+), 71 deletions(-) rename api/{api-app => api-ports/api-persister-postgres}/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt (100%) rename api/{api-app => api-ports/api-persister-postgres}/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt (100%) rename api/{api-app => api-ports/api-persister-postgres}/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt (100%) rename api/{api-app => api-ports/api-persister-postgres}/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt (100%) rename api/{api-app => api-ports/api-persister-postgres}/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt (100%) rename api/{api-app => api-ports/api-persister-postgres}/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt (100%) diff --git a/api/api-app/pom.xml b/api/api-app/pom.xml index 904a06970..4d69c7fc1 100644 --- a/api/api-app/pom.xml +++ b/api/api-app/pom.xml @@ -68,10 +68,6 @@ co.nilin.opex.utility.preferences preferences - - org.mockito.kotlin - mockito-kotlin - diff --git a/api/api-ports/api-persister-postgres/pom.xml b/api/api-ports/api-persister-postgres/pom.xml index 28ded2b84..310a95e50 100644 --- a/api/api-ports/api-persister-postgres/pom.xml +++ b/api/api-ports/api-persister-postgres/pom.xml @@ -1,67 +1,71 @@ - - - 4.0.0 - - - co.nilin.opex.api - api - 1.0-SNAPSHOT - ../../pom.xml - - - co.nilin.opex.api.ports.postgres - api-persister-postgres - api-persister-postgres - Persist items of Opex api on Postgres - - - - org.jetbrains.kotlin - kotlin-reflect - - - co.nilin.opex.api.core - api-core - - - co.nilin.opex.utility.error - error-handler - - - org.springframework.boot - spring-boot-starter-data-r2dbc - - - io.r2dbc - r2dbc-postgresql - runtime - - - org.postgresql - postgresql - runtime - - - io.projectreactor.kotlin - reactor-kotlin-extensions - - - org.jetbrains.kotlinx - kotlinx-coroutines-reactor - - - org.jetbrains.kotlinx - kotlinx-coroutines-core - - - com.google.code.gson - gson - - - io.projectreactor - reactor-test - test - - - + + + 4.0.0 + + + co.nilin.opex.api + api + 1.0-SNAPSHOT + ../../pom.xml + + + co.nilin.opex.api.ports.postgres + api-persister-postgres + api-persister-postgres + Persist items of Opex api on Postgres + + + + org.jetbrains.kotlin + kotlin-reflect + + + co.nilin.opex.api.core + api-core + + + co.nilin.opex.utility.error + error-handler + + + org.springframework.boot + spring-boot-starter-data-r2dbc + + + io.r2dbc + r2dbc-postgresql + runtime + + + org.postgresql + postgresql + runtime + + + io.projectreactor.kotlin + reactor-kotlin-extensions + + + org.jetbrains.kotlinx + kotlinx-coroutines-reactor + + + org.jetbrains.kotlinx + kotlinx-coroutines-core + + + com.google.code.gson + gson + + + io.projectreactor + reactor-test + test + + + org.mockito.kotlin + mockito-kotlin + + + diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt b/api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt similarity index 100% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt rename to api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/MarketQueryHandlerTest.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt b/api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt similarity index 100% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt rename to api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/OrderPersisterTest.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt b/api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt similarity index 100% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt rename to api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/SymbolMapperTest.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt b/api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt similarity index 100% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt rename to api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/TradePersisterTest.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt b/api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt similarity index 100% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt rename to api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/UserQueryHandlerTest.kt diff --git a/api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt b/api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt similarity index 100% rename from api/api-app/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt rename to api/api-ports/api-persister-postgres/src/test/kotlin/co/nilin/opex/api/ports/postgres/impl/sample/Samples.kt From 6672c614c9e8ed0c01af6bc20b55bd9175bd45f2 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 21:57:31 +0430 Subject: [PATCH 53/58] wallet: Move each test to its correspondence module --- wallet/wallet-app/pom.xml | 4 - wallet/wallet-core/pom.xml | 9 + .../core/service/TransferServiceTest.kt | 546 ++++++++++ .../core/service/TransferServiceTestBase.kt | 34 + .../wallet-persister-postgres/pom.xml | 4 + .../postgres/impl/CurrencyServiceTest.kt | 44 + .../postgres/impl/CurrencyServiceTestBase.kt | 9 + .../ports/postgres/impl/WalletManagerTest.kt | 958 ++++++++++++++++++ .../postgres/impl/WalletManagerTestBase.kt | 42 + .../postgres/impl/WalletOwnerManagerTest.kt | 283 ++++++ .../impl/WalletOwnerManagerTestBase.kt | 35 + 11 files changed, 1964 insertions(+), 4 deletions(-) create mode 100644 wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt create mode 100644 wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt create mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt create mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt create mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt create mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt create mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt create mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt diff --git a/wallet/wallet-app/pom.xml b/wallet/wallet-app/pom.xml index a799df3ce..f3fa59979 100644 --- a/wallet/wallet-app/pom.xml +++ b/wallet/wallet-app/pom.xml @@ -111,10 +111,6 @@ co.nilin.opex.utility.preferences preferences - - org.mockito.kotlin - mockito-kotlin - diff --git a/wallet/wallet-core/pom.xml b/wallet/wallet-core/pom.xml index 507cf69d3..3414a8f56 100644 --- a/wallet/wallet-core/pom.xml +++ b/wallet/wallet-core/pom.xml @@ -28,5 +28,14 @@ spring-tx provided + + org.mockito.kotlin + mockito-kotlin + + + org.jetbrains.kotlinx + kotlinx-coroutines-core + test + diff --git a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt new file mode 100644 index 000000000..53db6e1f7 --- /dev/null +++ b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -0,0 +1,546 @@ +package co.nilin.opex.wallet.core.service + +import co.nilin.opex.wallet.core.inout.TransferCommand +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.core.model.WalletOwner +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyString +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.stubbing +import java.math.BigDecimal + +private class TransferServiceTest : TransferServiceTestBase() { + @Test + fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { + stubbing(walletOwnerManager) { + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + } + stubbing(walletManager) { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + onBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } doReturn Unit + onBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } doReturn Unit + } + stubbing(transactionManager) { + onBlocking { save(any()) } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + val result = transferService.transfer(transferCommand).transferResult + + assertThat(result).isNotNull + assertThat(result.amount).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) + assertThat(result.sourceUuid).isEqualTo("fdf453d7-0633-4ec7-852d-a18148c99a82") + assertThat(result.destUuid).isEqualTo("e1950578-ef22-44e4-89f5-0b78feb03e2a") + assertThat(result.sourceWalletType).isEqualTo("main") + assertThat(result.destWalletType).isEqualTo("main") + assertThat(result.sourceBalanceBeforeAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1.5))) + assertThat(result.sourceBalanceAfterAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1))) + } + + @Test + fun givenOwnerNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + onBlocking { + isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) + } doReturn false + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + } + stubbing(walletManager) { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + onBlocking { save(any()) } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenWalletNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + } + stubbing(walletManager) { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn false + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + onBlocking { save(any()) } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenOwnerNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn false + } + stubbing(walletManager) { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + onBlocking { save(any()) } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenWalletNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + } + stubbing(walletManager) { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn false + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1)) + override fun currency() = currency + override fun type() = "main" + } + } + stubbing(walletListener) { + on { + runBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + onBlocking { save(any()) } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } + + @Test + fun givenNotExistWallet_whenTransfer_thenThrow(): Unit = runBlocking { + stubbing(walletOwnerManager) { + onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true + } + stubbing(walletManager) { + onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true + onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit + onBlocking { findWalletById(20L) } doReturn null + } + stubbing(walletListener) { + onBlocking { + onWithdraw( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + anyString(), + any() + ) + } doReturn Unit + on { + runBlocking { + onDeposit( + any(), + any(), + eq(Amount(currency, BigDecimal.valueOf(0.5))), + any(), + anyString(), + any() + ) + } + } doReturn Unit + } + stubbing(transactionManager) { + onBlocking { save(any()) } doReturn "1" + } + val sourceWalletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val sourceWallet = object : Wallet { + override fun id() = 20L + override fun owner() = sourceWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) + override fun currency() = currency + override fun type() = "main" + } + val destWalletOwner = object : WalletOwner { + override fun id() = 3L + override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + val destWallet = object : Wallet { + override fun id() = 30L + override fun owner() = destWalletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) + override fun currency() = currency + override fun type() = "main" + } + val transferCommand = TransferCommand( + sourceWallet, + destWallet, + Amount(currency, BigDecimal.valueOf(0.5)), + null, + null, + null + ) + + assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } + } +} diff --git a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt new file mode 100644 index 000000000..a4e649dbe --- /dev/null +++ b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt @@ -0,0 +1,34 @@ +package co.nilin.opex.wallet.core.service + +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.core.spi.TransactionManager +import co.nilin.opex.wallet.core.spi.WalletListener +import co.nilin.opex.wallet.core.spi.WalletManager +import co.nilin.opex.wallet.core.spi.WalletOwnerManager +import org.mockito.kotlin.mock + +internal open class TransferServiceTestBase { + protected var walletOwnerManager: WalletOwnerManager = mock() + protected var walletManager: WalletManager = mock() + protected var walletListener: WalletListener = mock() + protected var transactionManager: TransactionManager = mock() + protected var transferService: TransferService = + TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) + + protected val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + protected val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } +} diff --git a/wallet/wallet-ports/wallet-persister-postgres/pom.xml b/wallet/wallet-ports/wallet-persister-postgres/pom.xml index aef5f1549..84d618043 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/pom.xml +++ b/wallet/wallet-ports/wallet-persister-postgres/pom.xml @@ -60,5 +60,9 @@ reactor-test test + + org.mockito.kotlin + mockito-kotlin + diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt new file mode 100644 index 000000000..3db33151f --- /dev/null +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt @@ -0,0 +1,44 @@ +package co.nilin.opex.wallet.ports.postgres.impl + +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Mono + +private class CurrencyServiceTest : CurrencyServiceTestBase() { + @Test + fun givenCorrectSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol("ETH") } doReturn Mono.just(CurrencyModel("ETH", "Ethereum", 0.0001)) + } + val c = currencyService.getCurrency("ETH") + + assertThat(c).isNotNull + assertThat(c!!.getSymbol()).isEqualTo("ETH") + assertThat(c.getName()).isEqualTo("Ethereum") + assertThat(c.getPrecision()).isEqualTo(0.0001) + } + + @Test + fun givenNotExistCurrency_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol("ETH") } doReturn Mono.empty() + } + val c = currencyService.getCurrency("ETH") + + assertThat(c).isNull() + } + + @Test + fun givenEmptySymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol("") } doReturn Mono.empty() + } + val c = currencyService.getCurrency("") + + assertThat(c).isNull() + } +} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt new file mode 100644 index 000000000..03a712ffb --- /dev/null +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt @@ -0,0 +1,9 @@ +package co.nilin.opex.wallet.ports.postgres.impl + +import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository +import org.mockito.kotlin.mock + +internal open class CurrencyServiceTestBase { + protected var currencyRepository: CurrencyRepository = mock { } + protected var currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) +} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt new file mode 100644 index 000000000..ff1cf65ce --- /dev/null +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -0,0 +1,958 @@ +package co.nilin.opex.wallet.ports.postgres.impl + +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel +import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel +import co.nilin.opex.wallet.ports.postgres.model.WalletModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.mockito.kotlin.* +import reactor.core.publisher.Mono +import java.math.BigDecimal + +private class WalletManagerTest : WalletManagerTestBase() { + @Test + fun givenWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0.5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenNotExistWallet_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.isWithdrawAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenNotExistCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + stubbing(currencyRepository) { + on { findBySymbol(currency.getSymbol()) } doReturn Mono.empty() + on { findById(currency.getSymbol()) } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.isWithdrawAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenInsufficientAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenWrongAmount_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.isWithdrawAllowed( + wallet, + BigDecimal.valueOf(-1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenOwnerAndWalletTypeLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenOwnerAndWalletLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenLevelAndWalletTypeLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenAllUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenAllLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "withdraw", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(500)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(30)) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0.5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenNotExistWallet_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.isDepositAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenWrongCurrency_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.isDepositAllowed( + wallet, + BigDecimal.valueOf(0.5) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenInsufficientAmount_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + val isAllowed = runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } + + verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndWalletAndAction( + walletOwner.id()!!, + "ETH", + 20, + "withdraw" + ) + verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndActionAndWalletType( + walletOwner.id()!!, + "ETH", + "withdraw", + "main" + ) + verify(walletLimitsRepository, never()).findByLevelAndCurrencyAndActionAndWalletType( + "1", + "ETH", + "withdraw", + "main" + ) + assertThat(isAllowed).isFalse() + } + + @Test + fun givenWrongAmount_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(-1)) } } + } + + @Test + fun givenWalletWithUnreachedLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(5)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(1)) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenWalletWithWalletLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + null, + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.just( + WalletLimitsModel( + 1, + "1", + 2, + "deposit", + "ETH", + "main", + 30, + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + val wallet = object : Wallet { + override fun id() = 30L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(500)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(30)) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenEmptyWalletWithNoLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") + } doReturn Mono.empty() + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(0)) + override fun currency() = currency + override fun type() = "main" + } + + val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { + stubbing(walletOwnerRepository) { + on { findById(walletOwner.id()!!) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + stubbing(walletRepository) { + on { + findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1.2) + ) + ) + } + stubbing(currencyRepository) { + on { + findBySymbol(currency.getSymbol()) + } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + } + val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) + + assertThat(wallet).isNotNull + assertThat(wallet!!.owner().id()).isEqualTo(walletOwner.id()) + assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) + assertThat(wallet.type()).isEqualTo("main") + } + + @Test + fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { + stubbing(walletRepository) { + on { + save(WalletModel(null, walletOwner.id()!!, "main", currency.getSymbol(), BigDecimal.valueOf(1))) + } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(1) + ) + ) + } + val wallet = walletManagerImpl.createWallet( + walletOwner, + Amount(currency, BigDecimal.valueOf(1)), + currency, + "main" + ) + + assertThat(wallet).isNotNull + assertThat(wallet.owner().id()).isEqualTo(walletOwner.id()) + assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) + assertThat(wallet.type()).isEqualTo("main") + } + + @Test + fun givenWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(1) + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.increaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.doesNotThrowAnyException() + } + + @Test + fun givenNotExistWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(any(), eq(BigDecimal.valueOf(1))) + } doReturn Mono.just(0) + } + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.increaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenWrongAmount_whenIncreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), any()) + } doReturn Mono.just(0) + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.increaseBalance( + wallet, + BigDecimal.valueOf(-1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { + stubbing(walletRepository) { + on { updateBalance(eq(20), eq(BigDecimal.valueOf(-1))) } doReturn Mono.just(1) + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.decreaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.doesNotThrowAnyException() + } + + @Test + fun givenNotExist_whenDecreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(any(), eq(BigDecimal.valueOf(-1))) + } doReturn Mono.just(0) + } + val wallet = object : Wallet { + override fun id() = 40L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.decreaseBalance( + wallet, + BigDecimal.valueOf(1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenWrongAmount_whenDecreaseBalance_thenThrow(): Unit = runBlocking { + stubbing(walletRepository) { + on { + updateBalance(eq(20), eq(BigDecimal.valueOf(-1))) + } doReturn Mono.just(0) + } + val wallet = object : Wallet { + override fun id() = 20L + override fun owner() = walletOwner + override fun balance() = Amount(currency, BigDecimal.valueOf(2)) + override fun currency() = currency + override fun type() = "main" + } + + assertThatThrownBy { + runBlocking { + walletManagerImpl.decreaseBalance( + wallet, + BigDecimal.valueOf(-1) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { + stubbing(walletRepository) { + on { findById(20) } doReturn Mono.just( + WalletModel( + 20L, + walletOwner.id()!!, + "main", + currency.getSymbol(), + BigDecimal.valueOf(0.5) + ) + ) + } + stubbing(walletOwnerRepository) { + on { + findById(walletOwner.id()!!) + } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + stubbing(currencyRepository) { + on { + findById(currency.getSymbol()) + } doReturn Mono.just( + CurrencyModel( + currency.getSymbol(), + currency.getName(), + currency.getPrecision() + ) + ) + } + val wallet = walletManagerImpl.findWalletById(20) + + assertThat(wallet).isNotNull + assertThat(wallet!!.id()).isEqualTo(20) + assertThat(wallet.balance()).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) + assertThat(wallet.currency().getSymbol()).isEqualTo("ETH") + } +} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt new file mode 100644 index 000000000..ec07e0361 --- /dev/null +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt @@ -0,0 +1,42 @@ +package co.nilin.opex.wallet.ports.postgres.impl + +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.* +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import reactor.core.publisher.Mono + +internal open class WalletManagerTestBase { + protected var walletLimitsRepository: WalletLimitsRepository = mock() + protected var walletRepository: WalletRepository = mock() + protected var walletOwnerRepository: WalletOwnerRepository = mock() + + private var transactionRepository: TransactionRepository = mock { + on { calculateWithdrawStatistics(eq(2), eq(20), any(), any()) } doReturn Mono.empty() + } + + protected val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + protected val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + protected var currencyRepository: CurrencyRepository = mock { } + + protected var walletManagerImpl: WalletManagerImpl = WalletManagerImpl( + walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository + ) +} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt new file mode 100644 index 000000000..e0ba07d23 --- /dev/null +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -0,0 +1,283 @@ +package co.nilin.opex.wallet.ports.postgres.impl + +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel +import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel +import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.stubbing +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import java.math.BigDecimal + +private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { + @Test + fun givenOwnerWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenInvalidAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + + assertThatThrownBy { + runBlocking { + walletOwnerManagerImpl.isWithdrawAllowed( + walletOwner, + Amount(currency, BigDecimal.valueOf(-5)) + ) + } + }.isNotInstanceOf(NullPointerException::class.java) + } + + @Test + fun givenOwnerWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { + emit( + UserLimitsModel( + 1, + null, + walletOwner.id()!!, + "withdraw", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenLevelWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { + emit( + UserLimitsModel( + 1, + "1", + null, + "withdraw", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenOwnerWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenWrongAmount_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(-5))) + + assertThat(isAllowed).isTrue() + } + + @Test + fun givenOwnerWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { + emit( + UserLimitsModel( + 1, + null, + walletOwner.id()!!, + "deposit", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenLevelWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + stubbing(userLimitsRepository) { + on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { + emit( + UserLimitsModel( + 1, + "1", + null, + "deposit", + "main", + BigDecimal.valueOf(100), + 10, + BigDecimal.valueOf(3000), + 300 + ) + ) + } + } + stubbing(walletConfigRepository) { + on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) + } + stubbing(transactionRepository) { + on { + calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) + } doReturn Mono.empty() + } + val isAllowed = + walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) + + assertThat(isAllowed).isFalse() + } + + @Test + fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + stubbing(walletOwnerRepository) { + on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + val wo = walletOwnerManagerImpl.findWalletOwner(walletOwner.uuid()) + + assertThat(wo!!.id()).isEqualTo(walletOwner.id()) + assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) + } + + @Test + fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + stubbing(walletOwnerRepository) { + on { save(any()) } doReturn Mono.just( + WalletOwnerModel( + walletOwner.id(), + walletOwner.uuid(), + walletOwner.title(), + walletOwner.level(), + walletOwner.isTradeAllowed(), + walletOwner.isWithdrawAllowed(), + walletOwner.isDepositAllowed() + ) + ) + } + val wo = + walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) + + assertThat(wo.id()).isEqualTo(walletOwner.id()) + assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) + } +} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt new file mode 100644 index 000000000..cd5bd1bd0 --- /dev/null +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt @@ -0,0 +1,35 @@ +package co.nilin.opex.wallet.ports.postgres.impl + +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository +import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository +import org.mockito.kotlin.mock + +internal open class WalletOwnerManagerTestBase { + protected var userLimitsRepository: UserLimitsRepository = mock() + protected var transactionRepository: TransactionRepository = mock() + protected var walletOwnerRepository: WalletOwnerRepository = mock() + protected var walletConfigRepository: WalletConfigRepository = mock() + protected var walletOwnerManagerImpl: WalletOwnerManagerImpl = WalletOwnerManagerImpl( + userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository + ) + + protected val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + protected val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } +} From 872f65db1afa4f5c1596f82db507b5f9b5f8ec8a Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 22:04:04 +0430 Subject: [PATCH 54/58] wallet: Remove base classes --- .../core/service/TransferServiceTest.kt | 35 +++++++++++++--- .../core/service/TransferServiceTestBase.kt | 34 --------------- .../postgres/impl/CurrencyServiceTest.kt | 7 +++- .../postgres/impl/CurrencyServiceTestBase.kt | 9 ---- .../ports/postgres/impl/WalletManagerTest.kt | 34 ++++++++++++++- .../postgres/impl/WalletManagerTestBase.kt | 42 ------------------- .../postgres/impl/WalletOwnerManagerTest.kt | 37 +++++++++++++--- .../impl/WalletOwnerManagerTestBase.kt | 35 ---------------- 8 files changed, 101 insertions(+), 132 deletions(-) delete mode 100644 wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt delete mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt delete mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt delete mode 100644 wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt diff --git a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt index 53db6e1f7..52114977c 100644 --- a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt +++ b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -2,20 +2,45 @@ package co.nilin.opex.wallet.core.service import co.nilin.opex.wallet.core.inout.TransferCommand import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.core.spi.TransactionManager +import co.nilin.opex.wallet.core.spi.WalletListener +import co.nilin.opex.wallet.core.spi.WalletManager +import co.nilin.opex.wallet.core.spi.WalletOwnerManager import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.anyString -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.stubbing +import org.mockito.kotlin.* import java.math.BigDecimal -private class TransferServiceTest : TransferServiceTestBase() { +private class TransferServiceTest { + private val walletOwnerManager: WalletOwnerManager = mock() + private val walletManager: WalletManager = mock() + private val walletListener: WalletListener = mock() + private val transactionManager: TransactionManager = mock() + private val transferService: TransferService = + TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) + + private val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + + private val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + @Test fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { stubbing(walletOwnerManager) { diff --git a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt deleted file mode 100644 index a4e649dbe..000000000 --- a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt +++ /dev/null @@ -1,34 +0,0 @@ -package co.nilin.opex.wallet.core.service - -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.core.spi.TransactionManager -import co.nilin.opex.wallet.core.spi.WalletListener -import co.nilin.opex.wallet.core.spi.WalletManager -import co.nilin.opex.wallet.core.spi.WalletOwnerManager -import org.mockito.kotlin.mock - -internal open class TransferServiceTestBase { - protected var walletOwnerManager: WalletOwnerManager = mock() - protected var walletManager: WalletManager = mock() - protected var walletListener: WalletListener = mock() - protected var transactionManager: TransactionManager = mock() - protected var transferService: TransferService = - TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) - - protected val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - protected val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } -} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt index 3db33151f..48343fa29 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt @@ -1,14 +1,19 @@ package co.nilin.opex.wallet.ports.postgres.impl +import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import reactor.core.publisher.Mono -private class CurrencyServiceTest : CurrencyServiceTestBase() { +private class CurrencyServiceTest { + private val currencyRepository: CurrencyRepository = mock { } + private val currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) + @Test fun givenCorrectSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { stubbing(currencyRepository) { diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt deleted file mode 100644 index 03a712ffb..000000000 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt +++ /dev/null @@ -1,9 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository -import org.mockito.kotlin.mock - -internal open class CurrencyServiceTestBase { - protected var currencyRepository: CurrencyRepository = mock { } - protected var currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) -} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index ff1cf65ce..050b0286a 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -1,7 +1,10 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency import co.nilin.opex.wallet.core.model.Wallet +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.* import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletModel @@ -14,7 +17,36 @@ import org.mockito.kotlin.* import reactor.core.publisher.Mono import java.math.BigDecimal -private class WalletManagerTest : WalletManagerTestBase() { +private class WalletManagerTest { + private val walletLimitsRepository: WalletLimitsRepository = mock() + private val walletRepository: WalletRepository = mock() + private val walletOwnerRepository: WalletOwnerRepository = mock() + private val currencyRepository: CurrencyRepository = mock { } + + private var transactionRepository: TransactionRepository = mock { + on { calculateWithdrawStatistics(eq(2), eq(20), any(), any()) } doReturn Mono.empty() + } + + private val walletManagerImpl: WalletManagerImpl = WalletManagerImpl( + walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository + ) + + private val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + private val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + @Test fun givenWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt deleted file mode 100644 index ec07e0361..000000000 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt +++ /dev/null @@ -1,42 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.ports.postgres.dao.* -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import reactor.core.publisher.Mono - -internal open class WalletManagerTestBase { - protected var walletLimitsRepository: WalletLimitsRepository = mock() - protected var walletRepository: WalletRepository = mock() - protected var walletOwnerRepository: WalletOwnerRepository = mock() - - private var transactionRepository: TransactionRepository = mock { - on { calculateWithdrawStatistics(eq(2), eq(20), any(), any()) } doReturn Mono.empty() - } - - protected val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - protected val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - protected var currencyRepository: CurrencyRepository = mock { } - - protected var walletManagerImpl: WalletManagerImpl = WalletManagerImpl( - walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository - ) -} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index e0ba07d23..7e1e856a1 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -1,6 +1,12 @@ package co.nilin.opex.wallet.ports.postgres.impl import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.model.Currency +import co.nilin.opex.wallet.core.model.WalletOwner +import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository +import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository +import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel @@ -11,15 +17,36 @@ import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.anyLong import org.mockito.ArgumentMatchers.anyString -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.stubbing +import org.mockito.kotlin.* import reactor.core.publisher.Flux import reactor.core.publisher.Mono import java.math.BigDecimal -private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { +private class WalletOwnerManagerTest { + private val userLimitsRepository: UserLimitsRepository = mock() + private val transactionRepository: TransactionRepository = mock() + private val walletOwnerRepository: WalletOwnerRepository = mock() + private val walletConfigRepository: WalletConfigRepository = mock() + private val walletOwnerManagerImpl: WalletOwnerManagerImpl = WalletOwnerManagerImpl( + userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository + ) + + private val walletOwner = object : WalletOwner { + override fun id() = 2L + override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" + override fun title() = "wallet" + override fun level() = "1" + override fun isTradeAllowed() = true + override fun isWithdrawAllowed() = true + override fun isDepositAllowed() = true + } + + private val currency = object : Currency { + override fun getSymbol() = "ETH" + override fun getName() = "Ethereum" + override fun getPrecision() = 0.0001 + } + @Test fun givenOwnerWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(userLimitsRepository) { diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt deleted file mode 100644 index cd5bd1bd0..000000000 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt +++ /dev/null @@ -1,35 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository -import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository -import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository -import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository -import org.mockito.kotlin.mock - -internal open class WalletOwnerManagerTestBase { - protected var userLimitsRepository: UserLimitsRepository = mock() - protected var transactionRepository: TransactionRepository = mock() - protected var walletOwnerRepository: WalletOwnerRepository = mock() - protected var walletConfigRepository: WalletConfigRepository = mock() - protected var walletOwnerManagerImpl: WalletOwnerManagerImpl = WalletOwnerManagerImpl( - userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository - ) - - protected val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - protected val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } -} From 34db04772851d9cc83133c7984d00ad36c16a1bc Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 22:05:24 +0430 Subject: [PATCH 55/58] matching-gateway: Remove test base classes --- .../gateway/app/service/OrderServiceTest.kt | 21 +++++++++- .../app/service/OrderServiceTestBase.kt | 23 ----------- .../ports/postgres/impl/WalletManagerTest.kt | 38 +++++++++---------- .../postgres/impl/WalletOwnerManagerTest.kt | 20 +++++----- 4 files changed, 49 insertions(+), 53 deletions(-) delete mode 100644 matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt index 71cdc08a8..176f8ce06 100644 --- a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt +++ b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt @@ -7,17 +7,36 @@ import co.nilin.opex.matching.gateway.app.inout.CancelOrderRequest import co.nilin.opex.matching.gateway.app.inout.CreateOrderRequest import co.nilin.opex.matching.gateway.app.inout.PairConfig import co.nilin.opex.matching.gateway.app.inout.PairFeeConfig +import co.nilin.opex.matching.gateway.app.spi.AccountantApiProxy +import co.nilin.opex.matching.gateway.app.spi.PairConfigLoader import co.nilin.opex.matching.gateway.ports.kafka.submitter.inout.OrderSubmitResult +import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.EventSubmitter +import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.KafkaHealthIndicator +import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.OrderSubmitter import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import org.mockito.kotlin.any import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock import org.mockito.kotlin.stubbing import java.math.BigDecimal -private class OrderServiceTest : OrderServiceTestBase() { +private class OrderServiceTest { + private val accountantApiProxy: AccountantApiProxy = mock() + private val orderSubmitter: OrderSubmitter = mock() + private val eventSubmitter: EventSubmitter = mock() + private val pairConfigLoader: PairConfigLoader = mock() + private val kafkaHealthIndicator: KafkaHealthIndicator = mock() + private val orderService: OrderService = OrderService( + accountantApiProxy, + orderSubmitter, + eventSubmitter, + pairConfigLoader, + kafkaHealthIndicator + ) + @Test fun givenLimitASKOrder_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt deleted file mode 100644 index 1c9abd3dd..000000000 --- a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTestBase.kt +++ /dev/null @@ -1,23 +0,0 @@ -package co.nilin.opex.matching.gateway.app.service - -import co.nilin.opex.matching.gateway.app.spi.AccountantApiProxy -import co.nilin.opex.matching.gateway.app.spi.PairConfigLoader -import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.EventSubmitter -import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.KafkaHealthIndicator -import co.nilin.opex.matching.gateway.ports.kafka.submitter.service.OrderSubmitter -import org.mockito.kotlin.mock - -internal open class OrderServiceTestBase { - protected val accountantApiProxy: AccountantApiProxy = mock() - protected val orderSubmitter: OrderSubmitter = mock() - protected val eventSubmitter: EventSubmitter = mock() - protected val pairConfigLoader: PairConfigLoader = mock() - protected val kafkaHealthIndicator: KafkaHealthIndicator = mock() - protected val orderService: OrderService = OrderService( - accountantApiProxy, - orderSubmitter, - eventSubmitter, - pairConfigLoader, - kafkaHealthIndicator - ) -} diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index 050b0286a..89ade70f4 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -51,10 +51,10 @@ private class WalletManagerTest { fun givenWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "withdraw") } doReturn Mono.empty() on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id(), "ETH", "withdraw", "main") } doReturn Mono.empty() on { findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") @@ -412,10 +412,10 @@ private class WalletManagerTest { fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "withdraw") } doReturn Mono.empty() on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id(), "ETH", "withdraw", "main") } doReturn Mono.empty() on { findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") @@ -438,10 +438,10 @@ private class WalletManagerTest { fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "deposit") } doReturn Mono.empty() on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id(), "ETH", "deposit", "main") } doReturn Mono.empty() on { findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") @@ -504,10 +504,10 @@ private class WalletManagerTest { fun givenInsufficientAmount_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "withdraw") } doReturn Mono.empty() on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id(), "ETH", "withdraw", "main") } doReturn Mono.empty() on { findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") @@ -523,13 +523,13 @@ private class WalletManagerTest { val isAllowed = runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndWalletAndAction( - walletOwner.id()!!, + walletOwner.id(), "ETH", 20, "withdraw" ) verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndActionAndWalletType( - walletOwner.id()!!, + walletOwner.id(), "ETH", "withdraw", "main" @@ -696,10 +696,10 @@ private class WalletManagerTest { fun givenEmptyWalletWithNoLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") + findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "deposit") } doReturn Mono.empty() on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") + findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id(), "ETH", "deposit", "main") } doReturn Mono.empty() on { findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") @@ -721,7 +721,7 @@ private class WalletManagerTest { @Test fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { stubbing(walletOwnerRepository) { - on { findById(walletOwner.id()!!) } doReturn Mono.just( + on { findById(walletOwner.id()) } doReturn Mono.just( WalletOwnerModel( walletOwner.id(), walletOwner.uuid(), @@ -735,11 +735,11 @@ private class WalletManagerTest { } stubbing(walletRepository) { on { - findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) + findByOwnerAndTypeAndCurrency(walletOwner.id(), "main", currency.getSymbol()) } doReturn Mono.just( WalletModel( 20L, - walletOwner.id()!!, + walletOwner.id(), "main", currency.getSymbol(), BigDecimal.valueOf(1.2) @@ -769,11 +769,11 @@ private class WalletManagerTest { fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { stubbing(walletRepository) { on { - save(WalletModel(null, walletOwner.id()!!, "main", currency.getSymbol(), BigDecimal.valueOf(1))) + save(WalletModel(null, walletOwner.id(), "main", currency.getSymbol(), BigDecimal.valueOf(1))) } doReturn Mono.just( WalletModel( 20L, - walletOwner.id()!!, + walletOwner.id(), "main", currency.getSymbol(), BigDecimal.valueOf(1) @@ -947,7 +947,7 @@ private class WalletManagerTest { on { findById(20) } doReturn Mono.just( WalletModel( 20L, - walletOwner.id()!!, + walletOwner.id(), "main", currency.getSymbol(), BigDecimal.valueOf(0.5) @@ -956,7 +956,7 @@ private class WalletManagerTest { } stubbing(walletOwnerRepository) { on { - findById(walletOwner.id()!!) + findById(walletOwner.id()) } doReturn Mono.just( WalletOwnerModel( walletOwner.id(), diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index 7e1e856a1..af132b496 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -50,7 +50,7 @@ private class WalletOwnerManagerTest { @Test fun givenOwnerWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} } stubbing(walletConfigRepository) { @@ -69,7 +69,7 @@ private class WalletOwnerManagerTest { @Test fun givenInvalidAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} } stubbing(walletConfigRepository) { @@ -94,12 +94,12 @@ private class WalletOwnerManagerTest { @Test fun givenOwnerWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { + on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { emit( UserLimitsModel( 1, null, - walletOwner.id()!!, + walletOwner.id(), "withdraw", "main", BigDecimal.valueOf(100), @@ -128,7 +128,7 @@ private class WalletOwnerManagerTest { @Test fun givenLevelWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { emit( UserLimitsModel( @@ -162,7 +162,7 @@ private class WalletOwnerManagerTest { @Test fun givenOwnerWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id(), "deposit") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} } stubbing(walletConfigRepository) { @@ -181,7 +181,7 @@ private class WalletOwnerManagerTest { @Test fun givenWrongAmount_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id(), "deposit") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} } stubbing(walletConfigRepository) { @@ -200,12 +200,12 @@ private class WalletOwnerManagerTest { @Test fun givenOwnerWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { + on { findByOwnerAndAction(walletOwner.id(), "deposit") } doReturn flow { emit( UserLimitsModel( 1, null, - walletOwner.id()!!, + walletOwner.id(), "deposit", "main", BigDecimal.valueOf(100), @@ -234,7 +234,7 @@ private class WalletOwnerManagerTest { @Test fun givenLevelWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } + on { findByOwnerAndAction(walletOwner.id(), "deposit") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { emit( UserLimitsModel( From 5be79a0bbdad883113de0ef2fdabab1cc74ebcaf Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Sun, 22 May 2022 23:25:58 +0430 Subject: [PATCH 56/58] wallet: Remove test folder from wallet-app --- .../core/service/TransferServiceTest.kt | 546 ---------- .../core/service/TransferServiceTestBase.kt | 34 - .../postgres/impl/CurrencyServiceTest.kt | 44 - .../postgres/impl/CurrencyServiceTestBase.kt | 9 - .../ports/postgres/impl/WalletManagerTest.kt | 958 ------------------ .../postgres/impl/WalletManagerTestBase.kt | 42 - .../postgres/impl/WalletOwnerManagerTest.kt | 283 ------ .../impl/WalletOwnerManagerTestBase.kt | 35 - 8 files changed, 1951 deletions(-) delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt delete mode 100644 wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt deleted file mode 100644 index 53db6e1f7..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt +++ /dev/null @@ -1,546 +0,0 @@ -package co.nilin.opex.wallet.core.service - -import co.nilin.opex.wallet.core.inout.TransferCommand -import co.nilin.opex.wallet.core.model.Amount -import co.nilin.opex.wallet.core.model.Wallet -import co.nilin.opex.wallet.core.model.WalletOwner -import kotlinx.coroutines.runBlocking -import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.Assertions.assertThatThrownBy -import org.junit.jupiter.api.Test -import org.mockito.ArgumentMatchers.anyString -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.stubbing -import java.math.BigDecimal - -private class TransferServiceTest : TransferServiceTestBase() { - @Test - fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { - stubbing(walletOwnerManager) { - onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - } - stubbing(walletManager) { - onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { findWalletById(20L) } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - stubbing(walletListener) { - onBlocking { onWithdraw(any(), any(), any(), anyString(), any()) } doReturn Unit - onBlocking { onDeposit(any(), any(), any(), any(), anyString(), any()) } doReturn Unit - } - stubbing(transactionManager) { - onBlocking { save(any()) } doReturn "1" - } - val sourceWalletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val sourceWallet = object : Wallet { - override fun id() = 20L - override fun owner() = sourceWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) - override fun currency() = currency - override fun type() = "main" - } - val destWalletOwner = object : WalletOwner { - override fun id() = 3L - override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val destWallet = object : Wallet { - override fun id() = 30L - override fun owner() = destWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) - override fun currency() = currency - override fun type() = "main" - } - val transferCommand = TransferCommand( - sourceWallet, - destWallet, - Amount(currency, BigDecimal.valueOf(0.5)), - null, - null, - null - ) - - val result = transferService.transfer(transferCommand).transferResult - - assertThat(result).isNotNull - assertThat(result.amount).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) - assertThat(result.sourceUuid).isEqualTo("fdf453d7-0633-4ec7-852d-a18148c99a82") - assertThat(result.destUuid).isEqualTo("e1950578-ef22-44e4-89f5-0b78feb03e2a") - assertThat(result.sourceWalletType).isEqualTo("main") - assertThat(result.destWalletType).isEqualTo("main") - assertThat(result.sourceBalanceBeforeAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1.5))) - assertThat(result.sourceBalanceAfterAction).isEqualTo(Amount(currency, BigDecimal.valueOf(1))) - } - - @Test - fun givenOwnerNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { - stubbing(walletOwnerManager) { - onBlocking { - isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) - } doReturn false - onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - } - stubbing(walletManager) { - onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { findWalletById(20L) } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - stubbing(walletListener) { - on { - runBlocking { - onWithdraw( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - anyString(), - any() - ) - } - } doReturn Unit - on { - runBlocking { - onDeposit( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - any(), - anyString(), - any() - ) - } - } doReturn Unit - } - stubbing(transactionManager) { - onBlocking { save(any()) } doReturn "1" - } - val sourceWalletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val sourceWallet = object : Wallet { - override fun id() = 20L - override fun owner() = sourceWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) - override fun currency() = currency - override fun type() = "main" - } - val destWalletOwner = object : WalletOwner { - override fun id() = 3L - override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val destWallet = object : Wallet { - override fun id() = 30L - override fun owner() = destWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) - override fun currency() = currency - override fun type() = "main" - } - val transferCommand = TransferCommand( - sourceWallet, - destWallet, - Amount(currency, BigDecimal.valueOf(0.5)), - null, - null, - null - ) - - assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } - } - - @Test - fun givenWalletNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { - stubbing(walletOwnerManager) { - onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - } - stubbing(walletManager) { - onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn false - onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { findWalletById(20L) } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - stubbing(walletListener) { - on { - runBlocking { - onWithdraw( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - anyString(), - any() - ) - } - } doReturn Unit - on { - runBlocking { - onDeposit( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - any(), - anyString(), - any() - ) - } - } doReturn Unit - } - stubbing(transactionManager) { - onBlocking { save(any()) } doReturn "1" - } - val sourceWalletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val sourceWallet = object : Wallet { - override fun id() = 20L - override fun owner() = sourceWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) - override fun currency() = currency - override fun type() = "main" - } - val destWalletOwner = object : WalletOwner { - override fun id() = 3L - override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val destWallet = object : Wallet { - override fun id() = 30L - override fun owner() = destWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) - override fun currency() = currency - override fun type() = "main" - } - val transferCommand = TransferCommand( - sourceWallet, - destWallet, - Amount(currency, BigDecimal.valueOf(0.5)), - null, - null, - null - ) - - assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } - } - - @Test - fun givenOwnerNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { - stubbing(walletOwnerManager) { - onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn false - } - stubbing(walletManager) { - onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { findWalletById(20L) } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - stubbing(walletListener) { - on { - runBlocking { - onWithdraw( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - anyString(), - any() - ) - } - } doReturn Unit - on { - runBlocking { - onDeposit( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - any(), - anyString(), - any() - ) - } - } doReturn Unit - } - stubbing(transactionManager) { - onBlocking { save(any()) } doReturn "1" - } - val sourceWalletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val sourceWallet = object : Wallet { - override fun id() = 20L - override fun owner() = sourceWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) - override fun currency() = currency - override fun type() = "main" - } - val destWalletOwner = object : WalletOwner { - override fun id() = 3L - override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val destWallet = object : Wallet { - override fun id() = 30L - override fun owner() = destWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) - override fun currency() = currency - override fun type() = "main" - } - val transferCommand = TransferCommand( - sourceWallet, - destWallet, - Amount(currency, BigDecimal.valueOf(0.5)), - null, - null, - null - ) - - assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } - } - - @Test - fun givenWalletNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { - stubbing(walletOwnerManager) { - onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - } - stubbing(walletManager) { - onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn false - onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { findWalletById(20L) } doReturn object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1)) - override fun currency() = currency - override fun type() = "main" - } - } - stubbing(walletListener) { - on { - runBlocking { - onWithdraw( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - anyString(), - any() - ) - } - } doReturn Unit - on { - runBlocking { - onDeposit( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - any(), - anyString(), - any() - ) - } - } doReturn Unit - } - stubbing(transactionManager) { - onBlocking { save(any()) } doReturn "1" - } - val sourceWalletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val sourceWallet = object : Wallet { - override fun id() = 20L - override fun owner() = sourceWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) - override fun currency() = currency - override fun type() = "main" - } - val destWalletOwner = object : WalletOwner { - override fun id() = 3L - override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val destWallet = object : Wallet { - override fun id() = 30L - override fun owner() = destWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) - override fun currency() = currency - override fun type() = "main" - } - val transferCommand = TransferCommand( - sourceWallet, - destWallet, - Amount(currency, BigDecimal.valueOf(0.5)), - null, - null, - null - ) - - assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } - } - - @Test - fun givenNotExistWallet_whenTransfer_thenThrow(): Unit = runBlocking { - stubbing(walletOwnerManager) { - onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true - } - stubbing(walletManager) { - onBlocking { isWithdrawAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { isDepositAllowed(any(), eq(BigDecimal.valueOf(0.5))) } doReturn true - onBlocking { decreaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { increaseBalance(any(), eq(BigDecimal.valueOf(0.5))) } doReturn Unit - onBlocking { findWalletById(20L) } doReturn null - } - stubbing(walletListener) { - onBlocking { - onWithdraw( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - anyString(), - any() - ) - } doReturn Unit - on { - runBlocking { - onDeposit( - any(), - any(), - eq(Amount(currency, BigDecimal.valueOf(0.5))), - any(), - anyString(), - any() - ) - } - } doReturn Unit - } - stubbing(transactionManager) { - onBlocking { save(any()) } doReturn "1" - } - val sourceWalletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val sourceWallet = object : Wallet { - override fun id() = 20L - override fun owner() = sourceWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(1.5)) - override fun currency() = currency - override fun type() = "main" - } - val destWalletOwner = object : WalletOwner { - override fun id() = 3L - override fun uuid() = "e1950578-ef22-44e4-89f5-0b78feb03e2a" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - val destWallet = object : Wallet { - override fun id() = 30L - override fun owner() = destWalletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2.5)) - override fun currency() = currency - override fun type() = "main" - } - val transferCommand = TransferCommand( - sourceWallet, - destWallet, - Amount(currency, BigDecimal.valueOf(0.5)), - null, - null, - null - ) - - assertThatThrownBy { runBlocking { transferService.transfer(transferCommand) } } - } -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt deleted file mode 100644 index a4e649dbe..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTestBase.kt +++ /dev/null @@ -1,34 +0,0 @@ -package co.nilin.opex.wallet.core.service - -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.core.spi.TransactionManager -import co.nilin.opex.wallet.core.spi.WalletListener -import co.nilin.opex.wallet.core.spi.WalletManager -import co.nilin.opex.wallet.core.spi.WalletOwnerManager -import org.mockito.kotlin.mock - -internal open class TransferServiceTestBase { - protected var walletOwnerManager: WalletOwnerManager = mock() - protected var walletManager: WalletManager = mock() - protected var walletListener: WalletListener = mock() - protected var transactionManager: TransactionManager = mock() - protected var transferService: TransferService = - TransferService(walletManager, walletListener, walletOwnerManager, transactionManager) - - protected val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - protected val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt deleted file mode 100644 index 3db33151f..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt +++ /dev/null @@ -1,44 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import kotlinx.coroutines.runBlocking -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.stubbing -import reactor.core.publisher.Mono - -private class CurrencyServiceTest : CurrencyServiceTestBase() { - @Test - fun givenCorrectSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { - stubbing(currencyRepository) { - on { findBySymbol("ETH") } doReturn Mono.just(CurrencyModel("ETH", "Ethereum", 0.0001)) - } - val c = currencyService.getCurrency("ETH") - - assertThat(c).isNotNull - assertThat(c!!.getSymbol()).isEqualTo("ETH") - assertThat(c.getName()).isEqualTo("Ethereum") - assertThat(c.getPrecision()).isEqualTo(0.0001) - } - - @Test - fun givenNotExistCurrency_whenGetCurrency_thenReturnNull(): Unit = runBlocking { - stubbing(currencyRepository) { - on { findBySymbol("ETH") } doReturn Mono.empty() - } - val c = currencyService.getCurrency("ETH") - - assertThat(c).isNull() - } - - @Test - fun givenEmptySymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { - stubbing(currencyRepository) { - on { findBySymbol("") } doReturn Mono.empty() - } - val c = currencyService.getCurrency("") - - assertThat(c).isNull() - } -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt deleted file mode 100644 index 03a712ffb..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTestBase.kt +++ /dev/null @@ -1,9 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.ports.postgres.dao.CurrencyRepository -import org.mockito.kotlin.mock - -internal open class CurrencyServiceTestBase { - protected var currencyRepository: CurrencyRepository = mock { } - protected var currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt deleted file mode 100644 index ff1cf65ce..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ /dev/null @@ -1,958 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.core.model.Amount -import co.nilin.opex.wallet.core.model.Wallet -import co.nilin.opex.wallet.ports.postgres.model.CurrencyModel -import co.nilin.opex.wallet.ports.postgres.model.WalletLimitsModel -import co.nilin.opex.wallet.ports.postgres.model.WalletModel -import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel -import kotlinx.coroutines.runBlocking -import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.Assertions.assertThatThrownBy -import org.junit.jupiter.api.Test -import org.mockito.kotlin.* -import reactor.core.publisher.Mono -import java.math.BigDecimal - -private class WalletManagerTest : WalletManagerTestBase() { - @Test - fun givenWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0.5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenNotExistWallet_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { - val wallet = object : Wallet { - override fun id() = 40L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.isWithdrawAllowed( - wallet, - BigDecimal.valueOf(0.5) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenNotExistCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { - stubbing(currencyRepository) { - on { findBySymbol(currency.getSymbol()) } doReturn Mono.empty() - on { findById(currency.getSymbol()) } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.isWithdrawAllowed( - wallet, - BigDecimal.valueOf(0.5) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenInsufficientAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenWrongAmount_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.isWithdrawAllowed( - wallet, - BigDecimal.valueOf(-1) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenOwnerAndWalletTypeLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenOwnerAndWalletLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenLevelAndWalletTypeLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - "1", - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenAllUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - "1", - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(1)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenAllLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "withdraw", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - "1", - 2, - "withdraw", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(500)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(30)) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenEmptyWalletWithNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isWithdrawAllowed(wallet, BigDecimal.valueOf(0.5)) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0.5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenNotExistWallet_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { - val wallet = object : Wallet { - override fun id() = 40L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.isDepositAllowed( - wallet, - BigDecimal.valueOf(0.5) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenWrongCurrency_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.isDepositAllowed( - wallet, - BigDecimal.valueOf(0.5) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenInsufficientAmount_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "withdraw") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "withdraw", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - val isAllowed = runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } - - verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndWalletAndAction( - walletOwner.id()!!, - "ETH", - 20, - "withdraw" - ) - verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndActionAndWalletType( - walletOwner.id()!!, - "ETH", - "withdraw", - "main" - ) - verify(walletLimitsRepository, never()).findByLevelAndCurrencyAndActionAndWalletType( - "1", - "ETH", - "withdraw", - "main" - ) - assertThat(isAllowed).isFalse() - } - - @Test - fun givenWrongAmount_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(-1)) } } - } - - @Test - fun givenWalletWithUnreachedLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "deposit", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "deposit", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "deposit", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - "1", - 2, - "deposit", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(5)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(1)) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenWalletWithWalletLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "deposit", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByOwnerAndCurrencyAndActionAndWalletType(2, "ETH", "deposit", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - null, - 2, - "deposit", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") - } doReturn Mono.just( - WalletLimitsModel( - 1, - "1", - 2, - "deposit", - "ETH", - "main", - 30, - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - val wallet = object : Wallet { - override fun id() = 30L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(500)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(30)) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenEmptyWalletWithNoLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(walletLimitsRepository) { - on { - findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id()!!, "ETH", 20, "deposit") - } doReturn Mono.empty() - on { - findByOwnerAndCurrencyAndActionAndWalletType(walletOwner.id()!!, "ETH", "deposit", "main") - } doReturn Mono.empty() - on { - findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "deposit", "main") - } doReturn Mono.empty() - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(0)) - override fun currency() = currency - override fun type() = "main" - } - - val isAllowed = walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { - stubbing(walletOwnerRepository) { - on { findById(walletOwner.id()!!) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - stubbing(walletRepository) { - on { - findByOwnerAndTypeAndCurrency(walletOwner.id()!!, "main", currency.getSymbol()) - } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(1.2) - ) - ) - } - stubbing(currencyRepository) { - on { - findBySymbol(currency.getSymbol()) - } doReturn Mono.just( - CurrencyModel( - currency.getSymbol(), - currency.getName(), - currency.getPrecision() - ) - ) - } - val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) - - assertThat(wallet).isNotNull - assertThat(wallet!!.owner().id()).isEqualTo(walletOwner.id()) - assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) - assertThat(wallet.type()).isEqualTo("main") - } - - @Test - fun givenEmptyWalletWithNoLimit_whenCreateWallet_thenReturnWallet(): Unit = runBlocking { - stubbing(walletRepository) { - on { - save(WalletModel(null, walletOwner.id()!!, "main", currency.getSymbol(), BigDecimal.valueOf(1))) - } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(1) - ) - ) - } - val wallet = walletManagerImpl.createWallet( - walletOwner, - Amount(currency, BigDecimal.valueOf(1)), - currency, - "main" - ) - - assertThat(wallet).isNotNull - assertThat(wallet.owner().id()).isEqualTo(walletOwner.id()) - assertThat(wallet.currency().getSymbol()).isEqualTo(currency.getSymbol()) - assertThat(wallet.type()).isEqualTo("main") - } - - @Test - fun givenWallet_whenIncreaseBalance_thenSuccess(): Unit = runBlocking { - stubbing(walletRepository) { - on { - updateBalance(eq(20), any()) - } doReturn Mono.just(1) - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.increaseBalance( - wallet, - BigDecimal.valueOf(1) - ) - } - }.doesNotThrowAnyException() - } - - @Test - fun givenNotExistWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { - stubbing(walletRepository) { - on { - updateBalance(any(), eq(BigDecimal.valueOf(1))) - } doReturn Mono.just(0) - } - val wallet = object : Wallet { - override fun id() = 40L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.increaseBalance( - wallet, - BigDecimal.valueOf(1) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenWrongAmount_whenIncreaseBalance_thenThrow(): Unit = runBlocking { - stubbing(walletRepository) { - on { - updateBalance(eq(20), any()) - } doReturn Mono.just(0) - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.increaseBalance( - wallet, - BigDecimal.valueOf(-1) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenWallet_whenDecreaseBalance_thenSuccess(): Unit = runBlocking { - stubbing(walletRepository) { - on { updateBalance(eq(20), eq(BigDecimal.valueOf(-1))) } doReturn Mono.just(1) - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.decreaseBalance( - wallet, - BigDecimal.valueOf(1) - ) - } - }.doesNotThrowAnyException() - } - - @Test - fun givenNotExist_whenDecreaseBalance_thenThrow(): Unit = runBlocking { - stubbing(walletRepository) { - on { - updateBalance(any(), eq(BigDecimal.valueOf(-1))) - } doReturn Mono.just(0) - } - val wallet = object : Wallet { - override fun id() = 40L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.decreaseBalance( - wallet, - BigDecimal.valueOf(1) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenWrongAmount_whenDecreaseBalance_thenThrow(): Unit = runBlocking { - stubbing(walletRepository) { - on { - updateBalance(eq(20), eq(BigDecimal.valueOf(-1))) - } doReturn Mono.just(0) - } - val wallet = object : Wallet { - override fun id() = 20L - override fun owner() = walletOwner - override fun balance() = Amount(currency, BigDecimal.valueOf(2)) - override fun currency() = currency - override fun type() = "main" - } - - assertThatThrownBy { - runBlocking { - walletManagerImpl.decreaseBalance( - wallet, - BigDecimal.valueOf(-1) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { - stubbing(walletRepository) { - on { findById(20) } doReturn Mono.just( - WalletModel( - 20L, - walletOwner.id()!!, - "main", - currency.getSymbol(), - BigDecimal.valueOf(0.5) - ) - ) - } - stubbing(walletOwnerRepository) { - on { - findById(walletOwner.id()!!) - } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - stubbing(currencyRepository) { - on { - findById(currency.getSymbol()) - } doReturn Mono.just( - CurrencyModel( - currency.getSymbol(), - currency.getName(), - currency.getPrecision() - ) - ) - } - val wallet = walletManagerImpl.findWalletById(20) - - assertThat(wallet).isNotNull - assertThat(wallet!!.id()).isEqualTo(20) - assertThat(wallet.balance()).isEqualTo(Amount(currency, BigDecimal.valueOf(0.5))) - assertThat(wallet.currency().getSymbol()).isEqualTo("ETH") - } -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt deleted file mode 100644 index ec07e0361..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTestBase.kt +++ /dev/null @@ -1,42 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.ports.postgres.dao.* -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import reactor.core.publisher.Mono - -internal open class WalletManagerTestBase { - protected var walletLimitsRepository: WalletLimitsRepository = mock() - protected var walletRepository: WalletRepository = mock() - protected var walletOwnerRepository: WalletOwnerRepository = mock() - - private var transactionRepository: TransactionRepository = mock { - on { calculateWithdrawStatistics(eq(2), eq(20), any(), any()) } doReturn Mono.empty() - } - - protected val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - protected val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } - - protected var currencyRepository: CurrencyRepository = mock { } - - protected var walletManagerImpl: WalletManagerImpl = WalletManagerImpl( - walletLimitsRepository, transactionRepository, walletRepository, walletOwnerRepository, currencyRepository - ) -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt deleted file mode 100644 index e0ba07d23..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ /dev/null @@ -1,283 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.core.model.Amount -import co.nilin.opex.wallet.ports.postgres.model.UserLimitsModel -import co.nilin.opex.wallet.ports.postgres.model.WalletConfigModel -import co.nilin.opex.wallet.ports.postgres.model.WalletOwnerModel -import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.runBlocking -import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.Assertions.assertThatThrownBy -import org.junit.jupiter.api.Test -import org.mockito.ArgumentMatchers.anyLong -import org.mockito.ArgumentMatchers.anyString -import org.mockito.kotlin.any -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.eq -import org.mockito.kotlin.stubbing -import reactor.core.publisher.Flux -import reactor.core.publisher.Mono -import java.math.BigDecimal - -private class WalletOwnerManagerTest : WalletOwnerManagerTestBase() { - @Test - fun givenOwnerWithNoLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenInvalidAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - - assertThatThrownBy { - runBlocking { - walletOwnerManagerImpl.isWithdrawAllowed( - walletOwner, - Amount(currency, BigDecimal.valueOf(-5)) - ) - } - }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenOwnerWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { - emit( - UserLimitsModel( - 1, - null, - walletOwner.id()!!, - "withdraw", - "main", - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { } - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = - walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenLevelWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "withdraw") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { - emit( - UserLimitsModel( - 1, - "1", - null, - "withdraw", - "main", - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = - walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenOwnerWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenWrongAmount_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow {} - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(-5))) - - assertThat(isAllowed).isTrue() - } - - @Test - fun givenOwnerWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { - emit( - UserLimitsModel( - 1, - null, - walletOwner.id()!!, - "deposit", - "main", - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { } - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = - walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenLevelWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { - stubbing(userLimitsRepository) { - on { findByOwnerAndAction(walletOwner.id()!!, "deposit") } doReturn flow { } - on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { - emit( - UserLimitsModel( - 1, - "1", - null, - "deposit", - "main", - BigDecimal.valueOf(100), - 10, - BigDecimal.valueOf(3000), - 300 - ) - ) - } - } - stubbing(walletConfigRepository) { - on { findAll() } doReturn Flux.just(WalletConfigModel("default", "ETH")) - } - stubbing(transactionRepository) { - on { - calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) - } doReturn Mono.empty() - } - val isAllowed = - walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) - - assertThat(isAllowed).isFalse() - } - - @Test - fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { - stubbing(walletOwnerRepository) { - on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - val wo = walletOwnerManagerImpl.findWalletOwner(walletOwner.uuid()) - - assertThat(wo!!.id()).isEqualTo(walletOwner.id()) - assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) - } - - @Test - fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { - stubbing(walletOwnerRepository) { - on { save(any()) } doReturn Mono.just( - WalletOwnerModel( - walletOwner.id(), - walletOwner.uuid(), - walletOwner.title(), - walletOwner.level(), - walletOwner.isTradeAllowed(), - walletOwner.isWithdrawAllowed(), - walletOwner.isDepositAllowed() - ) - ) - } - val wo = - walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) - - assertThat(wo.id()).isEqualTo(walletOwner.id()) - assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) - } -} diff --git a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt b/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt deleted file mode 100644 index cd5bd1bd0..000000000 --- a/wallet/wallet-app/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTestBase.kt +++ /dev/null @@ -1,35 +0,0 @@ -package co.nilin.opex.wallet.ports.postgres.impl - -import co.nilin.opex.wallet.core.model.Currency -import co.nilin.opex.wallet.core.model.WalletOwner -import co.nilin.opex.wallet.ports.postgres.dao.TransactionRepository -import co.nilin.opex.wallet.ports.postgres.dao.UserLimitsRepository -import co.nilin.opex.wallet.ports.postgres.dao.WalletConfigRepository -import co.nilin.opex.wallet.ports.postgres.dao.WalletOwnerRepository -import org.mockito.kotlin.mock - -internal open class WalletOwnerManagerTestBase { - protected var userLimitsRepository: UserLimitsRepository = mock() - protected var transactionRepository: TransactionRepository = mock() - protected var walletOwnerRepository: WalletOwnerRepository = mock() - protected var walletConfigRepository: WalletConfigRepository = mock() - protected var walletOwnerManagerImpl: WalletOwnerManagerImpl = WalletOwnerManagerImpl( - userLimitsRepository, transactionRepository, walletConfigRepository, walletOwnerRepository - ) - - protected val walletOwner = object : WalletOwner { - override fun id() = 2L - override fun uuid() = "fdf453d7-0633-4ec7-852d-a18148c99a82" - override fun title() = "wallet" - override fun level() = "1" - override fun isTradeAllowed() = true - override fun isWithdrawAllowed() = true - override fun isDepositAllowed() = true - } - - protected val currency = object : Currency { - override fun getSymbol() = "ETH" - override fun getName() = "Ethereum" - override fun getPrecision() = 0.0001 - } -} From 307b23cbefeabc091e7d626cf0ba87ee3c5fa240 Mon Sep 17 00:00:00 2001 From: ebrahimmfadae Date: Mon, 23 May 2022 00:56:28 +0430 Subject: [PATCH 57/58] wallet: Improve test titles --- .../core/service/TransferServiceTest.kt | 12 ++--- .../postgres/impl/CurrencyServiceTest.kt | 6 +-- .../ports/postgres/impl/WalletManagerTest.kt | 48 +++++++++++++------ .../postgres/impl/WalletOwnerManagerTest.kt | 26 ++++++---- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt index 52114977c..d222a35a9 100644 --- a/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt +++ b/wallet/wallet-core/src/test/kotlin/co/nilin/opex/wallet/core/service/TransferServiceTest.kt @@ -42,7 +42,7 @@ private class TransferServiceTest { } @Test - fun givenTransferCommand_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { + fun givenWalletWithAllowedTransfer_whenTransfer_thenReturnTransferResultDetailed(): Unit = runBlocking { stubbing(walletOwnerManager) { onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true @@ -121,7 +121,7 @@ private class TransferServiceTest { } @Test - fun givenOwnerNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + fun givenWalletWithOwnerWithdrawNotAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) @@ -214,7 +214,7 @@ private class TransferServiceTest { } @Test - fun givenWalletNotWithdrawAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + fun givenWalletWithWithdrawNotAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true @@ -305,7 +305,7 @@ private class TransferServiceTest { } @Test - fun givenOwnerNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + fun givenWalletWithOwnerDepositNotAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn false @@ -396,7 +396,7 @@ private class TransferServiceTest { } @Test - fun givenWalletNotDepositAllowed_whenTransfer_thenThrow(): Unit = runBlocking { + fun givenWalletWithDepositNotAllowed_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true @@ -487,7 +487,7 @@ private class TransferServiceTest { } @Test - fun givenNotExistWallet_whenTransfer_thenThrow(): Unit = runBlocking { + fun givenNoWallet_whenTransfer_thenThrow(): Unit = runBlocking { stubbing(walletOwnerManager) { onBlocking { isWithdrawAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true onBlocking { isDepositAllowed(any(), eq(Amount(currency, BigDecimal.valueOf(0.5)))) } doReturn true diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt index 48343fa29..fc6701881 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/CurrencyServiceTest.kt @@ -15,7 +15,7 @@ private class CurrencyServiceTest { private val currencyService: CurrencyServiceImpl = CurrencyServiceImpl(currencyRepository) @Test - fun givenCorrectSymbol_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { + fun givenCurrency_whenGetCurrency_thenReturnCurrency(): Unit = runBlocking { stubbing(currencyRepository) { on { findBySymbol("ETH") } doReturn Mono.just(CurrencyModel("ETH", "Ethereum", 0.0001)) } @@ -28,7 +28,7 @@ private class CurrencyServiceTest { } @Test - fun givenNotExistCurrency_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + fun givenNoCurrency_whenGetCurrency_thenReturnNull(): Unit = runBlocking { stubbing(currencyRepository) { on { findBySymbol("ETH") } doReturn Mono.empty() } @@ -38,7 +38,7 @@ private class CurrencyServiceTest { } @Test - fun givenEmptySymbol_whenGetCurrency_thenReturnNull(): Unit = runBlocking { + fun givenNoCurrency_whenGetCurrencyWithEmptySymbol_thenReturnNull(): Unit = runBlocking { stubbing(currencyRepository) { on { findBySymbol("") } doReturn Mono.empty() } diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt index 89ade70f4..b799cc851 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletManagerTest.kt @@ -13,6 +13,8 @@ import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString import org.mockito.kotlin.* import reactor.core.publisher.Mono import java.math.BigDecimal @@ -74,14 +76,25 @@ private class WalletManagerTest { } @Test - fun givenNotExistWallet_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + fun givenNoWallet_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { val wallet = object : Wallet { - override fun id() = 40L + override fun id() = 20L override fun owner() = walletOwner override fun balance() = Amount(currency, BigDecimal.valueOf(0)) override fun currency() = currency override fun type() = "main" } + stubbing(walletLimitsRepository) { + on { + findByOwnerAndCurrencyAndWalletAndAction(anyLong(), "ETH", anyLong(), "withdraw") + } doReturn Mono.empty() + on { + findByOwnerAndCurrencyAndActionAndWalletType(anyLong(), "ETH", "withdraw", "main") + } doReturn Mono.empty() + on { + findByLevelAndCurrencyAndActionAndWalletType("1", "ETH", "withdraw", "main") + } doReturn Mono.empty() + } assertThatThrownBy { runBlocking { @@ -94,7 +107,7 @@ private class WalletManagerTest { } @Test - fun givenNotExistCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { + fun giNoCurrency_whenIsWithdrawAllowed_thenThrow(): Unit = runBlocking { stubbing(currencyRepository) { on { findBySymbol(currency.getSymbol()) } doReturn Mono.empty() on { findById(currency.getSymbol()) } doReturn Mono.empty() @@ -118,7 +131,7 @@ private class WalletManagerTest { } @Test - fun givenInsufficientAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenEmptyWallet_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -273,7 +286,7 @@ private class WalletManagerTest { } @Test - fun givenAllUnreachedLimit_whenIsWithdrawAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenAllLimits_whenIsWithdrawAllowedWithValidAmount_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") @@ -341,7 +354,7 @@ private class WalletManagerTest { } @Test - fun givenAllLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenAllLimits_whenIsWithdrawAllowedWithInvalidAmount_thenReturnFalse(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "withdraw") @@ -435,7 +448,7 @@ private class WalletManagerTest { } @Test - fun givenFullWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenWalletWithNoLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "deposit") @@ -481,7 +494,7 @@ private class WalletManagerTest { } @Test - fun givenWrongCurrency_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { + fun givenNoCurrency_whenIsDepositAllowed_thenThrow(): Unit = runBlocking { val wallet = object : Wallet { override fun id() = 20L override fun owner() = walletOwner @@ -489,6 +502,10 @@ private class WalletManagerTest { override fun currency() = currency override fun type() = "main" } + stubbing(currencyRepository) { + on { findBySymbol(anyString()) } doReturn Mono.empty() + on { findById(anyString()) } doReturn Mono.empty() + } assertThatThrownBy { runBlocking { @@ -501,7 +518,7 @@ private class WalletManagerTest { } @Test - fun givenInsufficientAmount_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { + fun givenEmptyWallet_whenIsDepositAllowed_thenFalse(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(walletOwner.id(), "ETH", 20, "withdraw") @@ -520,6 +537,7 @@ private class WalletManagerTest { override fun currency() = currency override fun type() = "main" } + val isAllowed = runBlocking { walletManagerImpl.isDepositAllowed(wallet, BigDecimal.valueOf(0.5)) } verify(walletLimitsRepository, never()).findByOwnerAndCurrencyAndWalletAndAction( @@ -557,7 +575,7 @@ private class WalletManagerTest { } @Test - fun givenWalletWithUnreachedLimit_whenIsDepositAllowed_thenReturnTrue(): Unit = runBlocking { + fun givenAllLimits_whenIsDepositAllowedWithValidAmount_thenReturnTrue(): Unit = runBlocking { stubbing(walletLimitsRepository) { on { findByOwnerAndCurrencyAndWalletAndAction(2, "ETH", 30, "deposit") @@ -719,7 +737,7 @@ private class WalletManagerTest { } @Test - fun givenWalletOwner_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { + fun givenWallet_whenFindWalletByOwnerAndCurrencyAndType_thenReturnWallet(): Unit = runBlocking { stubbing(walletOwnerRepository) { on { findById(walletOwner.id()) } doReturn Mono.just( WalletOwnerModel( @@ -757,6 +775,7 @@ private class WalletManagerTest { ) ) } + val wallet = walletManagerImpl.findWalletByOwnerAndCurrencyAndType(walletOwner, "main", currency) assertThat(wallet).isNotNull @@ -780,6 +799,7 @@ private class WalletManagerTest { ) ) } + val wallet = walletManagerImpl.createWallet( walletOwner, Amount(currency, BigDecimal.valueOf(1)), @@ -819,7 +839,7 @@ private class WalletManagerTest { } @Test - fun givenNotExistWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { + fun givenNoWallet_whenIncreaseBalance_thenThrow(): Unit = runBlocking { stubbing(walletRepository) { on { updateBalance(any(), eq(BigDecimal.valueOf(1))) @@ -892,7 +912,7 @@ private class WalletManagerTest { } @Test - fun givenNotExist_whenDecreaseBalance_thenThrow(): Unit = runBlocking { + fun givenNoWallet_whenDecreaseBalance_thenThrow(): Unit = runBlocking { stubbing(walletRepository) { on { updateBalance(any(), eq(BigDecimal.valueOf(-1))) @@ -942,7 +962,7 @@ private class WalletManagerTest { } @Test - fun givenId_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { + fun givenWallet_whenFindWalletById_thenReturnWallet(): Unit = runBlocking { stubbing(walletRepository) { on { findById(20) } doReturn Mono.just( WalletModel( diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt index af132b496..b29f7554e 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt +++ b/wallet/wallet-ports/wallet-persister-postgres/src/test/kotlin/co/nilin/opex/wallet/ports/postgres/impl/WalletOwnerManagerTest.kt @@ -61,13 +61,14 @@ private class WalletOwnerManagerTest { calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) assertThat(isAllowed).isTrue() } @Test - fun givenInvalidAmount_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenNoLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow {} @@ -92,7 +93,7 @@ private class WalletOwnerManagerTest { } @Test - fun givenOwnerWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenOwnerWithLimit_whenIsWithdrawAllowedWithInvalidAmount_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { emit( @@ -119,6 +120,7 @@ private class WalletOwnerManagerTest { calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) @@ -126,7 +128,7 @@ private class WalletOwnerManagerTest { } @Test - fun givenLevelWithLimit_whenIsWithdrawAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenLevelWithLimit_whenIsWithdrawAllowedInvalidAmount_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id(), "withdraw") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("withdraw")) } doReturn flow { @@ -153,6 +155,7 @@ private class WalletOwnerManagerTest { calculateWithdrawStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isWithdrawAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) @@ -173,6 +176,7 @@ private class WalletOwnerManagerTest { calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(0.5))) assertThat(isAllowed).isTrue() @@ -192,13 +196,14 @@ private class WalletOwnerManagerTest { calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(-5))) assertThat(isAllowed).isTrue() } @Test - fun givenOwnerWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenOwnerWithLimit_whenIsDepositAllowedInvalidAmount_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id(), "deposit") } doReturn flow { emit( @@ -225,6 +230,7 @@ private class WalletOwnerManagerTest { calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) @@ -232,7 +238,7 @@ private class WalletOwnerManagerTest { } @Test - fun givenLevelWithLimit_whenIsDepositAllowed_thenReturnFalse(): Unit = runBlocking { + fun givenLevelWithLimit_whenIsDepositAllowedInvalidAmount_thenReturnFalse(): Unit = runBlocking { stubbing(userLimitsRepository) { on { findByOwnerAndAction(walletOwner.id(), "deposit") } doReturn flow { } on { findByLevelAndAction(eq("1"), eq("deposit")) } doReturn flow { @@ -259,6 +265,7 @@ private class WalletOwnerManagerTest { calculateDepositStatisticsBasedOnCurrency(anyLong(), anyString(), any(), any(), anyString()) } doReturn Mono.empty() } + val isAllowed = walletOwnerManagerImpl.isDepositAllowed(walletOwner, Amount(currency, BigDecimal.valueOf(120))) @@ -266,7 +273,7 @@ private class WalletOwnerManagerTest { } @Test - fun givenUUID_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + fun givenWalletOwner_whenFindWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { stubbing(walletOwnerRepository) { on { findByUuid(walletOwner.uuid()) } doReturn Mono.just( WalletOwnerModel( @@ -280,6 +287,7 @@ private class WalletOwnerManagerTest { ) ) } + val wo = walletOwnerManagerImpl.findWalletOwner(walletOwner.uuid()) assertThat(wo!!.id()).isEqualTo(walletOwner.id()) @@ -287,7 +295,7 @@ private class WalletOwnerManagerTest { } @Test - fun givenOwnerInfo_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { + fun givenWalletOwner_whenCreateWalletOwner_thenReturnWalletOwner(): Unit = runBlocking { stubbing(walletOwnerRepository) { on { save(any()) } doReturn Mono.just( WalletOwnerModel( @@ -301,8 +309,8 @@ private class WalletOwnerManagerTest { ) ) } - val wo = - walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) + + val wo = walletOwnerManagerImpl.createWalletOwner(walletOwner.uuid(), walletOwner.title(), walletOwner.level()) assertThat(wo.id()).isEqualTo(walletOwner.id()) assertThat(wo.uuid()).isEqualTo(walletOwner.uuid()) From f1879e47ae8e0e1c5dc74b69f2d565e25229b461 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Mon, 23 May 2022 10:42:21 +0430 Subject: [PATCH 58/58] matching-gateway: Refactor order service tests --- .../gateway/app/service/OrderServiceTest.kt | 243 ++++++++---------- 1 file changed, 102 insertions(+), 141 deletions(-) diff --git a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt index 176f8ce06..60d75b6c4 100644 --- a/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt +++ b/matching-gateway/matching-gateway-app/src/test/kotlin/co/nilin/opex/matching/gateway/app/service/OrderServiceTest.kt @@ -38,17 +38,8 @@ private class OrderServiceTest { ) @Test - fun givenLimitASKOrder_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( - pairConfig, - "ASK", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -58,6 +49,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "ETH", order.quantity) @@ -78,17 +78,8 @@ private class OrderServiceTest { } @Test - fun givenLimitASKOrderWithInvalidSymbol_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByInvalidSymbol_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( - pairConfig, - "ASK", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "BTC_USDT", @@ -98,26 +89,6 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) - stubbing(accountantApiProxy) { - onBlocking { - canCreateOrder(order.uuid!!, "ETH", order.quantity) - } doReturn true - } - stubbing(orderSubmitter) { - onBlocking { - submit(any()) - } doReturn OrderSubmitResult(null) - } - stubbing(kafkaHealthIndicator) { - on { isHealthy } doReturn true - } - - assertThatThrownBy { runBlocking { orderService.submitNewOrder(order) } }.isNotInstanceOf(NullPointerException::class.java) - } - - @Test - fun givenLimitASKOrderWithNotExistOwner_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { - val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) stubbing(pairConfigLoader) { onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( pairConfig, @@ -127,15 +98,6 @@ private class OrderServiceTest { 0.01 ) } - val order = CreateOrderRequest( - "52f01a11-da0d-4050-bdd7-d38349ddfb6a", - "ETH_USDT", - BigDecimal.valueOf(100000), - BigDecimal.valueOf(0.001), - OrderDirection.ASK, - MatchConstraint.GTC, - OrderType.LIMIT_ORDER - ) stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "ETH", order.quantity) @@ -154,17 +116,8 @@ private class OrderServiceTest { } @Test - fun givenLimitASKOrderWithInvalidPrice_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByASKAndInvalidPrice_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( - pairConfig, - "ASK", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -174,6 +127,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "ETH", order.quantity) @@ -192,17 +154,8 @@ private class OrderServiceTest { } @Test - fun givenLimitASKOrderWithInvalidQuantity_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByASKAndInvalidQuantity_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( - pairConfig, - "ASK", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -212,6 +165,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "ETH", order.quantity) @@ -230,17 +192,8 @@ private class OrderServiceTest { } @Test - fun givenLimitASKOrderWithInvalidLevel_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByASKAndInvalidLevel_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.ASK, "1") } doReturn PairFeeConfig( - pairConfig, - "ASK", - "1", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -250,6 +203,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.ASK, "1") } doReturn PairFeeConfig( + pairConfig, + "ASK", + "1", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "ETH", order.quantity) @@ -268,17 +230,8 @@ private class OrderServiceTest { } @Test - fun givenLimitBIDOrder_whenSubmitNewOrder_thenOrderSubmitResult(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByBID_thenOrderSubmitResult(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( - pairConfig, - "BID", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -288,6 +241,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) @@ -307,19 +269,9 @@ private class OrderServiceTest { assertThat(orderSubmitResult).isNotNull } - @Test - fun givenLimitBIDOrderWithInvalidSymbol_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByBIDAndInvalidSymbol_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( - pairConfig, - "BID", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "BTC_USDT", @@ -329,6 +281,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) @@ -347,17 +308,8 @@ private class OrderServiceTest { } @Test - fun givenLimitBIDOrderWithNotExistOwner_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByBIDAndNotExistOwner_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( - pairConfig, - "BID", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "55408c0a-ed53-42d1-b5ee-b2edf531b9d5", "ETH_USDT", @@ -367,6 +319,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) @@ -385,17 +346,8 @@ private class OrderServiceTest { } @Test - fun givenLimitBIDOrderWithInvalidPrice_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByBIDAndInvalidPrice_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( - pairConfig, - "BID", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -405,6 +357,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) @@ -423,17 +384,8 @@ private class OrderServiceTest { } @Test - fun givenLimitBIDOrderWithInvalidQuantity_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByBIDAndInvalidQuantity_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( - pairConfig, - "BID", - "", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -443,6 +395,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "") } doReturn PairFeeConfig( + pairConfig, + "BID", + "", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) @@ -461,17 +422,8 @@ private class OrderServiceTest { } @Test - fun givenLimitBIDOrderWithInvalidLevel_whenSubmitNewOrder_thenThrow(): Unit = runBlocking { + fun givenPair_whenSubmitNewOrderByBIDAndInvalidLevel_thenThrow(): Unit = runBlocking { val pairConfig = PairConfig("ETH_USDT", "ETH", "USDT", 0.01, 0.0001) - stubbing(pairConfigLoader) { - onBlocking { load("ETH_USDT", OrderDirection.BID, "1") } doReturn PairFeeConfig( - pairConfig, - "BID", - "1", - 0.01, - 0.01 - ) - } val order = CreateOrderRequest( "a2930d06-0c84-4448-bff7-65134184bb1d", "ETH_USDT", @@ -481,6 +433,15 @@ private class OrderServiceTest { MatchConstraint.GTC, OrderType.LIMIT_ORDER ) + stubbing(pairConfigLoader) { + onBlocking { load("ETH_USDT", OrderDirection.BID, "1") } doReturn PairFeeConfig( + pairConfig, + "BID", + "1", + 0.01, + 0.01 + ) + } stubbing(accountantApiProxy) { onBlocking { canCreateOrder(order.uuid!!, "USDT", order.quantity * order.price) @@ -499,7 +460,7 @@ private class OrderServiceTest { } @Test - fun givenValidCancelOrder_whenCancelOrder_thenOrderSubmitResult(): Unit = runBlocking { + fun givenEventSubmitter_whenCancelOrder_thenOrderSubmitResult(): Unit = runBlocking { val order = CancelOrderRequest( "edee8090-62d9-4929-b70d-5b97de0c29eb", "a2930d06-0c84-4448-bff7-65134184bb1d",