gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] 02/02: testing payto/amount parsers


From: gnunet
Subject: [libeufin] 02/02: testing payto/amount parsers
Date: Wed, 08 Apr 2020 19:33:47 +0200

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository libeufin.

commit 107a5bc855579b588fe3c22210f810296075b5b0
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed Apr 8 19:33:37 2020 +0200

    testing payto/amount parsers
---
 nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt |  9 +++++----
 nexus/src/test/kotlin/taler.kt                     | 23 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt 
b/nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt
index c2d2472..2d9f338 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt
@@ -76,7 +76,7 @@ class Taler(app: Route) {
     data class Payto(
         val name: String,
         val iban: String,
-        val bic: String?
+        val bic: String // empty string in case no BIC was given
     )
     data class AmountWithCurrency(
         val currency: String,
@@ -86,10 +86,11 @@ class Taler(app: Route) {
     /** Helper functions */
 
     fun parsePayto(paytoUri: String): Payto {
-        val match = 
Regex("payto://.*/([A-Z0-9]+)/([A-Z0-9]+)?\\?name=(\\w+)").find(paytoUri) ?: 
throw
+        // payto://iban/BIC?/IBAN?name=<name>
+        val match = 
Regex("payto://iban/([A-Z0-9]+/)?([A-Z0-9]+)\\?name=(\\w+)").find(paytoUri) ?: 
throw
                 NexusError(HttpStatusCode.BadRequest, "invalid payto URI 
($paytoUri)")
-        val (iban, bic, name) = match.destructured
-        return Payto(name, iban, bic)
+        val (bic, iban, name) = match.destructured
+        return Payto(name, iban, bic.replace("/", ""))
     }
 
     fun parseAmount(amount: String): AmountWithCurrency {
diff --git a/nexus/src/test/kotlin/taler.kt b/nexus/src/test/kotlin/taler.kt
index a71e6e3..d582a97 100644
--- a/nexus/src/test/kotlin/taler.kt
+++ b/nexus/src/test/kotlin/taler.kt
@@ -5,13 +5,34 @@ import io.ktor.routing.Route
 import io.ktor.routing.RouteSelector
 import io.ktor.routing.RouteSelectorEvaluation
 import io.ktor.util.InternalAPI
+import org.junit.Before
 import org.junit.Test
 import tech.libeufin.nexus.Taler
+import tech.libeufin.util.Amount
+import java.math.BigDecimal
 
 class TalerTest {
+
+    @InternalAPI
+    val taler = Taler(Route(null, RootRouteSelector("unused")))
+
     @InternalAPI
     @Test
     fun paytoParserTest() {
-        val taler = Taler(Route(null, RootRouteSelector("unused")))
+        val payto = taler.parsePayto("payto://iban/ABC/XYZ?name=foo")
+        assert(payto.bic == "ABC" && payto.iban == "XYZ" && payto.name == 
"foo")
+        val paytoNoBic = taler.parsePayto("payto://iban/XYZ?name=foo")
+        assert(paytoNoBic.bic == "" && paytoNoBic.iban == "XYZ" && 
paytoNoBic.name == "foo")
+    }
+
+    @InternalAPI
+    @Test
+    fun amountParserTest() {
+        val amount = taler.parseAmount("EUR:1")
+        assert(amount.currency == "EUR" && amount.amount.equals(BigDecimal(1)))
+        val amount299 = taler.parseAmount("EUR:2.99")
+        assert(amount299.amount.compareTo(Amount("2.99")) == 0)
+        val amount25 = taler.parseAmount("EUR:2.5")
+        assert(amount25.amount.compareTo(Amount("2.5")) == 0)
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]