Binary data shall be emitted in the form of hexa numbers.
[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 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;
24
25 @RunWith(MockitoJUnitRunner.StrictStubs.class)
26 public class ChunkedFramingMechanismEncoderTest {
27
28     private int chunkSize;
29     @Mock
30     private ChannelHandlerContext ctx;
31
32     @Before
33     public void setUp() {
34         chunkSize = 256;
35     }
36
37     @Test(expected = IllegalArgumentException.class)
38     public void testIllegalSize() throws Exception {
39         new ChunkedFramingMechanismEncoder(10);
40     }
41
42     @Test(expected = IllegalArgumentException.class)
43     public void testIllegalSizeMax() throws Exception {
44         new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
45     }
46
47     @Test
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);
54
55         assertEquals(1077, destination.readableBytes());
56
57         byte[] buf = new byte[destination.readableBytes()];
58         destination.readBytes(buf);
59         String string = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(buf)).toString();
60
61         assertTrue(string.startsWith("\n#256\na"));
62         assertTrue(string.endsWith("\n#20\naaaaaaaaaaaaaaaaaaaa\n##\n"));
63     }
64
65     private static byte[] getByteArray(final int size) {
66         final byte[] bytes = new byte[size];
67         for (int i = 0; i < size; i++) {
68             bytes[i] = 'a';
69         }
70         return bytes;
71     }
72 }