gnunet-svn
[Top][All Lists]
Advanced

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

[libeufin] 10/11: Move more generic helpers to util package.


From: gnunet
Subject: [libeufin] 10/11: Move more generic helpers to util package.
Date: Thu, 30 Apr 2020 21:46:53 +0200

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

marcello pushed a commit to branch master
in repository libeufin.

commit a205b0821dcd4c137bc3c74ef2358c020496f84e
Author: Marcello Stanisci <address@hidden>
AuthorDate: Thu Apr 30 18:37:08 2020 +0200

    Move more generic helpers to util package.
---
 .../src/main/kotlin/tech/libeufin/nexus/Helpers.kt | 69 +---------------------
 nexus/src/main/kotlin/tech/libeufin/nexus/taler.kt |  3 +-
 nexus/src/test/kotlin/DateTest.kt                  | 16 +++++
 nexus/src/test/kotlin/LetterFormatTest.kt          |  3 +-
 util/src/main/kotlin/ParametersChecks.kt           | 33 +++++++++++
 util/src/main/kotlin/strings.kt                    | 24 +++++++-
 6 files changed, 76 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
index 40d7883..e7663eb 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt
@@ -57,22 +57,6 @@ fun extractFirstBic(bankCodes: 
List<EbicsTypes.AbstractBankCode>?): String? {
     return null
 }
 
-/**
- * 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.
  *
@@ -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
index a5abf92..bb16050 100644
--- 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
new file mode 100644
index 0000000..1c9285b
--- /dev/null
+++ 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
index 7c28b46..a268492 100644
--- 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
new file mode 100644
index 0000000..cc910b4
--- /dev/null
+++ 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
index d57b53a..68cfff3 100644
--- 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

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



reply via email to

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