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