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;
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.yangtools.yang.binding.DataContainer;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.openvpp.jvpp.dto.JVppReply;
23 public class TranslateUtilsTest {
25 private static class AnDataObject implements DataObject {
27 public Class<? extends DataContainer> getImplementedInterface() {
33 public void testGetReplyForWriteTimeout() throws Exception {
34 final Future<JVppReply<?>> future = mock(Future.class);
35 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
36 final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
38 TranslateUtils.getReplyForWrite(future, replyType);
39 } catch (WriteTimeoutException e) {
40 assertTrue(e.getCause() instanceof TimeoutException);
41 assertEquals(replyType, e.getFailedId());
44 fail("WriteTimeoutException was expected");
48 public void testGetReplyForReadTimeout() throws Exception {
49 final Future<JVppReply<?>> future = mock(Future.class);
50 final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
51 when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
53 TranslateUtils.getReplyForRead(future, replyType);
54 } catch (ReadTimeoutException e) {
55 assertTrue(e.getCause() instanceof TimeoutException);
56 assertEquals(replyType, e.getFailedId());
59 fail("ReadTimeoutException was expected");
63 public void testIpv4NoZone() throws Exception {
64 final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1");
65 byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr);
66 assertEquals((byte)192, bytes[0]);
67 // Simulating the magic of VPP
68 bytes = reverseBytes(bytes);
69 final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes);
70 assertEquals(ipv4Addr, ipv4AddressNoZone);
74 public void testToString() {
75 final byte[] expected = "test".getBytes();
76 final byte[] cString = new byte[expected.length + 10];
77 System.arraycopy(expected, 0, cString, 0, expected.length);
78 final String jString = TranslateUtils.toString(cString);
79 assertArrayEquals(expected, jString.getBytes());
83 public void testParseMac() throws Exception {
84 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
88 private void assertMac(final byte[] bytes) {
89 assertEquals(6, bytes.length);
90 assertEquals((byte) 0, bytes[0]);
91 assertEquals((byte) 255, bytes[1]);
92 assertEquals((byte) 127, bytes[2]);
93 assertEquals((byte) 21, bytes[3]);
94 assertEquals((byte) 94, bytes[4]);
95 assertEquals((byte) 169, bytes[5]);
98 @Test(expected = IllegalArgumentException.class)
99 public void testParseMacLonger() throws Exception {
100 byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
104 @Test(expected = IllegalArgumentException.class)
105 public void testParseMacShorter() throws Exception {
106 TranslateUtils.parseMac("00:fF:7f");
109 @Test(expected = IllegalArgumentException.class)
110 public void testParseRandomString() throws Exception {
111 TranslateUtils.parseMac("random{}}@$*&*!");
114 @Test(expected = NumberFormatException.class)
115 public void testParseMacNumberFormatEx() throws Exception {
116 TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
120 public void testBooleanToByte() {
121 assertEquals(0, TranslateUtils.booleanToByte(null));
122 assertEquals(0, TranslateUtils.booleanToByte(false));
123 assertEquals(1, TranslateUtils.booleanToByte(true));
127 public void testByteToBoolean() {
128 assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
129 assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
132 @Test(expected = IllegalArgumentException.class)
133 public void testByteToBooleanFailed() {
134 TranslateUtils.byteToBoolean((byte) 123);