libeufin

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

commit 617ea3f9edb3cb7ff83fcf16a5d01d4e34e0d550
parent 1cd6cfec3779d5b62278021bbc5b8711f6bf7a5a
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Mon,  2 Dec 2019 20:06:12 +0100

enforce fractional values in balance table

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt | 33+++++++++++++++++----------------
Msandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt | 5+++++
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 24++++++++++++++++++++++--
Asandbox/src/test/kotlin/DbTest.kt | 39+++++++++++++++++++++++++++++++++++++++
4 files changed, 83 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 @@ -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 @@ -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 @@ -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 @@ -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