d9cb52f3a67d2f82e03e8c93ee0342ca3de92e11
[hc2vpp.git] /
1 package io.fd.honeycomb.translate.v3po.util;
2
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;
12
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;
22
23 public class TranslateUtilsTest {
24
25     private static class AnDataObject implements DataObject {
26         @Override
27         public Class<? extends DataContainer> getImplementedInterface() {
28             return null;
29         }
30     }
31
32     @Test
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);
37         try {
38             TranslateUtils.getReplyForWrite(future, replyType);
39         } catch (WriteTimeoutException e) {
40             assertTrue(e.getCause() instanceof TimeoutException);
41             assertEquals(replyType, e.getFailedId());
42             return;
43         }
44         fail("WriteTimeoutException was expected");
45     }
46
47     @Test
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);
52         try {
53             TranslateUtils.getReplyForRead(future, replyType);
54         } catch (ReadTimeoutException e) {
55             assertTrue(e.getCause() instanceof TimeoutException);
56             assertEquals(replyType, e.getFailedId());
57             return;
58         }
59         fail("ReadTimeoutException was expected");
60     }
61
62     @Test
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);
71     }
72
73     @Test
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());
80     }
81
82     @Test
83     public void testParseMac() throws Exception {
84         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
85         assertMac(bytes);
86     }
87
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]);
96     }
97
98     @Test(expected = IllegalArgumentException.class)
99     public void testParseMacLonger() throws Exception {
100         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
101         assertMac(bytes);
102     }
103
104     @Test(expected = IllegalArgumentException.class)
105     public void testParseMacShorter() throws Exception {
106         TranslateUtils.parseMac("00:fF:7f");
107     }
108
109     @Test(expected = IllegalArgumentException.class)
110     public void testParseRandomString() throws Exception {
111         TranslateUtils.parseMac("random{}}@$*&*!");
112     }
113
114     @Test(expected = NumberFormatException.class)
115     public void testParseMacNumberFormatEx() throws Exception {
116         TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
117     }
118
119     @Test
120     public void testBooleanToByte() {
121         assertEquals(0, TranslateUtils.booleanToByte(null));
122         assertEquals(0, TranslateUtils.booleanToByte(false));
123         assertEquals(1, TranslateUtils.booleanToByte(true));
124     }
125
126     @Test
127     public void testByteToBoolean() {
128         assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
129         assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
130     }
131
132     @Test(expected = IllegalArgumentException.class)
133     public void testByteToBooleanFailed() {
134         TranslateUtils.byteToBoolean((byte) 123);
135     }
136
137 }