3a18ccefbae376677cc39bc3ffe22242e10b0f58
[bgpcep.git] / bmp / bmp-spi / src / test / java / org / opendaylight / protocol / bmp / spi / parser / TlvUtilTest.java
1 /*
2  * Copyright (c) 2015 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.protocol.bmp.spi.parser;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.math.BigInteger;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
19
20 public class TlvUtilTest {
21
22     private static final byte[] TLV_IN = {(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05};
23     private static final byte[] TLV_OUT = {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x05, (byte) 0x01,
24         (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05};
25     private static final byte[] TLV_COUNTER32_OUT = {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x00,
26         (byte) 0x00, (byte) 0x00, (byte) 0x05};
27     private static final byte[] TLV_GAUGE64_OUT = {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x08, (byte) 0x00,
28         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05};
29     private static final byte[] TLV_UTF8_OUT = {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x05, (byte) 0x69,
30         (byte) 0x6e, (byte) 0x66, (byte) 0x6f, (byte) 0x31};
31     private static final byte[] TLV_ASCII_OUT = {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x4E,
32         (byte) 0x61, (byte) 0x6D, (byte) 0x65};
33
34     @Test
35     public void testFormatTlv() throws Exception {
36         final ByteBuf out = Unpooled.buffer(TLV_OUT.length);
37         final ByteBuf in = Unpooled.copiedBuffer(TLV_IN);
38         TlvUtil.formatTlv(1, in, out);
39         Assert.assertArrayEquals(TLV_OUT, ByteArray.getAllBytes(out));
40     }
41
42     @Test
43     public void testFormatTlvCounter32() throws Exception {
44         ByteBuf out = Unpooled.buffer(TLV_COUNTER32_OUT.length);
45         TlvUtil.formatTlvCounter32(1, new Counter32(5L), out);
46         Assert.assertArrayEquals(TLV_COUNTER32_OUT, ByteArray.getAllBytes(out));
47         out = Unpooled.EMPTY_BUFFER;
48         TlvUtil.formatTlvCounter32(1, null, out);
49         Assert.assertFalse(out.isReadable());
50     }
51
52     @Test
53     public void testFormatTlvGauge64() throws Exception {
54         ByteBuf out = Unpooled.buffer(TLV_GAUGE64_OUT.length);
55         TlvUtil.formatTlvGauge64(1, new Gauge64(BigInteger.valueOf(5)), out);
56         Assert.assertArrayEquals(TLV_GAUGE64_OUT, ByteArray.getAllBytes(out));
57         out = Unpooled.EMPTY_BUFFER;
58         TlvUtil.formatTlvGauge64(1, null, out);
59         Assert.assertFalse(out.isReadable());
60     }
61
62     @Test
63     public void testFormatTlvUtf8() throws Exception {
64         ByteBuf out = Unpooled.buffer(TLV_UTF8_OUT.length);
65         TlvUtil.formatTlvUtf8(1, "info1", out);
66         Assert.assertArrayEquals(TLV_UTF8_OUT, ByteArray.getAllBytes(out));
67         out = Unpooled.EMPTY_BUFFER;
68         TlvUtil.formatTlvUtf8(1, null, out);
69         Assert.assertFalse(out.isReadable());
70     }
71
72     @Test
73     public void testFormatTlvAscii() throws Exception {
74         ByteBuf out = Unpooled.buffer(TLV_ASCII_OUT.length);
75         TlvUtil.formatTlvAscii(1, "Name", out);
76         Assert.assertArrayEquals(TLV_ASCII_OUT, ByteArray.getAllBytes(out));
77         out = Unpooled.EMPTY_BUFFER;
78         TlvUtil.formatTlvAscii(1, null, out);
79         Assert.assertFalse(out.isReadable());
80     }
81 }