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.protocol.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.fail;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.function.BiConsumer;
17 import java.util.function.Function;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.Uint16;
20 import org.opendaylight.yangtools.yang.common.Uint32;
21 import org.opendaylight.yangtools.yang.common.Uint64;
22 import org.opendaylight.yangtools.yang.common.Uint8;
24 public class ByteBufUtilsTest {
27 public void testByte() {
28 testWrite(Byte.valueOf((byte) 1), 1, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
29 ByteBufUtils::writeMandatory);
33 public void testShort() {
34 testWrite(Short.valueOf((short) 1), 2, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
35 ByteBufUtils::writeMandatory);
39 public void testInteger() {
40 testWrite(Integer.valueOf(1), 4, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
41 ByteBufUtils::writeMandatory);
45 public void testLong() {
46 testWrite(Long.valueOf(1L), 8, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
47 ByteBufUtils::writeMandatory);
51 public void testUint8() {
52 testRead(Uint8.ONE, ByteBufUtils::readUint8, (byte) 1);
53 testWrite(Uint8.ONE, 1, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero, ByteBufUtils::writeMandatory);
57 public void testUint16() {
58 testRead(Uint16.ONE, ByteBufUtils::readUint16, (byte) 0, (byte) 1);
59 testWrite(Uint16.ONE, 2, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero, ByteBufUtils::writeMandatory);
63 public void testUint32() {
64 testRead(Uint32.ONE, ByteBufUtils::readUint32, (byte) 0, (byte) 0, (byte) 0, (byte) 1);
65 testWrite(Uint32.ONE, 4, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero, ByteBufUtils::writeMandatory);
69 public void testUint64() {
70 testRead(Uint64.ONE, ByteBufUtils::readUint64,
71 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 1);
72 testWrite(Uint64.ONE, 8, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero, ByteBufUtils::writeMandatory);
75 private static <T> void testRead(final T expected, final Function<ByteBuf, T> readFunc, final byte... bytes) {
76 final ByteBuf buf = Unpooled.wrappedBuffer(bytes);
77 assertEquals(expected, readFunc.apply(buf));
78 assertFalse(buf.isReadable());
81 private static <T> void testWrite(final T obj, final int size, final WriteNullable<T> writeOptional,
82 final WriteNullable<T> writeOrZero, final WriteMandatory<T> writeMandatory) {
83 final ByteBuf buf = Unpooled.buffer(size);
85 writeOptional.accept(buf, null);
86 assertEquals(0, buf.writerIndex());
87 writeOptional.accept(buf, obj);
88 assertEquals(size, buf.writerIndex());
90 writeOrZero.accept(buf, null);
91 assertEquals(2 * size, buf.writerIndex());
92 writeOrZero.accept(buf, obj);
93 assertEquals(3 * size, buf.writerIndex());
97 writeMandatory.accept(buf, null, "name");
98 fail("IAE should have been thrown");
99 } catch (IllegalArgumentException e) {
100 assertEquals("name is mandatory", e.getMessage());
102 assertEquals(0, buf.writerIndex());
104 writeMandatory.accept(buf, obj, null);
105 assertEquals(size, buf.writerIndex());
109 interface WriteNullable<T> extends BiConsumer<ByteBuf, T> {
114 interface WriteMandatory<T> {
115 void accept(ByteBuf buf, T value, String name);