X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FNormalizedNodeStreamReaderWriterTest.java;h=4b45af48e900b29d67b8a34292828cd826bfa6ed;hp=aed87ad35543fece162ab782c7557340b6601849;hb=e373f634d72fa6ef692d4fd0d016c6deeba8ffe8;hpb=42b82f3d3e936b78f66301bdc08f6b744bcc740b 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 aed87ad355..4b45af48e9 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 @@ -30,15 +30,20 @@ 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; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder; @@ -69,10 +74,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(1049619, bytes.length); - NormalizedNode node = nnin.readNormalizedNode(); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + + NormalizedNode node = nnin.readNormalizedNode(); Assert.assertEquals(testContainer, node); node = nnin.readNormalizedNode(); @@ -111,8 +118,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeYangInstanceIdentifier(path); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(139, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier(); Assert.assertEquals(path, newPath); @@ -121,9 +130,9 @@ public class NormalizedNodeStreamReaderWriterTest { @Test public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteStreams.newDataOutput(bos)); NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder().build(); writer.writeNormalizedNode(testContainer); @@ -134,8 +143,10 @@ public class NormalizedNodeStreamReaderWriterTest { writer.writeYangInstanceIdentifier(path); - NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(826, bytes.length); + + NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); NormalizedNode node = reader.readNormalizedNode(); Assert.assertEquals(testContainer, node); @@ -148,7 +159,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)); @@ -195,8 +206,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeNormalizedNode(anyXmlContainer); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(229, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode(); @@ -222,8 +235,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeSchemaPath(expected); } - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(99, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); assertEquals(expected, nnin.readSchemaPath()); } @@ -237,11 +252,47 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writePathArgument(expected); } - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(103, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); assertEquals(expected, nnin.readPathArgument()); } + /* + * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string) + * and an integer. + */ + @Test + public void testHugeEntries() throws IOException { + final CollectionNodeBuilder mapBuilder = Builders.mapBuilder() + .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)); + final DataContainerNodeBuilder entryBuilder = + Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42)); + + for (int i = 0; i < 100_000; ++i) { + final String key = "xyzzy" + i; + mapBuilder.addChild(entryBuilder + .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME, + TestModel.CHILD_NAME_QNAME, key)) + .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key)) + .build()); + } + + final MapNode expected = mapBuilder.build(); + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) { + nnout.writeNormalizedNode(expected); + } + + final byte[] bytes = bos.toByteArray(); + assertEquals(2_289_103, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + assertEquals(expected, nnin.readNormalizedNode()); + } + private static String largeString(final int pow) { StringBuilder sb = new StringBuilder("X"); for (int i = 0; i < pow; i++) {