X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FNormalizedNodeStreamReaderWriterTest.java;h=2f910e649dba37e28cb49e6283366cb4510a3423;hb=b843cd2e6a2fdf894b458975a5fd9d168da62433;hp=81d97678b01762d456fb7c3a3af8d6c77203302a;hpb=81674d6fd50b419b868d0851062e23f34b34557d;p=controller.git 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 81d97678b0..2f910e649d 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 @@ -10,12 +10,12 @@ package org.opendaylight.controller.cluster.datastore.node.utils.stream; import static org.junit.Assert.assertEquals; -import com.google.common.base.Optional; import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; +import java.util.Optional; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; @@ -26,6 +26,7 @@ import org.apache.commons.lang.SerializationUtils; import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.cluster.datastore.util.TestModel; +import org.opendaylight.yangtools.yang.common.Empty; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; @@ -68,10 +69,12 @@ public class NormalizedNodeStreamReaderWriterTest { .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build(); nnout.writeNormalizedNode(toasterContainer); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(1049950, bytes.length); - NormalizedNode node = nnin.readNormalizedNode(); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + + NormalizedNode node = nnin.readNormalizedNode(); Assert.assertEquals(testContainer, node); node = nnin.readNormalizedNode(); @@ -79,7 +82,7 @@ public class NormalizedNodeStreamReaderWriterTest { } private static NormalizedNode createTestContainer() { - byte[] bytes1 = {1,2,3}; + byte[] bytes1 = {1, 2, 3}; LeafSetEntryNode entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier( new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build(); @@ -87,14 +90,12 @@ public class NormalizedNodeStreamReaderWriterTest { LeafSetEntryNode entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier( new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build(); - LeafSetEntryNode entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier( - new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, null)).withValue(null).build(); - return TestModel.createBaseTestContainerBuilder() .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier( new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)) - .withChild(entry1).withChild(entry2).withChild(entry3).build()) - .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1,2,3,4})) + .withChild(entry1).withChild(entry2).build()) + .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4})) + .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance())) .withChild(Builders.orderedMapBuilder() .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME)) .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME, @@ -112,8 +113,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeYangInstanceIdentifier(path); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(161, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier(); Assert.assertEquals(path, newPath); @@ -122,9 +125,9 @@ public class NormalizedNodeStreamReaderWriterTest { @Test public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput( + ByteStreams.newDataOutput(bos)); NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder().build(); writer.writeNormalizedNode(testContainer); @@ -135,8 +138,10 @@ public class NormalizedNodeStreamReaderWriterTest { writer.writeYangInstanceIdentifier(path); - NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(1163, bytes.length); + + NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); NormalizedNode node = reader.readNormalizedNode(); Assert.assertEquals(testContainer, node); @@ -149,7 +154,7 @@ public class NormalizedNodeStreamReaderWriterTest { @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000) public void testInvalidNormalizedNodeStream() throws IOException { - byte[] invalidBytes = {1,2,3}; + byte[] invalidBytes = {1, 2, 3}; NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( ByteStreams.newDataInput(invalidBytes)); @@ -196,8 +201,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeNormalizedNode(anyXmlContainer); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(235, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode(); @@ -215,17 +222,36 @@ 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); + } + + final byte[] bytes = bos.toByteArray(); + assertEquals(100, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + 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); + } + + final byte[] bytes = bos.toByteArray(); + assertEquals(105, bytes.length); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); - SchemaPath actual = nnin.readSchemaPath(); - assertEquals(expected, actual); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + assertEquals(expected, nnin.readPathArgument()); } private static String largeString(final int pow) {