ad05acf8623b92dd967928e3e45ccdfa4f9f4ae7
[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
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.InvocationTargetException;
18 import org.junit.Test;
19 import org.opendaylight.protocol.util.ByteArray;
20
21 public class UtilsTest {
22
23     @Test
24     public void testLabelUtil() {
25         byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
26         ByteBuf out = Unpooled.buffer();
27         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
28         LabelUtil.formatLabel(4, true, true, body, out);
29         assertArrayEquals(expected, ByteArray.getAllBytes(out));
30     }
31
32     @Test
33     public void testMessageUtil() {
34         byte[] expected = { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
35         ByteBuf out = Unpooled.buffer();
36         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
37         MessageUtil.formatMessage(8, body, out);
38         assertArrayEquals(expected, ByteArray.getAllBytes(out));
39     }
40
41     @Test
42     public void testObjectUtil() {
43         byte[] expected = { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
44         ByteBuf out = Unpooled.buffer();
45         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
46         ObjectUtil.formatSubobject(1, 8, true, true, body, out);
47         assertArrayEquals(expected, ByteArray.getAllBytes(out));
48     }
49
50     @Test
51     public void testObjectUtilFalse() {
52         byte[] expected = { 0x08, 0x10, 0, 0x06, 0x01, 0x02 };
53         ByteBuf out = Unpooled.buffer();
54         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
55         ObjectUtil.formatSubobject(1, 8, false, false, body, out);
56         assertArrayEquals(expected, ByteArray.getAllBytes(out));
57     }
58
59     @Test
60     public void testXROSubobjectUtil() {
61         byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
62         ByteBuf out = Unpooled.buffer();
63         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
64         body.markReaderIndex();
65         XROSubobjectUtil.formatSubobject(2, true, body, out);
66         assertArrayEquals(expected, ByteArray.getAllBytes(out));
67
68         expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
69         out.clear();
70         body.resetReaderIndex();
71         XROSubobjectUtil.formatSubobject(2, false, body, out);
72         assertArrayEquals(expected, ByteArray.getAllBytes(out));
73     }
74
75     @Test
76     public void testTlvUtil() {
77         byte[] expected = { 0, 4, 0, 4, 1, 2, 3, 4 };
78         ByteBuf out = Unpooled.buffer();
79         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
80         TlvUtil.formatTlv(4, body, out);
81         assertArrayEquals(expected, ByteArray.getAllBytes(out));
82
83         expected = new byte[]{ 0, 4, 0, 5, 1, 2, 3, 4, 5, 0, 0, 0 };
84         out.clear();
85         body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5 });
86         TlvUtil.formatTlv(4, body, out);
87         assertArrayEquals(expected, ByteArray.getAllBytes(out));
88     }
89
90     @Test
91     public void testRROSubobjectUtil() {
92         byte[] expected = { 4, 6, 1, 2, 3, 4 };
93         ByteBuf out = Unpooled.buffer();
94         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
95         RROSubobjectUtil.formatSubobject(4, body, out);
96         assertArrayEquals(expected, ByteArray.getAllBytes(out));
97     }
98
99     @Test
100     public void testEROSubobjectUtil() {
101         byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
102         ByteBuf out = Unpooled.buffer();
103         ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
104         body.markReaderIndex();
105         EROSubobjectUtil.formatSubobject(2, true, body, out);
106         assertArrayEquals(expected, ByteArray.getAllBytes(out));
107
108         expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
109         out.clear();
110         body.resetReaderIndex();
111         EROSubobjectUtil.formatSubobject(2, false, body, out);
112         assertArrayEquals(expected, ByteArray.getAllBytes(out));
113     }
114
115     @Test
116     public void testVendorInformationUtil() {
117         assertTrue(VendorInformationUtil.isVendorInformationTlv(VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE));
118         assertFalse(VendorInformationUtil.isVendorInformationTlv(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS));
119
120         assertTrue(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS, VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE));
121         assertFalse(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS, VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE));
122         assertFalse(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE, VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE));
123         assertFalse(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE, VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS));
124     }
125
126     @Test(expected=UnsupportedOperationException.class)
127     public void testVendorInformationUtilPrivateConstructor() throws Throwable {
128         final Constructor<VendorInformationUtil> c = VendorInformationUtil.class.getDeclaredConstructor();
129         c.setAccessible(true);
130         try {
131             c.newInstance();
132         } catch (InvocationTargetException e) {
133             throw e.getCause();
134         }
135     }
136
137     @Test(expected=UnsupportedOperationException.class)
138     public void testEROSubobjectUtilPrivateConstructor() throws Throwable {
139         final Constructor<EROSubobjectUtil> c = EROSubobjectUtil.class.getDeclaredConstructor();
140         c.setAccessible(true);
141         try {
142             c.newInstance();
143         } catch (InvocationTargetException e) {
144             throw e.getCause();
145         }
146     }
147
148     @Test(expected=UnsupportedOperationException.class)
149     public void testRROSubobjectUtilPrivateConstructor() throws Throwable {
150         final Constructor<RROSubobjectUtil> c = RROSubobjectUtil.class.getDeclaredConstructor();
151         c.setAccessible(true);
152         try {
153             c.newInstance();
154         } catch (InvocationTargetException e) {
155             throw e.getCause();
156         }
157     }
158
159     @Test(expected=UnsupportedOperationException.class)
160     public void testTlvUtilPrivateConstructor() throws Throwable {
161         final Constructor<TlvUtil> c = TlvUtil.class.getDeclaredConstructor();
162         c.setAccessible(true);
163         try {
164             c.newInstance();
165         } catch (InvocationTargetException e) {
166             throw e.getCause();
167         }
168     }
169
170     @Test(expected=UnsupportedOperationException.class)
171     public void testXROSubobjectUtilPrivateConstructor() throws Throwable {
172         final Constructor<XROSubobjectUtil> c = XROSubobjectUtil.class.getDeclaredConstructor();
173         c.setAccessible(true);
174         try {
175             c.newInstance();
176         } catch (InvocationTargetException e) {
177             throw e.getCause();
178         }
179     }
180
181     @Test(expected=UnsupportedOperationException.class)
182     public void testObjectUtilPrivateConstructor() throws Throwable {
183         final Constructor<ObjectUtil> c = ObjectUtil.class.getDeclaredConstructor();
184         c.setAccessible(true);
185         try {
186             c.newInstance();
187         } catch (InvocationTargetException e) {
188             throw e.getCause();
189         }
190     }
191
192     @Test(expected=UnsupportedOperationException.class)
193     public void testMessageUtilPrivateConstructor() throws Throwable {
194         final Constructor<MessageUtil> c = MessageUtil.class.getDeclaredConstructor();
195         c.setAccessible(true);
196         try {
197             c.newInstance();
198         } catch (InvocationTargetException e) {
199             throw e.getCause();
200         }
201     }
202
203     @Test(expected=UnsupportedOperationException.class)
204     public void testLabelUtilPrivateConstructor() throws Throwable {
205         final Constructor<LabelUtil> c = LabelUtil.class.getDeclaredConstructor();
206         c.setAccessible(true);
207         try {
208             c.newInstance();
209         } catch (InvocationTargetException e) {
210             throw e.getCause();
211         }
212     }
213
214     @Test(expected=UnsupportedOperationException.class)
215     public void testPCEPMessageConstantsPrivateConstructor() throws Throwable {
216         final Constructor<PCEPMessageConstants> c = PCEPMessageConstants.class.getDeclaredConstructor();
217         c.setAccessible(true);
218         try {
219             c.newInstance();
220         } catch (InvocationTargetException e) {
221             throw e.getCause();
222         }
223     }
224 }