396f1da7b639c74c6603a7ab1112427818acdaf8
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleXROSubobjectRegistryTest.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 io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
25 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
26 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
31
32 public class SimpleXROSubobjectRegistryTest {
33     private final int subObjectTypeOne = 1;
34     private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
35     private final SimpleXROSubobjectRegistry simpleXROSubobjectRegistry = new SimpleXROSubobjectRegistry();
36     @Mock
37     private XROSubobjectParser rroSubobjectParser;
38     @Mock
39     private XROSubobjectSerializer rroSubobjectSerializer;
40
41
42     @Before
43     public void setUp() throws RSVPParsingException {
44         MockitoAnnotations.initMocks(this);
45         this.simpleXROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this.rroSubobjectParser);
46         Mockito.doReturn(new SubobjectContainerBuilder().build()).when(this.rroSubobjectParser).parseSubobject(this
47             .input, false);
48         final ArgumentCaptor<SubobjectContainer> arg = ArgumentCaptor.forClass(SubobjectContainer.class);
49         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
50         Mockito.doAnswer(invocation -> {
51             final Object[] args = invocation.getArguments();
52             ((ByteBuf) args[1]).writeBoolean(Boolean.TRUE);
53             return null;
54         }).when(this.rroSubobjectSerializer).serializeSubobject(arg.capture(), bufArg.capture());
55     }
56
57     @Test
58     public void testUnrecognizedType() throws RSVPParsingException {
59         final int wrongType = 99;
60         assertNull(this.simpleXROSubobjectRegistry.parseSubobject(wrongType, this.input, false));
61         final ByteBuf output = Unpooled.EMPTY_BUFFER;
62         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
63             LabelCaseBuilder().build()).build();
64         this.simpleXROSubobjectRegistry.serializeSubobject(container, output);
65         assertEquals(0, output.readableBytes());
66     }
67
68     @Test(expected = IllegalArgumentException.class)
69     public void testParseWrongType() throws RSVPParsingException {
70         final int wrongType = 65536;
71         this.simpleXROSubobjectRegistry.parseSubobject(wrongType, this.input, false);
72     }
73
74     @Test(expected = IllegalArgumentException.class)
75     public void testRegisterWrongType() {
76         final int wrongType = 65536;
77         this.simpleXROSubobjectRegistry.registerSubobjectParser(wrongType, this.rroSubobjectParser);
78     }
79
80     @Test
81     public void testParserRegistration() throws RSVPParsingException {
82         assertNotNull(this.simpleXROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this
83             .rroSubobjectParser));
84         assertNotNull(this.simpleXROSubobjectRegistry.parseSubobject(this.subObjectTypeOne, this.input, false));
85     }
86
87     @Test
88     public void testSerializerRegistration() {
89         assertNotNull(this.simpleXROSubobjectRegistry.registerSubobjectSerializer(LabelCase.class, this
90             .rroSubobjectSerializer));
91         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
92             LabelCaseBuilder().build()).build();
93         final ByteBuf output = Unpooled.buffer();
94         this.simpleXROSubobjectRegistry.serializeSubobject(container, output);
95         assertEquals(1, output.readableBytes());
96     }
97
98 }