String performance and maintenability
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeStreamReaderWriterTest.java
index 014202fb347995abdf9652200134c7b24d9c9e68..81d97678b01762d456fb7c3a3af8d6c77203302a 100644 (file)
@@ -25,7 +25,6 @@ import javax.xml.transform.stream.StreamResult;
 import org.apache.commons.lang.SerializationUtils;
 import org.junit.Assert;
 import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
 import org.opendaylight.controller.cluster.datastore.util.TestModel;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -43,21 +42,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");
@@ -68,18 +66,16 @@ 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() {
@@ -105,28 +101,24 @@ public class NormalizedNodeStreamReaderWriterTest {
                               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);
     }
 
-    @SuppressWarnings("deprecation")
     @Test
     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
 
@@ -143,7 +135,7 @@ public class NormalizedNodeStreamReaderWriterTest {
 
         writer.writeYangInstanceIdentifier(path);
 
-        NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
+        NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
             ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
 
         NormalizedNode<?,?> node = reader.readNormalizedNode();
@@ -155,24 +147,20 @@ public class NormalizedNodeStreamReaderWriterTest {
         writer.close();
     }
 
-    @SuppressWarnings("deprecation")
     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
     public void testInvalidNormalizedNodeStream() throws IOException {
-        byte[] protobufBytes = new NormalizedNodeToNodeCodec().encode(
-                TestModel.createBaseTestContainerBuilder().build()).getNormalizedNode().toByteArray();
-
-        NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
-            ByteStreams.newDataInput(protobufBytes));
+        byte[] invalidBytes = {1,2,3};
+        NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
+                ByteStreams.newDataInput(invalidBytes));
 
         reader.readNormalizedNode();
     }
 
-    @SuppressWarnings("deprecation")
     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
     public void testInvalidYangInstanceIdentifierStream() throws IOException {
-        byte[] protobufBytes = {1,2,3};
-        NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
-            ByteStreams.newDataInput(protobufBytes));
+        byte[] invalidBytes = {1,2,3};
+        NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
+            ByteStreams.newDataInput(invalidBytes));
 
         reader.readYangInstanceIdentifier();
     }
@@ -180,15 +168,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 = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
@@ -205,16 +191,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<DataContainerChild<? extends PathArgument, ?>> child =
                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
@@ -227,17 +212,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();
     }
 }