libeufin

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

commit d943e1820abe29b7efbd2a8e6e66f0eb4d6504cd
parent 48f777aaaec036dc06083cc34cd1160898e67a9e
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Wed,  4 Dec 2019 23:16:59 +0100

avoid Float to represent money.

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt | 38++++++++++++++++++++++++++++++++++++--
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 11++++++-----
Msandbox/src/main/python/libeufin-cli | 1-
Msandbox/src/test/kotlin/DbTest.kt | 26+++++++++++---------------
4 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt @@ -32,6 +32,8 @@ const val EBICS_HOST_ID_MAX_LENGTH = 10 const val EBICS_USER_ID_MAX_LENGTH = 10 const val EBICS_PARTNER_ID_MAX_LENGTH = 10 const val EBICS_SYSTEM_ID_MAX_LENGTH = 10 +const val MAX_ID_LENGTH = 21 // enough to contain IBANs +const val MAX_SUBJECT_LENGTH = 140 // okay? /** * All the states to give a subscriber. @@ -90,6 +92,39 @@ fun Blob.toByteArray(): ByteArray { return this.binaryStream.readAllBytes() } +object BankTransactionsTable : IntIdTable() { + + /* Using varchar to store the IBAN - or possibly other formats + * - from the counterpart. */ + val counterpart = varchar("counterpart", MAX_ID_LENGTH) + val amountSign = integer("amountSign").check { (it eq 1) or (it eq -1)} + val amountValue = integer("amountValue").check { GreaterEqOp(it, intParam(0)) } + val amountFraction = integer("amountFraction").check { LessOp(it, intParam(100)) } + val subject = varchar("subject", MAX_SUBJECT_LENGTH) + val date = date("date") + val localCustomer = reference("localCustomer", BankCustomersTable) +} + +class BankTransactionEntity(id: EntityID<Int>) : IntEntity(id) { + + companion object : IntEntityClass<BankTransactionEntity>(BankTransactionsTable) + + /* the id of the local customer involved in this transaction, + * either as the credit or the debit part; makes lookups easier */ + var localCustomer by BankCustomerEntity referencedOn BankTransactionsTable.localCustomer + + /* keeping as strings, as to allow hosting IBANs and/or other + * unobvious formats. */ + var counterpart by BankTransactionsTable.counterpart + + var subject by BankTransactionsTable.subject + var date by BankTransactionsTable.date + + var amountValue by BankTransactionsTable.amountValue + var amountFraction by BankTransactionsTable.amountFraction + var amountSign by BankTransactionsTable.amountSign +} + /** * This table information *not* related to EBICS, for all * its customers. @@ -97,15 +132,14 @@ fun Blob.toByteArray(): ByteArray { object BankCustomersTable : IntIdTable() { // Customer ID is the default 'id' field provided by the constructor. val name = varchar("name", CUSTOMER_NAME_MAX_LENGTH).primaryKey() - val balance = float("balance") } class BankCustomerEntity(id: EntityID<Int>) : IntEntity(id) { companion object : IntEntityClass<BankCustomerEntity>(BankCustomersTable) var name by BankCustomersTable.name - var balance by BankCustomersTable.balance } + /** * This table stores RSA public keys of subscribers. */ diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -130,7 +130,6 @@ fun main() { val customerEntity = BankCustomerEntity.new { name = "Mina" - balance = 0.toFloat() } @@ -168,17 +167,19 @@ fun main() { routing { get("/{id}/balance") { - val (name, balanceFloat) = transaction { - val tmp = findCustomer(call.parameters["id"]) - Pair(tmp.name, tmp.balance) + val (name, balance) = transaction { + val tmp: BankCustomerEntity = findCustomer(call.parameters["id"]) + Pair(tmp.name, 0) // fixme/todo! } + call.respond( CustomerBalance( name = name, - balance = "EUR:${balanceFloat}" + balance = "EUR:${balance}" ) ) } + get("/") { call.respondText("Hello LibEuFin!\n", ContentType.Text.Plain) } diff --git a/sandbox/src/main/python/libeufin-cli b/sandbox/src/main/python/libeufin-cli @@ -28,7 +28,6 @@ def cli(ctx, base_url, bank_base_url): def ebics(): pass - @ebics.command(help="Restore private keys backup.") @click.pass_obj @click.option( diff --git a/sandbox/src/test/kotlin/DbTest.kt b/sandbox/src/test/kotlin/DbTest.kt @@ -4,6 +4,7 @@ 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.joda.time.DateTime import org.junit.Test import kotlin.test.assertFailsWith import kotlin.test.assertTrue @@ -17,27 +18,22 @@ class DbTest { transaction { + SchemaUtils.create(BankTransactionsTable) SchemaUtils.create(BankCustomersTable) - SchemaUtils.create(EbicsSubscribersTable) - SchemaUtils.create(EbicsSubscriberPublicKeysTable) val customer = BankCustomerEntity.new { - name = "username" - balance = Float.MIN_VALUE + name = "employee" } - val row = EbicsSubscriberEntity.new { - userId = "user id" - partnerId = "partner id" - nextOrderID = 0 - state = SubscriberState.NEW - bankCustomer = customer + val ledgerEntry = BankTransactionEntity.new { + amountSign = 1 + amountValue = 5 + amountFraction = 0 + counterpart = "IBAN" + subject = "Salary" + date = DateTime.now() + localCustomer = customer } - - customer.balance = 100.toFloat() - - logger.info("${row.bankCustomer.balance}") - assertTrue(row.bankCustomer.balance.equals(100.toFloat())) } } } \ No newline at end of file