2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.spi;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
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;
21 public class UtilsTest {
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));
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));
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));
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));
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));
68 expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
70 body.resetReaderIndex();
71 XROSubobjectUtil.formatSubobject(2, false, body, out);
72 assertArrayEquals(expected, ByteArray.getAllBytes(out));
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));
83 expected = new byte[]{ 0, 4, 0, 5, 1, 2, 3, 4, 5, 0, 0, 0 };
85 body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5 });
86 TlvUtil.formatTlv(4, body, out);
87 assertArrayEquals(expected, ByteArray.getAllBytes(out));
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));
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));
108 expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
110 body.resetReaderIndex();
111 EROSubobjectUtil.formatSubobject(2, false, body, out);
112 assertArrayEquals(expected, ByteArray.getAllBytes(out));
116 public void testVendorInformationUtil() {
117 assertTrue(VendorInformationUtil.isVendorInformationTlv(VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE));
118 assertFalse(VendorInformationUtil.isVendorInformationTlv(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS));
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));
126 @Test(expected=UnsupportedOperationException.class)
127 public void testVendorInformationUtilPrivateConstructor() throws Throwable {
128 final Constructor<VendorInformationUtil> c = VendorInformationUtil.class.getDeclaredConstructor();
129 c.setAccessible(true);
132 } catch (InvocationTargetException e) {
137 @Test(expected=UnsupportedOperationException.class)
138 public void testEROSubobjectUtilPrivateConstructor() throws Throwable {
139 final Constructor<EROSubobjectUtil> c = EROSubobjectUtil.class.getDeclaredConstructor();
140 c.setAccessible(true);
143 } catch (InvocationTargetException e) {
148 @Test(expected=UnsupportedOperationException.class)
149 public void testRROSubobjectUtilPrivateConstructor() throws Throwable {
150 final Constructor<RROSubobjectUtil> c = RROSubobjectUtil.class.getDeclaredConstructor();
151 c.setAccessible(true);
154 } catch (InvocationTargetException e) {
159 @Test(expected=UnsupportedOperationException.class)
160 public void testTlvUtilPrivateConstructor() throws Throwable {
161 final Constructor<TlvUtil> c = TlvUtil.class.getDeclaredConstructor();
162 c.setAccessible(true);
165 } catch (InvocationTargetException e) {
170 @Test(expected=UnsupportedOperationException.class)
171 public void testXROSubobjectUtilPrivateConstructor() throws Throwable {
172 final Constructor<XROSubobjectUtil> c = XROSubobjectUtil.class.getDeclaredConstructor();
173 c.setAccessible(true);
176 } catch (InvocationTargetException e) {
181 @Test(expected=UnsupportedOperationException.class)
182 public void testObjectUtilPrivateConstructor() throws Throwable {
183 final Constructor<ObjectUtil> c = ObjectUtil.class.getDeclaredConstructor();
184 c.setAccessible(true);
187 } catch (InvocationTargetException e) {
192 @Test(expected=UnsupportedOperationException.class)
193 public void testMessageUtilPrivateConstructor() throws Throwable {
194 final Constructor<MessageUtil> c = MessageUtil.class.getDeclaredConstructor();
195 c.setAccessible(true);
198 } catch (InvocationTargetException e) {
203 @Test(expected=UnsupportedOperationException.class)
204 public void testLabelUtilPrivateConstructor() throws Throwable {
205 final Constructor<LabelUtil> c = LabelUtil.class.getDeclaredConstructor();
206 c.setAccessible(true);
209 } catch (InvocationTargetException e) {
214 @Test(expected=UnsupportedOperationException.class)
215 public void testPCEPMessageConstantsPrivateConstructor() throws Throwable {
216 final Constructor<PCEPMessageConstants> c = PCEPMessageConstants.class.getDeclaredConstructor();
217 c.setAccessible(true);
220 } catch (InvocationTargetException e) {