29ea958b824116872a2b2c699328a09bdf4de1ea
[mdsal.git] / model / ietf / ietf-type-util / src / test / java / org / opendaylight / mdsal / model / ietf / util / AbstractIetfYangUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.model.ietf.util;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
14 import org.junit.Test;
15
16 public class AbstractIetfYangUtilTest {
17     private static final MacUtil UTIL = new MacUtil();
18     private static final byte[] BYTES = new byte[] { 1, 2, 30, 90, -5, -120 };
19     private static final String CANON = "01:02:1e:5a:fb:88";
20
21     @Test
22     public void testBytesToMac() throws Exception {
23         final MacClass mac = UTIL.macAddressFor(BYTES);
24         assertEquals(CANON, mac.getValue());
25     }
26
27     @Test
28     public void testMacToBytes() throws Exception {
29         final byte[] bytes1 = UTIL.macAddressBytes(new MacClass(CANON));
30         assertArrayEquals(BYTES, bytes1);
31
32         final byte[] bytes2 = UTIL.macAddressBytes(new MacClass("01:02:1E:5a:Fb:88"));
33         assertArrayEquals(BYTES, bytes2);
34     }
35
36     @Test
37     public void testPhysToBytes() throws Exception {
38         final byte[] bytes1 = UTIL.physAddressBytes(new PhysClass(CANON));
39         assertArrayEquals(BYTES, bytes1);
40
41         final byte[] bytes2 = UTIL.physAddressBytes(new PhysClass("01:02:1E:5a:Fb:88"));
42         assertArrayEquals(BYTES, bytes2);
43
44         assertArrayEquals(new byte[0], UTIL.physAddressBytes(new PhysClass("")));
45         assertArrayEquals(new byte[] { (byte) 0xaa }, UTIL.physAddressBytes(new PhysClass("aa")));
46         assertArrayEquals(new byte[] { (byte) 0xaa, (byte) 0xbb }, UTIL.physAddressBytes(new PhysClass("aa:bb")));
47     }
48
49     @Test
50     public void canonizeMACTest() throws Exception {
51         assertEquals(CANON, UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue());
52     }
53
54     @Test(expected = IllegalArgumentException.class)
55     public void hexValueWithExceptionTest() throws Exception {
56         AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000));
57         fail("Expected invalid character exception");
58     }
59 }