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