Move netconf-dom-api
[netconf.git] / netconf / netconf-impl / src / test / java / org / opendaylight / netconf / impl / MessageParserTest.java
index 562d4a4ba993b4d307ca366e9e261a8f86f256a7..06b76a213dff55ba3753d508e834d1d01f7c2be5 100644 (file)
@@ -5,22 +5,20 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.impl;
 
 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
-import com.google.common.base.Charsets;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.embedded.EmbeddedChannel;
 import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
 import java.util.Queue;
+import org.custommonkey.xmlunit.XMLUnit;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.netconf.api.NetconfMessage;
@@ -31,16 +29,14 @@ import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
 import org.opendaylight.netconf.util.messages.FramingMechanism;
-import org.opendaylight.netconf.util.messages.NetconfMessageConstants;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
 
 public class MessageParserTest {
-
     private NetconfMessage msg;
 
     @Before
     public void setUp() throws Exception {
-        this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
+        msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
     }
 
     @Test
@@ -48,13 +44,12 @@ public class MessageParserTest {
         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
                 new NetconfMessageToXMLEncoder(),
-
-                new NetconfChunkAggregator(),
+                new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
                 new NetconfXMLToMessageDecoder());
 
-        testChunkChannel.writeOutbound(this.msg);
+        testChunkChannel.writeOutbound(msg);
         Queue<Object> messages = testChunkChannel.outboundMessages();
-        assertFalse(messages.isEmpty());
+        assertEquals(1, messages.size());
 
         final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
         final ByteBuf out = Unpooled.buffer();
@@ -62,32 +57,33 @@ public class MessageParserTest {
         int msgLength = out.readableBytes();
 
         int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
-        if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
+        if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
             chunkCount++;
         }
+
+        byte[] endOfChunkBytes = FramingMechanism.CHUNK_END_STR.getBytes(StandardCharsets.US_ASCII);
         for (int i = 1; i <= chunkCount; i++) {
             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
             int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
             if (i == chunkCount) {
-                exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
-                byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
-                recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
-                        eom);
-                assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
+                exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
+                byte[] eom = new byte[endOfChunkBytes.length];
+                recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
+                assertArrayEquals(endOfChunkBytes, eom);
             }
 
-            byte[] header = new byte[String.valueOf(exptHeaderLength).length()
-                    + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
+            byte[] header = new byte[String.valueOf(exptHeaderLength).length() + 3];
             recievedOutbound.getBytes(0, header);
             assertEquals(exptHeaderLength, getHeaderLength(header));
 
             testChunkChannel.writeInbound(recievedOutbound);
         }
-        assertTrue(messages.isEmpty());
+        assertEquals(0, messages.size());
 
-        NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
+        NetconfMessage receivedMessage = testChunkChannel.readInbound();
         assertNotNull(receivedMessage);
-        assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
+        XMLUnit.setIgnoreWhitespace(true);
+        assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
     }
 
     @Test
@@ -96,22 +92,24 @@ public class MessageParserTest {
                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
                 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
 
-        testChunkChannel.writeOutbound(this.msg);
-        ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
+        testChunkChannel.writeOutbound(msg);
+        ByteBuf recievedOutbound = testChunkChannel.readOutbound();
 
-        byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
-        recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
-        assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
+        byte[] endOfMsgBytes = FramingMechanism.EOM_STR.getBytes(StandardCharsets.US_ASCII);
+        byte[] eom = new byte[endOfMsgBytes.length];
+        recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
+        assertArrayEquals(endOfMsgBytes, eom);
 
         testChunkChannel.writeInbound(recievedOutbound);
-        NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
+        NetconfMessage receivedMessage = testChunkChannel.readInbound();
         assertNotNull(receivedMessage);
-        assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
+        XMLUnit.setIgnoreWhitespace(true);
+        assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
     }
 
-    private static long getHeaderLength(byte[] bytes) {
-        byte[] HEADER_START = new byte[] { (byte) 0x0a, (byte) 0x23 };
-        return Long.parseLong(Charsets.US_ASCII.decode(
-                ByteBuffer.wrap(bytes, HEADER_START.length, bytes.length - HEADER_START.length - 1)).toString());
+    private static long getHeaderLength(final byte[] bytes) {
+        byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
+        return Long.parseLong(StandardCharsets.US_ASCII.decode(
+                ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());
     }
 }