AbstractNormalizedNodeDataOutput fails to write out header 24/84324/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Sep 2019 14:06:59 +0000 (16:06 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Sep 2019 15:12:14 +0000 (17:12 +0200)
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 <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java

index 8eeef9f..d54baf9 100755 (executable)
@@ -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);
 
index e1bf044..aed87ad 100644 (file)
@@ -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) {

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.