Fix sonar complains
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / XROSubobjectListParserTest.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 package org.opendaylight.protocol.rsvp.parser.spi.subobjects;
9
10 import static org.junit.Assert.assertEquals;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.Arrays;
14 import java.util.List;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
19 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
22
23 public class XROSubobjectListParserTest {
24
25     private final XROSubobjectRegistry registry = Mockito.mock(XROSubobjectRegistry.class);
26     private final SubobjectContainer subObj = Mockito.mock(SubobjectContainer.class);
27     private final XroListParser parser = new XroListParser(this.registry);
28     private final byte[] inputList = new byte[] {1, 3, 1, 2, 4, 1, 2};
29     private final byte[] emptyInput = new byte[] {1, 2};
30     private final byte[] wrongInput = new byte[] {1, 3};
31     private final List<SubobjectContainer> subobjects = Arrays.asList(this.subObj, this.subObj);
32
33     @Before
34     public void setUp() throws RSVPParsingException {
35         Mockito.doAnswer(invocation -> {
36             if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
37                 return null;
38             }
39             return XROSubobjectListParserTest.this.subObj;
40         }).when(this.registry).parseSubobject(Mockito.anyInt(), Mockito.any(ByteBuf.class), Mockito.anyBoolean());
41         Mockito.doReturn("lala").when(this.subObj).toString();
42         Mockito.doAnswer(invocation -> {
43             ((ByteBuf) invocation.getArguments()[1]).writeByte(1);
44             return null;
45         }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
46     }
47
48     @Test(expected=RSVPParsingException.class)
49     public void testWrongInput() throws RSVPParsingException {
50         this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
51     }
52
53     @Test
54     public void testParseList() throws RSVPParsingException {
55         assertEquals(0, this.parser.parseList(Unpooled.copiedBuffer(this.emptyInput)).size());
56         final ByteBuf toBeParsed = Unpooled.copiedBuffer(this.inputList);
57         assertEquals(2, this.parser.parseList(toBeParsed).size());
58     }
59
60     @Test
61     public void testSerializeList() {
62         final ByteBuf buffer = Unpooled.buffer(2);
63         assertEquals(0, buffer.readableBytes());
64         this.parser.serializeList(this.subobjects, buffer);
65         assertEquals(2, buffer.readableBytes());
66     }
67
68     private class XroListParser extends XROSubobjectListParser {
69         public XroListParser(final XROSubobjectRegistry subobjReg) {
70             super(subobjReg);
71         }
72         @Override
73         protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
74         }
75         @Override
76         protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
77             return null;
78         }
79     }
80 }