2f1090f22d56c73afc21fe87ccb1e7500774a7bb
[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.protocol.util.ByteBufWriteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.Uint32;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
25
26 public class PathIdUtilTest {
27
28     private ByteBuf buffer;
29
30     @Before
31     public void setUp() {
32         this.buffer = Unpooled.buffer();
33     }
34
35     @Test
36     public void testWritePathIdNull() {
37         PathIdUtil.writePathId(null, this.buffer);
38         Assert.assertEquals(0, this.buffer.readableBytes());
39     }
40
41     @Test
42     public void testWritePathIdZero() {
43         PathIdUtil.writePathId(NON_PATH_ID, this.buffer);
44         Assert.assertEquals(0, this.buffer.readableBytes());
45     }
46
47     @Test
48     public void testWritePathId() {
49         PathIdUtil.writePathId(new PathId(Uint32.valueOf(10)), this.buffer);
50         Assert.assertEquals(Integer.BYTES, this.buffer.readableBytes());
51     }
52
53     @Test
54     public void testReadPathId() {
55         final long expected = 10L;
56         ByteBufWriteUtil.writeUnsignedInt(expected, this.buffer);
57         final PathId pathId = PathIdUtil.readPathId(this.buffer);
58         Assert.assertEquals(expected, pathId.getValue().longValue());
59     }
60
61     @Test
62     public void testExtractPathId() {
63         final NodeIdentifier NII = new NodeIdentifier(QName.create("urn:opendaylight:params:xml:ns:yang:bgp-inet",
64             "2015-03-05", "path-id").intern());
65         final ContainerNode cont = ImmutableContainerNodeSchemaAwareBuilder.create().withNodeIdentifier(NII).addChild(
66             new ImmutableLeafNodeBuilder<>().withNodeIdentifier(NII).withValue(Uint32.ZERO).build()).build();
67         Assert.assertEquals(0L, PathIdUtil.extractPathId(cont, NII).longValue());
68     }
69
70
71     @Test(expected = IllegalArgumentException.class)
72     public void testReadPathIdBufferNull() {
73         PathIdUtil.readPathId(null);
74     }
75
76     @Test(expected = IllegalArgumentException.class)
77     public void testReadPathIdBufferEmpty() {
78         PathIdUtil.readPathId(this.buffer);
79     }
80 }