commit 4342e9c79e12e1e51b6fb2458fe9107f3c3454bf
parent 11bec95c267e88a5bb24273ee2444616812e95e1
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Wed, 16 Oct 2019 18:11:56 +0200
complete unit tests for JAXB conversion
Diffstat:
5 files changed, 72 insertions(+), 49 deletions(-)
diff --git a/sandbox/src/main/kotlin/Main.kt b/sandbox/src/main/kotlin/Main.kt
@@ -221,9 +221,9 @@ private suspend fun ApplicationCall.ebicsweb() {
)
)
- logger.info("Serving a ${bodyJaxb.header.static.orderDetails.orderType} request")
+ logger.info("Serving a ${bodyJaxb.value.header.static.orderDetails.orderType} request")
- when (bodyJaxb.header.static.orderDetails.orderType) {
+ when (bodyJaxb.value.header.static.orderDetails.orderType) {
"INI" -> {
@@ -232,7 +232,7 @@ private suspend fun ApplicationCall.ebicsweb() {
* the Base64 string into its byte[] form _at the same time_ it instantiates
* the object; in other words, there is no need to perform here the decoding.
*/
- val zkey = bodyJaxb.body.dataTransfer.orderData.value
+ val zkey = bodyJaxb.value.body.dataTransfer.orderData.value
/**
* The validation enforces zkey to be a base64 value, but does not check
diff --git a/sandbox/src/main/kotlin/XML.kt b/sandbox/src/main/kotlin/XML.kt
@@ -175,13 +175,13 @@ class XML {
* @param document the document to convert into JAXB.
* @return the JAXB object reflecting the original XML document.
*/
- fun <T>convertDomToJaxb(finalType: Class<T>, document: Document) : T {
+ fun <T>convertDomToJaxb(finalType: Class<T>, document: Document) : JAXBElement<T> {
val jc = JAXBContext.newInstance(finalType)
/* Marshalling the object into the document. */
val m = jc.createUnmarshaller()
- return m.unmarshal(document) as T // document "went" into Jaxb
+ return m.unmarshal(document, finalType) // document "went" into Jaxb
}
/**
@@ -213,7 +213,7 @@ class XML {
* has already got its setters called.
* @return the DOM Document, or null (if errors occur).
*/
- fun convertJaxbToDom(obj: JAXBElement<Unit>): Document? {
+ fun <T>convertJaxbToDom(obj: JAXBElement<T>): Document? {
try {
val jc = JAXBContext.newInstance(obj.declaredType)
diff --git a/sandbox/src/test/kotlin/DomToJaxbTest.kt b/sandbox/src/test/kotlin/DomToJaxbTest.kt
@@ -1,30 +0,0 @@
-package tech.libeufin.sandbox
-
-import org.junit.Test
-import org.w3c.dom.Element
-import tech.libeufin.messages.ebics.keyrequest.EbicsUnsecuredRequest
-import tech.libeufin.messages.ebics.keyrequest.OrderDetailsType
-import tech.libeufin.messages.ebics.keyrequest.UnsecuredReqOrderDetailsType
-
-class DomToJaxbTest {
-
- @Test
- fun domToJaxb() {
-
- val processor = XML()
- val classLoader = ClassLoader.getSystemClassLoader()
- val ini = classLoader.getResource("ebics_ini_request_sample.xml")
- val iniDom = processor.parseStringIntoDom(ini.readText())
- val x: Element = iniDom?.getElementsByTagName("OrderDetails")?.item(0) as Element
-
- x.setAttributeNS(
- "http://www.w3.org/2001/XMLSchema-instance",
- "type",
- "UnsecuredReqOrderDetailsType"
- )
-
- processor.convertDomToJaxb<EbicsUnsecuredRequest>(
- EbicsUnsecuredRequest::class.java,
- iniDom)
- }
-}
-\ No newline at end of file
diff --git a/sandbox/src/test/kotlin/JaxbTest.kt b/sandbox/src/test/kotlin/JaxbTest.kt
@@ -13,6 +13,14 @@ class JaxbTest {
val processor = tech.libeufin.sandbox.XML()
val classLoader = ClassLoader.getSystemClassLoader()
+ val hevResponseJaxb = HEVResponse(
+ "000000",
+ "EBICS_OK",
+ arrayOf(
+ ProtocolAndVersion("H003", "02.40"),
+ ProtocolAndVersion("H004", "02.50")
+ )
+ ).makeHEVResponse()
/**
@@ -36,7 +44,7 @@ class JaxbTest {
}
/**
- * Test the JAXB instantiation of a XmlRoot document.
+ * Test string -> JAXB
*/
@Test
fun stringToJaxb() {
@@ -54,21 +62,36 @@ class JaxbTest {
)
}
+ /**
+ * Test JAXB -> string
+ */
@Test
fun jaxbToString() {
- val hevResponse = HEVResponse(
- "000000",
- "EBICS_OK",
- arrayOf(
- ProtocolAndVersion("H003", "02.40"),
- ProtocolAndVersion("H004", "02.50")
- )
- )
+ processor.getStringFromJaxb(hevResponseJaxb)
+ }
+
+ /**
+ * Test JAXB -> DOM
+ */
+ @Test
+ fun jaxbToDom() {
+ processor.convertJaxbToDom(hevResponseJaxb)
+ }
+
- processor.getStringFromJaxb(hevResponse.makeHEVResponse())
+ /**
+ * Test DOM -> JAXB
+ */
+ @Test
+ fun domToJaxb() {
- // just checking that no exceptions were raised before
- assertTrue(true)
+ val ini = classLoader.getResource("ebics_ini_request_sample_patched.xml")
+ val iniDom = processor.parseStringIntoDom(ini.readText())
+ processor.convertDomToJaxb<EbicsUnsecuredRequest>(
+ EbicsUnsecuredRequest::class.java,
+ iniDom!!)
}
+
+
}
\ No newline at end of file
diff --git a/sandbox/src/test/kotlin/XsiTypeAttributeTest.kt b/sandbox/src/test/kotlin/XsiTypeAttributeTest.kt
@@ -0,0 +1,30 @@
+package tech.libeufin.sandbox
+
+import org.junit.Test
+import org.w3c.dom.Element
+import tech.libeufin.messages.ebics.keyrequest.EbicsUnsecuredRequest
+import tech.libeufin.messages.ebics.keyrequest.OrderDetailsType
+import tech.libeufin.messages.ebics.keyrequest.UnsecuredReqOrderDetailsType
+
+class XsiTypeAttributeTest {
+
+ @Test
+ fun domToJaxb() {
+
+ val processor = XML()
+ val classLoader = ClassLoader.getSystemClassLoader()
+ val ini = classLoader.getResource("ebics_ini_request_sample.xml")
+ val iniDom = processor.parseStringIntoDom(ini.readText())
+ val x: Element = iniDom?.getElementsByTagName("OrderDetails")?.item(0) as Element
+
+ x.setAttributeNS(
+ "http://www.w3.org/2001/XMLSchema-instance",
+ "type",
+ "UnsecuredReqOrderDetailsType"
+ )
+
+ processor.convertDomToJaxb<EbicsUnsecuredRequest>(
+ EbicsUnsecuredRequest::class.java,
+ iniDom)
+ }
+}
+\ No newline at end of file