From 90a7dd2bf46d76955d7ee9f92e572012a9269c50 Mon Sep 17 00:00:00 2001 From: Viktor Kolomeyko Date: Wed, 12 Sep 2018 11:29:36 +0100 Subject: [PATCH] ENT-2489: Fix serialization for some of the CordaExceptions. (#3925) Also add a unit test that exposes the problem. Without these changes AMQP serialization fails with the following: ``` net.corda.serialization.internal.amqp.AMQPNotSerializableException: Constructor parameter - "reason" - doesn't refer to a property of "class net.corda.node.services.statemachine.SessionRejectException" at net.corda.serialization.internal.amqp.SerializationHelperKt.toPropertyAccessorConstructor(SerializationHelper.kt:120) ~[corda-serialization-4.0-SNAPSHOT.jar:?] at net.corda.serialization.internal.amqp.SerializationHelperKt.propertiesForSerializationFromConstructor(SerializationHelper.kt:107) ~[corda-serialization-4.0-SNAPSHOT.jar:?] at net.corda.serialization.internal.amqp.custom.ThrowableSerializer.toProxy(ThrowableSerializer.kt:28) [corda-serialization-4.0-SNAPSHOT.jar:?] at net.corda.serialization.internal.amqp.custom.ThrowableSerializer.toProxy(ThrowableSerializer.kt:12) [corda-serialization-4.0-SNAPSHOT.jar:?] at net.corda.serialization.internal.amqp.CustomSerializer$Proxy.writeDescribedObject(CustomSerializer.kt:159) [corda-serialization-4.0-SNAPSHOT.jar:?] ``` --- .../core/node/services/IdentityService.kt | 2 +- .../net/corda/node/internal/AbstractNode.kt | 2 +- .../statemachine/SessionRejectException.kt | 2 +- .../ExceptionsSerializationTest.kt | 45 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 node/src/test/kotlin/net/corda/node/services/statemachine/ExceptionsSerializationTest.kt diff --git a/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt b/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt index 91c714b51f..b8f2bc7757 100644 --- a/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt +++ b/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt @@ -140,4 +140,4 @@ interface IdentityService { fun partiesFromName(query: String, exactMatch: Boolean): Set } -class UnknownAnonymousPartyException(msg: String) : CordaException(msg) +class UnknownAnonymousPartyException(message: String) : CordaException(message) diff --git a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt index e1107839ad..cb008ac462 100644 --- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt @@ -750,7 +750,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, } // Specific class so that MockNode can catch it. - class DatabaseConfigurationException(msg: String) : CordaException(msg) + class DatabaseConfigurationException(message: String) : CordaException(message) protected open fun startDatabase(metricRegistry: MetricRegistry? = null) { val props = configuration.dataSourceProperties diff --git a/node/src/main/kotlin/net/corda/node/services/statemachine/SessionRejectException.kt b/node/src/main/kotlin/net/corda/node/services/statemachine/SessionRejectException.kt index 90d4432f0d..cea4680411 100644 --- a/node/src/main/kotlin/net/corda/node/services/statemachine/SessionRejectException.kt +++ b/node/src/main/kotlin/net/corda/node/services/statemachine/SessionRejectException.kt @@ -5,4 +5,4 @@ import net.corda.core.CordaException /** * An exception propagated and thrown in case a session initiation fails. */ -class SessionRejectException(reason: String) : CordaException(reason) +class SessionRejectException(message: String) : CordaException(message) diff --git a/node/src/test/kotlin/net/corda/node/services/statemachine/ExceptionsSerializationTest.kt b/node/src/test/kotlin/net/corda/node/services/statemachine/ExceptionsSerializationTest.kt new file mode 100644 index 0000000000..e072716f0c --- /dev/null +++ b/node/src/test/kotlin/net/corda/node/services/statemachine/ExceptionsSerializationTest.kt @@ -0,0 +1,45 @@ +package net.corda.node.services.statemachine + +import net.corda.core.CordaException +import net.corda.core.node.services.UnknownAnonymousPartyException +import net.corda.core.serialization.SerializedBytes +import net.corda.core.serialization.deserialize +import net.corda.core.serialization.serialize +import net.corda.node.internal.AbstractNode +import net.corda.node.utilities.registration.CertificateRequestException +import net.corda.testing.core.SerializationEnvironmentRule +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import kotlin.test.assertEquals + +@RunWith(Parameterized::class) +class ExceptionsSerializationTest(private val initialException: CordaException, @Suppress("UNUSED_PARAMETER") description: String) { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{1}") + fun data(): Collection> = listOf( + arrayOf(SessionRejectException("test"), "SessionRejectException"), + arrayOf(CertificateRequestException("test"), "CertificateRequestException"), + arrayOf(UnknownAnonymousPartyException("test"), "UnknownAnonymousPartyException"), + arrayOf(AbstractNode.DatabaseConfigurationException("test"), "DatabaseConfigurationException") + ) + } + + @Rule + @JvmField + val testSerialization = SerializationEnvironmentRule() + + @Test + fun testMarshal() { + val fromSerialized = performRoundTripSerialization(initialException) + assertEquals(initialException.message, fromSerialized.message) + } + + private inline fun performRoundTripSerialization(obj: T): T { + val serializedForm: SerializedBytes = obj.serialize() + return serializedForm.deserialize() + } +} \ No newline at end of file