libeufin

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

commit 3a738ca06c616e6bab151d816f5eb1f5bad432f9
parent e186080e173eecae8fd4c53832e382b9e4323e2d
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Mon,  2 Dec 2019 20:50:07 +0100

return balance as JSON

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt | 3++-
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 30++++++++++++++++++++++--------
2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt @@ -289,7 +289,8 @@ class EbicsUploadTransactionChunkEntity(id : EntityID<String>): Entity<String>(i fun dbCreateTables() { - Database.connect("jdbc:sqlite:libeufin-sandbox.sqlite3", "org.sqlite.JDBC") + // Database.connect("jdbc:sqlite:libeufin-sandbox.sqlite3", "org.sqlite.JDBC") + Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver") TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE transaction { diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -51,11 +51,23 @@ import javax.xml.bind.JAXBContext val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox") -class CustomerNotFound(id: String?) : Exception("Customer {id} not found") +class CustomerNotFound(id: String?) : Exception("Customer ${id} not found") +class BadInputData(inputData: String?) : Exception("Customer provided invalid input data: ${inputData}") + fun findCustomer(id: String?): BankCustomerEntity { - if (id == null) throw Exception("Client gave null value as 'id'") - return BankCustomerEntity.findById(id.toInt()) ?: throw CustomerNotFound(id) + + val idN = try { + id!!.toInt() + } catch (e: Exception) { + e.printStackTrace() + throw BadInputData(id) + } + + return transaction { + + BankCustomerEntity.findById(idN) ?: throw CustomerNotFound(id) + } } fun findEbicsSubscriber(partnerID: String, userID: String, systemID: String?): EbicsSubscriberEntity? { @@ -101,7 +113,6 @@ inline fun <reified T> Document.toObject(): T { return m.unmarshal(this, T::class.java).value } - fun main() { dbCreateTables() @@ -121,7 +132,7 @@ fun main() { name = "Mina" balance = BalanceEntity.new { value = 0 - fraction = 0 + fraction = 99 } } @@ -159,10 +170,13 @@ fun main() { routing { get("/{id}/balance") { - val customer = findCustomer(call.parameters["id"]) + val (name, value, fraction) = transaction { + val tmp = findCustomer(call.parameters["id"]) + Triple(tmp.name, tmp.balance.value, tmp.balance.fraction) + } call.respond(CustomerBalance( - name = customer.name, - balance = "EUR:{customer.balance.value}.{customer.balance.fraction}" + name = name, + balance = "EUR:${value}.${fraction}" )) }