commit f6d0a7e9378137643513d088370ec6377ba78c1e
parent 5a6bfd153360b24f11dffd99836c5a7d0e3662e2
Author: Antoine A <>
Date: Mon, 22 Jul 2024 10:24:38 +0200
nexus: WIP WS fix serialization
Diffstat:
2 files changed, 146 insertions(+), 6 deletions(-)
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsWS.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsWS.kt
@@ -64,10 +64,10 @@ data class WssNotificationBTF(
@Serializable
data class WssNewData(
- val MCLASS: WssNotificationClass,
+ val MCLASS: List<WssNotificationClass>,
val PARTNERID: String,
val USERID: String? = null,
- val BTF: WssNotificationBTF,
+ val BTF: List<WssNotificationBTF>,
val ORDERTYPE: List<String>? = null
): WssNotification
@@ -79,12 +79,19 @@ data class WssInfo(
@Serializable
data class WssGeneralInfo(
- val MCLASS: WssNotificationClass,
- val INFO: WssInfo
+ val MCLASS: List<WssNotificationClass>,
+ val INFO: List<WssInfo>
): WssNotification
-@Serializable
-sealed interface WssNotification;
+@Serializable(with = WssNotification.Serializer::class)
+sealed interface WssNotification {
+ companion object Serializer : JsonContentPolymorphicSerializer<WssNotification>(WssNotification::class) {
+ override fun selectDeserializer(element: JsonElement) = when {
+ "INFO" in element.jsonObject -> WssGeneralInfo.serializer()
+ else -> WssNewData.serializer()
+ }
+ }
+}
suspend fun EbicsClient.wssParams(): WssParams {
diff --git a/nexus/src/test/kotlin/WsTest.kt b/nexus/src/test/kotlin/WsTest.kt
@@ -0,0 +1,132 @@
+/*
+* This file is part of LibEuFin.
+* Copyright (C) 2024 Taler Systems S.A.
+
+* LibEuFin is free software; you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation; either version 3, or
+* (at your option) any later version.
+
+* LibEuFin is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+* Public License for more details.
+
+* You should have received a copy of the GNU Affero General Public
+* License along with LibEuFin; see the file COPYING. If not, see
+* <http://www.gnu.org/licenses/>
+*/
+
+import com.github.ajalt.clikt.core.CliktCommand
+import com.github.ajalt.clikt.testing.test
+import tech.libeufin.common.crypto.CryptoUtil
+import tech.libeufin.nexus.*
+import tech.libeufin.nexus.ebics.*
+import kotlinx.serialization.json.*
+import kotlinx.serialization.*
+import java.io.ByteArrayOutputStream
+import java.io.PrintStream
+import kotlin.io.path.*
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+class WsTest {
+ inline fun <reified B> roundtrip(raw: String): B {
+ val json: JsonObject = Json.decodeFromString(raw)
+ val decoded: B = Json.decodeFromJsonElement(json)
+ val encoded = Json.encodeToJsonElement(decoded)
+ assertEquals(json, encoded)
+ return decoded
+ }
+
+ /** Test our serialization implementation works with spec examples */
+ @Test
+ fun serialization() {
+ roundtrip<WssParams>("""
+ {
+ "URL": "https://bankmitwebsocket.de",
+ "TOKEN": "550e8400-e29b-11d4-a716-446655440000",
+ "OTT": "N",
+ "VALIDITY": "2019-03-21T10:35:22Z",
+ "PARTNERID": "K1234567",
+ "USERID": "USER4711"
+ }
+ """)
+
+ val examples = sequenceOf(
+ """
+ {
+ "MCLASS": [
+ {
+ "NAME": "EBICS-HAA",
+ "VERS": "1.0",
+ "TIMESTAMP": "2019-05-13T12:21:50Z"
+ }
+ ],
+ "PARTNERID": "K1234567",
+ "USERID": "USER471",
+ "BTF": [
+ {
+ "SERVICE": "REP",
+ "SCOPE": "DE",
+ "CONTTYPE": "ZIP",
+ "MSGNAME": "camt.054"
+ }
+ ],
+ "ORDERTYPE": [
+ "C5N"
+ ]
+ }
+ """, """
+ {
+ "MCLASS": [
+ {
+ "NAME": "EBICS-HAA",
+ "VERS": "1.0",
+ "TIMESTAMP": "2019-05-13T12:21:53Z"
+ }
+ ],
+ "PARTNERID": "K1234567",
+ "USERID": "USER471",
+ "BTF": [
+ {
+ "SERVICE": "REP",
+ "SCOPE": "DE",
+ "CONTTYPE": "ZIP",
+ "MSGNAME": "camt.052"
+ },
+ {
+ "SERVICE": "REP",
+ "SCOPE": "DE",
+ "OPTION": "SCI",
+ "CONTTYPE": "ZIP",
+ "MSGNAME": "pain.002"
+ }
+ ],
+ "ORDERTYPE": [
+ "C52",
+ "CIZ"
+ ]
+ }
+ """, """
+ {
+ "MCLASS": [
+ {
+ "NAME": "INFO",
+ "VERS": "1.0",
+ "TIMESTAMP": "2019-03-25T12:25:34Z"
+ }
+ ],
+ "INFO": [
+ {
+ "LANG": "EN",
+ "FREE": " The EBICS-Service is limited on 30.03.2019 from 10:00 a.m. - 11:00a.m. due to maintenance work "
+ }
+ ]
+ }
+ """)
+ for (raw in examples) {
+ roundtrip<WssNotification>(raw)
+ }
+ }
+}
+\ No newline at end of file