Add ByteBufUtils
[bgpcep.git] / util / src / test / java / org / opendaylight / protocol / util / ByteBufUtilsTest.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.protocol.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.fail;
13
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;
23
24 public class ByteBufUtilsTest {
25
26     @Test
27     public void testByte() {
28         testWrite(Byte.valueOf((byte) 1), 1, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
29             ByteBufUtils::writeMandatory);
30     }
31
32     @Test
33     public void testShort() {
34         testWrite(Short.valueOf((short) 1), 2, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
35             ByteBufUtils::writeMandatory);
36     }
37
38     @Test
39     public void testInteger() {
40         testWrite(Integer.valueOf(1), 4, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
41             ByteBufUtils::writeMandatory);
42     }
43
44     @Test
45     public void testLong() {
46         testWrite(Long.valueOf(1L), 8, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero,
47             ByteBufUtils::writeMandatory);
48     }
49
50     @Test
51     public void testUint8() {
52         testRead(Uint8.ONE, ByteBufUtils::readUint8, (byte) 1);
53         testWrite(Uint8.ONE, 1, ByteBufUtils::writeOptional, ByteBufUtils::writeOrZero, ByteBufUtils::writeMandatory);
54     }
55
56     @Test
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);
60     }
61
62     @Test
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);
66     }
67
68     @Test
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);
73     }
74
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());
79     }
80
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);
84
85         writeOptional.accept(buf, null);
86         assertEquals(0, buf.writerIndex());
87         writeOptional.accept(buf, obj);
88         assertEquals(size, buf.writerIndex());
89
90         writeOrZero.accept(buf, null);
91         assertEquals(2 * size, buf.writerIndex());
92         writeOrZero.accept(buf, obj);
93         assertEquals(3 * size, buf.writerIndex());
94
95         buf.clear();
96         try {
97             writeMandatory.accept(buf, null, "name");
98             fail("IAE should have been thrown");
99         } catch (IllegalArgumentException e) {
100             assertEquals("name is mandatory", e.getMessage());
101         }
102         assertEquals(0, buf.writerIndex());
103
104         writeMandatory.accept(buf, obj, null);
105         assertEquals(size, buf.writerIndex());
106     }
107
108     @FunctionalInterface
109     interface WriteNullable<T> extends BiConsumer<ByteBuf, T> {
110
111     }
112
113     @FunctionalInterface
114     interface WriteMandatory<T> {
115         void accept(ByteBuf buf, T value, String name);
116     }
117 }