MVPN RFC6514 Extendend communities
[bgpcep.git] / bmp / bmp-spi / src / test / java / org / opendaylight / protocol / bmp / spi / registry / SimpleBmpTlvRegistryTest.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.bmp.spi.registry;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
24 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
25 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.Tlv;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
28
29 public class SimpleBmpTlvRegistryTest {
30
31     private final SimpleBmpTlvRegistry bmpTlvRegistry = new SimpleBmpTlvRegistry();
32     private final byte[] bytes = new byte[]{1, 2, 3};
33     private final ByteBuf input = Unpooled.wrappedBuffer(this.bytes);
34     private static final int DESCRIPTION_TLV_TYPE = 1;
35     private static final int OTHER_TLV_TYPE = 2;
36     @Mock
37     private BmpTlvParser descriptionTlvParser;
38     @Mock
39     private BmpTlvSerializer descriptionTlvSerializer;
40
41     @Before
42     public void setUp() throws BmpDeserializationException {
43         MockitoAnnotations.initMocks(this);
44         this.bmpTlvRegistry.registerBmpTlvParser(DESCRIPTION_TLV_TYPE, this.descriptionTlvParser);
45         this.bmpTlvRegistry.registerBmpTlvSerializer(MockDescriptionTlv.class, this.descriptionTlvSerializer);
46         Mockito.doReturn(new MockDescriptionTlv()).when(this.descriptionTlvParser).parseTlv(this.input);
47         final ArgumentCaptor<Tlv> tlvArg = ArgumentCaptor.forClass(Tlv.class);
48         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
49         Mockito.doNothing().when(this.descriptionTlvSerializer).serializeTlv(tlvArg.capture(), bufArg.capture());
50     }
51
52     @Test
53     public void testParserRegistration() {
54         assertNotNull(this.bmpTlvRegistry.registerBmpTlvParser(DESCRIPTION_TLV_TYPE, this.descriptionTlvParser));
55     }
56
57     @Test
58     public void testSerializerRegistration() {
59         assertNotNull(this.bmpTlvRegistry.registerBmpTlvSerializer(MockDescriptionTlv.class,
60                 this.descriptionTlvSerializer));
61     }
62
63     @Test
64     public void testUnrecognizedType() throws BmpDeserializationException {
65         assertNull(this.bmpTlvRegistry.parseTlv(OTHER_TLV_TYPE, this.input));
66         final ByteBuf output = Unpooled.EMPTY_BUFFER;
67         this.bmpTlvRegistry.serializeTlv(new MockTlv(), output);
68         assertEquals(0, output.readableBytes());
69     }
70
71     @Test
72     public void testParseTlv() throws BmpDeserializationException {
73         final Tlv output = this.bmpTlvRegistry.parseTlv(DESCRIPTION_TLV_TYPE, this.input);
74         assertNotNull(output);
75         assertTrue(output instanceof MockDescriptionTlv);
76
77         final ByteBuf aggregator = Unpooled.EMPTY_BUFFER;
78         this.bmpTlvRegistry.serializeTlv(output, aggregator);
79         Mockito.verify(this.descriptionTlvSerializer).serializeTlv(output, aggregator);
80     }
81
82     private final class MockDescriptionTlv implements Tlv {
83         @Override
84         public Class<? extends DataContainer> getImplementedInterface() {
85             return MockDescriptionTlv.class;
86         }
87     }
88
89     private final class MockTlv implements Tlv {
90
91         @Override
92         public Class<? extends DataContainer> getImplementedInterface() {
93             return MockTlv.class;
94         }
95
96     }
97 }