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