From a47348d52aaa2e94e1dc5dd0fc76ec53b23094e5 Mon Sep 17 00:00:00 2001 From: Katelyn Baker Date: Fri, 13 Jul 2018 15:03:10 +0100 Subject: [PATCH] CORDA-1662 - Post backport fixes --- .../amqp/JavaEvolutionTests.java | 20 ++-- .../serialization/amqp/AMQPTestUtils.kt | 32 ------ .../amqp/testutils/AMQPTestUtils.kt | 106 ++++++++++++++++++ .../testutils/TestSerializationContext.kt | 16 +++ .../JavaEvolutionTests.testN1AddsNullableInt | Bin 0 -> 267 bytes .../JavaEvolutionTests.testN2AddsPrimitive | Bin 0 -> 286 bytes 6 files changed, 129 insertions(+), 45 deletions(-) rename {serialization/src/test/java/net/corda/serialization/internal => node-api/src/test/java/net/corda/nodeapi/internal/serialization}/amqp/JavaEvolutionTests.java (82%) delete mode 100644 node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/AMQPTestUtils.kt create mode 100644 node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/AMQPTestUtils.kt create mode 100644 node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/TestSerializationContext.kt create mode 100644 node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN1AddsNullableInt create mode 100644 node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN2AddsPrimitive diff --git a/serialization/src/test/java/net/corda/serialization/internal/amqp/JavaEvolutionTests.java b/node-api/src/test/java/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.java similarity index 82% rename from serialization/src/test/java/net/corda/serialization/internal/amqp/JavaEvolutionTests.java rename to node-api/src/test/java/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.java index fa8449840e..69b7fabfba 100644 --- a/serialization/src/test/java/net/corda/serialization/internal/amqp/JavaEvolutionTests.java +++ b/node-api/src/test/java/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.java @@ -1,9 +1,7 @@ -package net.corda.serialization.internal.amqp; +package net.corda.nodeapi.internal.serialization.amqp; -import kotlin.Suppress; import net.corda.core.serialization.SerializedBytes; -import net.corda.serialization.internal.amqp.testutils.AMQPTestUtilsKt; -import net.corda.serialization.internal.amqp.testutils.TestSerializationContext; +import net.corda.nodeapi.internal.serialization.amqp.testutils.AMQPTestUtilsKt; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -25,6 +23,7 @@ public class JavaEvolutionTests { public String getWord() { return word; } } */ + // Class as it exists now with the newly added element static class N1 { private String word; @@ -69,14 +68,12 @@ public class JavaEvolutionTests { // Uncomment to regenerate the base state of the test /* N1 n = new N1("potato"); - AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize( - n, TestSerializationContext.testSerializationContext)); + AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(n)); */ N1 n2 = new DeserializationInput(factory).deserialize( new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)), - N1.class, - TestSerializationContext.testSerializationContext); + N1.class); assertEquals(n2.getWord(), "potato"); assertNull(n2.getWibble()); } @@ -86,15 +83,12 @@ public class JavaEvolutionTests { // Uncomment to regenerate the base state of the test /* N2 n = new N2("This is only a test"); - - AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize( - n, TestSerializationContext.testSerializationContext)); + AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(n)); */ exception.expect(NotSerializableException.class); new DeserializationInput(factory).deserialize( new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)), - N2.class, - TestSerializationContext.testSerializationContext); + N2.class); } } diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/AMQPTestUtils.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/AMQPTestUtils.kt deleted file mode 100644 index d7701aed52..0000000000 --- a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/AMQPTestUtils.kt +++ /dev/null @@ -1,32 +0,0 @@ -package net.corda.nodeapi.internal.serialization.amqp - -import org.apache.qpid.proton.codec.Data -import net.corda.nodeapi.internal.serialization.AllWhitelist -import net.corda.nodeapi.internal.serialization.EmptyWhitelist - -fun testDefaultFactory() = SerializerFactory(AllWhitelist, ClassLoader.getSystemClassLoader()) -fun testDefaultFactoryNoEvolution() = SerializerFactory(AllWhitelist, ClassLoader.getSystemClassLoader(), - EvolutionSerializerGetterTesting()) -fun testDefaultFactoryWithWhitelist() = SerializerFactory(EmptyWhitelist, ClassLoader.getSystemClassLoader()) - -class TestSerializationOutput( - private val verbose: Boolean, - serializerFactory: SerializerFactory = testDefaultFactory()) - : SerializationOutput(serializerFactory) { - - override fun writeSchema(schema: Schema, data: Data) { - if (verbose) println(schema) - super.writeSchema(schema, data) - } - - override fun writeTransformSchema(transformsSchema: TransformsSchema, data: Data) { - if(verbose) { - println ("Writing Transform Schema") - println (transformsSchema) - } - super.writeTransformSchema(transformsSchema, data) - } -} - -fun testName(): String = Thread.currentThread().stackTrace[2].methodName - diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/AMQPTestUtils.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/AMQPTestUtils.kt new file mode 100644 index 0000000000..9721cea830 --- /dev/null +++ b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/AMQPTestUtils.kt @@ -0,0 +1,106 @@ +package net.corda.nodeapi.internal.serialization.amqp.testutils + +import net.corda.core.internal.copyTo +import net.corda.core.internal.div + +import net.corda.core.serialization.SerializationContext +import net.corda.core.serialization.SerializedBytes +import net.corda.core.utilities.OpaqueBytes +import net.corda.nodeapi.internal.serialization.AllWhitelist +import net.corda.nodeapi.internal.serialization.EmptyWhitelist +import net.corda.nodeapi.internal.serialization.amqp.* +import net.corda.testing.common.internal.ProjectStructure +import org.apache.qpid.proton.codec.Data +import org.junit.Test +import java.io.File.separatorChar +import java.io.NotSerializableException +import java.nio.file.StandardCopyOption.REPLACE_EXISTING + +fun testDefaultFactory() = SerializerFactory(AllWhitelist, ClassLoader.getSystemClassLoader()) + +fun testDefaultFactoryNoEvolution(): SerializerFactory { + return SerializerFactory( + AllWhitelist, + ClassLoader.getSystemClassLoader(), + evolutionSerializerGetter = EvolutionSerializerGetterTesting()) +} + +fun testDefaultFactoryWithWhitelist() = SerializerFactory(EmptyWhitelist, ClassLoader.getSystemClassLoader()) + +class TestSerializationOutput( + private val verbose: Boolean, + serializerFactory: SerializerFactory = testDefaultFactory()) + : SerializationOutput(serializerFactory) { + + override fun writeSchema(schema: Schema, data: Data) { + if (verbose) println(schema) + super.writeSchema(schema, data) + } + + override fun writeTransformSchema(transformsSchema: TransformsSchema, data: Data) { + if(verbose) { + println ("Writing Transform Schema") + println (transformsSchema) + } + super.writeTransformSchema(transformsSchema, data) + } + + @Throws(NotSerializableException::class) + override fun serialize(obj: T): SerializedBytes { + try { + return _serialize(obj) + } finally { + andFinally() + } + } +} + +fun testName(): String { + val classLoader = Thread.currentThread().contextClassLoader + return Thread.currentThread().stackTrace.first { + try { + classLoader.loadClass(it.className).getMethod(it.methodName).isAnnotationPresent(Test::class.java) + } catch (e: Exception) { + false + } + }.methodName +} + +fun Any.testResourceName(): String = "${javaClass.simpleName}.${testName()}" + +fun Any.writeTestResource(bytes: OpaqueBytes) { + val dir = ProjectStructure.projectRootDir / "node-api" / "src" / "test" / "resources" / javaClass.`package`.name.replace('.', separatorChar) + bytes.open().copyTo(dir / testResourceName(), REPLACE_EXISTING) +} + +fun Any.readTestResource(): ByteArray = javaClass.getResourceAsStream(testResourceName()).readBytes() + +@Throws(NotSerializableException::class) +inline fun DeserializationInput.deserializeAndReturnEnvelope( + bytes: SerializedBytes, + context: SerializationContext? = null +) : ObjectAndEnvelope { + return deserializeAndReturnEnvelope(bytes, T::class.java) +} + +@Throws(NotSerializableException::class) +inline fun DeserializationInput.deserialize( + bytes: SerializedBytes, + context: SerializationContext? = null +) : T = deserialize(bytes, T::class.java) + + +@Throws(NotSerializableException::class) +fun SerializationOutput.serializeAndReturnSchema( + obj: T, context: SerializationContext? = null +): BytesAndSchemas = serializeAndReturnSchema(obj) + + +@Throws(NotSerializableException::class) +fun SerializationOutput.serialize(obj: T): SerializedBytes { + try { + return _serialize(obj) + } finally { + andFinally() + } +} diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/TestSerializationContext.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/TestSerializationContext.kt new file mode 100644 index 0000000000..890e0733ba --- /dev/null +++ b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/testutils/TestSerializationContext.kt @@ -0,0 +1,16 @@ +package net.corda.nodeapi.internal.serialization.amqp.testutils + +import net.corda.core.serialization.SerializationContext +import net.corda.nodeapi.internal.serialization.AllWhitelist +import net.corda.nodeapi.internal.serialization.SerializationContextImpl +import net.corda.nodeapi.internal.serialization.amqp.AmqpHeaderV1_0 + +val serializationProperties: MutableMap = mutableMapOf() + +val testSerializationContext = SerializationContextImpl( + preferredSerializationVersion = AmqpHeaderV1_0, + deserializationClassLoader = ClassLoader.getSystemClassLoader(), + whitelist = AllWhitelist, + properties = serializationProperties, + objectReferencesEnabled = false, + useCase = SerializationContext.UseCase.P2P) \ No newline at end of file diff --git a/node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN1AddsNullableInt b/node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN1AddsNullableInt new file mode 100644 index 0000000000000000000000000000000000000000..88164ea2ea0031ae273189fc9d48a7d97a5ea645 GIT binary patch literal 267 zcmYe!FG@*dWME)uIGO|`85kH3d}n4@tdy5pqL&Pkvq}vQc5;iTGAS_d%q{RM$&WM+ z_AyGgwLQSexR9+Nza+6FAFi3{z*@!ws~F*Y)&p}{7dpeu(#y+FNlh%s)XU5(NiE7t z%+V`OEy_&H$*f8&$;{8wOUx}S(DOs5a8zpm literal 0 HcmV?d00001 diff --git a/node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN2AddsPrimitive b/node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN2AddsPrimitive new file mode 100644 index 0000000000000000000000000000000000000000..d34c62256d0c517a05bf9652e4c8e2119cb5a979 GIT binary patch literal 286 zcmYe!FG@*dWME)uIGO|`85kHZ05Lm|!OXB&DKE7|FBzo5O53zBImFRN+qA$b%)BJU z+1JI-JjBt~_JA1ULgA2%%wh!~%FoNGR7g}PNi8mco5yruE#rYzjBq~dfw`;;o#6)S z<>jZOCKhDsW#*Nn7Ud=8=oP0HWhUliRwb5X=I7}p<`x#{c_o%5x|ZeVl!AmpfTkC# z_!&94!p&qppvFXuV;v4CA{@eUK!klEOF7VY3)zZGiZb)kT^$^poZyCX9%N(y0KDZ^ AD*ylh literal 0 HcmV?d00001