Add uint24.yang
[mdsal.git] / common / mdsal-uint24-netty / src / test / java / org / opendaylight / mdsal / uint24 / netty / Uint24ByteBufUtilsTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.uint24.netty;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.junit.Test;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.uint24.rev200104.Uint24;
16 import org.opendaylight.yangtools.yang.common.Uint32;
17
18 public class Uint24ByteBufUtilsTest {
19     private static final Uint24 ONE_TWO_THREE = new Uint24(Uint32.valueOf(0x010203));
20
21     @Test
22     public void testRead() {
23         final ByteBuf buf = Unpooled.buffer().writeMedium(0x010203);
24         assertEquals(ONE_TWO_THREE, Uint24ByteBufUtils.readUint24(buf));
25         assertEquals(0, buf.readableBytes());
26     }
27
28     @Test
29     public void testWrite() {
30         final ByteBuf buf = Unpooled.buffer();
31         Uint24ByteBufUtils.writeUint24(buf, ONE_TWO_THREE);
32         assertMedium(buf);
33     }
34
35     @Test
36     public void testWriteMandatory() {
37         final ByteBuf buf = Unpooled.buffer();
38         Uint24ByteBufUtils.writeMandatoryUint24(buf, ONE_TWO_THREE, "foo");
39         assertMedium(buf);
40     }
41
42     @Test(expected = IllegalArgumentException.class)
43     public void testWriteMandatoryNull() {
44         Uint24ByteBufUtils.writeMandatoryUint24(Unpooled.buffer(), null, "foo");
45     }
46
47     @Test
48     public void testWriteOptional() {
49         final ByteBuf buf = Unpooled.buffer();
50         Uint24ByteBufUtils.writeOptionalUint24(buf, ONE_TWO_THREE);
51         assertMedium(buf);
52     }
53
54     @Test
55     public void testWriteOptionalNull() {
56         final ByteBuf buf = Unpooled.buffer();
57         Uint24ByteBufUtils.writeOptionalUint24(buf, null);
58         assertEquals(0, buf.readableBytes());
59     }
60
61     @Test
62     public void testWriteOrZero() {
63         final ByteBuf buf = Unpooled.buffer();
64         Uint24ByteBufUtils.writeUint24OrZero(buf, ONE_TWO_THREE);
65         assertMedium(buf);
66     }
67
68     @Test
69     public void testWriteOrZeroNull() {
70         final ByteBuf buf = Unpooled.buffer();
71         Uint24ByteBufUtils.writeUint24OrZero(buf, null);
72         assertMedium(buf, 0);
73     }
74
75     private static void assertMedium(final ByteBuf buf) {
76         assertMedium(buf, 0x010203);
77     }
78
79     private static void assertMedium(final ByteBuf buf, final int value) {
80         assertEquals(3, buf.readableBytes());
81         assertEquals(value, buf.readMedium());
82     }
83 }