Merge "Remove blocking checkedGet call"
[netconf.git] / netconf / netconf-impl / src / test / java / org / opendaylight / netconf / impl / MessageParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.impl;
10
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17
18 import com.google.common.base.Charsets;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import io.netty.channel.embedded.EmbeddedChannel;
22 import java.nio.ByteBuffer;
23 import java.util.Queue;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.netconf.api.NetconfMessage;
27 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
28 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
29 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
31 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
32 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
33 import org.opendaylight.netconf.util.messages.FramingMechanism;
34 import org.opendaylight.netconf.util.messages.NetconfMessageConstants;
35 import org.opendaylight.netconf.util.test.XmlFileLoader;
36
37 public class MessageParserTest {
38
39     private NetconfMessage msg;
40
41     @Before
42     public void setUp() throws Exception {
43         this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
44     }
45
46     @Test
47     public void testChunkedFramingMechanismOnPipeline() throws Exception {
48         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
49                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
50                 new NetconfMessageToXMLEncoder(),
51
52                 new NetconfChunkAggregator(),
53                 new NetconfXMLToMessageDecoder());
54
55         testChunkChannel.writeOutbound(this.msg);
56         Queue<Object> messages = testChunkChannel.outboundMessages();
57         assertFalse(messages.isEmpty());
58
59         final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
60         final ByteBuf out = Unpooled.buffer();
61         enc.encode(null, msg, out);
62         int msgLength = out.readableBytes();
63
64         int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
65         if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
66             chunkCount++;
67         }
68         for (int i = 1; i <= chunkCount; i++) {
69             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
70             int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
71             if (i == chunkCount) {
72                 exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
73                 byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
74                 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
75                         eom);
76                 assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
77             }
78
79             byte[] header = new byte[String.valueOf(exptHeaderLength).length()
80                     + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
81             recievedOutbound.getBytes(0, header);
82             assertEquals(exptHeaderLength, getHeaderLength(header));
83
84             testChunkChannel.writeInbound(recievedOutbound);
85         }
86         assertTrue(messages.isEmpty());
87
88         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
89         assertNotNull(receivedMessage);
90         assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
91     }
92
93     @Test
94     public void testEOMFramingMechanismOnPipeline() throws Exception {
95         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
96                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
97                 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
98
99         testChunkChannel.writeOutbound(this.msg);
100         ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
101
102         byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
103         recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
104         assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
105
106         testChunkChannel.writeInbound(recievedOutbound);
107         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
108         assertNotNull(receivedMessage);
109         assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
110     }
111
112     private static long getHeaderLength(byte[] bytes) {
113         byte[] HEADER_START = new byte[] { (byte) 0x0a, (byte) 0x23 };
114         return Long.parseLong(Charsets.US_ASCII.decode(
115                 ByteBuffer.wrap(bytes, HEADER_START.length, bytes.length - HEADER_START.length - 1)).toString());
116     }
117 }