Fix sonar complains
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleRROSubobjectRegistryTest.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.RROSubobjectParser;
23 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
24 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
29
30 public class SimpleRROSubobjectRegistryTest {
31
32     private final int subObjectTypeOne = 1;
33     private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
34     private final SimpleRROSubobjectRegistry simpleRROSubobjectRegistry = new SimpleRROSubobjectRegistry();
35     @Mock
36     private RROSubobjectParser rroSubobjectParser;
37     @Mock
38     private RROSubobjectSerializer rroSubobjectSerializer;
39
40
41     @Before
42     public void setUp() throws RSVPParsingException {
43         MockitoAnnotations.initMocks(this);
44         this.simpleRROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this.rroSubobjectParser);
45         Mockito.doReturn(new SubobjectContainerBuilder().build()).when(this.rroSubobjectParser).parseSubobject(this.input);
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.simpleRROSubobjectRegistry.parseSubobject(wrongType, this.input));
59         final ByteBuf output = Unpooled.EMPTY_BUFFER;
60         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
61             LabelCaseBuilder().build()).build();
62         this.simpleRROSubobjectRegistry.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.simpleRROSubobjectRegistry.parseSubobject(wrongType, this.input);
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void testRegisterWrongType() {
74         final int wrongType = 65536;
75         this.simpleRROSubobjectRegistry.registerSubobjectParser(wrongType, this.rroSubobjectParser);
76     }
77
78     @Test
79     public void testParserRegistration() throws RSVPParsingException {
80         assertNotNull(this.simpleRROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this
81             .rroSubobjectParser));
82         assertNotNull(this.simpleRROSubobjectRegistry.parseSubobject(this.subObjectTypeOne, this.input));
83     }
84
85     @Test
86     public void testSerializerRegistration() {
87         assertNotNull(this.simpleRROSubobjectRegistry.registerSubobjectSerializer(LabelCase.class, this
88             .rroSubobjectSerializer));
89         final ByteBuf output = Unpooled.buffer();
90         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
91             LabelCaseBuilder().build()).build();
92         this.simpleRROSubobjectRegistry.serializeSubobject(container, output);
93         assertEquals(1, output.readableBytes());
94     }
95
96 }