Bump versions to 0.22.5-SNAPSHOT
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / LocalPreferenceAttributeParserTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.impl.message.update;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ServiceLoader;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.LocalPrefBuilder;
25 import org.opendaylight.yangtools.yang.common.Uint32;
26
27 public class LocalPreferenceAttributeParserTest {
28     private static final byte[] ATTRIBUTE_BYTES = {
29         (byte) 0x40, (byte) 0x05, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
30     };
31
32     private static final Attributes RESULT = new AttributesBuilder()
33             .setLocalPref(new LocalPrefBuilder().setPref(Uint32.ONE).build())
34             .build();
35
36     private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
37         .orElseThrow().getAttributeRegistry();
38
39     @Test
40     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
41         final ByteBuf actual = Unpooled.buffer();
42         registry.serializeAttribute(RESULT, actual);
43         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
44
45         final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
46         assertEquals(RESULT.getLocalPref(), attributeOut.getLocalPref());
47     }
48
49     @Test
50     public void testParseEmptyAttribute() {
51         final ByteBuf actual = Unpooled.buffer();
52         registry.serializeAttribute(new AttributesBuilder().setLocalPref(new LocalPrefBuilder().build()).build(),
53             actual);
54         assertEquals(Unpooled.buffer(), actual);
55     }
56 }