libeufin

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

commit 3f8e7a5f595be2175ca56713ad20a80e562125aa
parent a25c0115b69b6e37ad264d136e38a982993b0fbe
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Thu, 26 Mar 2020 11:17:09 +0100

Throw error if XPath fails.

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/DB.kt | 8++++----
Mnexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 23++++++++++++++++++-----
Mutil/src/main/kotlin/XMLUtil.kt | 13+++++++++----
3 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt @@ -31,15 +31,15 @@ object EbicsRawBankTransactionsTable : IdTable<Long>() { } class EbicsRawBankTransactionEntry(id: EntityID<Long>) : LongEntity(id) { - companion object : IntEntityClass<Pain001Entity>(Pain001Table) - var sourceType by EbicsRawBankTransactionsTable.sourceType + companion object : LongEntityClass<EbicsRawBankTransactionEntry>(EbicsRawBankTransactionsTable) + var sourceType by EbicsRawBankTransactionsTable.sourceType // C52 or C53 or C54? var sourceFileName by EbicsRawBankTransactionsTable.sourceFileName var unstructuredRemittanceInformation by EbicsRawBankTransactionsTable.unstructuredRemittanceInformation var transactionType by EbicsRawBankTransactionsTable.transactionType var currency by EbicsRawBankTransactionsTable.currency var amount by EbicsRawBankTransactionsTable.amount - var creditorIban = EbicsRawBankTransactionsTable.creditorIban - var debitorIban = EbicsRawBankTransactionsTable.debitorIban + var creditorIban by EbicsRawBankTransactionsTable.creditorIban + var debitorIban by EbicsRawBankTransactionsTable.debitorIban var nexusSubscriber by EbicsSubscriberEntity referencedOn EbicsRawBankTransactionsTable.nexusSubscriber } diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt @@ -645,6 +645,7 @@ fun main() { response.orderData.unzipWithLoop { val fileName = it.first val camt53doc = XMLUtil.parseStringIntoDom(it.second) + val creditorIban = XMLUtil.getStringFromXpath( camt53doc, "//*[local-name()='CdtrAcct']//*[local-name()='IBAN']" @@ -657,7 +658,7 @@ fun main() { camt53doc, "//*[local-name()='Ntry']//*[local-name()='CdtDbtInd']" ) - val amount = XMLUtil.getNodeFromXpath( + val amount = XMLUtil.getStringFromXpath( camt53doc, "//*[local-name()='Ntry']//*[local-name()='Amt']" ) @@ -665,19 +666,31 @@ fun main() { camt53doc, "//*[local-name()='RmtInf']//*[local-name()='Ustrd']" ) - val currency = amount?.attributes?.getNamedItem("Ccy")?.nodeValue - + val currency = XMLUtil.getStringFromXpath( + camt53doc, + "//*[local-name()='Ntry']//*[local-name()='Amt']/@Ccy" + ) println( "####" + "\n\tCreditor IBAN: $creditorIban," + "\n\tDebitor IBAN: $debitorIban," + "\n\tCurrency: $currency," + - "\n\tAmount: ${amount?.firstChild?.nodeValue}" + + "\n\tAmount: ${amount}" + "\n\tSubject: $subject," + "\n\tFile name: $fileName" ) - transaction { + EbicsRawBankTransactionEntry.new { + sourceType = "C53" + sourceFileName = fileName + unstructuredRemittanceInformation = subject + transactionType = creditOrDebit + this.currency = currency + this.amount = amount + this.creditorIban = creditorIban + this.debitorIban = debitorIban + nexusSubscriber = getSubscriberEntityFromId(id) + } } } call.respondText( diff --git a/util/src/main/kotlin/XMLUtil.kt b/util/src/main/kotlin/XMLUtil.kt @@ -21,6 +21,7 @@ package tech.libeufin.util import com.sun.org.apache.xerces.internal.dom.DOMInputImpl import com.sun.xml.bind.marshaller.NamespacePrefixMapper +import io.ktor.http.HttpStatusCode import org.slf4j.Logger import org.slf4j.LoggerFactory import org.w3c.dom.Document @@ -407,14 +408,18 @@ class XMLUtil private constructor() { return valResult } - fun getNodeFromXpath(doc: Document, query: String): Node? { + fun getNodeFromXpath(doc: Document, query: String): Node { val xpath = XPathFactory.newInstance().newXPath() - return xpath.evaluate(query, doc, XPathConstants.NODE) as Node? + val ret = xpath.evaluate(query, doc, XPathConstants.NODE) + ?: throw UtilError(HttpStatusCode.NotFound, "Unsuccessful XPath query string: $query") + return ret as Node } - fun getStringFromXpath(doc: Document, query: String): String? { + fun getStringFromXpath(doc: Document, query: String): String { val xpath = XPathFactory.newInstance().newXPath() - return xpath.evaluate(query, doc, XPathConstants.STRING) as String? + val ret = xpath.evaluate(query, doc, XPathConstants.STRING) + ?: throw UtilError(HttpStatusCode.NotFound, "Unsuccessful XPath query string: $query") + return ret as String } } }