From: Robert Varga Date: Wed, 11 Sep 2019 14:06:59 +0000 (+0200) Subject: AbstractNormalizedNodeDataOutput fails to write out header X-Git-Tag: release/magnesium~105 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=42b82f3d3e936b78f66301bdc08f6b744bcc740b AbstractNormalizedNodeDataOutput fails to write out header In case AbstractNormalizedNodeDataOutput is used in a way, where NormalizedNodeDataOutput.writePathArgument() is the first method invoked, the stream header would not be output, leading to it being unreadable. Change-Id: I4ababb360129b2ef673d0546333599dd10fbdd9e Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java index 8eeef9f215..d54baf92fc 100755 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java @@ -362,14 +362,19 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut output.writeInt(pathArguments.size()); for (PathArgument pathArgument : pathArguments) { - writePathArgument(pathArgument); + writePathArgumentInternal(pathArgument); } } - @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", - justification = "The casts in the switch clauses are indirectly confirmed via the determination of 'type'.") @Override public void writePathArgument(final PathArgument pathArgument) throws IOException { + ensureHeaderWritten(); + writePathArgumentInternal(pathArgument); + } + + @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", + justification = "The casts in the switch clauses are indirectly confirmed via the determination of 'type'.") + final void writePathArgumentInternal(final PathArgument pathArgument) throws IOException { byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument); diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java index e1bf0447e6..aed87ad355 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java @@ -214,17 +214,32 @@ public class NormalizedNodeStreamReaderWriterTest { } @Test - public void testSchemaPathSerialization() throws Exception { + public void testSchemaPathSerialization() throws IOException { final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); - nnout.writeSchemaPath(expected); + try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) { + nnout.writeSchemaPath(expected); + } + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( + bos.toByteArray())); + assertEquals(expected, nnin.readSchemaPath()); + } + + @Test + public void testWritePathArgument() throws IOException { + final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) { + nnout.writePathArgument(expected); + } NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( bos.toByteArray())); - SchemaPath actual = nnin.readSchemaPath(); - assertEquals(expected, actual); + assertEquals(expected, nnin.readPathArgument()); } private static String largeString(final int pow) {