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