BUG-4622: Sending Empty Optional Attributes
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / CommunitiesAttributeParserTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10
11 import com.google.common.collect.Lists;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import junit.framework.TestCase;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.NoopReferenceCache;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Communities;
24
25 public class CommunitiesAttributeParserTest extends TestCase {
26
27     private static final byte[] CommunitiesBytes = {(byte) 0xC0, (byte) 0x08, (byte) 0x10,
28         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x1,
29         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x2,
30         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x3,
31         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x10};
32
33     @Test
34     public void testCommunitiesAttributeParser() throws Exception {
35         final List<Communities> comms = Lists.newArrayList();
36         comms.add((Communities) CommunityUtil.NO_EXPORT);
37         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
38         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
39         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
40
41         final AttributesBuilder paBuilder = new AttributesBuilder();
42         paBuilder.setCommunities(comms);
43
44         final ByteBuf actual = Unpooled.buffer();
45         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
46             .serializeAttribute(paBuilder.build(), actual);
47         Assert.assertArrayEquals(CommunitiesBytes, ByteArray.getAllBytes(actual));
48         final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
49             .parseAttributes(actual);
50         assertEquals(comms,attributeOut.getCommunities());
51     }
52
53     @Test
54     public void testParseEmptyListAttribute() {
55         final List<Communities> comms = Lists.newArrayList();
56         final ByteBuf actual = Unpooled.buffer();
57         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
58             .serializeAttribute(new AttributesBuilder().setCommunities(comms).build(), actual);
59         assertEquals(Unpooled.buffer(), actual);
60     }
61
62     @Test
63     public void testParseEmptyAttribute() {
64         final ByteBuf actual = Unpooled.buffer();
65         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
66             .serializeAttribute(new AttributesBuilder().build(), actual);
67         assertEquals(Unpooled.buffer(), actual);
68     }
69 }