libeufin

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

commit 11bec95c267e88a5bb24273ee2444616812e95e1
parent bf7b6048db5ecd249ee9b49b7d4967cdcd003f8f
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Wed, 16 Oct 2019 17:19:04 +0200

unit tests

Diffstat:
Msandbox/src/main/kotlin/HEVResponse.kt | 4++++
Msandbox/src/main/kotlin/Main.kt | 17+++++++----------
Msandbox/src/main/kotlin/XML.kt | 33+++++++++++++++------------------
Msandbox/src/test/kotlin/DomToJaxbTest.kt | 2+-
Dsandbox/src/test/kotlin/IniLoadJaxbTest.kt | 42------------------------------------------
Asandbox/src/test/kotlin/JaxbTest.kt | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asandbox/src/test/resources/ebics_ini_request_sample_patched.xml | 21+++++++++++++++++++++
Msandbox/src/test/resources/hev_resp3.0.xml | 3++-
8 files changed, 125 insertions(+), 72 deletions(-)

diff --git a/sandbox/src/main/kotlin/HEVResponse.kt b/sandbox/src/main/kotlin/HEVResponse.kt @@ -35,6 +35,10 @@ class HEVResponse( value }() + fun getValue(): HEVResponseDataType { + return value + } + fun makeHEVResponse(): JAXBElement<HEVResponseDataType> { val of = ObjectFactory() return of.createEbicsHEVResponse(value) diff --git a/sandbox/src/main/kotlin/Main.kt b/sandbox/src/main/kotlin/Main.kt @@ -212,8 +212,8 @@ private suspend fun ApplicationCall.ebicsweb() { /* Manage request. */ - val bodyJaxb = xmlProcess.convertDomToJaxb<EbicsUnsecuredRequest>( - "tech.libeufin.messages.ebics.keyrequest", + val bodyJaxb = xmlProcess.convertDomToJaxb( + EbicsUnsecuredRequest::class.java, downcastXml( bodyDocument, "OrderDetails", @@ -264,16 +264,16 @@ private suspend fun ApplicationCall.ebicsweb() { println("That is the key element: ${result.toString(US_ASCII)}") - val keyObject = xmlProcess.convertStringToJaxb<SignaturePubKeyOrderDataType>( - "tech.libeufin.messages.ebics.keyrequest", + val keyObject = xmlProcess.convertStringToJaxb( + SignaturePubKeyOrderDataType::class.java, result.toString(US_ASCII) ) - println(keyObject.signaturePubKeyInfo.signatureVersion) + println(keyObject.value.signaturePubKeyInfo.signatureVersion) } } - + respond( HttpStatusCode.NotImplemented, SandboxError("Not implemented") @@ -292,10 +292,7 @@ private suspend fun ApplicationCall.ebicsweb() { ) val jaxbHEV: JAXBElement<HEVResponseDataType> = hevResponse.makeHEVResponse() - val responseText: String? = xmlProcess.getStringFromJaxb( - "tech.libeufin.messages.ebics.hev", - jaxbHEV - ) + val responseText: String? = xmlProcess.getStringFromJaxb(jaxbHEV) respondText( contentType = ContentType.Application.Xml, diff --git a/sandbox/src/main/kotlin/XML.kt b/sandbox/src/main/kotlin/XML.kt @@ -171,14 +171,13 @@ class XML { /** * Convert a DOM document - of a XML document - to the JAXB representation. * - * @param packageName the package containing the ObjectFactory used to - * instantiate the wanted object. + * @param finalType class type of the output * @param document the document to convert into JAXB. * @return the JAXB object reflecting the original XML document. */ - fun <T>convertDomToJaxb(packageName: String, document: Document) : T { + fun <T>convertDomToJaxb(finalType: Class<T>, document: Document) : T { - val jc = JAXBContext.newInstance(packageName) + val jc = JAXBContext.newInstance(finalType) /* Marshalling the object into the document. */ val m = jc.createUnmarshaller() @@ -188,18 +187,20 @@ class XML { /** * Convert a XML string to the JAXB representation. * - * @param packageName the package containing the ObjectFactory used to - * instantiate the wanted object. + * @param finalType class type of the object to instantiate * @param documentString the string to convert into JAXB. * @return the JAXB object reflecting the original XML document. */ - fun <T>convertStringToJaxb(packageName: String, documentString: String) : T { + fun <T>convertStringToJaxb(finalType: Class<T>, documentString: String) : JAXBElement<T> { - val jc = JAXBContext.newInstance(packageName) + val jc = JAXBContext.newInstance(finalType.packageName) /* Marshalling the object into the document. */ - val m = jc.createUnmarshaller() - return m.unmarshal(StringReader(documentString)) as T // document "went" into Jaxb + val u = jc.createUnmarshaller() + return u.unmarshal( + StreamSource(StringReader(documentString)), + finalType + ) // document "went" into Jaxb } @@ -208,16 +209,14 @@ class XML { * Return the DOM representation of the Java object, using the JAXB * interface. FIXME: narrow input type to JAXB type! * - * @param packageName the package containing the ObjectFactory used to - * instantiate the wanted object. * @param object to be transformed into DOM. Typically, the object * has already got its setters called. * @return the DOM Document, or null (if errors occur). */ - fun convertJaxbToDom(packageName: String, obj: JAXBElement<Unit>): Document? { + fun convertJaxbToDom(obj: JAXBElement<Unit>): Document? { try { - val jc = JAXBContext.newInstance(packageName) + val jc = JAXBContext.newInstance(obj.declaredType) /* Make the target document. */ val dbf = DocumentBuilderFactory.newInstance() @@ -270,16 +269,14 @@ class XML { /** * Extract String from JAXB. * - * @param packageName the package containing the ObjectFactory used to - * instantiate the wanted object. * @param obj the JAXB instance * @return String representation of @a object, or null if errors occur */ - fun <T> getStringFromJaxb(packageName: String, obj: JAXBElement<T>): String? { + fun <T> getStringFromJaxb(obj: JAXBElement<T>): String? { val sw = StringWriter() try { - val jc = JAXBContext.newInstance(packageName) + val jc = JAXBContext.newInstance(obj.declaredType) /* Getting the string. */ val m = jc.createMarshaller() m.marshal(obj, sw) diff --git a/sandbox/src/test/kotlin/DomToJaxbTest.kt b/sandbox/src/test/kotlin/DomToJaxbTest.kt @@ -24,7 +24,7 @@ class DomToJaxbTest { ) processor.convertDomToJaxb<EbicsUnsecuredRequest>( - "tech.libeufin.messages.ebics.keyrequest", + EbicsUnsecuredRequest::class.java, iniDom) } } \ No newline at end of file diff --git a/sandbox/src/test/kotlin/IniLoadJaxbTest.kt b/sandbox/src/test/kotlin/IniLoadJaxbTest.kt @@ -1,41 +0,0 @@ -package tech.libeufin.sandbox - -import kotlinx.coroutines.io.jvm.javaio.toByteReadChannel -import org.junit.Assert -import org.junit.Test -import org.junit.Assert.* -import org.xml.sax.InputSource -import tech.libeufin.messages.ebics.keyrequest.SignaturePubKeyOrderDataType -import java.io.File -import java.io.InputStream -import java.io.StringReader -import javax.xml.bind.JAXBContext -import javax.xml.bind.JAXBElement -import javax.xml.transform.Source -import javax.xml.transform.stream.StreamSource - -class IniKeyMaterialTest { - - val processor = tech.libeufin.sandbox.XML() - - @Test - fun importKey(){ - val classLoader = ClassLoader.getSystemClassLoader() - val ini = classLoader.getResource( - "ebics_ini_inner_key.xml" - ) - - // manual unmarshalling now. - val jc = JAXBContext.newInstance( - "tech.libeufin.messages.ebics.keyrequest" - ) - - /* Marshalling the object into the document. */ - val u = jc.createUnmarshaller() - - val js = u.unmarshal( - StreamSource(((StringReader(ini.readText())))), - SignaturePubKeyOrderDataType::class.java) - - } -} -\ No newline at end of file diff --git a/sandbox/src/test/kotlin/JaxbTest.kt b/sandbox/src/test/kotlin/JaxbTest.kt @@ -0,0 +1,74 @@ +package tech.libeufin.sandbox + +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertTrue +import org.junit.Test +import org.w3c.dom.Element +import tech.libeufin.messages.ebics.hev.HEVResponseDataType +import tech.libeufin.messages.ebics.keyrequest.EbicsUnsecuredRequest +import tech.libeufin.messages.ebics.keyrequest.SignaturePubKeyOrderDataType +import javax.xml.bind.JAXBElement + +class JaxbTest { + + val processor = tech.libeufin.sandbox.XML() + val classLoader = ClassLoader.getSystemClassLoader() + + + /** + * Tests the JAXB instantiation of non-XmlRootElement documents, + * as notably are the inner XML strings carrying keys in INI/HIA + * messages. + */ + @Test + fun importNonRoot() { + + val ini = classLoader.getResource( + "ebics_ini_inner_key.xml" + ) + + val jaxb = xmlProcess.convertStringToJaxb( + SignaturePubKeyOrderDataType::class.java, + ini.readText() + ) + + assertEquals("A006", jaxb.value.signaturePubKeyInfo.signatureVersion) + } + + /** + * Test the JAXB instantiation of a XmlRoot document. + */ + @Test + fun stringToJaxb() { + + val ini = classLoader.getResource("ebics_ini_request_sample_patched.xml") + + val jaxb = xmlProcess.convertStringToJaxb( + EbicsUnsecuredRequest::class.java, + ini.readText() + ) + + assertEquals( + "INI", + jaxb.value.header.static.orderDetails.orderType + ) + } + + @Test + fun jaxbToString() { + + val hevResponse = HEVResponse( + "000000", + "EBICS_OK", + arrayOf( + ProtocolAndVersion("H003", "02.40"), + ProtocolAndVersion("H004", "02.50") + ) + ) + + processor.getStringFromJaxb(hevResponse.makeHEVResponse()) + + // just checking that no exceptions were raised before + assertTrue(true) + } +} +\ No newline at end of file diff --git a/sandbox/src/test/resources/ebics_ini_request_sample_patched.xml b/sandbox/src/test/resources/ebics_ini_request_sample_patched.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<ebicsUnsecuredRequest Revision="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:org:ebics:H004 ebics_keymgmt_request_H004.xsd" Version="H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="urn:org:ebics:H004"> + <header authenticate="true"> + <static> + <HostID>foo</HostID> + <PartnerID>flokid</PartnerID> + <UserID>flouid</UserID> + <OrderDetails xsi:type="UnsecuredReqOrderDetailsType"> + <OrderType>INI</OrderType> + <OrderAttribute>DZNNN</OrderAttribute> + </OrderDetails> + <SecurityMedium>0000</SecurityMedium> + </static> + <mutable /> + </header> + <body> + <DataTransfer> + <OrderData>eJx9U1tzmkAUfu9M/wNDH524XIINjprBaLxSUASDL51FlvvFsIus/vpSYm3a1D6e7/vO/ZzeI00T5ogKHOZZn+XbHMugbJ+7Yeb32ZJ4dw/s4+Dzp54R+hkkZYH00lmgk1a4qBhBApnaP8NdisM+GxBy6AJQVVW7Ett54QOB43jwoi6NfYBSeBdmmMBsj1im1ndxAy7zPSRN8nfuyAn3uIlg1BGYmwxozO/4V3Ftil32UpKLb1TEAU4Gtcat3b5c1P/JztbtM8zfA5hlXt4QNfWGWDAp0QWqQRd314byAX9j1NwtkxIPsBSTgy/SSeTsdr61scm2xaEOxVufvxfM6pirYuqVauu1iGdQcnRzslpOO8P1Up6hyM6xHuRKJ6rUoUG1h6UHoY9OuzkRVKmaxsncPoueMiKrwuKdWFrLggZtIO927pRWIrVlkaTS3LMKdbSVR+OFNOQD82w/u1H5DbyWAsDuWE6rgAp2ZFeVa6zOx44aOMRYyJoyj/RJLnoxxQHppNBI10+uTw/JTDM0IU11vorMs2OZ6r5Yx/JipB4zR4yfaLhJW+h4pC+d+X11sHlH2ZZxnswdqCynyTPxNzm/zMrERGBIwwWVp4e0HCtQx/FD7LvlCz5L2utU8IF1qr5KVOLhwtKpthnqJ711H6/6/R54N+Q/Rz+mhzxDGRkoK2XY6K7IdXfgn8vrgY+L/n0W1tsPDRSO6/TAB7i5IXDriHo6LEiGitlo4CX5XRy6dbIr1ChMfKXLn/TFrl8T3PrNwQ+qBEY2</OrderData> + </DataTransfer> + </body> +</ebicsUnsecuredRequest> diff --git a/sandbox/src/test/resources/hev_resp3.0.xml b/sandbox/src/test/resources/hev_resp3.0.xml @@ -7,4 +7,4 @@ <VersionNumber ProtocolVersion="H003">02.40</VersionNumber> <VersionNumber ProtocolVersion="H004">02.50</VersionNumber> <VersionNumber ProtocolVersion="H005">03.00</VersionNumber> -</ebicsHEVResponse> +</ebicsHEVResponse> +\ No newline at end of file