libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

PaytoTest.kt (3810B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2024-2025 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 import org.junit.Test
     21 import tech.libeufin.common.BankPaytoCtx
     22 import tech.libeufin.common.CommonError
     23 import tech.libeufin.common.Payto
     24 import kotlin.test.assertEquals
     25 import kotlin.test.assertFailsWith
     26 import kotlin.test.assertNull
     27 import java.net.URL
     28 
     29 class PaytoTest {
     30     @Test
     31     fun wrongCases() {
     32         assertFailsWith<CommonError.Payto> { Payto.parse("http://iban/BIC123/IBAN123?receiver-name=The%20Name") }
     33         assertFailsWith<CommonError.Payto> { Payto.parse("payto:iban/BIC123/IBAN123?receiver-name=The%20Name&address=house") }
     34         assertFailsWith<CommonError.Payto> { Payto.parse("payto://wrong/BIC123/IBAN123?sender-name=Foo&receiver-name=Foo") }
     35     }
     36 
     37     @Test
     38     fun parsePaytoTest() {
     39         val withBic = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=The%20Name").expectIban()
     40         assertEquals(withBic.iban.value, "CH9300762011623852957")
     41         assertEquals(withBic.receiverName, "The Name")
     42         val complete = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=The%20Name&amount=EUR:1&message=donation").expectIban()
     43         assertEquals(complete.iban.value, "CH9300762011623852957")
     44         assertEquals(complete.receiverName, "The Name")
     45         assertEquals(complete.message, "donation")
     46         assertEquals(complete.amount.toString(), "EUR:1")
     47         val plusEncode = Payto.parse("payto://iban/BIC123/CH9300762011623852957?receiver-name=Santa+Claus&amount=EUR:1&message=donation").expectIban()
     48         assertEquals(plusEncode.iban.value, "CH9300762011623852957")
     49         assertEquals(plusEncode.receiverName, "Santa Claus")
     50         val withoutOptionals = Payto.parse("payto://iban/CH9300762011623852957").expectIban()
     51         assertNull(withoutOptionals.message)
     52         assertNull(withoutOptionals.receiverName)
     53         assertNull(withoutOptionals.amount)
     54     }
     55 
     56     @Test
     57     fun forms() {
     58         val ctx = BankPaytoCtx(
     59             bic = "TESTBIC",
     60             hostname = "test.com"
     61         )
     62         val canonical = "payto://iban/CH9300762011623852957"
     63         val bank = "payto://iban/TESTBIC/CH9300762011623852957?receiver-name=Name"
     64         val inputs = listOf(
     65             "payto://iban/BIC/CH9300762011623852957?receiver-name=NotGiven",
     66             "payto://iban/CH9300762011623852957?receiver-name=Grothoff%20Hans",
     67             "payto://iban/ch%209300-7620-1162-3852-957",
     68         )
     69         val names = listOf(
     70             "NotGiven", "Grothoff Hans", null
     71         )
     72         val full = listOf(
     73             "payto://iban/BIC/CH9300762011623852957?receiver-name=Santa",
     74             "payto://iban/CH9300762011623852957?receiver-name=Santa",
     75             "payto://iban/CH9300762011623852957?receiver-name=Santa",
     76         )
     77         for ((i, input) in inputs.withIndex()) {
     78             val payto = Payto.parse(input).expectIban()
     79             assertEquals(canonical, payto.canonical)
     80             assertEquals(bank, payto.bank("Name", ctx))
     81             assertEquals(full[i], payto.full("Santa"))
     82             assertEquals(names[i], payto.receiverName)
     83         }
     84     }
     85 }