Skip to content

Commit

Permalink
#396: replace Pageable with limit/offset
Browse files Browse the repository at this point in the history
  • Loading branch information
maryarm committed Nov 28, 2023
1 parent d2c37d7 commit 2e9049c
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package co.nilin.opex.wallet.app.service

import co.nilin.opex.wallet.core.inout.TransferCommand
import co.nilin.opex.wallet.core.model.Amount
import co.nilin.opex.wallet.core.spi.*
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration
import org.springframework.context.annotation.Import
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.ActiveProfiles
import java.math.BigDecimal
import java.time.LocalDateTime
import java.util.*

@SpringBootTest
@DirtiesContext
@ActiveProfiles("test")
@Import(TestChannelBinderConfiguration::class)

class TransactionManagerImplIT {
@Autowired
lateinit var transferManager: TransferManager

@Autowired
lateinit var currencyService: CurrencyService

@Autowired
lateinit var walletManager: WalletManager

@Autowired
lateinit var walletOwnerManager: WalletOwnerManager

@Autowired
lateinit var transactionManager: TransactionManager

val senderWalletType = "main"
val receiverWalletType = "exchange"
val cc = "CC"
val amount = BigDecimal.valueOf(10)
var sourceUuid: String? = null
var destUuid: String? = null

@BeforeEach
fun setup() {
sourceUuid = UUID.randomUUID().toString()
setupWallets(sourceUuid!!)
}

@Test
fun givenMultipleTransfer_whenFindTransactions_thenOrderedAndPaginated() {
runBlocking {
val currency = currencyService.getCurrency(cc)!!

destUuid = UUID.randomUUID().toString()
setupWallets(destUuid!!)

val sender = walletOwnerManager.findWalletOwner(sourceUuid!!)!!
val receiver = walletOwnerManager.findWalletOwner(destUuid!!)!!

val count = 5
for (i in 1..count) {
val sourceWallet = walletManager.findWalletByOwnerAndCurrencyAndType(sender, senderWalletType, currency)
val receiverWallet = walletManager.findWalletByOwnerAndCurrencyAndType(receiver, receiverWalletType, currency)
transferManager.transfer(
TransferCommand(
sourceWallet!!,
receiverWallet!!,
Amount(sourceWallet.currency, amount.divide(BigDecimal.valueOf(count * 1L))),
"Amount1 ${System.currentTimeMillis()}", "Ref1 ${System.currentTimeMillis()}",
"NORMAL",
mapOf(Pair("key", "val"))
)
)
}

val thSender = transactionManager.findTransactions(
sender.uuid, currency.symbol, "NORMAL", LocalDateTime.now().minusHours(1), LocalDateTime.now(), true, 3, 3
)

assertEquals(2, thSender.size)
assertTrue(thSender.first().date.compareTo(thSender.last().date) < 0)


val thReceiver = transactionManager.findTransactions(
receiver.uuid, currency.symbol, "NORMAL", LocalDateTime.now().minusHours(1), LocalDateTime.now(), false, 3, 1
)

assertEquals(3, thReceiver.size)
assertTrue(thReceiver.first().date.compareTo(thReceiver.last().date) > 0)

val thReceiverAll = transactionManager.findTransactions(
receiver.uuid, null, null, LocalDateTime.now().minusHours(1), LocalDateTime.now(), true, 100, 0
)
assertEquals(count, thReceiverAll.size)

}
}

fun setupWallets(sourceUuid: String) {
runBlocking {
var currency = currencyService.getCurrency(cc)
if (currency == null) {
currencyService.deleteCurrency(cc)
currencyService.addCurrency(cc, cc, BigDecimal.ONE)
currency = currencyService.getCurrency(cc)
}
val sourceOwner = walletOwnerManager.createWalletOwner(sourceUuid, "not set", "")
walletManager.createWallet(sourceOwner, Amount(currency!!, amount.multiply(BigDecimal.valueOf(2))), currency, senderWalletType)
walletManager.createWallet(
sourceOwner,
Amount(currency, BigDecimal.ZERO),
currency,
receiverWalletType
)

}
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package co.nilin.opex.wallet.ports.postgres.dao
import co.nilin.opex.wallet.ports.postgres.dto.DepositWithdrawTransaction
import co.nilin.opex.wallet.ports.postgres.dto.TransactionStat
import co.nilin.opex.wallet.ports.postgres.model.TransactionModel
import org.springframework.data.domain.Pageable
import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.data.repository.reactive.ReactiveCrudRepository
Expand Down Expand Up @@ -180,15 +179,46 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
and t.transaction_date <= :endTime
and (:category is null or t.transfer_category = :category)
and (:currency is null or w.currency = :currency)
order by date asc
limit :limit
offset :offset
"""
)
fun findTransactions(
fun findTransactionsAsc(
@Param("uuid") uuid: String,
@Param("currency") currency: String?,
@Param("category") category: String?,
@Param("startTime") startTime: LocalDateTime,
@Param("endTime") endTime: LocalDateTime,
pageable: Pageable
@Param("limit") limit: Int,
@Param("offset") offset: Int,
): Flux<DepositWithdrawTransaction>

@Query(
"""
select distinct t.id, w.currency, t.dest_amount as amount, t.description, t.transfer_ref as ref, t.transaction_date as date
, t.transfer_category as category, t.transfer_detail_json as detail, t.source_wallet as sender, t.dest_wallet as receiver, w.id as owner
from wallet as w
inner join wallet_owner as wo on (w.owner = wo.id)
inner join transaction as t on w.id in (t.source_wallet, t.dest_wallet)
where wo.uuid = :uuid
and t.transaction_date > :startTime
and t.transaction_date <= :endTime
and (:category is null or t.transfer_category = :category)
and (:currency is null or w.currency = :currency)
order by date desc
limit :limit
offset :offset
"""
)
fun findTransactionsDesc(
@Param("uuid") uuid: String,
@Param("currency") currency: String?,
@Param("category") category: String?,
@Param("startTime") startTime: LocalDateTime,
@Param("endTime") endTime: LocalDateTime,
@Param("limit") limit: Int,
@Param("offset") offset: Int,
): Flux<DepositWithdrawTransaction>

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import co.nilin.opex.wallet.ports.postgres.model.TransactionModel
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.reactive.awaitFirstOrElse
import kotlinx.coroutines.reactive.awaitSingle
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.time.ZoneId
Expand Down Expand Up @@ -108,7 +106,11 @@ class TransactionManagerImpl(
offset: Int
): List<TransactionHistory> {
val transactions =
transactionRepository.findTransactions(uuid, coin, category, startTime, endTime, PageRequest.of(offset, limit, Sort.by(if (asc) Sort.Direction.ASC else Sort.Direction.DESC, "transaction_date")))
if (asc)
transactionRepository.findTransactionsAsc(uuid, coin, category, startTime, endTime, limit, offset)
else
transactionRepository.findTransactionsDesc(uuid, coin, category, startTime, endTime, limit, offset)

return transactions.collectList()
.awaitFirstOrElse { emptyList() }
.map {
Expand Down

0 comments on commit 2e9049c

Please sign in to comment.