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