[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libeufin] 02/03: enforce fractional values in balance table
From: |
gnunet |
Subject: |
[libeufin] 02/03: enforce fractional values in balance table |
Date: |
Mon, 02 Dec 2019 20:08:56 +0100 |
This is an automated email from the git hooks/post-receive script.
marcello pushed a commit to branch master
in repository libeufin.
commit 617ea3f9edb3cb7ff83fcf16a5d01d4e34e0d550
Author: Marcello Stanisci <address@hidden>
AuthorDate: Mon Dec 2 20:06:12 2019 +0100
enforce fractional values in balance table
---
.../src/main/kotlin/tech/libeufin/sandbox/DB.kt | 33 ++++++++++---------
.../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, 82 insertions(+), 18 deletions(-)
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
index 37bd4b9..bc78981 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
@@ -20,9 +20,8 @@
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
@@ -91,6 +90,22 @@ fun Blob.toByteArray(): ByteArray {
return this.binaryStream.readAllBytes()
}
+object BalanceTable : 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.
@@ -107,20 +122,6 @@ class BankCustomerEntity(id: EntityID<Int>) :
IntEntity(id) {
var balance by BalanceEntity referencedOn BankCustomersTable.balance
}
-object BalanceTable : IntIdTable() {
- // Customer ID is the default 'id' field provided by the constructor.
- val value = integer("value")
- val fraction = integer("fraction") // from 0 to 99
-}
-
-class BalanceEntity(id: EntityID<Int>) : IntEntity(id) {
- companion object : IntEntityClass<BankCustomerEntity>(BankCustomersTable)
-
- var value by BalanceTable.value
- var fraction by BalanceTable.fraction
-}
-
-
/**
* This table stores RSA public keys of subscribers.
*/
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..5a7d3c2
--- /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(BalanceTable)
+ }
+
+ 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.