Minimize use of ByteBufWriteUtil in bgp-parser-spi
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / PathIdUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.bgp.parser.spi;
9
10 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
24
25 public class PathIdUtilTest {
26
27     private ByteBuf buffer;
28
29     @Before
30     public void setUp() {
31         this.buffer = Unpooled.buffer();
32     }
33
34     @Test
35     public void testWritePathIdNull() {
36         PathIdUtil.writePathId(null, this.buffer);
37         Assert.assertEquals(0, this.buffer.readableBytes());
38     }
39
40     @Test
41     public void testWritePathIdZero() {
42         PathIdUtil.writePathId(NON_PATH_ID, this.buffer);
43         Assert.assertEquals(0, this.buffer.readableBytes());
44     }
45
46     @Test
47     public void testWritePathId() {
48         PathIdUtil.writePathId(new PathId(Uint32.TEN), this.buffer);
49         Assert.assertEquals(Integer.BYTES, this.buffer.readableBytes());
50     }
51
52     @Test
53     public void testReadPathId() {
54         this.buffer.writeInt(10);
55         final PathId pathId = PathIdUtil.readPathId(this.buffer);
56         Assert.assertEquals(Uint32.TEN, pathId.getValue());
57     }
58
59     @Test
60     public void testExtractPathId() {
61         final NodeIdentifier NII = new NodeIdentifier(QName.create("urn:opendaylight:params:xml:ns:yang:bgp-inet",
62             "2015-03-05", "path-id").intern());
63         final ContainerNode cont = ImmutableContainerNodeSchemaAwareBuilder.create().withNodeIdentifier(NII).addChild(
64             new ImmutableLeafNodeBuilder<>().withNodeIdentifier(NII).withValue(Uint32.ZERO).build()).build();
65         Assert.assertEquals(0L, PathIdUtil.extractPathId(cont, NII).longValue());
66     }
67
68
69     @Test(expected = IllegalArgumentException.class)
70     public void testReadPathIdBufferNull() {
71         PathIdUtil.readPathId(null);
72     }
73
74     @Test(expected = IllegalArgumentException.class)
75     public void testReadPathIdBufferEmpty() {
76         PathIdUtil.readPathId(this.buffer);
77     }
78 }