Merge "Fix improper cleanup of operational data in sal-netconf-connector's disconnect"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / controller / 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.controller.netconf.nettyutil.handler;
10
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;
22
23 public class ChunkedFramingMechanismEncoderTest {
24
25     private int chunkSize;
26     @Mock
27     private ChannelHandlerContext ctx;
28
29     @Before
30     public void setUp() throws Exception {
31         MockitoAnnotations.initMocks(this);
32         chunkSize = 256;
33     }
34
35     @Test(expected = IllegalArgumentException.class)
36     public void testIllegalSize() throws Exception {
37         new ChunkedFramingMechanismEncoder(10);
38     }
39
40     @Test(expected = IllegalArgumentException.class)
41     public void testIllegalSizeMax() throws Exception {
42         new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
43     }
44
45     @Test
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);
52
53         assertEquals(1077, destination.readableBytes());
54
55         byte[] buf = new byte[destination.readableBytes()];
56         destination.readBytes(buf);
57         String s = Charsets.US_ASCII.decode(ByteBuffer.wrap(buf)).toString();
58
59         assertTrue(s.startsWith("\n#256\na"));
60         assertTrue(s.endsWith("\n#20\naaaaaaaaaaaaaaaaaaaa\n##\n"));
61     }
62
63     private byte[] getByteArray(final int size) {
64         final byte[] bytes = new byte[size];
65         for (int i = 0; i < size; i++) {
66             bytes[i] = 'a';
67         }
68         return bytes;
69     }
70 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.