BUG 2230: RSVP SPI POJO Tests
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleRSVPObjectRegistryTest.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.RSVPParsingException;
25 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectParser;
26 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectSerializer;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.explicit.route.object.SecondaryExplicitRouteObject;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.explicit.route.object.SecondaryExplicitRouteObjectBuilder;
30 import org.opendaylight.yangtools.yang.binding.DataContainer;
31
32 public class SimpleRSVPObjectRegistryTest {
33     private final int subObjectTypeOne = 1;
34     private final int subObjectCTypeOne = 1;
35     private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
36     private SimpleRSVPObjectRegistry simpleRSVPObjectRegistry = new SimpleRSVPObjectRegistry();
37     @Mock
38     private RSVPTeObjectParser rsvpTeObjectParser;
39     @Mock
40     private RSVPTeObjectSerializer rsvpTeObjectSerializer;
41
42     @Before
43     public void setUp() throws RSVPParsingException {
44         MockitoAnnotations.initMocks(this);
45         this.simpleRSVPObjectRegistry.registerRsvpObjectParser(subObjectTypeOne, subObjectCTypeOne, this.rsvpTeObjectParser);
46         this.simpleRSVPObjectRegistry.registerRsvpObjectSerializer(SecondaryExplicitRouteObject.class, this.rsvpTeObjectSerializer);
47         Mockito.doReturn(new SecondaryExplicitRouteObjectBuilder().build()).when(this.rsvpTeObjectParser).parseObject(this.input);
48         final ArgumentCaptor<RsvpTeObject> arg = ArgumentCaptor.forClass(RsvpTeObject.class);
49         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
50         Mockito.doNothing().when(this.rsvpTeObjectSerializer).serializeObject(arg.capture(), bufArg.capture());
51     }
52
53     @Test
54     public void testParserRegistration() {
55         this.simpleRSVPObjectRegistry.registerRsvpObjectParser(subObjectTypeOne, subObjectCTypeOne, this.rsvpTeObjectParser);
56     }
57
58     @Test
59     public void testSerializerRegistration() {
60         this.simpleRSVPObjectRegistry.registerRsvpObjectSerializer(SecondaryExplicitRouteObject.class, this.rsvpTeObjectSerializer);
61     }
62
63     @Test
64     public void testParseWrongType() throws RSVPParsingException {
65         final int wrongType = 65536;
66         assertNull(this.simpleRSVPObjectRegistry.parseRSPVTe(wrongType, subObjectCTypeOne, this.input));
67     }
68
69     @Test
70     public void testUnrecognizedType() throws RSVPParsingException {
71         final int wrongType = 99;
72         assertNull(this.simpleRSVPObjectRegistry.parseRSPVTe(wrongType, subObjectCTypeOne, this.input));
73         final ByteBuf output = Unpooled.EMPTY_BUFFER;
74         this.simpleRSVPObjectRegistry.serializeRSPVTe(new SecondaryExplicitRouteObjectBuilder().build(), output);
75         assertEquals(0, output.readableBytes());
76     }
77
78     @Test
79     public void testParseRSVP() throws RSVPParsingException {
80         final RsvpTeObject output = this.simpleRSVPObjectRegistry.parseRSPVTe(subObjectTypeOne, subObjectCTypeOne, this.input);
81         assertNotNull(output);
82         assertTrue(output instanceof SecondaryExplicitRouteObject);
83
84         final ByteBuf aggregator = Unpooled.EMPTY_BUFFER;
85         this.simpleRSVPObjectRegistry.serializeRSPVTe(output, aggregator);
86         Mockito.verify(this.rsvpTeObjectSerializer).serializeObject(output, aggregator);
87     }
88
89
90     @Test
91     public void testRegisterWrongCType() throws RSVPParsingException {
92         final int wrongCType = 65536;
93         assertNull(this.simpleRSVPObjectRegistry.parseRSPVTe(subObjectTypeOne, wrongCType, this.input));
94     }
95
96     @Test(expected = IllegalArgumentException.class)
97     public void testRegisterWrongType() {
98         final int wrongType = 65536;
99         this.simpleRSVPObjectRegistry.registerRsvpObjectParser(wrongType, subObjectCTypeOne, this.rsvpTeObjectParser);
100     }
101
102     private final class MockRsvpTeObjectClass implements RsvpTeObject {
103         @Override
104         public Class<? extends DataContainer> getImplementedInterface() {
105             return MockRsvpTeObjectClass.class;
106         }
107     }
108 }