commit 9c7079e5323eed4d16e24c1c4245d6586cecac53
parent d75824cd235b81680ca9e38ad9e6bebb3e3a1010
Author: MS <ms@taler.net>
Date: Tue, 4 Apr 2023 17:23:20 +0200
Amount comparison.
Checking zero amounts as strings, rather than
via brittle BigDecimal operations.
Diffstat:
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt
@@ -804,8 +804,8 @@ fun circuitApi(circuitRoute: Route) {
resourceName,
withBankFault = true // See comment "CUSTOMER AND BANK ACCOUNT INVARIANT".
)
- val balance = getBalance(bankAccount)
- if (balance != BigDecimal.ZERO) {
+ val balance: BigDecimal = getBalance(bankAccount)
+ if (!isAmountZero(balance)) {
logger.error("Account $resourceName has $balance balance. Won't delete it")
throw SandboxError(
HttpStatusCode.PreconditionFailed,
diff --git a/util/src/main/kotlin/amounts.kt b/util/src/main/kotlin/amounts.kt
@@ -2,6 +2,7 @@ package tech.libeufin.util
import UtilError
import io.ktor.http.*
+import java.math.BigDecimal
/*
* This file is part of LibEuFin.
@@ -36,4 +37,12 @@ fun parseAmount(amount: String): AmountWithCurrency {
throw UtilError(HttpStatusCode.BadRequest, "invalid amount: $amount")
val (currency, number) = match.destructured
return AmountWithCurrency(currency = currency, amount = number)
+}
+
+fun isAmountZero(a: BigDecimal): Boolean {
+ a.abs().toPlainString().forEach {
+ if (it != '0' && it != '.')
+ return false
+ }
+ return true
}
\ No newline at end of file
diff --git a/util/src/test/kotlin/AmountTest.kt b/util/src/test/kotlin/AmountTest.kt
@@ -1,6 +1,11 @@
+import io.ktor.util.reflect.*
import org.junit.Test
+import tech.libeufin.sandbox.roundToTwoDigits
+import tech.libeufin.util.isAmountZero
import tech.libeufin.util.parseAmount
import tech.libeufin.util.validatePlainAmount
+import java.math.BigDecimal
+import kotlin.reflect.typeOf
inline fun <reified ExceptionType> assertException(block: () -> Unit) {
try {
@@ -13,6 +18,12 @@ inline fun <reified ExceptionType> assertException(block: () -> Unit) {
}
class AmountTest {
@Test
+ fun equalTest() {
+ val z = BigDecimal("-0000000000.0000000000")
+ assert(isAmountZero(z))
+ }
+
+ @Test
fun parse() {
var res = parseAmount("KUDOS:5.5")
assert(res.amount == "5.5")