a55c3053a2d7355d763e8a7235778d1c71c1c1be
[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.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;
25
26 public class TranslateUtilsTest {
27
28     private static class AnDataObject implements DataObject {
29         @Override
30         public Class<? extends DataContainer> getImplementedInterface() {
31             return null;
32         }
33     }
34
35     @Test
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);
40         try {
41             TranslateUtils.getReplyForWrite(future, replyType);
42         } catch (WriteTimeoutException e) {
43             assertTrue(e.getCause() instanceof TimeoutException);
44             assertEquals(replyType, e.getFailedId());
45             return;
46         }
47         fail("WriteTimeoutException was expected");
48     }
49
50     @Test
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);
55         try {
56             TranslateUtils.getReplyForRead(future, replyType);
57         } catch (ReadTimeoutException e) {
58             assertTrue(e.getCause() instanceof TimeoutException);
59             assertEquals(replyType, e.getFailedId());
60             return;
61         }
62         fail("ReadTimeoutException was expected");
63     }
64
65     @Test
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);
74     }
75
76     @Test
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());
83     }
84
85     @Test
86     public void testParseMac() throws Exception {
87         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
88         assertMac(bytes);
89     }
90
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]);
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void testParseMacLonger() throws Exception {
103         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
104         assertMac(bytes);
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     public void testParseMacShorter() throws Exception {
109         TranslateUtils.parseMac("00:fF:7f");
110     }
111
112     @Test(expected = IllegalArgumentException.class)
113     public void testParseRandomString() throws Exception {
114         TranslateUtils.parseMac("random{}}@$*&*!");
115     }
116
117     @Test(expected = NumberFormatException.class)
118     public void testParseMacNumberFormatEx() throws Exception {
119         TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
120     }
121
122     @Test
123     public void testBooleanToByte() {
124         assertEquals(0, TranslateUtils.booleanToByte(null));
125         assertEquals(0, TranslateUtils.booleanToByte(false));
126         assertEquals(1, TranslateUtils.booleanToByte(true));
127     }
128
129     @Test
130     public void testByteToBoolean() {
131         assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
132         assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
133     }
134
135     @Test(expected = IllegalArgumentException.class)
136     public void testByteToBooleanFailed() {
137         TranslateUtils.byteToBoolean((byte) 123);
138     }
139
140     @Test
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]);
145
146         bytes = reverseBytes(bytes);
147         final Ipv6AddressNoZone ivp6AddressNoZone = TranslateUtils.arrayToIpv6AddressNoZone(bytes);
148         assertEquals(ipv6Addr,ivp6AddressNoZone);
149     }
150
151     @Test
152     public void testByteArrayToMacUnseparated(){
153         byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
154
155         String converted = TranslateUtils.byteArrayToMacUnseparated(address);
156
157         assertEquals("aabbccddeeff",converted);
158     }
159
160     @Test
161     public void testByteArrayToMacSeparated(){
162         byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
163
164         String converted = TranslateUtils.byteArrayToMacSeparated(address);
165
166         assertEquals("aa:bb:cc:dd:ee:ff",converted);
167     }
168
169     @Test(expected = IllegalArgumentException.class)
170     public void testByteArrayToMacUnseparatedIllegal(){
171         TranslateUtils.byteArrayToMacUnseparated(new byte[]{54,26,87,32,14});
172     }
173
174     @Test(expected = IllegalArgumentException.class)
175     public void testByteArrayToMacSeparatedIllegal() {
176         TranslateUtils.byteArrayToMacSeparated(new byte[]{54, 26, 87, 32, 14});
177     }
178
179     @Test
180     public void testIpv4AddressPrefixToArray() {
181         byte[] ip = TranslateUtils.ipv4AddressPrefixToArray(new Ipv4Prefix("192.168.2.1/24"));
182
183         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(ip).getValue());
184     }
185
186     @Test
187     public void testIpv6AddressPrefixToArray() {
188         byte[] ip = TranslateUtils.ipv6AddressPrefixToArray(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48"));
189
190         assertEquals("cf67:21fe:fff8:2:300:4545:19:fe3f", TranslateUtils.arrayToIpv6AddressNoZone(ip).getValue());
191     }
192
193     @Test
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")));
197     }
198 }