Bump MDSAL to 4.0.0
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleLabelRegistryTest.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.rsvp.parser.spi.pojo;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
25 import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
26 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
28
29 public class SimpleLabelRegistryTest {
30     private final short ctype = 1;
31     private final SimpleLabelRegistry simpleLabelRegistry = new SimpleLabelRegistry();
32     private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
33     @Mock
34     private LabelParser labelParser;
35     @Mock
36     private LabelSerializer labelSerializer;
37
38     @Before
39     public void setUp() throws RSVPParsingException {
40         MockitoAnnotations.initMocks(this);
41         this.simpleLabelRegistry.registerLabelParser(this.ctype, this.labelParser);
42         this.simpleLabelRegistry.registerLabelSerializer(MockLabel.class, this.labelSerializer);
43         Mockito.doReturn(new MockLabel()).when(this.labelParser).parseLabel(this.input);
44         final ArgumentCaptor<LabelType> tlvArg = ArgumentCaptor.forClass(LabelType.class);
45         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
46         Mockito.doNothing().when(this.labelSerializer).serializeLabel(Mockito.anyBoolean(), Mockito.anyBoolean(),
47             tlvArg.capture(), bufArg.capture());
48     }
49
50     @Test
51     public void testParserRegistration() {
52         assertNotNull(this.simpleLabelRegistry.registerLabelParser(this.ctype, this.labelParser));
53     }
54
55     @Test
56     public void testSerializerRegistration() {
57         assertNotNull(this.simpleLabelRegistry.registerLabelSerializer(MockLabelClass.class, this.labelSerializer));
58     }
59
60     @Test
61     public void testUnrecognizedType() throws RSVPParsingException {
62         final int wrongLabelType = 99;
63         assertNull(this.simpleLabelRegistry.parseLabel(wrongLabelType, this.input));
64         final ByteBuf output = Unpooled.EMPTY_BUFFER;
65         this.simpleLabelRegistry.serializeLabel(false, false, new MockLabel(), output);
66         assertEquals(0, output.readableBytes());
67     }
68
69     @Test(expected = IllegalArgumentException.class)
70     public void testParseWrongType() throws RSVPParsingException {
71         final int wrongType = 65536;
72         this.simpleLabelRegistry.parseLabel(wrongType, this.input);
73     }
74
75     @Test(expected = IllegalArgumentException.class)
76     public void testRegisterWrongType() {
77         final int wrongType = 65536;
78         this.simpleLabelRegistry.registerLabelParser(wrongType, this.labelParser);
79     }
80
81     @Test
82     public void testParseLabel() throws RSVPParsingException {
83         final LabelType output = this.simpleLabelRegistry.parseLabel(this.ctype, this.input);
84         assertNotNull(output);
85         assertTrue(output instanceof MockLabel);
86
87         final ByteBuf aggregator = Unpooled.EMPTY_BUFFER;
88         this.simpleLabelRegistry.serializeLabel(false, false, output, aggregator);
89         Mockito.verify(this.labelSerializer).serializeLabel(false, false, output, aggregator);
90     }
91
92     private final class MockLabelClass implements LabelType {
93         @Override
94         public Class<? extends LabelType> implementedInterface() {
95             return MockLabelClass.class;
96         }
97     }
98
99     private final class MockLabel implements LabelType {
100         @Override
101         public Class<? extends LabelType> implementedInterface() {
102             return MockLabel.class;
103         }
104     }
105 }