Improve ietf-type-util assertions
[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.assertThrows;
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() {
24         final MacClass mac = UTIL.macAddressFor(BYTES);
25         assertEquals(CANON, mac.getValue());
26     }
27
28     @Test
29     public void testMacToBytes() {
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 testQuadBits() {
67         assertEquals(0x01020304, UTIL.dottedQuadBits(new QuadClass("1.2.3.4")));
68         assertEquals(0xFFFFFFFF, UTIL.dottedQuadBits(new QuadClass("255.255.255.255")));
69     }
70
71     @Test
72     public void testQuadBytes() {
73         assertArrayEquals(new byte[] { 1, 2, 3, 4 }, UTIL.dottedQuadBytes(new QuadClass("1.2.3.4")));
74     }
75
76     @Test
77     public void testQuadForBits() {
78         assertEquals("1.2.3.4", UTIL.dottedQuadFor(0x01020304).getValue());
79         assertEquals("255.255.255.255", UTIL.dottedQuadFor(0xFFFFFFFF).getValue());
80     }
81
82     @Test
83     public void testQuadForBytes() {
84         assertEquals("1.2.3.4", UTIL.dottedQuadFor(new byte[] { 1, 2, 3, 4 }).getValue());
85     }
86
87     @Test
88     public void testUuidFor() {
89         final UUID uuid = UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
90         assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", UTIL.uuidFor(uuid).getValue());
91     }
92
93     @Test
94     public void canonizeMACTest() {
95         assertEquals(CANON, UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue());
96     }
97
98     @Test
99     public void hexValueWithExceptionTest() {
100         final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
101             () -> AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000)));
102         assertEquals("Invalid character 'ퟀ' encountered", ex.getMessage());
103     }
104 }