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.netconf.nettyutil.handler;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
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 java.nio.charset.StandardCharsets;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
25 @RunWith(MockitoJUnitRunner.StrictStubs.class)
26 public class ChunkedFramingMechanismEncoderTest {
28 private int chunkSize;
30 private ChannelHandlerContext ctx;
37 @Test(expected = IllegalArgumentException.class)
38 public void testIllegalSize() throws Exception {
39 new ChunkedFramingMechanismEncoder(10);
42 @Test(expected = IllegalArgumentException.class)
43 public void testIllegalSizeMax() throws Exception {
44 new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
48 public void testEncode() throws Exception {
49 final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(chunkSize);
50 final int lastChunkSize = 20;
51 final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(chunkSize * 4 + lastChunkSize));
52 final ByteBuf destination = Unpooled.buffer();
53 encoder.encode(ctx, src, destination);
55 assertEquals(1077, destination.readableBytes());
57 byte[] buf = new byte[destination.readableBytes()];
58 destination.readBytes(buf);
59 String string = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(buf)).toString();
61 assertTrue(string.startsWith("\n#256\na"));
62 assertTrue(string.endsWith("\n#20\naaaaaaaaaaaaaaaaaaaa\n##\n"));
65 private static byte[] getByteArray(final int size) {
66 final byte[] bytes = new byte[size];
67 for (int i = 0; i < size; i++) {