1 package io.fd.honeycomb.translate.v3po.util;
3 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseBytes;
4 import static org.junit.Assert.assertArrayEquals;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8 import static org.mockito.Matchers.anyLong;
9 import static org.mockito.Matchers.eq;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
21 import org.opendaylight.yangtools.yang.binding.DataContainer;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.openvpp.jvpp.dto.JVppReply;
26 public class TranslateUtilsTest {
28 private static class AnDataObject implements DataObject {
30 public Class<? extends DataContainer> getImplementedInterface() {
36 public void testGetReplyForWriteTimeout() throws Exception {
37 final Future<JVppReply<?>> future = mock(Future.class);
38 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
39 final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
41 TranslateUtils.getReplyForWrite(future, replyType);
42 } catch (WriteTimeoutException e) {
43 assertTrue(e.getCause() instanceof TimeoutException);
44 assertEquals(replyType, e.getFailedId());
47 fail("WriteTimeoutException was expected");
51 public void testGetReplyForReadTimeout() throws Exception {
52 final Future<JVppReply<?>> future = mock(Future.class);
53 final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
54 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
56 TranslateUtils.getReplyForRead(future, replyType);
57 } catch (ReadTimeoutException e) {
58 assertTrue(e.getCause() instanceof TimeoutException);
59 assertEquals(replyType, e.getFailedId());
62 fail("ReadTimeoutException was expected");
66 public void testIpv4NoZone() throws Exception {
67 final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1");
68 byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr);
69 assertEquals((byte)192, bytes[0]);
70 // Simulating the magic of VPP
71 bytes = reverseBytes(bytes);
72 final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes);
73 assertEquals(ipv4Addr, ipv4AddressNoZone);
77 public void testToString() {
78 final byte[] expected = "test".getBytes();
79 final byte[] cString = new byte[expected.length + 10];
80 System.arraycopy(expected, 0, cString, 0, expected.length);
81 final String jString = TranslateUtils.toString(cString);
82 assertArrayEquals(expected, jString.getBytes());
86 public void testParseMac() throws Exception {
87 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
91 private void assertMac(final byte[] bytes) {
92 assertEquals(6, bytes.length);
93 assertEquals((byte) 0, bytes[0]);
94 assertEquals((byte) 255, bytes[1]);
95 assertEquals((byte) 127, bytes[2]);
96 assertEquals((byte) 21, bytes[3]);
97 assertEquals((byte) 94, bytes[4]);
98 assertEquals((byte) 169, bytes[5]);
101 @Test(expected = IllegalArgumentException.class)
102 public void testParseMacLonger() throws Exception {
103 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
107 @Test(expected = IllegalArgumentException.class)
108 public void testParseMacShorter() throws Exception {
109 TranslateUtils.parseMac("00:fF:7f");
112 @Test(expected = IllegalArgumentException.class)
113 public void testParseRandomString() throws Exception {
114 TranslateUtils.parseMac("random{}}@$*&*!");
117 @Test(expected = NumberFormatException.class)
118 public void testParseMacNumberFormatEx() throws Exception {
119 TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
123 public void testBooleanToByte() {
124 assertEquals(0, TranslateUtils.booleanToByte(null));
125 assertEquals(0, TranslateUtils.booleanToByte(false));
126 assertEquals(1, TranslateUtils.booleanToByte(true));
130 public void testByteToBoolean() {
131 assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
132 assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
135 @Test(expected = IllegalArgumentException.class)
136 public void testByteToBooleanFailed() {
137 TranslateUtils.byteToBoolean((byte) 123);
141 public void testIpv6NoZone(){
142 final Ipv6AddressNoZone ipv6Addr = new Ipv6AddressNoZone("3ffe:1900:4545:3:200:f8ff:fe21:67cf");
143 byte[] bytes = TranslateUtils.ipv6AddressNoZoneToArray(ipv6Addr);
144 assertEquals((byte)63,bytes[0]);
146 bytes = reverseBytes(bytes);
147 final Ipv6AddressNoZone ivp6AddressNoZone = TranslateUtils.arrayToIpv6AddressNoZone(bytes);
148 assertEquals(ipv6Addr,ivp6AddressNoZone);
152 public void testByteArrayToMacUnseparated(){
153 byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
155 String converted = TranslateUtils.byteArrayToMacUnseparated(address);
157 assertEquals("aabbccddeeff",converted);
161 public void testByteArrayToMacSeparated(){
162 byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
164 String converted = TranslateUtils.byteArrayToMacSeparated(address);
166 assertEquals("aa:bb:cc:dd:ee:ff",converted);
169 @Test(expected = IllegalArgumentException.class)
170 public void testByteArrayToMacUnseparatedIllegal(){
171 TranslateUtils.byteArrayToMacUnseparated(new byte[]{54,26,87,32,14});
174 @Test(expected = IllegalArgumentException.class)
175 public void testByteArrayToMacSeparatedIllegal() {
176 TranslateUtils.byteArrayToMacSeparated(new byte[]{54, 26, 87, 32, 14});
180 public void testIpv4AddressPrefixToArray() {
181 byte[] ip = TranslateUtils.ipv4AddressPrefixToArray(new Ipv4Prefix("192.168.2.1/24"));
183 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(ip).getValue());
187 public void testIpv6AddressPrefixToArray() {
188 byte[] ip = TranslateUtils.ipv6AddressPrefixToArray(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48"));
190 assertEquals("cf67:21fe:fff8:2:300:4545:19:fe3f", TranslateUtils.arrayToIpv6AddressNoZone(ip).getValue());
194 public void testExtractPrefix() {
195 assertEquals(24, TranslateUtils.extractPrefix(new Ipv4Prefix("192.168.2.1/24")));
196 assertEquals(48, TranslateUtils.extractPrefix(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48")));