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 0000000000..88164ea2ea Binary files /dev/null and b/node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN1AddsNullableInt differ 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 0000000000..d34c62256d Binary files /dev/null and b/node-api/src/test/resources/net/corda/nodeapi/internal/serialization/amqp/JavaEvolutionTests.testN2AddsPrimitive differ