9c5a83e625919a64f15dbd7ebf903fc7c5e67435
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfChunkAggregatorTest.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.nettyutil.handler;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.nio.charset.StandardCharsets;
15 import java.util.ArrayList;
16 import org.junit.Test;
17
18 public class NetconfChunkAggregatorTest {
19     private static final String CHUNKED_MESSAGE = "\n#4\n"
20             + "<rpc"
21             + "\n#18\n"
22             + " message-id=\"102\"\n"
23             + "\n#79\n"
24             + "     xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
25             + "  <close-session/>\n"
26             + "</rpc>"
27             + "\n##\n";
28
29     private static final String EXPECTED_MESSAGE = "<rpc message-id=\"102\"\n"
30             + "     xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
31             + "  <close-session/>\n"
32             + "</rpc>";
33
34     private static final String CHUNKED_MESSAGE_ONE = "\n#101\n" + EXPECTED_MESSAGE + "\n##\n";
35
36     private final NetconfChunkAggregator agr = new NetconfChunkAggregator(4096);
37
38     @Test
39     public void testMultipleChunks() throws Exception {
40         final var output = new ArrayList<>();
41         final var input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(StandardCharsets.UTF_8));
42         agr.decode(null, input, output);
43
44         assertEquals(1, output.size());
45         final var chunk = (ByteBuf) output.get(0);
46
47         assertEquals(EXPECTED_MESSAGE, chunk.toString(StandardCharsets.UTF_8));
48     }
49
50     @Test
51     public void testOneChunks() throws Exception {
52         final var output = new ArrayList<>();
53         final var input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(StandardCharsets.UTF_8));
54         agr.decode(null, input, output);
55
56         assertEquals(1, output.size());
57         final ByteBuf chunk = (ByteBuf) output.get(0);
58
59         assertEquals(EXPECTED_MESSAGE, chunk.toString(StandardCharsets.UTF_8));
60     }
61 }