Fix netconf-connector-config groupId
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / ChunkedFramingMechanismEncoderTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.netconf.nettyutil.handler;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.base.Charsets;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import io.netty.channel.ChannelHandlerContext;
18 import java.nio.ByteBuffer;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23
24 public class ChunkedFramingMechanismEncoderTest {
25
26     private int chunkSize;
27     @Mock
28     private ChannelHandlerContext ctx;
29
30     @Before
31     public void setUp() throws Exception {
32         MockitoAnnotations.initMocks(this);
33         chunkSize = 256;
34     }
35
36     @Test(expected = IllegalArgumentException.class)
37     public void testIllegalSize() throws Exception {
38         new ChunkedFramingMechanismEncoder(10);
39     }
40
41     @Test(expected = IllegalArgumentException.class)
42     public void testIllegalSizeMax() throws Exception {
43         new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
44     }
45
46     @Test
47     public void testEncode() throws Exception {
48         final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(chunkSize);
49         final int lastChunkSize = 20;
50         final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(chunkSize * 4 + lastChunkSize));
51         final ByteBuf destination = Unpooled.buffer();
52         encoder.encode(ctx, src, destination);
53
54         assertEquals(1077, destination.readableBytes());
55
56         byte[] buf = new byte[destination.readableBytes()];
57         destination.readBytes(buf);
58         String s = Charsets.US_ASCII.decode(ByteBuffer.wrap(buf)).toString();
59
60         assertTrue(s.startsWith("\n#256\na"));
61         assertTrue(s.endsWith("\n#20\naaaaaaaaaaaaaaaaaaaa\n##\n"));
62     }
63
64     private static byte[] getByteArray(final int size) {
65         final byte[] bytes = new byte[size];
66         for (int i = 0; i < size; i++) {
67             bytes[i] = 'a';
68         }
69         return bytes;
70     }
71 }