2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.model.ietf.util;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import java.util.UUID;
15 import org.junit.Test;
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";
23 public void testBytesToMac() throws Exception {
24 final MacClass mac = UTIL.macAddressFor(BYTES);
25 assertEquals(CANON, mac.getValue());
29 public void testMacToBytes() throws Exception {
30 final byte[] bytes1 = UTIL.macAddressBytes(new MacClass(CANON));
31 assertArrayEquals(BYTES, bytes1);
33 final byte[] bytes2 = UTIL.macAddressBytes(new MacClass("01:02:1E:5a:Fb:88"));
34 assertArrayEquals(BYTES, bytes2);
38 public void testBytesToHex() {
39 final HexClass hex = UTIL.hexStringFor(BYTES);
40 assertEquals(CANON, hex.getValue());
44 public void testHexToBytes() {
45 final byte[] bytes1 = UTIL.hexStringBytes(new HexClass(CANON));
46 assertArrayEquals(BYTES, bytes1);
48 final byte[] bytes2 = UTIL.hexStringBytes(new HexClass("01:02:1E:5a:Fb:88"));
49 assertArrayEquals(BYTES, bytes2);
53 public void testPhysToBytes() {
54 final byte[] bytes1 = UTIL.physAddressBytes(new PhysClass(CANON));
55 assertArrayEquals(BYTES, bytes1);
57 final byte[] bytes2 = UTIL.physAddressBytes(new PhysClass("01:02:1E:5a:Fb:88"));
58 assertArrayEquals(BYTES, bytes2);
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")));
66 public void testQuadBytes() {
67 assertArrayEquals(new byte[] { 1, 2, 3, 4 }, UTIL.dottedQuadBytes(new QuadClass("1.2.3.4")));
71 public void testQuadFor() {
72 assertEquals("1.2.3.4", UTIL.dottedQuadFor(new byte[] { 1, 2, 3, 4 }).getValue());
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());
82 public void canonizeMACTest() throws Exception {
83 assertEquals(CANON, UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue());
86 @Test(expected = IllegalArgumentException.class)
87 public void hexValueWithExceptionTest() throws Exception {
88 AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000));
89 fail("Expected invalid character exception");