Deprecate DEFAULT_MAXIMUM_CHUNK_SIZE
[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 package org.opendaylight.netconf.impl;
9
10 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import io.netty.channel.embedded.EmbeddedChannel;
18 import java.nio.ByteBuffer;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Queue;
21 import org.custommonkey.xmlunit.XMLUnit;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
26 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
27 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
28 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
29 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
31 import org.opendaylight.netconf.util.messages.FramingMechanism;
32 import org.opendaylight.netconf.util.test.XmlFileLoader;
33
34 public class MessageParserTest {
35     private NetconfMessage msg;
36
37     @Before
38     public void setUp() throws Exception {
39         msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
40     }
41
42     @Test
43     public void testChunkedFramingMechanismOnPipeline() throws Exception {
44         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
45                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
46                 new NetconfMessageToXMLEncoder(),
47                 new NetconfChunkAggregator(ChunkedFramingMechanismEncoder.MAX_CHUNK_SIZE),
48                 new NetconfXMLToMessageDecoder());
49
50         testChunkChannel.writeOutbound(msg);
51         Queue<Object> messages = testChunkChannel.outboundMessages();
52         assertEquals(1, messages.size());
53
54         final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
55         final ByteBuf out = Unpooled.buffer();
56         enc.encode(null, msg, out);
57         int msgLength = out.readableBytes();
58
59         int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
60         if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
61             chunkCount++;
62         }
63
64         byte[] endOfChunkBytes = FramingMechanism.CHUNK_END_STR.getBytes(StandardCharsets.US_ASCII);
65         for (int i = 1; i <= chunkCount; i++) {
66             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
67             int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
68             if (i == chunkCount) {
69                 exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
70                 byte[] eom = new byte[endOfChunkBytes.length];
71                 recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
72                 assertArrayEquals(endOfChunkBytes, eom);
73             }
74
75             byte[] header = new byte[String.valueOf(exptHeaderLength).length() + 3];
76             recievedOutbound.getBytes(0, header);
77             assertEquals(exptHeaderLength, getHeaderLength(header));
78
79             testChunkChannel.writeInbound(recievedOutbound);
80         }
81         assertEquals(0, messages.size());
82
83         NetconfMessage receivedMessage = testChunkChannel.readInbound();
84         assertNotNull(receivedMessage);
85         XMLUnit.setIgnoreWhitespace(true);
86         assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
87     }
88
89     @Test
90     public void testEOMFramingMechanismOnPipeline() throws Exception {
91         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
92                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
93                 new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
94
95         testChunkChannel.writeOutbound(msg);
96         ByteBuf recievedOutbound = testChunkChannel.readOutbound();
97
98         byte[] endOfMsgBytes = FramingMechanism.EOM_STR.getBytes(StandardCharsets.US_ASCII);
99         byte[] eom = new byte[endOfMsgBytes.length];
100         recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
101         assertArrayEquals(endOfMsgBytes, eom);
102
103         testChunkChannel.writeInbound(recievedOutbound);
104         NetconfMessage receivedMessage = testChunkChannel.readInbound();
105         assertNotNull(receivedMessage);
106         XMLUnit.setIgnoreWhitespace(true);
107         assertXMLEqual(msg.getDocument(), receivedMessage.getDocument());
108     }
109
110     private static long getHeaderLength(final byte[] bytes) {
111         byte[] headerStart = new byte[]{(byte) 0x0a, (byte) 0x23};
112         return Long.parseLong(StandardCharsets.US_ASCII.decode(
113                 ByteBuffer.wrap(bytes, headerStart.length, bytes.length - headerStart.length - 1)).toString());
114     }
115 }