1 package io.fd.honeycomb.translate.v3po.util;
3 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseAddress;
4 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseBytes;
5 import static org.junit.Assert.assertArrayEquals;
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
9 import static org.mockito.Matchers.anyLong;
10 import static org.mockito.Matchers.eq;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.openvpp.jvpp.dto.JVppReply;
30 public class TranslateUtilsTest {
32 private static class AnDataObject implements DataObject {
34 public Class<? extends DataContainer> getImplementedInterface() {
40 public void testGetReplyForWriteTimeout() throws Exception {
41 final Future<JVppReply<?>> future = mock(Future.class);
42 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
43 final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
45 TranslateUtils.getReplyForWrite(future, replyType);
46 } catch (WriteTimeoutException e) {
47 assertTrue(e.getCause() instanceof TimeoutException);
48 assertEquals(replyType, e.getFailedId());
51 fail("WriteTimeoutException was expected");
55 public void testGetReplyForReadTimeout() throws Exception {
56 final Future<JVppReply<?>> future = mock(Future.class);
57 final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
58 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
60 TranslateUtils.getReplyForRead(future, replyType);
61 } catch (ReadTimeoutException e) {
62 assertTrue(e.getCause() instanceof TimeoutException);
63 assertEquals(replyType, e.getFailedId());
66 fail("ReadTimeoutException was expected");
70 public void testIpv4NoZone() throws Exception {
71 final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1");
72 byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr);
73 assertEquals((byte) 192, bytes[0]);
74 // Simulating the magic of VPP
75 bytes = reverseBytes(bytes);
76 final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes);
77 assertEquals(ipv4Addr, ipv4AddressNoZone);
81 public void testToString() {
82 final byte[] expected = "test".getBytes();
83 final byte[] cString = new byte[expected.length + 10];
84 System.arraycopy(expected, 0, cString, 0, expected.length);
85 final String jString = TranslateUtils.toString(cString);
86 assertArrayEquals(expected, jString.getBytes());
90 public void testParseMac() throws Exception {
91 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
95 private void assertMac(final byte[] bytes) {
96 assertEquals(6, bytes.length);
97 assertEquals((byte) 0, bytes[0]);
98 assertEquals((byte) 255, bytes[1]);
99 assertEquals((byte) 127, bytes[2]);
100 assertEquals((byte) 21, bytes[3]);
101 assertEquals((byte) 94, bytes[4]);
102 assertEquals((byte) 169, bytes[5]);
105 @Test(expected = IllegalArgumentException.class)
106 public void testParseMacLonger() throws Exception {
107 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
111 @Test(expected = IllegalArgumentException.class)
112 public void testParseMacShorter() throws Exception {
113 TranslateUtils.parseMac("00:fF:7f");
116 @Test(expected = IllegalArgumentException.class)
117 public void testParseRandomString() throws Exception {
118 TranslateUtils.parseMac("random{}}@$*&*!");
121 @Test(expected = NumberFormatException.class)
122 public void testParseMacNumberFormatEx() throws Exception {
123 TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
127 public void testBooleanToByte() {
128 assertEquals(0, TranslateUtils.booleanToByte(null));
129 assertEquals(0, TranslateUtils.booleanToByte(false));
130 assertEquals(1, TranslateUtils.booleanToByte(true));
134 public void testByteToBoolean() {
135 assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
136 assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
139 @Test(expected = IllegalArgumentException.class)
140 public void testByteToBooleanFailed() {
141 TranslateUtils.byteToBoolean((byte) 123);
145 public void testIpv6NoZone() {
146 final Ipv6AddressNoZone ipv6Addr = new Ipv6AddressNoZone("3ffe:1900:4545:3:200:f8ff:fe21:67cf");
147 byte[] bytes = TranslateUtils.ipv6AddressNoZoneToArray(ipv6Addr);
148 assertEquals((byte) 63, bytes[0]);
150 bytes = reverseBytes(bytes);
151 final Ipv6AddressNoZone ivp6AddressNoZone = TranslateUtils.arrayToIpv6AddressNoZone(bytes);
152 assertEquals(ipv6Addr, ivp6AddressNoZone);
156 public void testByteArrayToMacUnseparated() {
157 byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
159 String converted = TranslateUtils.byteArrayToMacUnseparated(address);
161 assertEquals("aabbccddeeff", converted);
165 public void testByteArrayToMacSeparated() {
166 byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
168 String converted = TranslateUtils.byteArrayToMacSeparated(address);
170 assertEquals("aa:bb:cc:dd:ee:ff", converted);
173 @Test(expected = IllegalArgumentException.class)
174 public void testByteArrayToMacUnseparatedIllegal() {
175 TranslateUtils.byteArrayToMacUnseparated(new byte[]{54, 26, 87, 32, 14});
178 @Test(expected = IllegalArgumentException.class)
179 public void testByteArrayToMacSeparatedIllegal() {
180 TranslateUtils.byteArrayToMacSeparated(new byte[]{54, 26, 87, 32, 14});
184 public void testIpv4AddressPrefixToArray() {
185 byte[] ip = TranslateUtils.ipv4AddressPrefixToArray(new Ipv4Prefix("192.168.2.1/24"));
187 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(ip).getValue());
191 public void testIpv6AddressPrefixToArray() {
192 byte[] ip = TranslateUtils.ipv6AddressPrefixToArray(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48"));
194 assertEquals("cf67:21fe:fff8:2:300:4545:19:fe3f", TranslateUtils.arrayToIpv6AddressNoZone(ip).getValue());
198 public void testExtractPrefix() {
199 assertEquals(24, TranslateUtils.extractPrefix(new Ipv4Prefix("192.168.2.1/24")));
200 assertEquals(48, TranslateUtils.extractPrefix(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48")));
204 public void testRevertAddress() {
205 assertEquals("1.2.168.192",
206 reverseAddress(new IpAddress(new Ipv4Address("192.168.2.1"))).getIpv4Address().getValue());
207 assertEquals("3473:7003:2e8a::a385:b80d:120",
208 reverseAddress(new IpAddress(new Ipv6Address("2001:db8:85a3:0:0:8a2e:370:7334"))).getIpv6Address()