gnunet-svn
[Top][All Lists]
Advanced

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

[libeufin] branch master updated (40584d1 -> d33a863)


From: gnunet
Subject: [libeufin] branch master updated (40584d1 -> d33a863)
Date: Mon, 02 Dec 2019 20:08:54 +0100

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

marcello pushed a change to branch master
in repository libeufin.

    from 40584d1  introduce balances (database)
     new 1cd6cfe  minor fix
     new 617ea3f  enforce fractional values in balance table
     new d33a863  respect plural names for tables

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/main/kotlin/tech/libeufin/sandbox/DB.kt    | 40 ++++++++++++----------
 .../src/main/kotlin/tech/libeufin/sandbox/JSON.kt  |  5 +++
 .../src/main/kotlin/tech/libeufin/sandbox/Main.kt  | 24 +++++++++++--
 sandbox/src/test/kotlin/DbTest.kt                  | 38 ++++++++++++++++++++
 4 files changed, 86 insertions(+), 21 deletions(-)
 create mode 100644 sandbox/src/test/kotlin/DbTest.kt

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
index 99b135b..e411b7d 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
@@ -20,13 +20,13 @@
 package tech.libeufin.sandbox
 
 import org.jetbrains.exposed.dao.*
-import org.jetbrains.exposed.sql.Database
+import org.jetbrains.exposed.sql.*
 import org.jetbrains.exposed.sql.transactions.TransactionManager
-import org.jetbrains.exposed.sql.SchemaUtils
 import org.jetbrains.exposed.sql.transactions.transaction
 import java.sql.Blob
 import java.sql.Connection
 
+
 const val CUSTOMER_NAME_MAX_LENGTH = 20
 const val EBICS_HOST_ID_MAX_LENGTH = 10
 const val EBICS_USER_ID_MAX_LENGTH = 10
@@ -90,6 +90,22 @@ fun Blob.toByteArray(): ByteArray {
     return this.binaryStream.readAllBytes()
 }
 
+object BalancesTable : IntIdTable() {
+    // Customer ID is the default 'id' field provided by the constructor.
+    val value = integer("value")
+    val fraction = integer("fraction").check {
+        LessEqOp(it, intParam(100))
+    } // enforcing fractional values to be up to 100
+
+}
+
+class BalanceEntity(id: EntityID<Int>) : IntEntity(id) {
+    companion object : IntEntityClass<BalanceEntity>(BalanceTable)
+
+    var value by BalanceTable.value
+    var fraction by BalanceTable.fraction
+}
+
 /**
  * This table information *not* related to EBICS, for all
  * its customers.
@@ -103,24 +119,9 @@ object BankCustomersTable : IntIdTable() {
 class BankCustomerEntity(id: EntityID<Int>) : IntEntity(id) {
     companion object : IntEntityClass<BankCustomerEntity>(BankCustomersTable)
     var name by BankCustomersTable.name
-    val balance by BalanceTable referencedOn BankCustomersTable.balance
-}
-
-object BalanceTable : IntIdTable() {
-    // Customer ID is the default 'id' field provided by the constructor.
-    val value = int("value")
-    val fraction = int("fraction") // from 0 to 99
-}
-
-class BalanceEntity(id: EntityID<Int>) : IntEntity(id) {
-    companion object : IntEntityClass<BankCustomerEntity>(BankCustomersTable)
-
-    var balance by BalanceTable.balance
+    var balance by BalanceEntity referencedOn BankCustomersTable.balance
 }
 
-
-
-
 /**
  * This table stores RSA public keys of subscribers.
  */
@@ -193,7 +194,7 @@ class EbicsSubscriberEntity(id: EntityID<Int>) : 
IntEntity(id) {
     var nextOrderID by EbicsSubscribersTable.nextOrderID
 
     var state by EbicsSubscribersTable.state
-    var balance by BalanceTable referencedOn EbicsSubscribersTable.balance
+    var balance by BalanceEntity referencedOn EbicsSubscribersTable.balance
 }
 
 
@@ -295,6 +296,7 @@ fun dbCreateTables() {
         // addLogger(StdOutSqlLogger)
 
         SchemaUtils.createMissingTablesAndColumns(
+            BalancesTable,
             BankCustomersTable,
             EbicsSubscribersTable,
             EbicsHostsTable,
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
index 428b9c4..2f7bcd1 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
@@ -37,6 +37,11 @@ data class CustomerResponse(
     val id: Int
 )
 
+data class CustomerBalance(
+    val name: String,
+    val balance: String
+)
+
 /**
  * Response for GET /admin/customers/:id
  */
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
index 2d14a0f..6cb118d 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -42,6 +42,7 @@ import org.jetbrains.exposed.sql.transactions.transaction
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
 import org.w3c.dom.Document
+import java.lang.NumberFormatException
 import java.security.interfaces.RSAPublicKey
 import java.text.DateFormat
 import javax.sql.rowset.serial.SerialBlob
@@ -50,6 +51,12 @@ import javax.xml.bind.JAXBContext
 
 val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox")
 
+class CustomerNotFound(id: String?) : Exception("Customer {id} not found")
+
+fun findCustomer(id: String?): BankCustomerEntity {
+    if (id == null) throw Exception("Client gave null value as 'id'")
+    return BankCustomerEntity.findById(id.toInt()) ?: throw 
CustomerNotFound(id)
+}
 
 fun findEbicsSubscriber(partnerID: String, userID: String, systemID: String?): 
EbicsSubscriberEntity? {
     return if (systemID == null) {
@@ -118,10 +125,13 @@ fun main() {
             nextOrderID = 1
         }
 
+
         BankCustomerEntity.new {
             name = "Mina"
-            balance = 0
-            ebicsSubscriber = subscriber
+            balance = BalanceEntity.new {
+                value = 0
+                fraction = 0
+            }
         }
     }
 
@@ -147,6 +157,16 @@ fun main() {
             }
         }
         routing {
+
+            get("/{id}/balance") {
+                val customer = findCustomer(call.parameters["id"])
+                call.respond(CustomerBalance(
+                    name = customer.name,
+                    balance = 
"EUR:{customer.balance.value}.{customer.balance.fraction}"
+                ))
+            }
+
+
             //trace { logger.info(it.buildText()) }
             get("/") {
                 call.respondText("Hello LibEuFin!\n", ContentType.Text.Plain)
diff --git a/sandbox/src/test/kotlin/DbTest.kt 
b/sandbox/src/test/kotlin/DbTest.kt
new file mode 100644
index 0000000..fbcfac8
--- /dev/null
+++ b/sandbox/src/test/kotlin/DbTest.kt
@@ -0,0 +1,38 @@
+package tech.libeufin.sandbox
+
+import org.jetbrains.exposed.exceptions.ExposedSQLException
+import org.jetbrains.exposed.sql.Database
+import org.jetbrains.exposed.sql.SchemaUtils
+import org.jetbrains.exposed.sql.transactions.transaction
+import org.junit.Test
+import kotlin.test.assertFailsWith
+import kotlin.test.assertTrue
+
+class DbTest {
+
+    @Test
+    fun valuesRange() {
+
+        Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = 
"org.h2.Driver")
+
+        transaction {
+            SchemaUtils.create(BalancesTable)
+        }
+
+        assertFailsWith<ExposedSQLException> {
+            transaction {
+                BalanceEntity.new {
+                    value = 101
+                    fraction = 101
+                }
+            }
+        }
+
+        transaction {
+            BalanceEntity.new {
+                value = 101
+                fraction = 100
+            }
+        }
+    }
+}
\ 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]