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=e1bf0447e657c88162a284f384efe6c684742245;hp=825e123f820a9dabda74f43d453edcce5de28d33;hb=68f179dcd8483dd7f681e134268a1eee29d09d55;hpb=5486a047d7ccf57fe707e754b561bc7c3189a3d9 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 825e123f82..e1bf0447e6 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; @@ -42,21 +43,20 @@ import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableCo import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.w3c.dom.Node; import org.xml.sax.InputSource; public class NormalizedNodeStreamReaderWriterTest { - @SuppressWarnings("deprecation") @Test public void testNormalizedNodeStreaming() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); NormalizedNode testContainer = createTestContainer(); - writer.writeNormalizedNode(testContainer); + nnout.writeNormalizedNode(testContainer); QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster"); QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor"); @@ -67,22 +67,20 @@ public class NormalizedNodeStreamReaderWriterTest { ContainerNode toasterContainer = Builders.containerBuilder() .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build(); - writer.writeNormalizedNode(toasterContainer); + nnout.writeNormalizedNode(toasterContainer); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( + bos.toByteArray())); - NormalizedNode node = reader.readNormalizedNode(); + NormalizedNode node = nnin.readNormalizedNode(); Assert.assertEquals(testContainer, node); - node = reader.readNormalizedNode(); + node = nnin.readNormalizedNode(); Assert.assertEquals(toasterContainer, node); - - writer.close(); } 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(); @@ -90,46 +88,41 @@ 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, TestModel.ID_QNAME, 11)).build()).build(); } - @SuppressWarnings("deprecation") @Test public void testYangInstanceIdentifierStreaming() throws IOException { YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH) .node(TestModel.OUTER_LIST_QNAME).nodeWithKey( TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build(); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = - new NormalizedNodeOutputStreamWriter(ByteStreams.newDataOutput(byteArrayOutputStream)); - writer.writeYangInstanceIdentifier(path); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + nnout.writeYangInstanceIdentifier(path); - YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier(); - Assert.assertEquals(path, newPath); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( + bos.toByteArray())); - writer.close(); + YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier(); + Assert.assertEquals(path, newPath); } @Test public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( + NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput( ByteStreams.newDataOutput(byteArrayOutputStream)); NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder().build(); @@ -174,15 +167,13 @@ public class NormalizedNodeStreamReaderWriterTest { @Test public void testWithSerializable() { NormalizedNode input = TestModel.createTestContainer(); - SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input ); + SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input); SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable); Assert.assertEquals(input, clone.getInput()); - } - @SuppressWarnings("deprecation") @Test public void testAnyXmlStreaming() throws Exception { String xml = "onetwo"; @@ -199,16 +190,15 @@ public class NormalizedNodeStreamReaderWriterTest { Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)) .withValue(new DOMSource(xmlNode)).build()).build(); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); - writer.writeNormalizedNode(anyXmlContainer); + nnout.writeNormalizedNode(anyXmlContainer); - NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( + bos.toByteArray())); - ContainerNode deserialized = (ContainerNode)reader.readNormalizedNode(); + ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode(); Optional> child = deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME)); @@ -221,17 +211,27 @@ public class NormalizedNodeStreamReaderWriterTest { assertEquals("XML", xml, xmlOutput.getWriter().toString()); assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI()); + } - writer.close(); + @Test + public void testSchemaPathSerialization() throws Exception { + final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos)); + nnout.writeSchemaPath(expected); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( + bos.toByteArray())); + SchemaPath actual = nnin.readSchemaPath(); + assertEquals(expected, actual); } private static String largeString(final int pow) { - String str = "X"; + StringBuilder sb = new StringBuilder("X"); for (int i = 0; i < pow; i++) { - StringBuilder buf = new StringBuilder(); - buf.append(str).append(str); - str = buf.toString(); + sb.append(sb); } - return str; + return sb.toString(); } }