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