2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.nettyutil.handler;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import com.google.common.base.Charsets;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import io.netty.channel.ChannelHandlerContext;
17 import java.nio.ByteBuffer;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
23 public class ChunkedFramingMechanismEncoderTest {
25 private int chunkSize;
27 private ChannelHandlerContext ctx;
30 public void setUp() throws Exception {
31 MockitoAnnotations.initMocks(this);
35 @Test(expected = IllegalArgumentException.class)
36 public void testIllegalSize() throws Exception {
37 new ChunkedFramingMechanismEncoder(10);
40 @Test(expected = IllegalArgumentException.class)
41 public void testIllegalSizeMax() throws Exception {
42 new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
46 public void testEncode() throws Exception {
47 final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(chunkSize);
48 final int lastChunkSize = 20;
49 final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(chunkSize * 4 + lastChunkSize));
50 final ByteBuf destination = Unpooled.buffer();
51 encoder.encode(ctx, src, destination);
53 assertEquals(1077, destination.readableBytes());
55 byte[] buf = new byte[destination.readableBytes()];
56 destination.readBytes(buf);
57 String s = Charsets.US_ASCII.decode(ByteBuffer.wrap(buf)).toString();
59 assertTrue(s.startsWith("\n#256\na"));
60 assertTrue(s.endsWith("\n#20\naaaaaaaaaaaaaaaaaaaa\n##\n"));
63 private static byte[] getByteArray(final int size) {
64 final byte[] bytes = new byte[size];
65 for (int i = 0; i < size; i++) {