Add HexString/DottedQuad/Uuid support to IetfYangUtil
[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 java.util.UUID;
15 import org.junit.Test;
16
17 public class AbstractIetfYangUtilTest {
18     private static final MacUtil UTIL = new MacUtil();
19     private static final byte[] BYTES = new byte[] { 1, 2, 30, 90, -5, -120 };
20     private static final String CANON = "01:02:1e:5a:fb:88";
21
22     @Test
23     public void testBytesToMac() throws Exception {
24         final MacClass mac = UTIL.macAddressFor(BYTES);
25         assertEquals(CANON, mac.getValue());
26     }
27
28     @Test
29     public void testMacToBytes() throws Exception {
30         final byte[] bytes1 = UTIL.macAddressBytes(new MacClass(CANON));
31         assertArrayEquals(BYTES, bytes1);
32
33         final byte[] bytes2 = UTIL.macAddressBytes(new MacClass("01:02:1E:5a:Fb:88"));
34         assertArrayEquals(BYTES, bytes2);
35     }
36
37     @Test
38     public void testBytesToHex() {
39         final HexClass hex = UTIL.hexStringFor(BYTES);
40         assertEquals(CANON, hex.getValue());
41     }
42
43     @Test
44     public void testHexToBytes() {
45         final byte[] bytes1 = UTIL.hexStringBytes(new HexClass(CANON));
46         assertArrayEquals(BYTES, bytes1);
47
48         final byte[] bytes2 = UTIL.hexStringBytes(new HexClass("01:02:1E:5a:Fb:88"));
49         assertArrayEquals(BYTES, bytes2);
50     }
51
52     @Test
53     public void testPhysToBytes() {
54         final byte[] bytes1 = UTIL.physAddressBytes(new PhysClass(CANON));
55         assertArrayEquals(BYTES, bytes1);
56
57         final byte[] bytes2 = UTIL.physAddressBytes(new PhysClass("01:02:1E:5a:Fb:88"));
58         assertArrayEquals(BYTES, bytes2);
59
60         assertArrayEquals(new byte[0], UTIL.physAddressBytes(new PhysClass("")));
61         assertArrayEquals(new byte[] { (byte) 0xaa }, UTIL.physAddressBytes(new PhysClass("aa")));
62         assertArrayEquals(new byte[] { (byte) 0xaa, (byte) 0xbb }, UTIL.physAddressBytes(new PhysClass("aa:bb")));
63     }
64
65     @Test
66     public void testQuadBytes() {
67         assertArrayEquals(new byte[] { 1, 2, 3, 4 }, UTIL.dottedQuadBytes(new QuadClass("1.2.3.4")));
68     }
69
70     @Test
71     public void testQuadFor() {
72         assertEquals("1.2.3.4", UTIL.dottedQuadFor(new byte[] { 1, 2, 3, 4 }).getValue());
73     }
74
75     @Test
76     public void testUuidFor() {
77         final UUID uuid = UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
78         assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", UTIL.uuidFor(uuid).getValue());
79     }
80
81     @Test
82     public void canonizeMACTest() throws Exception {
83         assertEquals(CANON, UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue());
84     }
85
86     @Test(expected = IllegalArgumentException.class)
87     public void hexValueWithExceptionTest() throws Exception {
88         AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000));
89         fail("Expected invalid character exception");
90     }
91 }