libeufin

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

commit a205b0821dcd4c137bc3c74ef2358c020496f84e
parent 2109595edd337422be4eb29cac520ce99c5599c0
Author: Marcello Stanisci <ms@taler.net>
Date:   Thu, 30 Apr 2020 18:37:08 +0200

Move more generic helpers to util package.

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt | 69++-------------------------------------------------------------------
Mnexus/src/main/kotlin/tech/libeufin/nexus/taler.kt | 3+--
Anexus/src/test/kotlin/DateTest.kt | 17+++++++++++++++++
Mnexus/src/test/kotlin/LetterFormatTest.kt | 3+--
Autil/src/main/kotlin/ParametersChecks.kt | 34++++++++++++++++++++++++++++++++++
Mutil/src/main/kotlin/strings.kt | 24+++++++++++++++++++++++-
6 files changed, 78 insertions(+), 72 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt @@ -58,22 +58,6 @@ fun extractFirstBic(bankCodes: List<EbicsTypes.AbstractBankCode>?): String? { } /** - * Get EBICS subscriber details from bank account id. - * bank account id => ... => ebics details - */ -fun getSubscriberDetailsFromBankAccount(bankAccountId: String): EbicsClientSubscriberDetails { - return transaction { - val map = BankAccountMapEntity.find { - BankAccountMapsTable.bankAccount eq bankAccountId - }.firstOrNull() ?: throw NexusError( - HttpStatusCode.NotFound, - "Such bank account '$bankAccountId' has no EBICS subscriber associated" - ) - getSubscriberDetailsInternal(map.ebicsSubscriber) - } -} - -/** * Given a nexus user id, returns the _list_ of bank accounts associated to it. * * @param id the subscriber id @@ -293,28 +277,6 @@ fun createPain001entity(entry: Pain001Data, nexusUser: NexusUserEntity): Pain001 } } -/** - * Inserts spaces every 2 characters, and a newline after 8 pairs. - */ -fun chunkString(input: String): String { - val ret = StringBuilder() - var columns = 0 - for (i in input.indices) { - if ((i + 1).rem(2) == 0) { - if (columns == 15) { - ret.append(input[i] + "\n") - columns = 0 - continue - } - ret.append(input[i] + " ") - columns++ - continue - } - ret.append(input[i]) - } - return ret.toString().toUpperCase() -} - fun expectId(param: String?): String { return param ?: throw NexusError(HttpStatusCode.BadRequest, "Bad ID given") } @@ -332,34 +294,6 @@ fun extractNexusUser(param: String?): NexusUserEntity { } } -fun ApplicationCall.expectUrlParameter(name: String): String { - return this.request.queryParameters[name] - ?: throw NexusError(HttpStatusCode.BadRequest, "Parameter '$name' not provided in URI") -} - -fun expectInt(param: String): Int { - return try { - param.toInt() - } catch (e: Exception) { - throw NexusError(HttpStatusCode.BadRequest,"'$param' is not Int") - } -} - -fun expectLong(param: String): Long { - return try { - param.toLong() - } catch (e: Exception) { - throw NexusError(HttpStatusCode.BadRequest,"'$param' is not Long") - } -} - -fun expectLong(param: String?): Long? { - if (param != null) { - return expectLong(param) - } - return null -} - /* Needs a transaction{} block to be called */ fun expectAcctidTransaction(param: String?): BankAccountEntity { if (param == null) { @@ -387,7 +321,8 @@ fun extractUserAndHashedPassword(authorizationHeader: String): Pair<String, Byte } /** - * Test HTTP basic auth. Throws error if password is wrong + * Test HTTP basic auth. Throws error if password is wrong, + * and makes sure that the user exists in the system. * * @param authorization the Authorization:-header line. * @return subscriber id diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt @@ -16,8 +16,7 @@ import org.jetbrains.exposed.dao.IdTable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction import org.joda.time.DateTime -import tech.libeufin.util.Amount -import tech.libeufin.util.CryptoUtil +import tech.libeufin.util.* import kotlin.math.abs import kotlin.math.min diff --git a/nexus/src/test/kotlin/DateTest.kt b/nexus/src/test/kotlin/DateTest.kt @@ -0,0 +1,16 @@ +package tech.libeufin.nexus + +import org.joda.time.DateTime +import org.junit.Test +import tech.libeufin.util.toDashedDate +import tech.libeufin.util.parseDashedDate + +class DateTest { + @Test + fun dashedDateParsing() { + val parseddate = parseDashedDate("2020-04-30") + println("Parsed value: " + parseddate.toLocalDate()) + println("To dashed value: " + parseddate.toDashedDate()) + println("System now(): " + DateTime.now().toLocalDate()) + } +} +\ No newline at end of file diff --git a/nexus/src/test/kotlin/LetterFormatTest.kt b/nexus/src/test/kotlin/LetterFormatTest.kt @@ -1,8 +1,7 @@ package tech.libeufin.nexus import org.junit.Test -import tech.libeufin.nexus.chunkString -import tech.libeufin.nexus.getNonce +import tech.libeufin.util.chunkString import tech.libeufin.util.toHexString import java.security.SecureRandom diff --git a/util/src/main/kotlin/ParametersChecks.kt b/util/src/main/kotlin/ParametersChecks.kt @@ -0,0 +1,33 @@ +package tech.libeufin.util + +import io.ktor.application.ApplicationCall +import io.ktor.http.HttpStatusCode + +fun expectInt(param: String): Int { + return try { + param.toInt() + } catch (e: Exception) { + throw UtilError(HttpStatusCode.BadRequest,"'$param' is not Int") + } +} + +fun expectLong(param: String): Long { + return try { + param.toLong() + } catch (e: Exception) { + throw UtilError(HttpStatusCode.BadRequest,"'$param' is not Long") + } +} + +fun expectLong(param: String?): Long? { + if (param != null) { + return expectLong(param) + } + return null +} + + +fun ApplicationCall.expectUrlParameter(name: String): String { + return this.request.queryParameters[name] + ?: throw UtilError(HttpStatusCode.BadRequest, "Parameter '$name' not provided in URI") +} +\ No newline at end of file diff --git a/util/src/main/kotlin/strings.kt b/util/src/main/kotlin/strings.kt @@ -1,5 +1,5 @@ package tech.libeufin.util -import org.apache.commons.codec.binary.Base32 + import java.math.BigInteger import java.util.* @@ -47,4 +47,26 @@ fun BigInteger.toUnsignedHexString(): String { val start = if (signedValue[0] == 0.toByte()) { 1 } else { 0 } val bytes = Arrays.copyOfRange(signedValue, start, signedValue.size) return bytes.toHexString() +} + +/** + * Inserts spaces every 2 characters, and a newline after 8 pairs. + */ +fun chunkString(input: String): String { + val ret = StringBuilder() + var columns = 0 + for (i in input.indices) { + if ((i + 1).rem(2) == 0) { + if (columns == 15) { + ret.append(input[i] + "\n") + columns = 0 + continue + } + ret.append(input[i] + " ") + columns++ + continue + } + ret.append(input[i]) + } + return ret.toString().toUpperCase() } \ No newline at end of file