1 package io.fd.honeycomb.v3po.translate.v3po.util;
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;
8 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
10 public class TranslateUtilsTest {
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);
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());
33 public void testParseMac() throws Exception {
34 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
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]);
48 @Test(expected = IllegalArgumentException.class)
49 public void testParseMacLonger() throws Exception {
50 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
54 @Test(expected = IllegalArgumentException.class)
55 public void testParseMacShorter() throws Exception {
56 TranslateUtils.parseMac("00:fF:7f");
59 @Test(expected = IllegalArgumentException.class)
60 public void testParseRandomString() throws Exception {
61 TranslateUtils.parseMac("random{}}@$*&*!");
64 @Test(expected = NumberFormatException.class)
65 public void testParseMacNumberFormatEx() throws Exception {
66 TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
70 public void testBooleanToByte() {
71 assertEquals(0, TranslateUtils.booleanToByte(null));
72 assertEquals(0, TranslateUtils.booleanToByte(false));
73 assertEquals(1, TranslateUtils.booleanToByte(true));
77 public void testByteToBoolean() {
78 assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
79 assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
82 @Test(expected = IllegalArgumentException.class)
83 public void testByteToBooleanFailed() {
84 TranslateUtils.byteToBoolean((byte) 123);