2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.nettyutil.handler;
10 import static org.junit.Assert.assertEquals;
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;
18 public class NetconfChunkAggregatorTest {
19 private static final String CHUNKED_MESSAGE = "\n#4\n"
22 + " message-id=\"102\"\n"
24 + " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
25 + " <close-session/>\n"
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"
34 private static final String CHUNKED_MESSAGE_ONE = "\n#101\n" + EXPECTED_MESSAGE + "\n##\n";
36 private final NetconfChunkAggregator agr = new NetconfChunkAggregator(4096);
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);
44 assertEquals(1, output.size());
45 final var chunk = (ByteBuf) output.get(0);
47 assertEquals(EXPECTED_MESSAGE, chunk.toString(StandardCharsets.UTF_8));
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);
56 assertEquals(1, output.size());
57 final ByteBuf chunk = (ByteBuf) output.get(0);
59 assertEquals(EXPECTED_MESSAGE, chunk.toString(StandardCharsets.UTF_8));