Bump MDSAL to 4.0.0
[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
28 public class SimpleBmpTlvRegistryTest {
29
30     private final SimpleBmpTlvRegistry bmpTlvRegistry = new SimpleBmpTlvRegistry();
31     private final byte[] bytes = new byte[]{1, 2, 3};
32     private final ByteBuf input = Unpooled.wrappedBuffer(this.bytes);
33     private static final int DESCRIPTION_TLV_TYPE = 1;
34     private static final int OTHER_TLV_TYPE = 2;
35     @Mock
36     private BmpTlvParser descriptionTlvParser;
37     @Mock
38     private BmpTlvSerializer descriptionTlvSerializer;
39
40     @Before
41     public void setUp() throws BmpDeserializationException {
42         MockitoAnnotations.initMocks(this);
43         this.bmpTlvRegistry.registerBmpTlvParser(DESCRIPTION_TLV_TYPE, this.descriptionTlvParser);
44         this.bmpTlvRegistry.registerBmpTlvSerializer(MockDescriptionTlv.class, this.descriptionTlvSerializer);
45         Mockito.doReturn(new MockDescriptionTlv()).when(this.descriptionTlvParser).parseTlv(this.input);
46         final ArgumentCaptor<Tlv> tlvArg = ArgumentCaptor.forClass(Tlv.class);
47         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
48         Mockito.doNothing().when(this.descriptionTlvSerializer).serializeTlv(tlvArg.capture(), bufArg.capture());
49     }
50
51     @Test
52     public void testParserRegistration() {
53         assertNotNull(this.bmpTlvRegistry.registerBmpTlvParser(DESCRIPTION_TLV_TYPE, this.descriptionTlvParser));
54     }
55
56     @Test
57     public void testSerializerRegistration() {
58         assertNotNull(this.bmpTlvRegistry.registerBmpTlvSerializer(MockDescriptionTlv.class,
59                 this.descriptionTlvSerializer));
60     }
61
62     @Test
63     public void testUnrecognizedType() throws BmpDeserializationException {
64         assertNull(this.bmpTlvRegistry.parseTlv(OTHER_TLV_TYPE, this.input));
65         final ByteBuf output = Unpooled.EMPTY_BUFFER;
66         this.bmpTlvRegistry.serializeTlv(new MockTlv(), output);
67         assertEquals(0, output.readableBytes());
68     }
69
70     @Test
71     public void testParseTlv() throws BmpDeserializationException {
72         final Tlv output = this.bmpTlvRegistry.parseTlv(DESCRIPTION_TLV_TYPE, this.input);
73         assertNotNull(output);
74         assertTrue(output instanceof MockDescriptionTlv);
75
76         final ByteBuf aggregator = Unpooled.EMPTY_BUFFER;
77         this.bmpTlvRegistry.serializeTlv(output, aggregator);
78         Mockito.verify(this.descriptionTlvSerializer).serializeTlv(output, aggregator);
79     }
80
81     private final class MockDescriptionTlv implements Tlv {
82         @Override
83         public Class<MockDescriptionTlv> implementedInterface() {
84             return MockDescriptionTlv.class;
85         }
86     }
87
88     private final class MockTlv implements Tlv {
89
90         @Override
91         public Class<MockTlv> implementedInterface() {
92             return MockTlv.class;
93         }
94
95     }
96 }