1 package io.fd.honeycomb.v3po.translate.v3po.util;
3 import static org.junit.Assert.assertArrayEquals;
4 import static org.junit.Assert.assertEquals;
7 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
9 public class TranslateUtilsTest {
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);
22 private byte[] reverseBytes(final byte[] bytes) {
23 final byte[] reversed = new byte[bytes.length];
25 for (byte aByte : bytes) {
26 reversed[bytes.length - i++] = aByte;
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());
42 public void testParseMac() throws Exception {
43 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
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]);
57 @Test(expected = IllegalArgumentException.class)
58 public void testParseMacLonger() throws Exception {
59 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
63 @Test(expected = IllegalArgumentException.class)
64 public void testParseMacShorter() throws Exception {
65 TranslateUtils.parseMac("00:fF:7f");
68 @Test(expected = IllegalArgumentException.class)
69 public void testParseRandomString() throws Exception {
70 TranslateUtils.parseMac("random{}}@$*&*!");
73 @Test(expected = NumberFormatException.class)
74 public void testParseMacNumberFormatEx() throws Exception {
75 TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
79 public void testBooleanToByte() {
80 assertEquals(0, TranslateUtils.booleanToByte(null));
81 assertEquals(0, TranslateUtils.booleanToByte(false));
82 assertEquals(1, TranslateUtils.booleanToByte(true));
86 public void testByteToBoolean() {
87 assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
88 assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
91 @Test(expected = IllegalArgumentException.class)
92 public void testByteToBooleanFailed() {
93 TranslateUtils.byteToBoolean((byte) 123);