gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [libeufin] branch master updated: unit tests


From: gnunet
Subject: [GNUnet-SVN] [libeufin] branch master updated: unit tests
Date: Wed, 16 Oct 2019 17:19:11 +0200

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 11bec95  unit tests
11bec95 is described below

commit 11bec95c267e88a5bb24273ee2444616812e95e1
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed Oct 16 17:19:04 2019 +0200

    unit tests
---
 sandbox/src/main/kotlin/HEVResponse.kt             |  4 ++
 sandbox/src/main/kotlin/Main.kt                    | 17 ++---
 sandbox/src/main/kotlin/XML.kt                     | 33 +++++-----
 sandbox/src/test/kotlin/DomToJaxbTest.kt           |  2 +-
 sandbox/src/test/kotlin/IniLoadJaxbTest.kt         | 41 ------------
 sandbox/src/test/kotlin/JaxbTest.kt                | 74 ++++++++++++++++++++++
 .../resources/ebics_ini_request_sample_patched.xml | 21 ++++++
 sandbox/src/test/resources/hev_resp3.0.xml         |  2 +-
 8 files changed, 123 insertions(+), 71 deletions(-)

diff --git a/sandbox/src/main/kotlin/HEVResponse.kt 
b/sandbox/src/main/kotlin/HEVResponse.kt
index 363cec2..a4207ac 100644
--- 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
index 75bc3a4..d9981f0 100644
--- 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
index 3764850..f2aa666 100644
--- 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
index c436aa9..05ecaec 100644
--- 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
deleted file mode 100644
index de9fc9d..0000000
--- a/sandbox/src/test/kotlin/IniLoadJaxbTest.kt
+++ /dev/null
@@ -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
new file mode 100644
index 0000000..e217ed4
--- /dev/null
+++ 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
new file mode 100644
index 0000000..821d7fd
--- /dev/null
+++ 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/u1H5DbyWAsD
 [...]
+    </DataTransfer>
+  </body>
+</ebicsUnsecuredRequest>
diff --git a/sandbox/src/test/resources/hev_resp3.0.xml 
b/sandbox/src/test/resources/hev_resp3.0.xml
index effd781..72997ae 100644
--- 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

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]