93b421d8002659b23c549a4856767ef74bd30218
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleExtendedCommunityRegistryTest.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.spi.pojo;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
20 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunities;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginIpv4CaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4Case;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
28
29 public class SimpleExtendedCommunityRegistryTest {
30
31     private SimpleExtendedCommunityRegistry register;
32
33     private final ExtendedCommunityParser parser = Mockito.mock(ExtendedCommunityParser.class);
34     private final ExtendedCommunitySerializer serializer = Mockito.mock(ExtendedCommunitySerializer.class);
35
36     @Before
37     public void setup() throws BGPDocumentedException, BGPParsingException {
38         this.register = new SimpleExtendedCommunityRegistry();
39         this.register.registerExtendedCommunityParser(0, 0, this.parser);
40         this.register.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, this.serializer);
41         Mockito.doReturn(0).when(this.serializer).getType(Mockito.anyBoolean());
42         Mockito.doReturn(0).when(this.serializer).getSubType();
43         Mockito.doNothing().when(this.serializer).serializeExtendedCommunity(Mockito.any(ExtendedCommunity.class), Mockito.any(ByteBuf.class));
44         Mockito.doReturn(null).when(this.parser).parseExtendedCommunity(Mockito.any(ByteBuf.class));
45
46     }
47
48     @Test
49     public void testExtendedCommunityRegistry() throws BGPDocumentedException, BGPParsingException {
50         final ByteBuf output = Unpooled.buffer();
51         this.register.serializeExtendedCommunity(
52                 new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(new RouteTargetIpv4Builder().build()).build()).build(), output);
53         Mockito.verify(this.serializer).serializeExtendedCommunity(Mockito.any(ExtendedCommunity.class), Mockito.any(ByteBuf.class));
54         //no value serialized, just header
55         Assert.assertEquals(2, output.readableBytes());
56
57         final ExtendedCommunities parsedExtendedCommunity = this.register.parseExtendedCommunity(Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}));
58         Mockito.verify(this.parser).parseExtendedCommunity(Mockito.any(ByteBuf.class));
59         Assert.assertTrue(parsedExtendedCommunity.isTransitive());
60         //no value parser
61         Assert.assertNull(parsedExtendedCommunity.getExtendedCommunity());
62     }
63
64     @Test
65     public void testExtendedCommunityRegistryUnknown() throws BGPDocumentedException, BGPParsingException {
66         final ByteBuf output = Unpooled.buffer();
67         this.register.serializeExtendedCommunity(
68                 new ExtendedCommunitiesBuilder().setTransitive(false).setExtendedCommunity(new RouteOriginIpv4CaseBuilder().build()).build(), output);
69         //no ex. community was serialized
70         Assert.assertEquals(0, output.readableBytes());
71         Mockito.verify(this.serializer, Mockito.never()).serializeExtendedCommunity(Mockito.any(ExtendedCommunity.class), Mockito.any(ByteBuf.class));
72
73         final ExtendedCommunities noExtCommunity = this.register.parseExtendedCommunity(Unpooled.copiedBuffer(new byte[] {0, 1, 0, 0, 0, 0, 0, 0}));
74         //no ext. community was parsed
75         Assert.assertNull(noExtCommunity);
76         Mockito.verify(this.parser, Mockito.never()).parseExtendedCommunity(Mockito.any(ByteBuf.class));
77     }
78
79     @Test(expected=IllegalArgumentException.class)
80     public void testRegisterParserOutOfRangeType() {
81         this.register.registerExtendedCommunityParser(1234, 0, this.parser);
82     }
83
84     @Test(expected=IllegalArgumentException.class)
85     public void testRegisterParserOutOfRangeSubType() {
86         this.register.registerExtendedCommunityParser(0, 1234, this.parser);
87     }
88
89 }