2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.uint24.netty;
10 import static org.junit.Assert.assertEquals;
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;
18 public class Uint24ByteBufUtilsTest {
19 private static final Uint24 ONE_TWO_THREE = new Uint24(Uint32.valueOf(0x010203));
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());
29 public void testWrite() {
30 final ByteBuf buf = Unpooled.buffer();
31 Uint24ByteBufUtils.writeUint24(buf, ONE_TWO_THREE);
36 public void testWriteMandatory() {
37 final ByteBuf buf = Unpooled.buffer();
38 Uint24ByteBufUtils.writeMandatoryUint24(buf, ONE_TWO_THREE, "foo");
42 @Test(expected = IllegalArgumentException.class)
43 public void testWriteMandatoryNull() {
44 Uint24ByteBufUtils.writeMandatoryUint24(Unpooled.buffer(), null, "foo");
48 public void testWriteOptional() {
49 final ByteBuf buf = Unpooled.buffer();
50 Uint24ByteBufUtils.writeOptionalUint24(buf, ONE_TWO_THREE);
55 public void testWriteOptionalNull() {
56 final ByteBuf buf = Unpooled.buffer();
57 Uint24ByteBufUtils.writeOptionalUint24(buf, null);
58 assertEquals(0, buf.readableBytes());
62 public void testWriteOrZero() {
63 final ByteBuf buf = Unpooled.buffer();
64 Uint24ByteBufUtils.writeUint24OrZero(buf, ONE_TWO_THREE);
69 public void testWriteOrZeroNull() {
70 final ByteBuf buf = Unpooled.buffer();
71 Uint24ByteBufUtils.writeUint24OrZero(buf, null);
75 private static void assertMedium(final ByteBuf buf) {
76 assertMedium(buf, 0x010203);
79 private static void assertMedium(final ByteBuf buf, final int value) {
80 assertEquals(3, buf.readableBytes());
81 assertEquals(value, buf.readMedium());