Bug 8153: Enforce check-style rules for netconf - netconf-netty-util
[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 com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.nio.charset.StandardCharsets;
16 import java.util.List;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19
20 public class NetconfChunkAggregatorTest {
21
22     private static final String CHUNKED_MESSAGE = "\n#4\n"
23             + "<rpc"
24             + "\n#18\n"
25             + " message-id=\"102\"\n"
26             + "\n#79\n"
27             + "     xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
28             + "  <close-session/>\n"
29             + "</rpc>"
30             + "\n##\n";
31
32     public static final String EXPECTED_MESSAGE = "<rpc message-id=\"102\"\n"
33             + "     xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
34             + "  <close-session/>\n"
35             + "</rpc>";
36
37     private static final String CHUNKED_MESSAGE_ONE = "\n#101\n" + EXPECTED_MESSAGE + "\n##\n";
38
39     private static NetconfChunkAggregator agr;
40
41     @BeforeClass
42     public static void setUp() throws Exception {
43         agr = new NetconfChunkAggregator();
44     }
45
46     @Test
47     public void testMultipleChunks() throws Exception {
48         final List<Object> output = Lists.newArrayList();
49         final ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(StandardCharsets.UTF_8));
50         agr.decode(null, input, output);
51
52         assertEquals(1, output.size());
53         final ByteBuf chunk = (ByteBuf) output.get(0);
54
55         assertEquals(EXPECTED_MESSAGE, chunk.toString(StandardCharsets.UTF_8));
56     }
57
58     @Test
59     public void testOneChunks() throws Exception {
60         final List<Object> output = Lists.newArrayList();
61         final ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(StandardCharsets.UTF_8));
62         agr.decode(null, input, output);
63
64         assertEquals(1, output.size());
65         final ByteBuf chunk = (ByteBuf) output.get(0);
66
67         assertEquals(EXPECTED_MESSAGE, chunk.toString(StandardCharsets.UTF_8));
68     }
69
70
71 }