Merge "Bug 2160: Added concurrent 3-phase commit coordinator"
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / MessageParserTest.java
index 7e9318e0d69796eeaf4b54fe8080e7eef8ff395f..abf2ad862f779cb5651f200fb484138482bbb5c0 100644 (file)
 
 package org.opendaylight.controller.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 java.io.IOException;
-
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.custommonkey.xmlunit.XMLAssert;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.embedded.EmbeddedChannel;
+import java.util.Queue;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
+import org.opendaylight.controller.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.controller.netconf.nettyutil.handler.NetconfChunkAggregator;
+import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEOMAggregator;
+import org.opendaylight.controller.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
+import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.opendaylight.protocol.framework.DeserializerException;
-import org.opendaylight.protocol.framework.DocumentedException;
-import org.opendaylight.protocol.util.ByteArray;
-import org.w3c.dom.Document;
-import org.xml.sax.SAXException;
 
 public class MessageParserTest {
 
-    private NetconfMessageFactory parser = null;
+    private NetconfMessage msg;
 
     @Before
-    public void setUp() {
-        this.parser = new NetconfMessageFactory();
+    public void setUp() throws Exception {
+        this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
     }
 
     @Test
-    public void testPutEOM() throws IOException, SAXException, ParserConfigurationException {
-        final NetconfMessage msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/client_hello.xml");
-        final byte[] bytes = this.parser.put(msg);
-        assertArrayEquals(NetconfMessageFactory.endOfMessage, ByteArray.subByte(bytes, bytes.length
-                - NetconfMessageFactory.endOfMessage.length, NetconfMessageFactory.endOfMessage.length));
-    }
+    public void testChunkedFramingMechanismOnPipeline() throws Exception {
+        EmbeddedChannel testChunkChannel = new EmbeddedChannel(
+                FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
+                new NetconfMessageToXMLEncoder(),
 
-    @Ignore
-    @Test
-    // TODO not working on WINDOWS
-    // arrays first differed at element [4]; expected:<49> but was:<53>
-    // at
-    // org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:52)
-    public void testPutChunk() throws IOException, SAXException, ParserConfigurationException {
-        final NetconfMessage msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/client_hello.xml");
-        this.parser.setFramingMechanism(FramingMechanism.CHUNK);
-        final byte[] bytes = this.parser.put(msg);
-        final byte[] header = new byte[] { (byte) 0x0a, (byte) 0x23 , (byte) 0x32, (byte) 0x31, (byte) 0x31, (byte) 0x0a};
-        assertArrayEquals(header, ByteArray.subByte(bytes, 0, header.length));
-        assertArrayEquals(NetconfMessageFactory.endOfChunk, ByteArray.subByte(bytes, bytes.length
-                - NetconfMessageFactory.endOfChunk.length, NetconfMessageFactory.endOfChunk.length));
-    }
+                new NetconfChunkAggregator(),
+                new NetconfXMLToMessageDecoder());
 
-    @Test
-    public void testParseEOM() throws IOException, SAXException, DeserializerException, DocumentedException,
-            ParserConfigurationException {
-        final Document msg = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
-        final byte[] bytes = this.parser.put(new NetconfMessage(msg));
-        final Document doc = this.parser
-                .parse(ByteArray.subByte(bytes, 0, bytes.length - NetconfMessageFactory.endOfMessage.length))
-                .getDocument();
-        assertEquals(XmlUtil.toString(msg), XmlUtil.toString(doc));
-        XMLAssert.assertXMLEqual(msg, doc);
+        testChunkChannel.writeOutbound(this.msg);
+        Queue<Object> messages = testChunkChannel.outboundMessages();
+        assertFalse(messages.isEmpty());
+
+        final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
+        final ByteBuf out = Unpooled.buffer();
+        enc.encode(null, msg, out);
+        int msgLength = out.readableBytes();
+
+        int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
+        if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
+            chunkCount++;
+        }
+        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);
+            }
+
+            byte[] header = new byte[String.valueOf(exptHeaderLength).length()
+                    + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
+            recievedOutbound.getBytes(0, header);
+            NetconfMessageHeader messageHeader = NetconfMessageHeader.fromBytes(header);
+            assertEquals(exptHeaderLength, messageHeader.getLength());
+
+            testChunkChannel.writeInbound(recievedOutbound);
+        }
+        assertTrue(messages.isEmpty());
+
+        NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
+        assertNotNull(receivedMessage);
+        assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
     }
 
     @Test
-    public void testParseChunk() throws IOException, SAXException, DeserializerException, DocumentedException,
-            ParserConfigurationException {
-        final Document msg = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
-        this.parser.setFramingMechanism(FramingMechanism.CHUNK);
-        final byte[] bytes = this.parser.put(new NetconfMessage(msg));
-        final Document doc = this.parser
-                .parse(ByteArray.subByte(bytes, 6, bytes.length - NetconfMessageFactory.endOfChunk.length - 6))
-                .getDocument();
-        assertEquals(XmlUtil.toString(msg), XmlUtil.toString(doc));
-        XMLAssert.assertXMLEqual(msg, doc);
-    }
+    public void testEOMFramingMechanismOnPipeline() throws Exception {
+        EmbeddedChannel testChunkChannel = new EmbeddedChannel(
+                FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
+                new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
 
+        testChunkChannel.writeOutbound(this.msg);
+        ByteBuf recievedOutbound = (ByteBuf) 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);
+
+        testChunkChannel.writeInbound(recievedOutbound);
+        NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
+        assertNotNull(receivedMessage);
+        assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
+    }
 }