ba3861b995fed67338979f83d79ab1c0ac26b7ee
[hc2vpp.git] /
1 package io.fd.honeycomb.v3po.translate.v3po.util;
2
3 import static org.junit.Assert.assertArrayEquals;
4 import static org.junit.Assert.assertEquals;
5
6 import org.junit.Test;
7 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
8
9 public class TranslateUtilsTest {
10
11     @Test
12     public void testIpv4NoZone() throws Exception {
13         final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1");
14         byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr);
15         assertEquals((byte)192, bytes[0]);
16         // Simulating the magic of VPP
17         bytes = reverseBytes(bytes);
18         final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes);
19         assertEquals(ipv4Addr, ipv4AddressNoZone);
20     }
21
22     private byte[] reverseBytes(final byte[] bytes) {
23         final byte[] reversed = new byte[bytes.length];
24         int i = 1;
25         for (byte aByte : bytes) {
26             reversed[bytes.length - i++] = aByte;
27         }
28
29         return reversed;
30     }
31
32     @Test
33     public void testToString() {
34         final byte[] expected = "test".getBytes();
35         final byte[] cString = new byte[expected.length + 10];
36         System.arraycopy(expected, 0, cString, 0, expected.length);
37         final String jString = TranslateUtils.toString(cString);
38         assertArrayEquals(expected, jString.getBytes());
39     }
40
41     @Test
42     public void testParseMac() throws Exception {
43         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
44         assertMac(bytes);
45     }
46
47     private void assertMac(final byte[] bytes) {
48         assertEquals(6, bytes.length);
49         assertEquals((byte) 0, bytes[0]);
50         assertEquals((byte) 255, bytes[1]);
51         assertEquals((byte) 127, bytes[2]);
52         assertEquals((byte) 21, bytes[3]);
53         assertEquals((byte) 94, bytes[4]);
54         assertEquals((byte) 169, bytes[5]);
55     }
56
57     @Test(expected = IllegalArgumentException.class)
58     public void testParseMacLonger() throws Exception {
59         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
60         assertMac(bytes);
61     }
62
63     @Test(expected = IllegalArgumentException.class)
64     public void testParseMacShorter() throws Exception {
65         TranslateUtils.parseMac("00:fF:7f");
66     }
67
68     @Test(expected = IllegalArgumentException.class)
69     public void testParseRandomString() throws Exception {
70         TranslateUtils.parseMac("random{}}@$*&*!");
71     }
72
73     @Test(expected = NumberFormatException.class)
74     public void testParseMacNumberFormatEx() throws Exception {
75         TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
76     }
77
78     @Test
79     public void testBooleanToByte() {
80         assertEquals(0, TranslateUtils.booleanToByte(null));
81         assertEquals(0, TranslateUtils.booleanToByte(false));
82         assertEquals(1, TranslateUtils.booleanToByte(true));
83     }
84
85     @Test
86     public void testByteToBoolean() {
87         assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
88         assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
89     }
90
91     @Test(expected = IllegalArgumentException.class)
92     public void testByteToBooleanFailed() {
93         TranslateUtils.byteToBoolean((byte) 123);
94     }
95 }