2 * Copyright (c) 2016 Cisco Systems, Inc. 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.bgp.parser.spi;
10 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
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.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;
25 public class PathIdUtilTest {
27 private ByteBuf buffer;
31 this.buffer = Unpooled.buffer();
35 public void testWritePathIdNull() {
36 PathIdUtil.writePathId(null, this.buffer);
37 Assert.assertEquals(0, this.buffer.readableBytes());
41 public void testWritePathIdZero() {
42 PathIdUtil.writePathId(NON_PATH_ID, this.buffer);
43 Assert.assertEquals(0, this.buffer.readableBytes());
47 public void testWritePathId() {
48 PathIdUtil.writePathId(new PathId(10L), this.buffer);
49 Assert.assertEquals(Integer.BYTES, this.buffer.readableBytes());
53 public void testReadPathId() {
54 final long expected = 10L;
55 ByteBufWriteUtil.writeUnsignedInt(expected, this.buffer);
56 final PathId pathId = PathIdUtil.readPathId(this.buffer);
57 Assert.assertEquals(expected, pathId.getValue().longValue());
61 public void testExtractPathId() {
62 final NodeIdentifier NII = new NodeIdentifier(QName.create("urn:opendaylight:params:xml:ns:yang:bgp-inet",
63 "2015-03-05", "path-id").intern());
64 final long pathIdValue = 0;
65 final ContainerNode cont = ImmutableContainerNodeSchemaAwareBuilder.create().withNodeIdentifier(NII).addChild(
66 new ImmutableLeafNodeBuilder<>().withNodeIdentifier(NII).withValue(pathIdValue).build()).build();
67 Assert.assertEquals(pathIdValue, PathIdUtil.extractPathId(cont, NII).longValue());
71 @Test(expected = IllegalArgumentException.class)
72 public void testReadPathIdBufferNull() {
73 PathIdUtil.readPathId(null);
76 @Test(expected = IllegalArgumentException.class)
77 public void testReadPathIdBufferEmpty() {
78 PathIdUtil.readPathId(this.buffer);