Enforce pcep-spi checkstyle
[bgpcep.git] / pcep / spi / src / test / java / org / opendaylight / protocol / pcep / spi / UtilsTest.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 package org.opendaylight.protocol.pcep.spi;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS;
14 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE;
15 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE;
16
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import org.junit.Test;
20 import org.opendaylight.protocol.util.ByteArray;
21
22 public class UtilsTest {
23
24     @Test
25     public void testLabelUtil() {
26         final byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
27         final ByteBuf out = Unpooled.buffer();
28         final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
29         LabelUtil.formatLabel(4, true, true, body, out);
30         assertArrayEquals(expected, ByteArray.readAllBytes(out));
31
32         final byte[] ex = { 0, 0x05, 0x01, 0x02, 0x03, 0x04 };
33         body.resetReaderIndex();
34         LabelUtil.formatLabel(5, null, null, body, out);
35         assertArrayEquals(ex, ByteArray.getAllBytes(out));
36     }
37
38     @Test
39     public void testMessageUtil() {
40         final byte[] expected = { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
41         final ByteBuf out = Unpooled.buffer();
42         final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
43         MessageUtil.formatMessage(8, body, out);
44         assertArrayEquals(expected, ByteArray.getAllBytes(out));
45     }
46
47     @Test
48     public void testObjectUtil() {
49         final byte[] expected = { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
50         final ByteBuf out = Unpooled.buffer();
51         final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
52         ObjectUtil.formatSubobject(1, 8, true, true, body, out);
53         assertArrayEquals(expected, ByteArray.getAllBytes(out));
54     }
55
56     @Test
57     public void testObjectUtilFalse() {
58         final byte[] expected = { 0x08, 0x10, 0, 0x06, 0x01, 0x02 };
59         final ByteBuf out = Unpooled.buffer();
60         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
61         ObjectUtil.formatSubobject(1, 8, false, false, body, out);
62         assertArrayEquals(expected, ByteArray.readAllBytes(out));
63
64         body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
65         ObjectUtil.formatSubobject(1, 8, null, null, body, out);
66         assertArrayEquals(expected, ByteArray.getAllBytes(out));
67     }
68
69     @Test
70     public void testXROSubobjectUtil() {
71         byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
72         final ByteBuf out = Unpooled.buffer();
73         final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
74         body.markReaderIndex();
75         XROSubobjectUtil.formatSubobject(2, true, body, out);
76         assertArrayEquals(expected, ByteArray.getAllBytes(out));
77
78         expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
79         out.clear();
80         body.resetReaderIndex();
81         XROSubobjectUtil.formatSubobject(2, false, body, out);
82         assertArrayEquals(expected, ByteArray.getAllBytes(out));
83
84         out.clear();
85         body.resetReaderIndex();
86         XROSubobjectUtil.formatSubobject(2, null, body, out);
87         assertArrayEquals(expected, ByteArray.getAllBytes(out));
88     }
89
90     @Test
91     public void testTlvUtil() {
92         byte[] expected = { 0, 4, 0, 4, 1, 2, 3, 4 };
93         final ByteBuf out = Unpooled.buffer();
94         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
95         TlvUtil.formatTlv(4, body, out);
96         assertArrayEquals(expected, ByteArray.getAllBytes(out));
97
98         expected = new byte[]{ 0, 4, 0, 5, 1, 2, 3, 4, 5, 0, 0, 0 };
99         out.clear();
100         body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5 });
101         TlvUtil.formatTlv(4, body, out);
102         assertArrayEquals(expected, ByteArray.getAllBytes(out));
103     }
104
105     @Test
106     public void testRROSubobjectUtil() {
107         final byte[] expected = { 4, 6, 1, 2, 3, 4 };
108         final ByteBuf out = Unpooled.buffer();
109         final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
110         RROSubobjectUtil.formatSubobject(4, body, out);
111         assertArrayEquals(expected, ByteArray.getAllBytes(out));
112     }
113
114     @Test
115     public void testEROSubobjectUtil() {
116         byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
117         final ByteBuf out = Unpooled.buffer();
118         final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
119         body.markReaderIndex();
120         EROSubobjectUtil.formatSubobject(2, true, body, out);
121         assertArrayEquals(expected, ByteArray.getAllBytes(out));
122
123         expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
124         out.clear();
125         body.resetReaderIndex();
126         EROSubobjectUtil.formatSubobject(2, false, body, out);
127         assertArrayEquals(expected, ByteArray.getAllBytes(out));
128     }
129
130     @Test
131     public void testVendorInformationUtil() {
132         assertTrue(VendorInformationUtil.isVendorInformationTlv(VENDOR_INFORMATION_TLV_TYPE));
133         assertFalse(VendorInformationUtil.isVendorInformationTlv(VENDOR_INFORMATION_OBJECT_CLASS));
134
135         assertTrue(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_OBJECT_CLASS,
136             VENDOR_INFORMATION_OBJECT_TYPE));
137         assertFalse(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_OBJECT_CLASS,
138             VENDOR_INFORMATION_TLV_TYPE));
139         assertFalse(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_TLV_TYPE,
140             VENDOR_INFORMATION_OBJECT_TYPE));
141         assertFalse(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_OBJECT_TYPE,
142             VENDOR_INFORMATION_OBJECT_CLASS));
143     }
144 }