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 8eeef9f215efe471fd06872b1b4b1e00590f91d0..d54baf92fc43538be2c11ab2c23c945d16c41cb5 100755 (executable)
@@ -362,14 +362,19 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
         output.writeInt(pathArguments.size());
 
         for (PathArgument pathArgument : pathArguments) {
         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 {
     @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);
 
 
         byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
 
index e1bf0447e657c88162a284f384efe6c684742245..aed87ad35543fece162ab782c7557340b6601849 100644 (file)
@@ -214,17 +214,32 @@ public class NormalizedNodeStreamReaderWriterTest {
     }
 
     @Test
     }
 
     @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();
         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()));
 
         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) {
     }
 
     private static String largeString(final int pow) {